Skip to content

Commit

Permalink
Fix an issue where bad regions aren't caught at cli (#3545)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Jul 30, 2024
1 parent 16a194f commit e17d66e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/cfnlint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cfnlint.maintenance
from cfnlint.config import ConfigMixIn, configure_logging
from cfnlint.decode.decode import decode
from cfnlint.helpers import REGIONS
from cfnlint.rules import Match, Rules
from cfnlint.rules.errors import ParseError, TransformError
from cfnlint.schema import PROVIDER_SCHEMA_MANAGER
Expand Down Expand Up @@ -122,6 +123,18 @@ def run(self) -> Iterator[Match]:
Match: The matches found by running the rules against the template.
"""
LOGGER.info("Run scan of template %s", self.cfn.filename)
if not set(self.config.regions).issubset(set(REGIONS)):
unsupported_regions = list(
set(self.config.regions).difference(set(REGIONS))
)
raise InvalidRegionException(
(
f"Regions {unsupported_regions!r} are unsupported. "
f"Supported regions are {REGIONS!r}"
),
32,
)

matches = self.cfn.transform()
if matches:
if self.rules.is_rule_enabled(TransformError(), self.config):
Expand Down Expand Up @@ -425,7 +438,11 @@ def cli(self) -> None:
self.config.parser.print_help()
sys.exit(1)

self._cli_output(list(self.run()))
try:
self._cli_output(list(self.run()))
except CfnLintExitException as e:
LOGGER.error(str(e))
sys.exit(e.exit_code)


def main() -> None:
Expand Down
17 changes: 17 additions & 0 deletions test/unit/module/runner/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,20 @@ def test_print_help(self, mock_isatty, mock_print_help):
self.assertEqual(e.exception.code, 1)
mock_print_help.assert_called_once()
mock_isatty.assert_called_once()

def test_bad_regions(self):
config = ConfigMixIn(
[
"--regions",
"us-north-5",
"--template",
"test/fixtures/templates/good/generic.yaml",
]
)

runner = Runner(config)

with self.assertRaises(SystemExit) as e:
runner.cli()

self.assertEqual(e.exception.code, 32)

0 comments on commit e17d66e

Please sign in to comment.