From 21b49f6fca42fe014773bea1fb1bd7aa9fac628e Mon Sep 17 00:00:00 2001 From: Dave Redmin Date: Sat, 11 Apr 2020 10:14:13 -0400 Subject: [PATCH 1/2] Handle the case where the tags element exists, but it is set to None. --- project_setup/scripts/terraform-to-secrets | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project_setup/scripts/terraform-to-secrets b/project_setup/scripts/terraform-to-secrets index 00c8fbc..f0b2d4f 100755 --- a/project_setup/scripts/terraform-to-secrets +++ b/project_setup/scripts/terraform-to-secrets @@ -130,6 +130,8 @@ def parse_tagged_resources( """Search all resources for tags requesting the creation of a secret.""" for resource in find_resources(terraform_state, None): tags: Dict[str, str] = resource["values"].get("tags", dict()) + if tags is None: + tags = dict() secret_name: Optional[str] = tags.get(GITHUB_SECRET_NAME_TAG) lookup_tag: Optional[str] = tags.get(GITHUB_SECRET_TERRAFORM_LOOKUP_TAG) secret_value: str From 99545185ac48c295d3217c7c6692f45b9f057aa3 Mon Sep 17 00:00:00 2001 From: Dave Redmin Date: Sun, 12 Apr 2020 09:48:53 -0400 Subject: [PATCH 2/2] Make code a bit clearer and cleaner. Thanks to @felddy for the suggestion. --- project_setup/scripts/terraform-to-secrets | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/project_setup/scripts/terraform-to-secrets b/project_setup/scripts/terraform-to-secrets index f0b2d4f..7bf3eba 100755 --- a/project_setup/scripts/terraform-to-secrets +++ b/project_setup/scripts/terraform-to-secrets @@ -129,9 +129,13 @@ def parse_tagged_resources( ) -> Generator[Tuple[str, str], None, None]: """Search all resources for tags requesting the creation of a secret.""" for resource in find_resources(terraform_state, None): - tags: Dict[str, str] = resource["values"].get("tags", dict()) - if tags is None: + # Ensure "tags" key exists in resource["values"] and if it does, + # make sure its value is not None. Both of these cases can occur. + tags: Dict[str, str] + if "tags" not in resource["values"] or resource["values"].get("tags") is None: tags = dict() + else: + tags = resource["values"]["tags"] secret_name: Optional[str] = tags.get(GITHUB_SECRET_NAME_TAG) lookup_tag: Optional[str] = tags.get(GITHUB_SECRET_TERRAFORM_LOOKUP_TAG) secret_value: str