forked from longtao.wu/droboticflow
80 lines
4.0 KiB
Python
80 lines
4.0 KiB
Python
"""Private output contract for test-lane credential generation."""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from urllib.parse import urlparse, unquote
|
|
|
|
from scripts.internal_test_lane.credentials import generate_credentials
|
|
|
|
|
|
def config():
|
|
return {
|
|
"schemaVersion": 1, "batchId": "secret-check", "hostname": "test.example.test",
|
|
"productionOrigins": ["http://120.48.180.80"],
|
|
"image": "example.test/api@sha256:" + "a" * 64,
|
|
"mongoImage": "example.test/mongo@sha256:" + "b" * 64,
|
|
"buildCommit": "c" * 40, "workspaceGi": 10, "mongoGi": 5,
|
|
"tlsSecretName": "internal-test-tls", "imagePullSecretName": "internal-test-registry",
|
|
"ingressNamespace": "ingress-nginx",
|
|
"resources": {name: {"requests": {"cpu": "100m", "memory": "128Mi"},
|
|
"limits": {"cpu": "500m", "memory": "512Mi"}}
|
|
for name in ("api", "processor", "mongo")},
|
|
}
|
|
|
|
|
|
class CredentialTests(unittest.TestCase):
|
|
def test_private_outputs_bind_only_test_target_and_do_not_echo_passwords(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory).resolve() / "credentials"
|
|
result = generate_credentials(config(), output)
|
|
secret_file = Path(result["secretManifest"])
|
|
accounts_file = Path(result["accountsFile"])
|
|
self.assertEqual(os.stat(secret_file).st_mode & 0o777, 0o600)
|
|
self.assertEqual(os.stat(accounts_file).st_mode & 0o777, 0o600)
|
|
self.assertEqual(os.stat(output).st_mode & 0o777, 0o700)
|
|
secret = json.loads(secret_file.read_text())
|
|
self.assertEqual(secret["metadata"]["namespace"], "droboticflow-internal-test")
|
|
data = secret["stringData"]
|
|
uri = urlparse(data["MONGO_URI"])
|
|
self.assertEqual(uri.hostname, "internal-test-secret-check-mongo.droboticflow-internal-test.svc.cluster.local")
|
|
self.assertEqual(uri.path, "/droboticflow_internal_test_secret_check")
|
|
self.assertEqual(unquote(uri.password), data["MONGO_APP_PASSWORD"])
|
|
self.assertEqual(Path(result["mongoUriFile"]).read_text().strip(), data["MONGO_URI"])
|
|
self.assertEqual(os.stat(result["mongoUriFile"]).st_mode & 0o777, 0o600)
|
|
accounts = json.loads(accounts_file.read_text())["accounts"]
|
|
self.assertEqual({a["role"] for a in accounts},
|
|
{"admin", "supplier_manager", "annotator", "quality_reviewer", "acceptance_reviewer"})
|
|
passwords = [a["password"] for a in accounts]
|
|
self.assertEqual(len(set(passwords)), 5)
|
|
self.assertTrue(all(len(password) >= 32 for password in passwords))
|
|
self.assertTrue(all(password not in json.dumps(result) for password in passwords + list(data.values())))
|
|
|
|
def test_generation_refuses_overwrite_or_symlinked_destination(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory).resolve() / "credentials"
|
|
generate_credentials(config(), output)
|
|
before = (output / "accounts.json").read_bytes()
|
|
with self.assertRaises((ValueError, FileExistsError)):
|
|
generate_credentials(config(), output)
|
|
self.assertEqual((output / "accounts.json").read_bytes(), before)
|
|
linked = Path(directory).resolve() / "linked"
|
|
linked.symlink_to(output, target_is_directory=True)
|
|
with self.assertRaises((ValueError, FileExistsError)):
|
|
generate_credentials(config(), linked / "nested")
|
|
|
|
def test_invalid_environment_is_rejected_before_any_output(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory).resolve() / "credentials"
|
|
unsafe = config()
|
|
unsafe["namespace"] = "droboticflow-fanyu"
|
|
with self.assertRaises(ValueError):
|
|
generate_credentials(unsafe, output)
|
|
self.assertFalse(output.exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|