Skip to content

Commit 7d4e7e8

Browse files
authored
Switch E3683 to E3684 for health check protocol (#4094)
1 parent 72d3c8b commit 7d4e7e8

File tree

5 files changed

+139
-36
lines changed

5 files changed

+139
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"if": {
3+
"properties": {
4+
"HealthCheckProtocol": {
5+
"type": "string"
6+
}
7+
},
8+
"required": [
9+
"HealthCheckProtocol"
10+
]
11+
},
12+
"then": {
13+
"allOf": [
14+
{
15+
"else": {
16+
"properties": {
17+
"Matcher": false,
18+
"ProtocolVersion": false
19+
}
20+
},
21+
"if": {
22+
"properties": {
23+
"HealthCheckProtocol": {
24+
"enum": [
25+
"HTTP",
26+
"HTTPS"
27+
]
28+
}
29+
},
30+
"required": [
31+
"HealthCheckProtocol"
32+
]
33+
}
34+
}
35+
]
36+
}
37+
}

src/cfnlint/data/schemas/extensions/aws_elasticloadbalancingv2_targetgroup/protocol_restrictions.json

-21
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,6 @@
1111
},
1212
"then": {
1313
"allOf": [
14-
{
15-
"else": {
16-
"properties": {
17-
"Matcher": false,
18-
"ProtocolVersion": false
19-
}
20-
},
21-
"if": {
22-
"properties": {
23-
"Protocol": {
24-
"enum": [
25-
"HTTP",
26-
"HTTPS"
27-
]
28-
}
29-
},
30-
"required": [
31-
"Protocol"
32-
]
33-
}
34-
},
3514
{
3615
"if": {
3716
"properties": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from typing import Any
7+
8+
import cfnlint.data.schemas.extensions.aws_elasticloadbalancingv2_targetgroup
9+
from cfnlint.jsonschema import ValidationResult, Validator
10+
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails
11+
12+
13+
class TargetGroupHealthCheckProtocolRestrictions(CfnLintJsonSchema):
14+
id = "E3684"
15+
shortdesc = "Validate target group health check protocol property restrictions"
16+
description = (
17+
"When a TargetGroup health check protocol is specified there are "
18+
"restrictions on other properties."
19+
)
20+
tags = ["resources"]
21+
22+
def __init__(self) -> None:
23+
super().__init__(
24+
keywords=[
25+
"Resources/AWS::ElasticLoadBalancingV2::TargetGroup/Properties",
26+
],
27+
schema_details=SchemaDetails(
28+
module=cfnlint.data.schemas.extensions.aws_elasticloadbalancingv2_targetgroup,
29+
filename="healthcheckprotocol_restrictions.json",
30+
),
31+
all_matches=True,
32+
)
33+
34+
def validate(
35+
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
36+
) -> ValidationResult:
37+
for err in super().validate(validator, keywords, instance, schema):
38+
if not err.schema:
39+
err.message = (
40+
f"Additional properties are not allowed ({err.path[0]!r} "
41+
"was unexpected)"
42+
)
43+
err.validator = "additionalProperties"
44+
yield err
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from collections import deque
7+
8+
import pytest
9+
10+
from cfnlint.jsonschema import ValidationError
11+
12+
# ruff: noqa: E501
13+
from cfnlint.rules.resources.elasticloadbalancingv2.TargetGroupHealthCheckProtocolRestrictions import (
14+
TargetGroupHealthCheckProtocolRestrictions,
15+
)
16+
17+
18+
@pytest.fixture(scope="module")
19+
def rule():
20+
rule = TargetGroupHealthCheckProtocolRestrictions()
21+
yield rule
22+
23+
24+
@pytest.mark.parametrize(
25+
"instance,expected",
26+
[
27+
(
28+
{"HealthCheckProtocol": "HTTPS", "Matcher": {}},
29+
[],
30+
),
31+
(
32+
{"HealthCheckProtocol": "TCP"},
33+
[],
34+
),
35+
(
36+
[],
37+
[],
38+
),
39+
(
40+
{"HealthCheckProtocol": "TCP", "Matcher": {}},
41+
[
42+
ValidationError(
43+
"Additional properties are not allowed ('Matcher' was unexpected)",
44+
rule=TargetGroupHealthCheckProtocolRestrictions(),
45+
validator="additionalProperties",
46+
path=deque(["Matcher"]),
47+
schema_path=deque(
48+
["then", "allOf", 0, "else", "properties", "Matcher"]
49+
),
50+
)
51+
],
52+
),
53+
],
54+
)
55+
def test_backup_lifecycle(instance, expected, rule, validator):
56+
errs = list(rule.validate(validator, "", instance, {}))
57+
assert errs == expected, f"Expected {expected} got {errs}"

test/unit/rules/resources/elbv2/test_target_group_protocol_restrictions.py

+1-15
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,7 @@ def rule():
4949
path=deque(["Port"]),
5050
validator="enum",
5151
schema_path=deque(
52-
["then", "allOf", 1, "then", "properties", "Port", "enum"]
53-
),
54-
)
55-
],
56-
),
57-
(
58-
{"Protocol": "TLS", "Matcher": {}},
59-
[
60-
ValidationError(
61-
"Additional properties are not allowed ('Matcher' was unexpected)",
62-
rule=TargetGroupProtocolRestrictions(),
63-
validator="additionalProperties",
64-
path=deque(["Matcher"]),
65-
schema_path=deque(
66-
["then", "allOf", 0, "else", "properties", "Matcher"]
52+
["then", "allOf", 0, "then", "properties", "Port", "enum"]
6753
),
6854
)
6955
],

0 commit comments

Comments
 (0)