From 7e2cd923200fc0942158772dd212b9746c7f8fd7 Mon Sep 17 00:00:00 2001 From: rafcio19 Date: Fri, 23 Jun 2023 21:46:07 +0100 Subject: [PATCH] SSM: missing type from param update response (#6436) --- moto/ssm/models.py | 20 +++++++++++++------- tests/test_ssm/test_ssm_boto3.py | 22 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/moto/ssm/models.py b/moto/ssm/models.py index 36865ec1a9de..f3c8734b0c2b 100644 --- a/moto/ssm/models.py +++ b/moto/ssm/models.py @@ -213,7 +213,7 @@ def __init__( ): self.account_id = account_id self.name = name - self.type = parameter_type + self.parameter_type = parameter_type self.description = description self.allowed_pattern = allowed_pattern self.keyid = keyid @@ -224,7 +224,7 @@ def __init__( self.labels = labels or [] self.source_result = source_result - if self.type == "SecureString": + if self.parameter_type == "SecureString": if not self.keyid: self.keyid = "alias/aws/ssm" @@ -236,7 +236,7 @@ def encrypt(self, value: str) -> str: return f"kms:{self.keyid}:" + value def decrypt(self, value: str) -> Optional[str]: - if self.type != "SecureString": + if self.parameter_type != "SecureString": return value prefix = f"kms:{self.keyid or 'default'}:" @@ -249,7 +249,7 @@ def response_object( ) -> Dict[str, Any]: r: Dict[str, Any] = { "Name": self.name, - "Type": self.type, + "Type": self.parameter_type, "Value": self.decrypt(self.value) if decrypt else self.value, "Version": self.version, "LastModifiedDate": round(self.last_modified_date, 3), @@ -1507,7 +1507,7 @@ def describe_parameters( break elif _filter["Key"] == "Type": for v in _filter["Values"]: - if ssm_parameter.type == v: + if ssm_parameter.parameter_type == v: result.append(ssm_parameter) break elif _filter["Key"] == "KeyId": @@ -1793,7 +1793,7 @@ def _match_filters( what = "/" + parameter.name.lstrip("/") values = ["/" + value.strip("/") for value in values] elif key == "Type": - what = parameter.type + what = parameter.parameter_type elif key == "Label": what = parameter.labels # Label filter can only have option="Equals" (also valid implicitly) @@ -1994,7 +1994,10 @@ def put_parameter( "formed as a mix of letters, numbers and the following 3 symbols .-_" ) raise ValidationException(invalid_prefix_error) - + if not parameter_type and not overwrite and not self._parameters[name]: + raise ValidationException( + "A parameter type is required when you create a parameter." + ) if ( not _valid_parameter_type(parameter_type) and not overwrite @@ -2022,6 +2025,9 @@ def put_parameter( if not overwrite: return None + # overwriting a parameter, Type is not included in boto3 call + if not parameter_type and overwrite: + parameter_type = previous_parameter.parameter_type if len(previous_parameter_versions) >= PARAMETER_VERSION_LIMIT: self._check_for_parameter_version_limit_exception(name) diff --git a/tests/test_ssm/test_ssm_boto3.py b/tests/test_ssm/test_ssm_boto3.py index 6c227ee8d0dc..29aa73adec02 100644 --- a/tests/test_ssm/test_ssm_boto3.py +++ b/tests/test_ssm/test_ssm_boto3.py @@ -431,16 +431,35 @@ def test_put_parameter_invalid_type(): ) +@mock_ssm +def test_put_parameter_no_type(): + client = boto3.client("ssm", "us-east-1") + with pytest.raises(ClientError) as e: + client.put_parameter( + Name="test_name", + Value="some_value", + ) + ex = e.value + assert ex.operation_name == "PutParameter" + assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400 + assert ex.response["Error"]["Code"] == "ValidationException" + assert ( + ex.response["Error"]["Message"] + == "A parameter type is required when you create a parameter." + ) + + @mock_ssm def test_update_parameter(): # Setup client = boto3.client("ssm", "us-east-1") param_name = "test_param" + param_type = "String" updated_value = "UpdatedValue" client.put_parameter( Description="Description", Name=param_name, - Type="String", + Type=param_type, Value="Value", ) @@ -454,6 +473,7 @@ def test_update_parameter(): # Verify assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 + assert new_param["Parameter"]["Type"] == param_type assert new_param["Parameter"]["Value"] == updated_value