Skip to content

Commit e17d66e

Browse files
authored
Fix an issue where bad regions aren't caught at cli (#3545)
1 parent 16a194f commit e17d66e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/cfnlint/runner.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import cfnlint.maintenance
1616
from cfnlint.config import ConfigMixIn, configure_logging
1717
from cfnlint.decode.decode import decode
18+
from cfnlint.helpers import REGIONS
1819
from cfnlint.rules import Match, Rules
1920
from cfnlint.rules.errors import ParseError, TransformError
2021
from cfnlint.schema import PROVIDER_SCHEMA_MANAGER
@@ -122,6 +123,18 @@ def run(self) -> Iterator[Match]:
122123
Match: The matches found by running the rules against the template.
123124
"""
124125
LOGGER.info("Run scan of template %s", self.cfn.filename)
126+
if not set(self.config.regions).issubset(set(REGIONS)):
127+
unsupported_regions = list(
128+
set(self.config.regions).difference(set(REGIONS))
129+
)
130+
raise InvalidRegionException(
131+
(
132+
f"Regions {unsupported_regions!r} are unsupported. "
133+
f"Supported regions are {REGIONS!r}"
134+
),
135+
32,
136+
)
137+
125138
matches = self.cfn.transform()
126139
if matches:
127140
if self.rules.is_rule_enabled(TransformError(), self.config):
@@ -425,7 +438,11 @@ def cli(self) -> None:
425438
self.config.parser.print_help()
426439
sys.exit(1)
427440

428-
self._cli_output(list(self.run()))
441+
try:
442+
self._cli_output(list(self.run()))
443+
except CfnLintExitException as e:
444+
LOGGER.error(str(e))
445+
sys.exit(e.exit_code)
429446

430447

431448
def main() -> None:

test/unit/module/runner/test_cli.py

+17
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,20 @@ def test_print_help(self, mock_isatty, mock_print_help):
8080
self.assertEqual(e.exception.code, 1)
8181
mock_print_help.assert_called_once()
8282
mock_isatty.assert_called_once()
83+
84+
def test_bad_regions(self):
85+
config = ConfigMixIn(
86+
[
87+
"--regions",
88+
"us-north-5",
89+
"--template",
90+
"test/fixtures/templates/good/generic.yaml",
91+
]
92+
)
93+
94+
runner = Runner(config)
95+
96+
with self.assertRaises(SystemExit) as e:
97+
runner.cli()
98+
99+
self.assertEqual(e.exception.code, 32)

0 commit comments

Comments
 (0)