"""Compare known private values against tracked/unignored source, image metadata and logs.""" import json,re,subprocess from pathlib import Path ROOT=Path(__file__).resolve().parents[1];values=[] def collect(obj): if isinstance(obj,dict): for v in obj.values():collect(v) elif isinstance(obj,list): for v in obj:collect(v) elif isinstance(obj,str) and len(obj)>=16:values.append(obj.encode()) for name in ['credentials.json','bao-operator.json']: p=ROOT/'.secrets'/name if p.exists():collect(json.loads(p.read_text())) for line in (ROOT/'.env').read_text().splitlines(): if 'PASSWORD=' in line:values.append(line.split('=',1)[1].encode()) files=subprocess.check_output(['git','ls-files','--cached','--others','--exclude-standard','-z']).split(b'\0');checked=0 pattern=re.compile(rb'-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----\r?\n') for name in files: if not name:continue p=ROOT/name.decode() if not p.is_file():continue data=p.read_bytes();assert not pattern.search(data),'private key file in Git sources' assert not any(v in data for v in values),'known secret in Git sources/evidence';checked+=1 logs=subprocess.check_output(['./scripts/compose.sh','logs','--no-color'],stderr=subprocess.DEVNULL) assert not any(v in logs for v in values),'known secret in container logs' audit=subprocess.check_output(['./scripts/compose.sh','exec','-T','openbao','cat','/bao/data/audit.json']) assert b'hmac-sha256:' in audit and not any(v in audit for v in values),'Bao audit contains known plaintext secret or lacks HMAC' history=subprocess.check_output(['docker','history','--no-trunc','mtusi-lab7-api:local']) assert not any(v in history for v in values),'known secret in image history' probe="from pathlib import Path; import os; assert not Path('/app/.secrets').exists(); assert not Path('/app/.env').exists(); assert not any(Path('/app').rglob('*.pem')); assert 'DATABASE_URL' not in os.environ; print('image contains no private bootstrap files/default DB credentials')" subprocess.run(['docker','run','--rm','--network','none','--entrypoint','python','mtusi-lab7-api:local','-c',probe],check=True,stdout=subprocess.DEVNULL) print(json.dumps({'status':'PASS','source_files_checked':checked,'known_private_values_checked':len(values),'container_logs_clean':True,'image_history_clean':True,'image_private_files_absent':True,'bao_audit_hmac_and_no_known_plaintext':True,'limitation':'Known-secret comparison is not proof against every possible secret format'},indent=2))