diff --git a/.github/workflows/test-server.yml b/.github/workflows/test-server.yml index a774015d..506df018 100644 --- a/.github/workflows/test-server.yml +++ b/.github/workflows/test-server.yml @@ -12,7 +12,7 @@ on: jobs: test-server: - runs-on: macos-14-xlarge + runs-on: macos-15-xlarge permissions: id-token: write contents: read diff --git a/compliance_exceptions/kms_keyring_exceptions.md b/compliance_exceptions/kms_keyring_exceptions.md index 0da55fb3..3ceee721 100644 --- a/compliance_exceptions/kms_keyring_exceptions.md +++ b/compliance_exceptions/kms_keyring_exceptions.md @@ -20,22 +20,6 @@ The Python S3 Encryption Client implementation takes a pragmatic approach that: ## Initialization Validation -##= specification/s3-encryption/materials/s3-kms-keyring.md#initialization -##= type=exception -##% The KmsKeyring MAY validate that the AWS KMS key identifier is not null or empty. - -Justification: This validation is not implemented. The Python implementation relies on attrs field validation and KMS SDK to catch invalid key identifiers. - ---- - -##= specification/s3-encryption/materials/s3-kms-keyring.md#initialization -##= type=exception -##% If the KmsKeyring validates that the AWS KMS key identifier is not null or empty, then it MUST throw an exception. - -Justification: Not applicable since the MAY validation above is not implemented. If we don't validate, we don't need to throw an exception for validation failure. - ---- - ##= specification/s3-encryption/materials/s3-kms-keyring.md#initialization ##= type=exception ##% The KmsKeyring MAY validate that the AWS KMS key identifier is [a valid AWS KMS Key identifier](../../framework/aws-kms/aws-kms-key-arn.md#a-valid-aws-kms-identifier). diff --git a/src/s3_encryption/materials/kms_keyring.py b/src/s3_encryption/materials/kms_keyring.py index d829d886..91955d26 100644 --- a/src/s3_encryption/materials/kms_keyring.py +++ b/src/s3_encryption/materials/kms_keyring.py @@ -52,6 +52,17 @@ class KmsKeyring(S3Keyring): ##% The KmsV1 mode MUST be only enabled when legacy wrapping algorithms are enabled. enable_legacy_wrapping_algorithms: bool = field(default=False) + ##= specification/s3-encryption/materials/s3-kms-keyring.md#initialization + ##= type=implementation + ##% The KmsKeyring MAY validate that the AWS KMS key identifier is not null or empty. + ##= specification/s3-encryption/materials/s3-kms-keyring.md#initialization + ##= type=implementation + ##% If the KmsKeyring validates that the AWS KMS key identifier is not null or empty, then it MUST throw an exception. + @kms_key_id.validator + def _validate_kms_key_id(self, attribute, value): + if value is None or value == "": + raise S3EncryptionClientError("Kms Key ID cannot be empty or null") + def __attrs_post_init__(self): # noqa: D105 from .._utils import _USER_AGENT_SUFFIX, append_user_agent diff --git a/test/integration/test_i_s3_encryption.py b/test/integration/test_i_s3_encryption.py index 4074c6ed..5dcd4927 100644 --- a/test/integration/test_i_s3_encryption.py +++ b/test/integration/test_i_s3_encryption.py @@ -314,6 +314,21 @@ def test_inaccessible_kms_key_raises_access_denied(): assert cause.response["Error"]["Code"] == "AccessDeniedException" +##= specification/s3-encryption/materials/s3-kms-keyring.md#initialization +##= type=test +##% The KmsKeyring MAY validate that the AWS KMS key identifier is not null or empty. +##= specification/s3-encryption/materials/s3-kms-keyring.md#initialization +##= type=test +##% If the KmsKeyring validates that the AWS KMS key identifier is not null or empty, then it MUST throw an exception. +@pytest.mark.parametrize("invalid_kms_key_id", [None, ""], ids=["null", "empty"]) +def test_null_or_empty_kms_key_id_raises_at_keyring_creation(invalid_kms_key_id): + """A null or empty KMS key ID MUST be rejected when the KmsKeyring is created.""" + kms_client = boto3.client("kms", region_name=region) + + with pytest.raises(S3EncryptionClientError): + KmsKeyring(kms_client, invalid_kms_key_id) + + def test_get_nonexistent_object_raises_no_such_key(): """get_object for a key that doesn't exist MUST surface NoSuchKey.""" from botocore.exceptions import ClientError