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

Change transform check in W3037 #4041

Merged
merged 1 commit into from
Mar 20, 2025
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
6 changes: 2 additions & 4 deletions src/cfnlint/rules/functions/SubUnneeded.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import regex as re

from cfnlint.helpers import REGEX_SUB_PARAMETERS, ensure_list
from cfnlint.helpers import REGEX_SUB_PARAMETERS, TRANSFORM_SAM, ensure_list
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
from cfnlint.rules import CloudFormationLintRule

Expand All @@ -24,9 +24,7 @@ class SubUnneeded(CloudFormationLintRule):
def validate(
self, validator: Validator, s: Any, instance: Any, schema: Any
) -> ValidationResult:
if "AWS::Serverless-2016-10-31" in ensure_list(
validator.cfn.transform_pre.get("Transform")
):
if TRANSFORM_SAM in ensure_list(validator.cfn.transform_pre.get("Transform")):
return
if validator.is_type(instance, "string"):
variables = re.findall(REGEX_SUB_PARAMETERS, instance)
Expand Down
3 changes: 2 additions & 1 deletion src/cfnlint/rules/resources/HardCodedArnProperties.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import regex as re

from cfnlint._typing import RuleMatches
from cfnlint.helpers import TRANSFORM_SAM
from cfnlint.rules import CloudFormationLintRule, RuleMatch
from cfnlint.template import Template

Expand Down Expand Up @@ -87,7 +88,7 @@ def match(self, cfn: Template) -> RuleMatches:

transforms = cfn.transform_pre["Transform"]
transforms = transforms if isinstance(transforms, list) else [transforms]
if "AWS::Serverless-2016-10-31" in cfn.transform_pre["Transform"]:
if TRANSFORM_SAM in cfn.transform_pre["Transform"]:
return matches

# Get a list of paths to every leaf node string containing at least one ${parameter}
Expand Down
4 changes: 2 additions & 2 deletions src/cfnlint/rules/resources/iam/Permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import regex as re

from cfnlint.data import AdditionalSpecs
from cfnlint.helpers import ensure_list, load_resource
from cfnlint.helpers import TRANSFORM_SAM, ensure_list, load_resource
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword

Expand Down Expand Up @@ -41,7 +41,7 @@ def validate(
) -> ValidationResult:
# Escape validation when using SAM transforms as a result of
# https://github.com/aws/serverless-application-model/issues/3633
if validator.context.transforms.has_sam_transform():
if TRANSFORM_SAM in validator.cfn.transform_pre["Transform"]:
return

actions = ensure_list(instance)
Expand Down
7 changes: 4 additions & 3 deletions src/cfnlint/template/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def __init__(
self.transform_pre["Fn::Sub"] = self.search_deep_keys("Fn::Sub")
self.transform_pre["Fn::If"] = self.search_deep_keys("Fn::If")
self.transform_pre["Fn::FindInMap"] = self.search_deep_keys("Fn::FindInMap")
self.transform_pre["Transform"] = self.template.get("Transform", [])
self.transform_pre["Transform"] = cfnlint.helpers.ensure_list(
self.template.get("Transform", [])
)
self.transform_pre["Fn::ForEach"] = self.search_deep_keys(
cfnlint.helpers.FUNCTION_FOR_EACH
)
Expand Down Expand Up @@ -155,14 +157,13 @@ def has_serverless_transform(self) -> bool:
Returns:
bool: True if the AWS::Serverless-2016-10-31 transform is declared, False otherwise.
"""
lang_extensions_transform = "AWS::Serverless-2016-10-31"
transform_declaration = self.transform_pre["Transform"]
transform_type = (
transform_declaration
if isinstance(transform_declaration, list)
else [transform_declaration]
)
return bool(lang_extensions_transform in transform_type)
return bool(cfnlint.helpers.TRANSFORM_SAM in transform_type)

def is_cdk_template(self) -> bool:
"""Check if the template was created by the AWS Cloud Development Kit (CDK).
Expand Down
6 changes: 3 additions & 3 deletions src/cfnlint/template/transforms/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from cfnlint.conditions import Conditions
from cfnlint.context import create_context_for_template
from cfnlint.graph import Graph
from cfnlint.helpers import format_json_string
from cfnlint.helpers import TRANSFORM_SAM, format_json_string
from cfnlint.match import Match
from cfnlint.template.transforms._language_extensions import language_extension
from cfnlint.template.transforms._sam import sam
Expand All @@ -23,7 +23,7 @@
class Transform:
def __init__(self) -> None:
self.transforms: Mapping[str, Callable[[Any], TransformResult]] = {
"AWS::Serverless-2016-10-31": sam,
TRANSFORM_SAM: sam,
"AWS::LanguageExtensions": language_extension,
}

Expand Down Expand Up @@ -62,7 +62,7 @@ def transform(self, cfn: Any) -> list[Match]:
# SAM will erase the entire Transform section
# this sets it back with all transforms except SAM
cfn.template["Transform"] = [
t for t in transform_type if t != "AWS::Serverless-2016-10-31"
t for t in transform_type if t != TRANSFORM_SAM
]

LOGGER.info("Transformed template: \n%s", format_json_string(cfn.template))
Expand Down
Loading