From 7e88a901d9ed7612d92bdeb9dd67056c99bf3126 Mon Sep 17 00:00:00 2001 From: stromp Date: Tue, 1 Mar 2022 23:50:42 +0100 Subject: [PATCH] fix for duplicate tag keys in secretsmanager (#4900) --- moto/secretsmanager/models.py | 10 ++++++++++ tests/test_secretsmanager/test_secretsmanager.py | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/moto/secretsmanager/models.py b/moto/secretsmanager/models.py index 783f9542d..b04b51dae 100644 --- a/moto/secretsmanager/models.py +++ b/moto/secretsmanager/models.py @@ -735,6 +735,16 @@ class SecretsManagerBackend(BaseBackend): old_tags = secret.tags for tag in tags: + existing_key_name = next( + ( + old_key + for old_key in old_tags + if old_key.get("Key") == tag.get("Key") + ), + None, + ) + if existing_key_name: + old_tags.remove(existing_key_name) old_tags.append(tag) return secret_id diff --git a/tests/test_secretsmanager/test_secretsmanager.py b/tests/test_secretsmanager/test_secretsmanager.py index 62ebc721b..48c69c4c8 100644 --- a/tests/test_secretsmanager/test_secretsmanager.py +++ b/tests/test_secretsmanager/test_secretsmanager.py @@ -1282,14 +1282,16 @@ def test_tag_resource(pass_arn): conn.tag_resource( SecretId=secret_id, Tags=[{"Key": "FirstTag", "Value": "SomeValue"},], ) - + conn.tag_resource( + SecretId="test-secret", Tags=[{"Key": "FirstTag", "Value": "SomeOtherValue"},], + ) conn.tag_resource( SecretId=secret_id, Tags=[{"Key": "SecondTag", "Value": "AnotherValue"},], ) secrets = conn.list_secrets() assert secrets["SecretList"][0].get("Tags") == [ - {"Key": "FirstTag", "Value": "SomeValue"}, + {"Key": "FirstTag", "Value": "SomeOtherValue"}, {"Key": "SecondTag", "Value": "AnotherValue"}, ]