forked from GoogleCloudPlatform/policy-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp_iam_allowed_bindings_v2.yaml
148 lines (127 loc) · 5.38 KB
/
gcp_iam_allowed_bindings_v2.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
# Copyright 2019 Google Inc.
#
# 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-iam-allowed-bindings-v2
spec:
crd:
spec:
names:
kind: GCPIAMAllowedBindingsConstraintV2
validation:
openAPIV3Schema:
properties:
mode:
description: "Enforcement mode, defaults to whitelist"
type: string
enum: [denylist, allowlist]
assetType:
description: "Restrict which asset type this policy applies to"
type: string
assetNames:
description: "CAI name of asset to restrict which specific assets this policy applies to.
assetNames must have the same assetType defined above.
E.g. '//bigquery.googleapis.com/projects/my-project/datasets/my-dataset'"
type: array
items:
type: string
role:
description: "Role to restrict bindings for,
ex. roles/owner; Wildcards (*) supported"
type: string
members:
description: "Members to either allow or deny for the given role,
depending on mode; Wildcards (*) supported"
type: array
items:
type: string
targets:
validation.gcp.forsetisecurity.org:
rego: |
#
# Copyright 2020 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.GCPIAMAllowedBindingsConstraintV2
import data.validator.gcp.lib as lib
deny[{
"msg": message,
"details": metadata,
}] {
constraint := input.constraint
lib.get_constraint_params(constraint, params)
asset := input.asset
check_asset_type(asset, params)
# Check if resource is part of asset names to scan
include_list := lib.get_default(params, "assetNames", [])
is_included(include_list, asset.name)
binding := asset.iam_policy.bindings[_]
member := binding.members[_]
role := binding.role
glob.match(params.role, ["/"], role)
mode := lib.get_default(params, "mode", "allowlist")
matches_found = [m | m = config_pattern(params.members[_]); glob.match(m, [], member)]
target_match_count(mode, desired_count)
count(matches_found) != desired_count
message := sprintf("IAM policy for %v grants %v to %v", [asset.name, role, member])
metadata := {
"resource": asset.name,
"member": member,
"role": role,
}
}
###########################
# Rule Utilities
###########################
# Determine the overlap between matches under test and constraint
target_match_count(mode) = 0 {
mode == "denylist"
}
target_match_count(mode) = 1 {
mode == "allowlist"
}
check_asset_type(asset, params) {
lib.has_field(params, "assetType")
params.assetType == asset.asset_type
}
check_asset_type(asset, params) {
lib.has_field(params, "assetType") == false
}
is_included(include_list, asset_name) {
include_list != []
glob.match(include_list[_], ["/"], asset_name)
}
is_included(include_list, asset_name) {
include_list == []
}
# If the member in constraint is written as a single "*", turn it into super
# glob "**". Otherwise, we won't be able to match everything.
config_pattern(old_pattern) = "**" {
old_pattern == "*"
}
config_pattern(old_pattern) = old_pattern {
old_pattern != "*"
}