forked from GoogleCloudPlatform/policy-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp_lb_forwarding_rules.yaml
162 lines (138 loc) · 5.29 KB
/
gcp_lb_forwarding_rules.yaml
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
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
apiVersion: templates.gatekeeper.sh/v1alpha1
kind: ConstraintTemplate
metadata:
name: gcp-lb-allowed-forwarding-rules-v2
annotations:
description: "Constraint to restrict forwarding rules based on a allowlist"
spec:
crd:
spec:
names:
kind: GCPLBAllowedForwardingRulesConstraintV2
validation:
openAPIV3Schema:
properties:
allowlist:
type: array
items:
type: object
properties:
target:
type: string
ip_address:
type: string
ip_protocol:
type: string
port_range:
type: string
load_balancing_scheme:
type: string
required:
- target
targets:
validation.gcp.forsetisecurity.org:
rego: | #INLINE("validator/gcp_lb_forwarding_rules.rego")
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package templates.gcp.GCPLBAllowedForwardingRulesConstraintV2
import data.validator.gcp.lib as lib
###########################
# Find allowlist Violations
###########################
deny[{
"msg": message,
"details": metadata,
}] {
constraint := input.constraint
lib.get_constraint_params(constraint, params)
asset := input.asset
is_forwarding_rule(asset.asset_type)
instance := asset.resource.data
rule := get_allowlist_entry(params.allowlist, instance.target)
rule
invalid_forwarding_rule(rule, instance)
message := sprintf("%v is not allowed, violates allowlist policy.", [asset.name])
metadata := {"resource": asset.name}
}
###########################
# Rule Utilities
###########################
is_forwarding_rule(asset_type) {
asset_type == "compute.googleapis.com/ForwardingRule"
}
is_forwarding_rule(asset_type) {
asset_type == "compute.googleapis.com/GlobalForwardingRule"
}
get_allowlist_entry(allowlist, target) = output {
rule := allowlist[_]
rule.target == target
output = rule
}
check_scheme(rule, resource) {
rule.load_balancing_scheme
rule.load_balancing_scheme == resource.loadBalancingScheme
}
check_scheme(rule, resource) {
not rule.load_balancing_scheme
}
check_ip_protocol(rule, resource) {
rule.ip_protocol
rule.ip_protocol == resource.IPProtocol
}
check_ip_protocol(rule, resource) {
not rule.ip_protocol
}
check_ip_address(rule, resource) {
rule.ip_address
rule.ip_address == resource.IPAddress
}
check_ip_address(rule, resource) {
not rule.ip_address
}
check_port_range(rule, resource) {
rule.port_range
rule.port_range == resource.portRange
}
check_port_range(rule, resource) {
not rule.port_range
}
invalid_forwarding_rule(rule, resource) {
not check_scheme(rule, resource)
}
invalid_forwarding_rule(rule, resource) {
not check_ip_address(rule, resource)
}
invalid_forwarding_rule(rule, resource) {
not check_ip_protocol(rule, resource)
}
invalid_forwarding_rule(rule, resource) {
not check_port_range(rule, resource)
}
#ENDINLINE