88 lines
4.6 KiB
Python
88 lines
4.6 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
path = Path(__file__).resolve().parents[2] / 'scripts/doctor-dex-drift.py'
|
|
spec = importlib.util.spec_from_file_location('dex_drift', path)
|
|
drift = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(drift)
|
|
|
|
|
|
class DexDriftTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.source = {'DEX_GRAFANA_CLIENT_SECRET': 'secret-a', 'DEX_GITEA_CLIENT_SECRET': 'secret-b',
|
|
'DEX_USER_EMAIL': 'user@example.com', 'DEX_USER_NAME': 'user', 'DEX_USER_ID': 'uid'}
|
|
self.deployed = {'staticClients': [{'id': 'grafana', 'secret': 'secret-a'},
|
|
{'id': 'gitea', 'secret': 'secret-b'}],
|
|
'staticPasswords': [{'email': 'user@example.com', 'username': 'user', 'userID': 'uid'}]}
|
|
|
|
def test_matching_source_and_optional_client(self):
|
|
clients, identity = drift.compare(self.source, self.deployed)
|
|
self.assertEqual(clients['grafana'], 'match')
|
|
self.assertEqual(clients['argocd'], 'not configured')
|
|
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'})
|
|
clients, _ = drift.compare(self.source, self.deployed)
|
|
self.assertEqual(clients['grafana'], 'different')
|
|
self.assertEqual(clients['custom'], 'not managed by Jeannie')
|
|
self.assertNotIn('secret-c', str(clients))
|
|
|
|
def test_extra_users_and_duplicate_clients_refused(self):
|
|
self.deployed['staticPasswords'].append({'email': 'another@example.com'})
|
|
with self.assertRaises(ValueError):
|
|
drift.compare(self.source, self.deployed)
|
|
self.deployed['staticPasswords'].pop()
|
|
self.deployed['staticClients'].append({'id': 'grafana', 'secret': 'other'})
|
|
with self.assertRaises(ValueError):
|
|
drift.compare(self.source, self.deployed)
|
|
|
|
def test_dirty_source_blocks_and_does_not_expose_values(self):
|
|
self.source['DEX_USER_PASSWORD'] = 'password-value'
|
|
decrypted = json.dumps({'stringData': self.source}).encode()
|
|
result = subprocess.CompletedProcess([], 0, stdout=decrypted, stderr=b'')
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
secret = Path(directory) / 'secret.yaml'
|
|
secret.write_text('sops: {}\nstringData:\n sample: ENC[test]\n')
|
|
with patch.object(drift, 'git_state', return_value='modified or staged'), \
|
|
patch.object(drift, 'read_deployed', return_value=self.deployed), \
|
|
patch.object(drift.subprocess, 'run', return_value=result):
|
|
report = drift.diagnose(Path(directory), secret, Path('/example/config'))
|
|
self.assertEqual(report['result'], 'uncommitted')
|
|
self.assertNotIn('password-value', json.dumps(report))
|
|
self.assertNotIn('secret-a', json.dumps(report))
|
|
self.assertEqual(drift.diagnose(Path(directory), secret, Path('/example/config'), True)['result'], 'ok')
|
|
|
|
def test_plaintext_source_is_refused_before_decryption(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
secret = Path(directory) / 'secret.yaml'
|
|
secret.write_text('sops: {}\nstringData:\n secret: plaintext\n')
|
|
with patch.object(drift, 'git_state', return_value='committed in local HEAD'), \
|
|
patch.object(drift.subprocess, 'run') as run:
|
|
with self.assertRaises(ValueError):
|
|
drift.diagnose(Path(directory), secret, Path('/example/config'))
|
|
run.assert_not_called()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|