Skip to content

Fix ssm list parameter tags #12407

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions localstack-core/localstack/services/ssm/patches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from moto.ssm.models import SimpleSystemManagerBackend

from localstack.utils.patch import patch


def apply_all_patches():
@patch(SimpleSystemManagerBackend.list_tags_for_resource)
def ssm_validate_resource_type_and_id(fn, self, resource_type: str, resource_id: str):
if resource_type != "Parameter":
return fn(self, resource_type, resource_id)

if resource_id.startswith("/") and resource_id.count("/") == 1:
resource_id = resource_id.lstrip("/")

return fn(self, resource_type, resource_id)
12 changes: 10 additions & 2 deletions localstack-core/localstack/services/ssm/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import logging
import time
from abc import ABC
from typing import Dict, Optional

from localstack.aws.api import CommonServiceException, RequestContext
Expand Down Expand Up @@ -78,6 +77,8 @@
)
from localstack.aws.connect import connect_to
from localstack.services.moto import call_moto, call_moto_with_request
from localstack.services.plugins import ServiceLifecycleHook
from localstack.services.ssm.patches import apply_all_patches
from localstack.utils.aws.arns import extract_resource_from_arn, is_arn
from localstack.utils.bootstrap import is_api_enabled
from localstack.utils.collections import remove_attributes
Expand All @@ -104,7 +105,14 @@ def __init__(self):


# TODO: check if _normalize_name(..) calls are still required here
class SsmProvider(SsmApi, ABC):
class SsmProvider(SsmApi, ServiceLifecycleHook):
def on_after_init(self):
self.apply_patches()

@staticmethod
def apply_patches():
apply_all_patches()

def get_parameters(
self,
context: RequestContext,
Expand Down
57 changes: 57 additions & 0 deletions tests/aws/services/ssm/test_ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,60 @@ def test_parameters_with_path(self, create_parameter, aws_client, snapshot):
Names=[get_parameter_response["Parameter"]["ARN"]]
)
snapshot.match("get-parameters-by-arn-response", get_parameters_by_arn_response)

@markers.aws.validated
@markers.snapshot.skip_snapshot_verify(paths=["$..Error.Message"]) # Moto adds an error message
def test_list_tags_for_parameter(self, aws_client, create_parameter, snapshot):
param_name = f"param-{short_uid()}"

create_parameter(
Name=param_name,
Description="test",
Value="123",
Type="String",
Tags=[{"Key": "my-tags", "Value": "123"}],
)
tags = aws_client.ssm.list_tags_for_resource(
ResourceId=param_name, ResourceType="Parameter"
)
snapshot.match("list-tags", tags)

tags = aws_client.ssm.list_tags_for_resource(
ResourceId=f"/{param_name}", ResourceType="Parameter"
)
snapshot.match("list-tags-with-leading-slash", tags)

with pytest.raises(aws_client.ssm.exceptions.InvalidResourceId) as exc:
aws_client.ssm.list_tags_for_resource(
ResourceId=f"//{param_name}", ResourceType="Parameter"
)
snapshot.match("invalid-resource-with-two-slashes", exc.value.response)

@markers.aws.validated
@markers.snapshot.skip_snapshot_verify(paths=["$..Error.Message"]) # Moto adds an error message
def test_list_tags_for_nested_parameter(self, aws_client, create_parameter, snapshot):
param_name = f"/nested/param-{short_uid()}"

create_parameter(
Name=param_name,
Description="test",
Value="123",
Type="String",
Tags=[{"Key": "my-tags", "Value": "123"}],
)
tags = aws_client.ssm.list_tags_for_resource(
ResourceId=param_name, ResourceType="Parameter"
)
snapshot.match("list-tags", tags)

with pytest.raises(aws_client.ssm.exceptions.InvalidResourceId) as exc:
aws_client.ssm.list_tags_for_resource(
ResourceId=param_name.lstrip("/"), ResourceType="Parameter"
)
snapshot.match("list-nested-tags-missing-leading-slash", exc.value.response)

with pytest.raises(aws_client.ssm.exceptions.InvalidResourceId) as exc:
aws_client.ssm.list_tags_for_resource(
ResourceId=f"//{param_name}", ResourceType="Parameter"
)
snapshot.match("invalid-resource-with-two-slashes", exc.value.response)
76 changes: 76 additions & 0 deletions tests/aws/services/ssm/test_ssm.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,81 @@
}
}
}
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_list_tags_for_parameter": {
"recorded-date": "19-03-2025, 02:18:23",
"recorded-content": {
"list-tags": {
"TagList": [
{
"Key": "my-tags",
"Value": "123"
}
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"list-tags-with-leading-slash": {
"TagList": [
{
"Key": "my-tags",
"Value": "123"
}
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"invalid-resource-with-two-slashes": {
"Error": {
"Code": "InvalidResourceId",
"Message": ""
},
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 400
}
}
}
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_list_tags_for_nested_parameter": {
"recorded-date": "19-03-2025, 02:20:27",
"recorded-content": {
"list-tags": {
"TagList": [
{
"Key": "my-tags",
"Value": "123"
}
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"list-nested-tags-missing-leading-slash": {
"Error": {
"Code": "InvalidResourceId",
"Message": ""
},
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 400
}
},
"invalid-resource-with-two-slashes": {
"Error": {
"Code": "InvalidResourceId",
"Message": ""
},
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 400
}
}
}
}
}
6 changes: 6 additions & 0 deletions tests/aws/services/ssm/test_ssm.validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_get_parameter_by_arn": {
"last_validated_date": "2024-07-16T17:17:40+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_list_tags_for_nested_parameter": {
"last_validated_date": "2025-03-19T02:20:27+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_list_tags_for_parameter": {
"last_validated_date": "2025-03-19T02:18:23+00:00"
},
"tests/aws/services/ssm/test_ssm.py::TestSSM::test_parameters_with_path": {
"last_validated_date": "2025-01-08T17:04:53+00:00"
}
Expand Down
Loading