secrets-engine/tests/signature_support.py

56 lines
2.5 KiB
Python
Raw Normal View History

"""Native producer signing fixtures, never an authentication bypass."""
import json
import os
from pathlib import Path
import subprocess
import pytest
PUBLIC='IVL40Zt5HSRFMkLhXy6rbLfP-ntqXtMAl5YOBpiB2xI'
FOUNDER='founder@synthetic'
def admission_anchor(directory):
"""Synthetic founder key plus allowed_signers anchor, like /etc/gexec."""
key=directory/'founder'
subprocess.run(['ssh-keygen','-q','-t','ed25519','-N','','-C',FOUNDER,'-f',str(key)],check=True,timeout=30)
anchor=directory/'anchor';anchor.mkdir(mode=0o755)
(anchor/'allowed_signers').write_text(FOUNDER+' '+(directory/'founder.pub').read_text())
(anchor/'allowed_signers').chmod(0o644)
return {'key':key,'anchor':anchor}
def admit(path,namespace='secrets-engine-pdp-trust',key=None):
"""Founder-sign exact file bytes as `<path>.sig` (ssh-keygen -Y sign)."""
import os as _os
key=key or Path(_os.environ['SECRETS_ENGINE_ADMISSION_ANCHOR']).parent/'founder'
Path(str(path)+'.sig').unlink(missing_ok=True)
subprocess.run(['ssh-keygen','-q','-Y','sign','-f',str(key),'-n',namespace,str(path)],check=True,timeout=30,capture_output=True)
return path
@pytest.fixture(scope='session')
def signature_tools(tmp_path_factory):
directory=tmp_path_factory.mktemp('signature-tools')
# Private writable build cache keeps tests runnable in the workspace sandbox.
env={**os.environ,'GOCACHE':os.environ.get('GOCACHE',str(directory/'cache'))}
root=Path(__file__).parents[1]/'verifier'
for name,package in [('verify','.'),('sign','./testsupport/signer')]:
subprocess.run(['go','build','-o',str(directory/name),package],cwd=root,env=env,check=True,timeout=120)
return directory
def admit_tools(cfg, directory, tools):
trust={'schema':'secrets-engine.pdp-trust.v1','evaluator':'stub/local','mode':'standalone',
'policy_package':cfg.authorization_policy_package,'policy_version':cfg.authorization_policy_version,
'policy_package_digest':'sha256:'+'1'*64,'keys':{'testdata-ed25519':PUBLIC}}
path=directory/'pdp-trust.json';path.write_text(json.dumps(trust));admit(path)
from dataclasses import is_dataclass, replace
if is_dataclass(cfg):
return replace(cfg,pdp_trust_file=path,pdp_verifier=tools/'verify')
cfg.pdp_trust_file=path;cfg.pdp_verifier=tools/'verify'
return cfg
def sign_envelope(envelope,tools,next_key=False):
result=subprocess.run([str(tools/'sign')]+(['next'] if next_key else []),input=json.dumps(envelope),text=True,capture_output=True,check=True,timeout=3)
return json.loads(result.stdout)