Skip to content

Commit b34c67e

Browse files
committed
Add mpr validations to rpdk
1 parent 0e1551d commit b34c67e

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/rpdk/core/data_loaders.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
TIMEOUT_IN_SECONDS = 10
2323
STDIN_NAME = "<stdin>"
24+
MAX_CONFIGURATION_SCHEMA_LENGTH = 60 * 1024 # 60 KiB
2425

2526

2627
def resource_stream(package_name, resource_name, encoding="utf-8"):
@@ -152,6 +153,12 @@ def load_resource_spec(resource_spec_file): # pylint: disable=R # noqa: C901
152153
LOG.debug("Resource spec decode failed", exc_info=True)
153154
raise SpecValidationError(str(e)) from e
154155

156+
# check TypeConfiguration schema size
157+
if len(json.dumps(resource_spec).encode("utf-8")) > MAX_CONFIGURATION_SCHEMA_LENGTH:
158+
raise SpecValidationError(
159+
"TypeConfiguration schema exceeds maximum length of 60 KiB"
160+
)
161+
155162
validator = make_resource_validator()
156163
additional_properties_validator = (
157164
make_resource_validator_with_additional_properties_check()

src/rpdk/core/project.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
# https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateRole.html
9696
MIN_ROLE_TIMEOUT_SECONDS = 3600 # 1 hour
9797
MAX_ROLE_TIMEOUT_SECONDS = 43200 # 12 hours
98+
MAX_RPDK_CONFIG_LENGTH = 10 * 1024 # 10 KiB
99+
MAX_CONFIGURATION_SCHEMA_LENGTH = 60 * 1024 # 60 KiB
100+
101+
PROTOCOL_VERSION_VALUES = frozenset({"1.0.0", "2.0.0"})
98102

99103
CFN_METADATA_FILENAME = ".cfn_metadata.json"
100104

@@ -282,6 +286,31 @@ def load_settings(self):
282286
f"Project file '{self.settings_path}' is invalid", e
283287
)
284288

289+
# check size of RPDK config
290+
if len(json.dumps(raw_settings).encode("utf-8")) > MAX_RPDK_CONFIG_LENGTH:
291+
raise InvalidProjectError(
292+
f"Project file '{self.settings_path}' exceeds maximum length of 10 KiB."
293+
)
294+
# validate protocol version, if specified
295+
try:
296+
settings = raw_settings["settings"]
297+
if "protocolVersion" in settings:
298+
protocol_version = settings["protocolVersion"]
299+
if protocol_version not in PROTOCOL_VERSION_VALUES:
300+
raise InvalidProjectError(
301+
f"Invalid 'protocolVersion' settings in '{self.settings_path}"
302+
)
303+
else:
304+
LOG.warning(
305+
"No protovolVersion found: this will default to version 1.0.0 during registration. "
306+
"Please consider upgrading to CFN-CLI 2.0 following the guide: "
307+
"https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/what-is-cloudformation-cli.html"
308+
)
309+
except KeyError:
310+
raise InvalidProjectError(
311+
f"Error extracting protocol version from '{self.settings_path}'"
312+
)
313+
285314
# backward compatible
286315
if "artifact_type" not in raw_settings:
287316
raw_settings["artifact_type"] = ARTIFACT_TYPE_RESOURCE

src/rpdk/core/validate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
LOG = logging.getLogger(__name__)
1010

1111

12+
# validations for cfn validate are done in both project.py and data_loaders.py
1213
def validate(_args):
1314
project = Project()
1415
project.load()

0 commit comments

Comments
 (0)