diff --git a/src/cfnlint/data/schemas/extensions/aws_rds_dbcluster/multiaz.json b/src/cfnlint/data/schemas/extensions/aws_rds_dbcluster/multiaz.json index c7af11b7ed..4dd6308c9c 100644 --- a/src/cfnlint/data/schemas/extensions/aws_rds_dbcluster/multiaz.json +++ b/src/cfnlint/data/schemas/extensions/aws_rds_dbcluster/multiaz.json @@ -14,6 +14,52 @@ ] }, "then": { + "allOf": [ + { + "if": { + "properties": { + "StorageType": { + "enum": [ + "io1", + "io2" + ] + } + }, + "required": [ + "StorageType" + ], + "type": "object" + }, + "then": { + "required": [ + "Iops" + ] + } + }, + { + "if": { + "properties": { + "StorageType": false + }, + "type": "object" + }, + "then": { + "required": [ + "Iops" + ] + } + } + ], + "else": { + "required": [ + "AllocatedStorage" + ] + }, + "if": { + "required": [ + "ReplicationSourceIdentifier" + ] + }, "properties": { "AvailabilityZones": false, "BacktrackWindow": false, @@ -25,7 +71,6 @@ "EngineMode": false, "GlobalClusterIdentifier": false, "NetworkType": false, - "ReplicationSourceIdentifier": false, "ScalingConfiguration": false, "ServerlessV2ScalingConfiguration": false, "SourceRegion": false, @@ -43,10 +88,12 @@ } }, "required": [ - "AllocatedStorage", - "DBClusterInstanceClass", - "Iops", - "StorageType" - ] + "DBClusterInstanceClass" + ], + "then": { + "properties": { + "AllocatedStorage": false + } + } } } diff --git a/src/cfnlint/rules/resources/rds/DbClusterMultiAz.py b/src/cfnlint/rules/resources/rds/DbClusterMultiAz.py index 041522563c..b6f47bc66f 100644 --- a/src/cfnlint/rules/resources/rds/DbClusterMultiAz.py +++ b/src/cfnlint/rules/resources/rds/DbClusterMultiAz.py @@ -11,6 +11,10 @@ from cfnlint.jsonschema import ValidationResult, Validator from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails +# Schema paths of the two Iops requirements in multiaz.json "then.allOf" +_IOPS_FOR_PROVISIONED_STORAGE = ["then", "allOf", 0, "then", "required"] +_IOPS_FOR_NO_STORAGE_TYPE = ["then", "allOf", 1, "then", "required"] + class DbClusterMultiAz(CfnLintJsonSchema): id = "E3692" @@ -32,17 +36,33 @@ def __init__(self) -> None: all_matches=True, ) - # DBClusterInstanceClass is required - # AllocatedStorage is required - # Iops is required + # Rewrite messages for forbidden properties and for conditional Iops def validate( self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any] ) -> ValidationResult: for err in super().validate(validator, keywords, instance, schema): - if err.schema is False: + schema_path = list(err.schema_path) + if schema_path == _IOPS_FOR_PROVISIONED_STORAGE: + err.message = ( + "'Iops' is a required property when 'StorageType' is " + f"{err.instance.get('StorageType')!r}" + ) + elif schema_path == _IOPS_FOR_NO_STORAGE_TYPE: err.message = ( - "Additional properties are not allowed " - f"{err.path[0]!r} when creating Multi-AZ cluster" + "Set 'Iops' or set 'StorageType' to 'gp3'; without either, " + "RDS uses gp2 storage, which Multi-AZ DB clusters do not " + "support" ) + elif err.schema is False: + if schema_path[:2] == ["then", "then"]: + err.message = ( + f"{err.path[0]!r} is not allowed on a Multi-AZ DB " + "cluster read replica (ReplicationSourceIdentifier is set)" + ) + else: + err.message = ( + "Additional properties are not allowed " + f"{err.path[0]!r} when creating Multi-AZ cluster" + ) yield err diff --git a/test/unit/rules/resources/rds/test_db_cluster_multiaz.py b/test/unit/rules/resources/rds/test_db_cluster_multiaz.py index e5d640e4ec..edb1d711e6 100644 --- a/test/unit/rules/resources/rds/test_db_cluster_multiaz.py +++ b/test/unit/rules/resources/rds/test_db_cluster_multiaz.py @@ -3,10 +3,12 @@ SPDX-License-Identifier: MIT-0 """ +import json from collections import deque import pytest +from cfnlint.api import lint from cfnlint.jsonschema import ValidationError from cfnlint.rules.resources.rds.DbClusterMultiAz import DbClusterMultiAz @@ -30,6 +32,15 @@ def rule(): }, [], ), + ( + { + "Engine": "postgres", + "AllocatedStorage": 100, + "DBClusterInstanceClass": "foo", + "Iops": 1000, + }, + [], + ), ( { "Engine": "aurora-mysql", @@ -78,7 +89,7 @@ def rule(): rule=DbClusterMultiAz(), path=deque([]), validator="required", - schema_path=deque(["then", "required"]), + schema_path=deque(["then", "else", "required"]), ), ValidationError( "'DBClusterInstanceClass' is a required property", @@ -88,19 +99,179 @@ def rule(): schema_path=deque(["then", "required"]), ), ValidationError( - "'Iops' is a required property", + "Set 'Iops' or set 'StorageType' to 'gp3'; without either, " + "RDS uses gp2 storage, which Multi-AZ DB clusters do not " + "support", rule=DbClusterMultiAz(), path=deque([]), validator="required", - schema_path=deque(["then", "required"]), + schema_path=deque(["then", "allOf", 1, "then", "required"]), + ), + ], + ), + ( + { + "Engine": "postgres", + "DBClusterInstanceClass": "foo", + "Iops": 1000, + "StorageType": "io1", + "ReplicationSourceIdentifier": { + "Fn::GetAtt": ["SourceInstance", "DBInstanceArn"] + }, + }, + [], + ), + ( + { + "Engine": "postgres", + "DBClusterInstanceClass": "foo", + "Iops": 1000, + "ReplicationSourceIdentifier": {"Ref": "SourceDbArn"}, + }, + [], + ), + ( + { + "Engine": "postgres", + "DBClusterInstanceClass": "foo", + "Iops": 1000, + "StorageType": "gp2", + "ReplicationSourceIdentifier": {"Ref": "SourceDbArn"}, + }, + [ + ValidationError( + "'gp2' is not one of ['io1', 'io2', 'gp3']", + rule=DbClusterMultiAz(), + path=deque(["StorageType"]), + validator="enum", + schema_path=deque( + ["then", "properties", "StorageType", "then", "enum"] + ), + ), + ], + ), + ( + { + "Engine": "mysql", + "AllocatedStorage": 100, + "DBClusterInstanceClass": "foo", + "Iops": 1000, + "StorageType": "io1", + "ReplicationSourceIdentifier": {"Ref": "SourceDbArn"}, + }, + [ + ValidationError( + "'AllocatedStorage' is not allowed on a Multi-AZ DB " + "cluster read replica (ReplicationSourceIdentifier is set)", + rule=DbClusterMultiAz(), + path=deque(["AllocatedStorage"]), + validator=None, + schema_path=deque( + ["then", "then", "properties", "AllocatedStorage"] + ), ), + ], + ), + ( + { + "Engine": "mysql", + "ReplicationSourceIdentifier": {"Ref": "SourceDbArn"}, + }, + [ ValidationError( - "'StorageType' is a required property", + "'DBClusterInstanceClass' is a required property", rule=DbClusterMultiAz(), path=deque([]), validator="required", schema_path=deque(["then", "required"]), ), + ValidationError( + "Set 'Iops' or set 'StorageType' to 'gp3'; without either, " + "RDS uses gp2 storage, which Multi-AZ DB clusters do not " + "support", + rule=DbClusterMultiAz(), + path=deque([]), + validator="required", + schema_path=deque(["then", "allOf", 1, "then", "required"]), + ), + ], + ), + ( + { + "Engine": "postgres", + "AllocatedStorage": 100, + "DBClusterInstanceClass": "foo", + "StorageType": "gp3", + }, + [], + ), + ( + { + "Engine": "postgres", + "DBClusterInstanceClass": "foo", + "StorageType": "gp3", + "ReplicationSourceIdentifier": {"Ref": "SourceDbArn"}, + }, + [], + ), + ( + { + "Engine": "postgres", + "DBClusterInstanceClass": "foo", + "StorageType": "io1", + "ReplicationSourceIdentifier": {"Ref": "SourceDbArn"}, + }, + [ + ValidationError( + "'Iops' is a required property when 'StorageType' is 'io1'", + rule=DbClusterMultiAz(), + path=deque([]), + validator="required", + schema_path=deque(["then", "allOf", 0, "then", "required"]), + ), + ], + ), + ( + { + "Engine": "postgres", + "AllocatedStorage": 100, + "DBClusterInstanceClass": "foo", + "StorageType": {"Ref": "StorageType"}, + }, + [], + ), + ( + { + "Engine": "postgres", + "AllocatedStorage": 100, + "DBClusterInstanceClass": "foo", + "StorageType": "io2", + }, + [ + ValidationError( + "'Iops' is a required property when 'StorageType' is 'io2'", + rule=DbClusterMultiAz(), + path=deque([]), + validator="required", + schema_path=deque(["then", "allOf", 0, "then", "required"]), + ), + ], + ), + ( + { + "Engine": "mysql", + "AllocatedStorage": 100, + "DBClusterInstanceClass": "foo", + "StorageType": "io1", + }, + [ + ValidationError( + "'Iops' is a required property when 'StorageType' is 'io1'", + rule=DbClusterMultiAz(), + path=deque([]), + validator="required", + schema_path=deque(["then", "allOf", 0, "then", "required"]), + ), ], ), ( @@ -128,3 +299,77 @@ def rule(): def test_validate(instance, expected, rule, validator): errs = list(rule.validate(validator, "", instance, {})) assert errs == expected, f"Expected {expected} got {errs}" + + +@pytest.mark.parametrize( + "name,allocated_storage,expected", + [ + ( + "AllocatedStorage only on the primary branch", + {"Fn::If": ["IsReplica", {"Ref": "AWS::NoValue"}, 100]}, + [], + ), + ( + "AllocatedStorage on both branches", + 100, + [ + "'AllocatedStorage' is not allowed on a Multi-AZ DB " + "cluster read replica (ReplicationSourceIdentifier is set)" + ], + ), + ], +) +def test_conditional_read_replica(name, allocated_storage, expected): + template = { + "Parameters": {"SourceDbArn": {"Type": "String", "Default": ""}}, + "Conditions": { + "IsReplica": {"Fn::Not": [{"Fn::Equals": [{"Ref": "SourceDbArn"}, ""]}]} + }, + "Resources": { + "Cluster": { + "Type": "AWS::RDS::DBCluster", + "Properties": { + "Engine": "postgres", + "DBClusterInstanceClass": "db.m6gd.large", + "Iops": 1000, + "StorageType": "io1", + "AllocatedStorage": allocated_storage, + "ReplicationSourceIdentifier": { + "Fn::If": [ + "IsReplica", + {"Ref": "SourceDbArn"}, + {"Ref": "AWS::NoValue"}, + ] + }, + }, + } + }, + } + matches = lint(json.dumps(template), regions=["us-east-1"]) + + assert [ + match.message for match in matches if match.rule.id == "E3692" + ] == expected, f"Test {name!r} failed with {matches!r}" + + +def test_conditional_storage_type(): + template = { + "Parameters": {"UseIo1": {"Type": "String", "Default": "false"}}, + "Conditions": {"IsIo1": {"Fn::Equals": [{"Ref": "UseIo1"}, "true"]}}, + "Resources": { + "Cluster": { + "Type": "AWS::RDS::DBCluster", + "Properties": { + "Engine": "postgres", + "DBClusterInstanceClass": "db.m6gd.large", + "AllocatedStorage": 100, + "StorageType": {"Fn::If": ["IsIo1", "io1", "gp3"]}, + }, + } + }, + } + matches = lint(json.dumps(template), regions=["us-east-1"]) + + assert [match.message for match in matches if match.rule.id == "E3692"] == [ + "'Iops' is a required property when 'StorageType' is 'io1'" + ], f"Got {matches!r}"