Skip to content
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

Bring back stateful resources json #3728

Merged
merged 1 commit into from
Oct 2, 2024
Merged
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
35 changes: 35 additions & 0 deletions src/cfnlint/data/AdditionalSpecs/StatefulResources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"ResourceTypes": {
"AWS::Backup::BackupVault": {},
"AWS::CloudFormation::Stack": {},
"AWS::Cognito::UserPool": {},
"AWS::DocDB::DBCluster": {},
"AWS::DocDB::DBInstance": {},
"AWS::DynamoDB::GlobalTable": {},
"AWS::DynamoDB::Table": {},
"AWS::EC2::Volume": {},
"AWS::EFS::FileSystem": {},
"AWS::EMR::Cluster": {},
"AWS::ElastiCache::CacheCluster": {},
"AWS::ElastiCache::ReplicationGroup": {},
"AWS::Elasticsearch::Domain": {},
"AWS::FSx::FileSystem": {},
"AWS::KMS::Key": {},
"AWS::Kinesis::Stream": {},
"AWS::Logs::LogGroup": {},
"AWS::Neptune::DBCluster": {},
"AWS::Neptune::DBInstance": {},
"AWS::OpenSearchService::Domain": {},
"AWS::Organizations::Account": {},
"AWS::QLDB::Ledger": {},
"AWS::RDS::DBCluster": {},
"AWS::RDS::DBInstance": {},
"AWS::Redshift::Cluster": {},
"AWS::S3::Bucket": {
"DeleteRequiresEmptyResource": true
},
"AWS::SDB::Domain": {},
"AWS::SQS::Queue": {},
"AWS::SecretsManager::Secret": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

from typing import Any

import cfnlint.data.AdditionalSpecs
from cfnlint.helpers import load_resource
from cfnlint.jsonschema import Validator
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema


class UpdateReplacePolicyDeletionPolicyOnStatefulResourceTypes(CfnLintJsonSchema):
"""Check for UpdateReplacePolicy / DeletionPolicy"""

id = "I3011"
shortdesc = "Check stateful resources have a set UpdateReplacePolicy/DeletionPolicy"
description = (
Expand All @@ -28,36 +28,13 @@ def __init__(self):
all_matches=True,
)

self.config["types"] = [
"AWS::Backup::BackupVault",
"AWS::CloudFormation::Stack",
"AWS::Cognito::UserPool",
"AWS::DocDB::DBCluster",
"AWS::DocDB::DBInstance",
"AWS::DynamoDB::GlobalTable",
"AWS::DynamoDB::Table",
"AWS::EC2::Volume",
"AWS::EFS::FileSystem",
"AWS::EMR::Cluster",
"AWS::ElastiCache::CacheCluster",
"AWS::ElastiCache::ReplicationGroup",
"AWS::Elasticsearch::Domain",
"AWS::FSx::FileSystem",
"AWS::KMS::Key",
"AWS::Kinesis::Stream",
"AWS::Logs::LogGroup",
"AWS::Neptune::DBCluster",
"AWS::Neptune::DBInstance",
"AWS::OpenSearchService::Domain",
"AWS::Organizations::Account",
"AWS::QLDB::Ledger",
"AWS::RDS::DBCluster",
"AWS::RDS::DBInstance",
"AWS::Redshift::Cluster",
# "AWS::S3::Bucket", # can't be deleted without being empty
"AWS::SDB::Domain",
"AWS::SQS::Queue",
"AWS::SecretsManager::Secret",
spec = load_resource(cfnlint.data.AdditionalSpecs, "StatefulResources.json")
self.likely_stateful_resource_types = [
resource_type
for resource_type, descr in spec["ResourceTypes"].items()
# Resources that won't be deleted if they're not empty (ex: S3)
# don't need to be checked for policies, as chance of mistakes are low.
if not descr.get("DeleteRequiresEmptyResource", False)
]

self._schema = {"required": ["DeletionPolicy", "UpdateReplacePolicy"]}
Expand All @@ -68,7 +45,7 @@ def validate(self, validator: Validator, s: Any, instance: Any, schema: Any):
if not isinstance(resource_type, str):
return

if resource_type not in self.config.get("types"): # type: ignore
if resource_type not in self.likely_stateful_resource_types: # type: ignore
return

for err in super().validate(validator, s, instance, self._schema):
Expand Down
Loading