Skip to content

Fix: Make secret value decoded for specific usecases #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion localstack-core/localstack/services/secretsmanager/provider.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

import base64
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using from base64 import b64decode for a more specific import

import json
import logging
import re
import time
from typing import Final, Optional, Union
from typing import Any, Dict, Final, Optional, Union

import moto.secretsmanager.exceptions as moto_exception
from botocore.utils import InvalidArnException
Expand Down Expand Up @@ -246,6 +247,7 @@ def get_secret_value(
self._raise_if_default_kms_key(secret_id, context, backend)
try:
response = backend.get_secret_value(secret_id, version_id, version_stage)
response = decode_secret_binary_from_response(response)
except moto_exception.SecretNotFoundException:
raise ResourceNotFoundException(
f"Secrets Manager can't find the specified secret value for staging label: {version_stage}"
Expand Down Expand Up @@ -863,6 +865,13 @@ def get_resource_policy_response(self):
return self.backend.get_resource_policy(secret_id=secret_id)


def decode_secret_binary_from_response(response: Dict[str, Any]):
if "SecretBinary" in response:
response["SecretBinary"] = base64.b64decode(response["SecretBinary"])

return response


def delete_resource_policy_model(self, secret_id):
if self._is_valid_identifier(secret_id):
self.secrets[secret_id].policy = None
Expand Down
43 changes: 43 additions & 0 deletions tests/aws/services/secretsmanager/test_secretsmanager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import json
import logging
import os
Expand Down Expand Up @@ -2395,6 +2396,48 @@ def test_get_secret_value_errors(self, aws_client, create_secret, sm_snapshot):
)
sm_snapshot.match("mismatch_version_id_and_stage", exc.value.response)

@markers.aws.validated
@markers.snapshot.skip_snapshot_verify(paths=["$..CreatedDate"])
def test_get_secret_value(
self, aws_client, aws_http_client_factory, region_name, create_secret, sm_snapshot
):
secret_name = short_uid()
secret_string = "footest"
secret_string_b64_encoded = base64.b64encode(secret_string.encode())

response = create_secret(
Name=secret_name,
SecretBinary=secret_string,
)

sm_snapshot.add_transformers_list(
sm_snapshot.transform.secretsmanager_secret_id_arn(response, 0)
)

secret_arn = response["ARN"]

secret_value_response = aws_client.secretsmanager.get_secret_value(SecretId=secret_arn)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a constant or configuration value for the secret string instead of hardcoding 'footest'


sm_snapshot.match("secret_value_response", secret_value_response)

assert secret_value_response["SecretBinary"] == secret_string.encode()

client = aws_http_client_factory(
"secretsmanager", region=region_name, signer_factory=SigV4Auth
)
parameters = {"SecretId": secret_name}

headers = {
"X-Amz-Target": "secretsmanager.GetSecretValue",
"Content-Type": "application/x-amz-json-1.1",
}

response = client.post("/", data=json.dumps(parameters), headers=headers)
json_response = response.json()
sm_snapshot.match("secret_value_http_response", json_response)

assert json_response["SecretBinary"] == str(secret_string_b64_encoded, encoding="utf-8")


class TestSecretsManagerMultiAccounts:
@markers.aws.validated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4509,5 +4509,34 @@
"get_secret_value_version_not_found_ex": "An error occurred (ResourceNotFoundException) when calling the GetSecretValue operation: Secrets Manager can't find the specified secret value for VersionId: <version-id>",
"get_secret_value_stage_not_found_ex": "An error occurred (ResourceNotFoundException) when calling the GetSecretValue operation: Secrets Manager can't find the specified secret value for staging label: AWSPENDING"
}
},
"tests/aws/services/secretsmanager/test_secretsmanager.py::TestSecretsManager::test_get_secret_value": {
"recorded-date": "19-09-2024, 20:19:26",
"recorded-content": {
"secret_value_response": {
"ARN": "arn:<partition>:secretsmanager:<region>:111111111111:secret:<SecretId-0idx><ArnPart-0idx>",
"CreatedDate": "datetime",
"Name": "<SecretId-0idx>",
"SecretBinary": "b'footest'",
"VersionId": "<version_uuid:1>",
"VersionStages": [
"AWSCURRENT"
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"secret_value_http_response": {
"ARN": "arn:<partition>:secretsmanager:<region>:111111111111:secret:<SecretId-0idx><ArnPart-0idx>",
"CreatedDate": 1726777166.068,
"Name": "<SecretId-0idx>",
"SecretBinary": "Zm9vdGVzdA==",
"VersionId": "<version_uuid:1>",
"VersionStages": [
"AWSCURRENT"
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"tests/aws/services/secretsmanager/test_secretsmanager.py::TestSecretsManager::test_get_random_exclude_characters_and_symbols": {
"last_validated_date": "2024-03-15T08:12:01+00:00"
},
"tests/aws/services/secretsmanager/test_secretsmanager.py::TestSecretsManager::test_get_secret_value": {
"last_validated_date": "2024-09-19T20:19:26+00:00"
},
"tests/aws/services/secretsmanager/test_secretsmanager.py::TestSecretsManager::test_get_secret_value_errors": {
"last_validated_date": "2024-04-11T05:37:47+00:00"
},
Expand Down