async def test_sign_resign( image_config: ImageConfig, image_config_signed: ImageConfig, config_digest_canonical: str, ): """Test configuration resigning for signed and unsigned configurations.""" signer = FakeSigner() assert ( await image_config.sign(signer, SignatureTypes.RESIGN) == signer.signature_value ) assert ( await image_config_signed.sign(signer, SignatureTypes.RESIGN) == signer.signature_value ) # Previously unsigned configurations should now contain the new signature. assert b"BEGIN FAKE SIGNATURE" in image_config.get_bytes() signatures = image_config.get_signature_list() assert len(signatures) == 1 assert signatures[0]["digest"] == config_digest_canonical assert signatures[0]["signature"] == signer.signature_value # Previously signed configurations should now contain (only) the new signature. assert b"BEGIN FAKE SIGNATURE" in image_config_signed.get_bytes() assert b"BEGIN PGP SIGNATURE" not in image_config_signed.get_bytes() signatures_signed = image_config_signed.get_signature_list() assert len(signatures_signed) == 1 assert signatures[0]["digest"] == config_digest_canonical assert signatures[0]["signature"] == signer.signature_value
async def test_sign_endorse( image_config: ImageConfig, image_config_signed: ImageConfig, config_digest_canonical: str, config_digest_signed_canonical: str, signature: str, ): """Test configuration endorsement for signed and unsigned configurations.""" signer = FakeSigner() assert ( await image_config.sign(signer, SignatureTypes.ENDORSE) == signer.signature_value ) assert ( await image_config_signed.sign(signer, SignatureTypes.ENDORSE) == signer.signature_value ) # Previously unsigned configurations should now contain the new signature. assert b"BEGIN FAKE SIGNATURE" in image_config.get_bytes() signatures = image_config.get_signature_list() assert len(signatures) == 1 assert signatures[0]["digest"] == config_digest_canonical assert signatures[0]["signature"] == signer.signature_value # Previously signed configurations should now contain the original signature(s) and the new signature. assert b"BEGIN FAKE SIGNATURE" in image_config_signed.get_bytes() assert b"BEGIN PGP SIGNATURE" in image_config_signed.get_bytes() signatures_signed = image_config_signed.get_signature_list() assert len(signatures_signed) == 2 assert signatures_signed[0]["digest"] == config_digest_canonical assert signatures_signed[0]["signature"] == signature assert signatures_signed[1]["digest"] == config_digest_signed_canonical assert signatures_signed[1]["signature"] == signer.signature_value
def test_get_bytes( image_config: ImageConfig, image_config_signed: ImageConfig, json_bytes: bytes, json_bytes_signed: bytes, ): """Test get_bytes() pass-through for signed and unsigned configurations.""" assert image_config.get_bytes() == json_bytes assert image_config_signed.get_bytes() == json_bytes_signed
async def test_sign_resign( config_digest_canonical: str, gnupg_keypair: GnuPGKeypair, image_config: ImageConfig, image_config_signed: ImageConfig, ): """Test configuration resigning for signed and unsigned configurations.""" signer = GPGSigner( keyid=gnupg_keypair.keyid, passphrase=gnupg_keypair.passphrase, homedir=gnupg_keypair.gnupg_home, ) sig = await image_config.sign(signer, SignatureTypes.RESIGN) assert "PGP SIGNATURE" in sig sig_signed = await image_config_signed.sign(signer, SignatureTypes.RESIGN) assert "PGP SIGNATURE" in sig_signed # Previously unsigned configurations should now contain the new signature. signatures = image_config.get_signature_list() assert len(signatures) == 1 assert signatures[0].digest == config_digest_canonical assert signatures[0].signature == sig # Previously signed configurations should now contain (only) the new signature. assert image_config_signed.get_bytes().count(b"BEGIN PGP SIGNATURE") == 1 signatures_signed = image_config_signed.get_signature_list() assert len(signatures_signed) == 1 assert signatures[0].digest == config_digest_canonical assert signatures[0].signature == sig_signed
async def test_sign_endorse( config_digest_canonical: str, config_digest_signed_canonical: str, gnupg_keypair: GnuPGKeypair, image_config: ImageConfig, image_config_signed: ImageConfig, signature: str, ): """Test configuration endorsement for signed and unsigned configurations.""" signer = GPGSigner( keyid=gnupg_keypair.keyid, passphrase=gnupg_keypair.passphrase, homedir=gnupg_keypair.gnupg_home, ) sig = await image_config.sign(signer, SignatureTypes.ENDORSE) assert "PGP SIGNATURE" in sig sig_signed = await image_config_signed.sign(signer, SignatureTypes.ENDORSE) assert "PGP SIGNATURE" in sig_signed # Previously unsigned configurations should now contain the new signature. signatures = image_config.get_signature_list() assert len(signatures) == 1 assert signatures[0].digest == config_digest_canonical assert signatures[0].signature == sig # Previously signed configurations should now contain the original signature(s) and the new signature. assert image_config_signed.get_bytes().count(b"BEGIN PGP SIGNATURE") == 2 signatures_signed = image_config_signed.get_signature_list() assert len(signatures_signed) == 2 assert signatures_signed[0].digest == config_digest_canonical assert signatures_signed[0].signature == signature assert signatures_signed[1].digest == config_digest_signed_canonical assert signatures_signed[1].signature == sig_signed
async def test_minimal(): """Test minimal image configuration (for non-conformant labels)k.""" # Note: At a minimum, [Cc]onfig key must exist with non-null value image_config = ImageConfig(b'{"Config":{}}') config_digest_canonical = image_config.get_digest_canonical() signer = FakeSigner() assert await image_config.sign(signer) == signer.signature_value # A signature should always be able to be added ... assert b"BEGIN FAKE SIGNATURE" in image_config.get_bytes() signatures = image_config.get_signature_list() assert len(signatures) == 1 assert signatures[0]["digest"] == config_digest_canonical assert signatures[0]["signature"] == signer.signature_value
async def test_minimal(gnupg_keypair: GnuPGKeypair): """Test minimal image configuration (for non-conformant labels)k.""" signer = GPGSigner( keyid=gnupg_keypair.keyid, passphrase=gnupg_keypair.passphrase, homedir=gnupg_keypair.gnupg_home, ) # Note: At a minimum, [Cc]onfig key must exist with non-null value image_config = ImageConfig(b'{"Config":{}}') config_digest_canonical = image_config.get_digest_canonical() signature = await image_config.sign(signer) assert "PGP SIGNATURE" in signature # A signature should always be able to be added ... assert b"BEGIN PGP SIGNATURE" in image_config.get_bytes() signatures = image_config.get_signature_list() assert len(signatures) == 1 assert signatures[0].digest == config_digest_canonical assert signatures[0].signature == signature