Source code for aeat.adapters.persistence.storage.bucket._keystore_paths
"""Keystore separation contract enforcing the isolation invariant.The KEK / DEK / passphrase / OS-keystore custody artefacts live under akeystore root that is structurally outside the buckets parent. The twoinvariants enforced here are:- The keystore root is sibling to ``buckets/`` under the AEAT root (``<aeat-root>/keystore/<bucket-id>/``), never nested inside any bucket directory and never co-located under the relational database directory.- A configuration that resolves the keystore path under either parent is rejected by :func:`validate_keystore_separation` so a subsequent unlock cannot silently violate the invariant.The pure helpers do not materialise the directory; the cryptographic core(P03) owns provisioning when an enrolment first lands."""from__future__importannotationsfrompathlibimportPathfrom.._namespace_registryimportBUCKETS_DIRNAME,KEYSTORE_DIRNAMEfrom._errorsimportBucketValidationErrorfrom._layoutimportBucketPaths,bucket_paths_KEYSTORE_VALIDATION_SURFACE="bucket_keystore"
[docs]defkeystore_root(root:Path)->Path:"""Return the keystore parent ``<root>/keystore/`` (no IO)."""returnroot/KEYSTORE_DIRNAME
[docs]defkeystore_path(root:Path,bucket_id:str)->Path:"""Return ``<root>/keystore/<bucket_id>/`` (no IO). Args: root: The AEAT root directory. bucket_id: Bucket identifier to include in the path. Returns: The computed keystore directory path. Raises: BucketValidationError: When ``bucket_id`` is empty or carries a path separator. """ifnotbucket_id:raiseBucketValidationError("bucket_id must be non-empty")if"/"inbucket_idor"\\"inbucket_id:raiseBucketValidationError("bucket_id must not contain a path separator")returnkeystore_root(root)/bucket_id
def_is_under(child:Path,parent:Path)->bool:"""True if ``child`` resolves to a path beneath ``parent``. Uses ``Path.relative_to`` semantics on the lexically-resolved forms so the check is OS-portable; symlink traversal is intentionally not followed because the call site validates configuration before any filesystem state exists. """try:resolved_child=child.resolve(strict=False)resolved_parent=parent.resolve(strict=False)exceptOSError:resolved_child=childresolved_parent=parenttry:resolved_child.relative_to(resolved_parent)exceptValueError:returnFalsereturnTrue
[docs]defvalidate_keystore_separation(root:Path,bucket_id:str,*,configured_keystore:Path|None=None,)->None:"""Fail closed if the keystore path resolves under the buckets parent or db dir. Args: root: The AEAT root directory. bucket_id: The bucket identifier whose layout to validate against. configured_keystore: Optional override path; defaults to :func:`keystore_path`. A custom configuration that points at a location nested under the buckets parent or the per-bucket relational database directory is rejected. Raises: BucketValidationError: When the configured keystore path violates separation. """paths:BucketPaths=bucket_paths(root,bucket_id)target=configured_keystoreifconfigured_keystoreisnotNoneelsekeystore_path(root,bucket_id)buckets_parent=root/BUCKETS_DIRNAMEif_is_under(target,paths.db_dir):raiseBucketValidationError("keystore path resolves under bucket db dir",context={"reason":"under_bucket_db_dir","surface":_KEYSTORE_VALIDATION_SURFACE,},)if_is_under(target,buckets_parent):raiseBucketValidationError("keystore path resolves under buckets parent",context={"reason":"under_buckets_parent","surface":_KEYSTORE_VALIDATION_SURFACE,},)