-
Notifications
You must be signed in to change notification settings - Fork 605
/
Copy pathPipelineArtifactCounts.py
190 lines (178 loc) · 7.57 KB
/
PipelineArtifactCounts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from __future__ import annotations
from collections import deque
from typing import Any
from cfnlint.jsonschema import ValidationResult, Validator
from cfnlint.rules.helpers import get_value_from_path
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
class PipelineArtifactCounts(CfnLintKeyword):
id = "E3702"
shortdesc = "Validate the number of input and output artifacts in a CodePipeline"
description = (
"When using AWS::CodePipeline::Pipeline action types "
"have different contraints for InputArtifacts and "
"OutputArtifacts"
)
tags = ["resources", "codepipeline"]
def __init__(self) -> None:
super().__init__(
keywords=[
"Resources/AWS::CodePipeline::Pipeline/Properties/Stages/*/Actions/*",
],
)
self._artifact_counts: dict[str, dict[str, dict[str, Any]]] = {
"AWS": {
"Source": {
"S3": {
"InputArtifacts": {"maxItems": 0},
"OutputArtifacts": {"minItems": 1, "maxItems": 1},
},
"CodeCommit": {
"InputArtifacts": {"maxItems": 0},
"OutputArtifacts": {"minItems": 1, "maxItems": 1},
},
"ECR": {
"InputArtifacts": {"maxItems": 0},
"OutputArtifacts": {"minItems": 1, "maxItems": 1},
},
},
"Test": {
"CodeBuild": {
"InputArtifacts": {"minItems": 1, "maxItems": 5},
"OutputArtifacts": {"minItems": 0, "maxItems": 5},
},
"DeviceFarm": {
"InputArtifacts": {"minItems": 1, "maxItems": 1},
"OutputArtifacts": {"maxItems": 0},
},
},
"Build": {
"CodeBuild": {
"InputArtifacts": {"minItems": 1, "maxItems": 5},
"OutputArtifacts": {"minItems": 0, "maxItems": 5},
}
},
"Approval": {
"Manual": {
"InputArtifacts": {"maxItems": 0},
"OutputArtifacts": {"maxItems": 0},
}
},
"Deploy": {
"S3": {
"InputArtifacts": {"minItems": 1, "maxItems": 1},
"OutputArtifacts": {"maxItems": 0},
},
"CloudFormation": {
"InputArtifacts": {"minItems": 0, "maxItems": 10},
"OutputArtifacts": {"minItems": 0, "maxItems": 1},
},
"CodeDeploy": {
"InputArtifacts": {"minItems": 1, "maxItems": 1},
"OutputArtifacts": {"maxItems": 0},
},
"ElasticBeanstalk": {
"InputArtifacts": {"minItems": 1, "maxItems": 1},
"OutputArtifacts": {"maxItems": 0},
},
"OpsWorks": {
"InputArtifacts": {"minItems": 1, "maxItems": 1},
"OutputArtifacts": {"maxItems": 0},
},
"ECS": {
"InputArtifacts": {"minItems": 1, "maxItems": 1},
"OutputArtifacts": {"maxItems": 0},
},
"ServiceCatalog": {
"InputArtifacts": {"minItems": 1, "maxItems": 1},
"OutputArtifacts": {"maxItems": 0},
},
},
"Invoke": {
"Lambda": {
"InputArtifacts": {"minItems": 0, "maxItems": 5},
"OutputArtifacts": {"minItems": 0, "maxItems": 5},
}
},
},
"ThirdParty": {
"Source": {
"GitHub": {
"InputArtifacts": {"maxItems": 0},
"OutputArtifacts": {"minItems": 1, "maxItems": 1},
}
},
"Deploy": {
"AlexaSkillsKit": {
"InputArtifacts": {"minItems": 0, "maxItems": 2},
"OutputArtifacts": {"maxItems": 0},
},
},
},
"Custom": {
"Build": {
"Jenkins": {
"InputArtifacts": {"minItems": 0, "maxItems": 5},
"OutputArtifacts": {"minItems": 0, "maxItems": 5},
},
},
"Test": {
"Jenkins": {
"InputArtifacts": {"minItems": 0, "maxItems": 5},
"OutputArtifacts": {"minItems": 0, "maxItems": 5},
},
},
},
}
def validate(
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
) -> ValidationResult:
if not validator.is_type(instance, "object"):
return
for action, action_validator in get_value_from_path(
validator, instance, path=deque(["ActionTypeId"])
):
for owner, owner_validator in get_value_from_path(
action_validator, action, path=deque(["Owner"])
):
owner_artifact_counts = self._artifact_counts.get(owner)
if not owner_artifact_counts:
continue
for category, categor_validator in get_value_from_path(
owner_validator, action, path=deque(["Category"])
):
category_artifact_counts = owner_artifact_counts.get(category)
if not category_artifact_counts:
continue
for provider, provider_provider in get_value_from_path(
categor_validator, action, deque(["Provider"])
):
count_schema: dict[str, Any] = {
"properties": category_artifact_counts.get(provider),
"required": [],
}
if not count_schema.get("properties"):
continue
for key in ["InputArtifacts", "OutputArtifacts"]:
if (
count_schema.get("properties", {})
.get(key, {})
.get("minItems", 0)
> 0
):
count_schema["required"].append(key)
provider_provider = provider_provider.evolve(
schema=count_schema,
)
repr = {
"Owner": owner,
"Category": category,
"Provider": provider,
}
for err in provider_provider.iter_errors(instance):
err.message = f"{err.message} when using {repr!r}"
err.rule = self
yield err