Recognize generated OpsProof Dex client
This commit is contained in:
parent
380ff65c43
commit
3bdb2ae6eb
|
|
@ -204,6 +204,7 @@ dex_drift_check() {
|
|||
--repo "${REPO_ROOT}" \
|
||||
--secret "$(dex_secret_file)" \
|
||||
--config "${LAB_DEX_INSTALL_DIR:-/data/homelab-dex}/config.yaml" \
|
||||
--opsproof-env "${LAB_OPSPROOF_INSTALL_DIR:-/data/homelab-opsproof}/.oidc.env" \
|
||||
"${args[@]}"
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ import sys
|
|||
import yaml
|
||||
|
||||
CLIENTS = {'grafana': 'DEX_GRAFANA_CLIENT_SECRET', 'gitea': 'DEX_GITEA_CLIENT_SECRET',
|
||||
'nextcloud': 'DEX_OFFICE_CLIENT_SECRET', 'argocd': 'DEX_ARGOCD_CLIENT_SECRET'}
|
||||
'nextcloud': 'DEX_OFFICE_CLIENT_SECRET', 'argocd': 'DEX_ARGOCD_CLIENT_SECRET',
|
||||
'opsproof': 'DEX_OPSPROOF_CLIENT_SECRET'}
|
||||
IDENTITY = {'email': 'DEX_USER_EMAIL', 'username': 'DEX_USER_NAME', 'userID': 'DEX_USER_ID'}
|
||||
|
||||
|
||||
|
|
@ -77,7 +78,25 @@ def read_deployed(config):
|
|||
return document
|
||||
|
||||
|
||||
def diagnose(repo, secret, config, allow_dirty=False):
|
||||
def generated_opsproof_secret(path):
|
||||
"""Read the private generated client secret without returning it in reports."""
|
||||
if path is None or not path.is_file():
|
||||
return None
|
||||
values = {}
|
||||
for line in path.read_text().splitlines():
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
key, separator, value = line.partition('=')
|
||||
if not separator:
|
||||
raise ValueError('OpsProof OIDC environment contains an invalid line')
|
||||
values[key] = value
|
||||
secret = values.get('OPSPROOF_DEX_CLIENT_SECRET')
|
||||
if secret is not None and not secret:
|
||||
raise ValueError('OpsProof OIDC client secret is empty')
|
||||
return secret
|
||||
|
||||
|
||||
def diagnose(repo, secret, config, allow_dirty=False, opsproof_env=None):
|
||||
state = git_state(repo, secret)
|
||||
encrypted = yaml.safe_load(secret.read_bytes())
|
||||
if not isinstance(encrypted, dict) or not isinstance(encrypted.get('sops'), dict):
|
||||
|
|
@ -97,6 +116,9 @@ def diagnose(repo, secret, config, allow_dirty=False):
|
|||
values = dict(document.get('stringData') or {})
|
||||
for key, value in (document.get('data') or {}).items():
|
||||
values[key] = base64.b64decode(value, validate=True).decode()
|
||||
opsproof_secret = generated_opsproof_secret(opsproof_env)
|
||||
if opsproof_secret:
|
||||
values['DEX_OPSPROOF_CLIENT_SECRET'] = opsproof_secret
|
||||
required = ('DEX_GRAFANA_CLIENT_SECRET', 'DEX_GITEA_CLIENT_SECRET',
|
||||
'DEX_USER_EMAIL', 'DEX_USER_NAME', 'DEX_USER_ID', 'DEX_USER_PASSWORD')
|
||||
if any(not isinstance(values.get(key), str) or not values[key] for key in required):
|
||||
|
|
@ -129,10 +151,11 @@ def main():
|
|||
parser.add_argument('--repo', type=Path, required=True)
|
||||
parser.add_argument('--secret', type=Path, required=True)
|
||||
parser.add_argument('--config', type=Path, required=True)
|
||||
parser.add_argument('--opsproof-env', type=Path)
|
||||
parser.add_argument('--allow-dirty', action='store_true', help=argparse.SUPPRESS)
|
||||
args = parser.parse_args()
|
||||
try:
|
||||
report = diagnose(args.repo, args.secret, args.config, args.allow_dirty)
|
||||
report = diagnose(args.repo, args.secret, args.config, args.allow_dirty, args.opsproof_env)
|
||||
print(json.dumps(report, sort_keys=True))
|
||||
return 0 if report['result'] == 'ok' else 1
|
||||
except (OSError, ValueError, TypeError, RuntimeError, yaml.YAMLError, subprocess.SubprocessError):
|
||||
|
|
|
|||
|
|
@ -27,6 +27,18 @@ class DexDriftTest(unittest.TestCase):
|
|||
self.assertTrue(all(value == 'match' for value in identity.values()))
|
||||
self.assertNotIn('secret-a', str(clients))
|
||||
|
||||
def test_generated_opsproof_client_is_managed_without_exposing_secret(self):
|
||||
self.deployed['staticClients'].append({'id': 'opsproof', 'secret': 'private-generated'})
|
||||
source = dict(self.source, DEX_OPSPROOF_CLIENT_SECRET='private-generated')
|
||||
clients, _ = drift.compare(source, self.deployed)
|
||||
self.assertEqual(clients['opsproof'], 'match')
|
||||
self.assertNotIn('private-generated', json.dumps(clients))
|
||||
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / '.oidc.env'
|
||||
path.write_text('OPSPROOF_PUBLIC_URL=http://example.test\nOPSPROOF_DEX_CLIENT_SECRET=private-generated\n')
|
||||
self.assertEqual(drift.generated_opsproof_secret(path), 'private-generated')
|
||||
|
||||
def test_changed_and_unknown_clients_are_flagged(self):
|
||||
self.deployed['staticClients'][0]['secret'] = 'changed'
|
||||
self.deployed['staticClients'].append({'id': 'custom', 'secret': 'secret-c'})
|
||||
|
|
|
|||
|
|
@ -72,6 +72,8 @@ class OpsProofPipelineTests(unittest.TestCase):
|
|||
self.assertIn('"OPSPROOF_JEANNIE_TOKEN=${jeannie_api_token}"', pipeline)
|
||||
self.assertIn('"OPSPROOF_DEX_CLIENT_SECRET=${oidc_secret}"', pipeline)
|
||||
self.assertIn('"OPSPROOF_OPERATOR_EMAILS=${operator_emails}"', pipeline)
|
||||
security = (ROOT / "lib/jeannie/security.sh").read_text(encoding="utf-8")
|
||||
self.assertIn('--opsproof-env "${LAB_OPSPROOF_INSTALL_DIR:-/data/homelab-opsproof}/.oidc.env"', security)
|
||||
self.assertIn("JEANNIE_SERVER_TOKEN=//p", pipeline)
|
||||
self.assertIn('"${release_dir}/opsproof" migrate --force', pipeline)
|
||||
self.assertIn(
|
||||
|
|
|
|||
Loading…
Reference in New Issue