Skip to content

Commit 7a71d09

Browse files
authored
Add rule E3636 to validate codebuild s3 locations (#3991)
1 parent ef15a85 commit 7a71d09

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

src/cfnlint/data/schemas/extensions/aws_codebuild_project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"if": {
3+
"properties": {
4+
"Type": {
5+
"const": "S3",
6+
"type": "string"
7+
}
8+
},
9+
"required": [
10+
"Type"
11+
],
12+
"type": "object"
13+
},
14+
"then": {
15+
"required": [
16+
"Location"
17+
]
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
6+
from __future__ import annotations
7+
8+
from typing import Any
9+
10+
import cfnlint.data.schemas.extensions.aws_codebuild_project
11+
from cfnlint.jsonschema import ValidationError
12+
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails
13+
14+
15+
class ProjectS3Location(CfnLintJsonSchema):
16+
id = "E3636"
17+
shortdesc = "Validate CodeBuild projects using S3 also have Location"
18+
description = "When using 'S3' for 'Type' then you must also specify " "'Location'"
19+
tags = ["resources", "codebuild"]
20+
21+
def __init__(self) -> None:
22+
super().__init__(
23+
keywords=[
24+
"Resources/AWS::CodeBuild::Project/Properties/Artifacts",
25+
"Resources/AWS::CodeBuild::Project/Properties/Source",
26+
],
27+
schema_details=SchemaDetails(
28+
module=cfnlint.data.schemas.extensions.aws_codebuild_project,
29+
filename="s3_locations.json",
30+
),
31+
)
32+
33+
def message(self, instance: Any, err: ValidationError) -> str:
34+
return f"{err.message} when using 'Type' of 'S3'"

src/cfnlint/rules/resources/codebuild/__init__.py

Whitespace-only changes.

test/unit/rules/resources/codebuild/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
from cfnlint.rules.resources.codebuild.ProjectS3Location import ProjectS3Location
12+
13+
14+
@pytest.fixture(scope="module")
15+
def rule():
16+
rule = ProjectS3Location()
17+
yield rule
18+
19+
20+
@pytest.mark.parametrize(
21+
"instance,expected",
22+
[
23+
(
24+
{
25+
"Type": "S3",
26+
"Location": "path",
27+
},
28+
[],
29+
),
30+
(
31+
[], # wrong type
32+
[],
33+
),
34+
(
35+
{
36+
"Type": {"Ref": "AWS::StackName"}, # not a string
37+
"Location": "path",
38+
},
39+
[],
40+
),
41+
(
42+
{
43+
"Type": "S3",
44+
},
45+
[
46+
ValidationError(
47+
"'Location' is a required property when using 'Type' of 'S3'",
48+
rule=ProjectS3Location(),
49+
path=deque([]),
50+
validator="required",
51+
schema_path=deque(["then", "required"]),
52+
)
53+
],
54+
),
55+
],
56+
)
57+
def test_validate(instance, expected, rule, validator):
58+
errs = list(rule.validate(validator, "", instance, {}))
59+
assert errs == expected, f"Expected {expected} got {errs}"

0 commit comments

Comments
 (0)