Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit 5403598

Browse files
author
Mathieu Benoit
committed
Add AllowedKccResources Constraints
1 parent e91be24 commit 5403598

File tree

1 file changed

+81
-7
lines changed

1 file changed

+81
-7
lines changed

app/content/tenant-project/enforce-gcp-resources-policies.md

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tags: ["org-admin", "policies", "security-tips"]
77
![Org Admin](/images/org-admin.png)
88
_{{< param description >}}_
99

10-
In this section, you will set up policies in order to enforce governance against the Kubernetes manifests defining your Google Cloud services. As an example, you will limit the locations available for the Google Cloud services.
10+
In this section, you will set up policies in order to enforce governance against the Kubernetes manifests defining your Google Cloud services. As an example, you will limit the locations and the kind available for the Google Cloud services.
1111

1212
Initialize variables:
1313
```Bash
@@ -20,6 +20,86 @@ source ${WORK_DIR}acm-workshop-variables.sh
2020
We are defining the `GKE_LOCATION` in `northamerica-northeast1` this will be used later for the location of the VPC, GKE, Artifact Registry, etc. in the Tenant project. We are using this region because that's the [greenest Google Cloud region (Low CO2)](https://cloud.google.com/sustainability/region-carbon) in the regions supported by [GKE Confidential Nodes](https://cloud.google.com/kubernetes-engine/docs/how-to/confidential-gke-nodes#availability) used in this workshop.
2121
{{% /notice %}}
2222

23+
## Define "Allowed KCC resources" policies
24+
25+
Define the `ConstraintTemplate` resource:
26+
```Bash
27+
cat <<EOF > ${WORK_DIR}$HOST_PROJECT_DIR_NAME/policies/templates/allowedkccresources.yaml
28+
apiVersion: templates.gatekeeper.sh/v1
29+
kind: ConstraintTemplate
30+
metadata:
31+
name: allowedkccresources
32+
annotations:
33+
description: "Requirements for any KCC resources."
34+
spec:
35+
crd:
36+
spec:
37+
names:
38+
kind: AllowedKccResources
39+
validation:
40+
legacySchema: false
41+
openAPIV3Schema:
42+
properties:
43+
allowedKinds:
44+
items:
45+
type: string
46+
type: array
47+
type: object
48+
targets:
49+
- target: admission.k8s.gatekeeper.sh
50+
rego: |-
51+
package allowedkccresources
52+
violation[{"msg": msg}] {
53+
_matches_group(input.review.kind.group)
54+
objectKind := input.review.kind.kind
55+
not _matches_kind(input.parameters.allowedKinds, objectKind)
56+
msg := sprintf("KCC resource of kind: %v is not allowed", [objectKind])
57+
}
58+
_matches_group(group) {
59+
endswith(group, ".cnrm.cloud.google.com")
60+
not group == "core.cnrm.cloud.google.com"
61+
}
62+
_matches_kind(allowedKinds, objectKind) {
63+
allowedKinds[_] = objectKind
64+
}
65+
EOF
66+
```
67+
68+
Define the `Constraint` resource:
69+
```Bash
70+
cat <<EOF > ${WORK_DIR}$HOST_PROJECT_DIR_NAME/policies/constraints/allowed-kcc-resources.yaml
71+
apiVersion: constraints.gatekeeper.sh/v1beta1
72+
kind: AllowedKccResources
73+
metadata:
74+
name: allowedkccresources
75+
spec:
76+
enforcementAction: deny
77+
parameters:
78+
allowedKinds:
79+
- ArtifactRegistryRepository
80+
- ComputeAddress
81+
- ComputeNetwork
82+
- ComputeRouter
83+
- ComputeRouterNAT
84+
- ComputeSecurityPolicy
85+
- ComputeSSLPolicy
86+
- ComputeSubnetwork
87+
- ContainerCluster
88+
- ContainerNodePool
89+
- GKEHubFeature
90+
- GKEHubFeatureMembership
91+
- GKEHubMembership
92+
- IAMPartialPolicy
93+
- IAMPolicyMember
94+
- IAMServiceAccount
95+
- Project
96+
- RedisInstance
97+
- Service
98+
- SpannerDatabase
99+
- SpannerInstance
100+
EOF
101+
```
102+
23103
## Define "Allowed GCP locations" policies
24104

25105
Define the `ConstraintTemplate` resource:
@@ -79,12 +159,6 @@ metadata:
79159
name: allowed-locations
80160
spec:
81161
enforcementAction: deny
82-
match:
83-
kinds:
84-
- apiGroups:
85-
- '*.cnrm.cloud.google.com'
86-
kinds:
87-
- '*'
88162
parameters:
89163
locations:
90164
- "northamerica-northeast1"

0 commit comments

Comments
 (0)