Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Kustomize so that policies can be enforced as groups #141

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
82 changes: 82 additions & 0 deletions open-policy-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,85 @@ For example, applying 'kubeadmin' temporary user removal validation policy
$ oc apply -f https://raw.githubusercontent.com/openshift-4-compliance/openshift-4-compliance-automation/master/open-policy-agent/authentication-user-management/delete-kubeadmin/template.yaml
$ oc apply -f https://raw.githubusercontent.com/openshift-4-compliance/openshift-4-compliance-automation/master/open-policy-agent/authentication-user-management/delete-kubeadmin/constraint.yaml
```

## Applying policies using GitOps
You can use the `dryrun` enforcement mode in either all policies or in policy groups.
You can use the `deny` enforcement mode in either all policies, in policy groups, or individually.


You can't include specific policies in `dryrun` enforcement mode, you can use the alternative workflow suggested at the end of the paragraph in [this link](https://kubectl.docs.kubernetes.io/faq/kustomize/eschewedfeatures/#removal-directives).



### Import this kustomization
#### By extending this project
```
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
# All policies
- <PATH_TO_/open-policy-agent_FOLDER>
# Specific groups
- <PATH_TO_/open-policy-agent_FOLDER/policy-folder>
...
```

#### By importing this kustomization to your repo
```
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
# All policies
- https://github.com/openshift-4-compliance/openshift-4-compliance-automation.git//open-policy-agent?ref=<TAG>
# Specific groups
- https://github.com/openshift-4-compliance/openshift-4-compliance-automation.git//open-policy-agent/policy-folder?ref=<TAG>
...
```

### Modify this kustomization
#### Enforce specific policies
```
...

patchesJson6902:
- target:
name: <POLICY_CONSTRAINT_RESOURCE_NAME>
patch: |-
- op: replace
path: "/spec/enforcementAction"
value: deny
```

#### Enforce a group of policies
```
...

patchesJson6902:
- target:
name: .*
group: constraints.gatekeeper.sh
version: v1beta1
labelSelector: policy-group=<POLICY_GROUP_NAME>
patch: |-
- op: replace
path: "/spec/enforcementAction"
value: deny
```

#### Enforce all policies
```
...

patchesJson6902:
- target:
name: .*
group: constraints.gatekeeper.sh
version: v1beta1
patch: |-
- op: replace
path: "/spec/enforcementAction"
value: deny
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@galshi I'd add an example of implementation using ArgoCD at the end - which configurations should be applied in the Application for both template and constraint resources to be created and not result in constraint creation failure due to kustomization issues.

I know it might be an overkill, but as for now, if a simple user tries to use the examples we provide, it'll fail. The issue should be covered, and a solution needs to be provided for a complete GitOps solution.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./delete-kubeadmin/template.yaml
- ./delete-kubeadmin/constraint.yaml
- ./shorten-tokens/template.yaml
- ./shorten-tokens/constraint.yaml
- ./oauth-secured-identity-providers-only/template.yaml
- ./oauth-secured-identity-providers-only/constraint.yaml
- ./disallow-anonymous-users/template.yaml
- ./disallow-anonymous-users/constraint.yaml
commonLabels:
policy-group: authentication-user-management
17 changes: 17 additions & 0 deletions open-policy-agent/authorization/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./disallow-privileged-scc-usage/template.yaml
- ./disallow-privileged-scc-usage/constraint.yaml
- ./prevent-default-serviceaccount-usage/template.yaml
- ./prevent-default-serviceaccount-usage/constraint.yaml
- ./disallow-host-network/template.yaml
- ./disallow-host-network/constraint.yaml
- ./disallow-host-namespaces/template.yaml
- ./disallow-host-namespaces/constraint.yaml
- ./disallow-cluster-admin/template.yaml
- ./disallow-cluster-admin/constraint.yaml
- ./disallow-scc-runasany/template.yaml
- ./disallow-scc-runasany/constraint.yaml
commonLabels:
policy-group: authorization
7 changes: 7 additions & 0 deletions open-policy-agent/etcd-security/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./verify-etcd-encryption/template.yaml
- ./verify-etcd-encryption/constraint.yaml
commonLabels:
policy-group: etcd-security
26 changes: 7 additions & 19 deletions open-policy-agent/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./authentication-user-management/delete-kubeadmin/template.yaml
- ./authentication-user-management/shorten-tokens/template.yaml
- ./authentication-user-management/oauth-secured-identity-providers-only/template.yaml
- ./authorization/disallow-privileged-scc-usage/template.yaml
- ./authorization/prevent-default-serviceaccount-usage/template.yaml
- ./etcd-security/verify-etcd-encryption/template.yaml
- ./networking/block-nodeport-services/template.yaml
- ./networking/external-ips/template.yaml
- ./networking/httpsonly/template.yaml
- ./resource-exhaustion/disallow-self-provisioner/template.yaml
- ./trusted-image-sources/disallowedtags/template.yaml
- ./trusted-image-sources/disallowed-registries/template.yaml
- ./resource-exhaustion/pod-resource-limits/template.yaml
- ./authorization/disallow-host-network/template.yaml
- ./authorization/disallow-host-namespaces/template.yaml
- ./authorization/disallow-cluster-admin/template.yaml
- ./authorization/disallow-scc-runasany/template.yaml
- ./authentication/disallow-anonymous-users/template.yaml
- ./storage/disallow-emptydir/template.yaml
- ./authentication-user-management
- ./authorization
- ./etcd-security
- ./networking
- ./resource-exhaustion
- ./storage
- ./trusted-image-sources
11 changes: 11 additions & 0 deletions open-policy-agent/networking/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./block-nodeport-services/template.yaml
- ./block-nodeport-services/constraint.yaml
- ./external-ips/template.yaml
- ./external-ips/constraint.yaml
- ./httpsonly/template.yaml
- ./httpsonly/constraint.yaml
commonLabels:
policy-group: networking
9 changes: 9 additions & 0 deletions open-policy-agent/resource-exhaustion/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./disallow-self-provisioner/template.yaml
- ./disallow-self-provisioner/constraint.yaml
- ./pod-resource-limits/template.yaml
- ./pod-resource-limits/constraint.yaml
commonLabels:
policy-group: resource-exhaustion
7 changes: 7 additions & 0 deletions open-policy-agent/storage/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./disallow-emptydir/template.yaml
- ./disallow-emptydir/constraint.yaml
commonLabels:
policy-group: storage
9 changes: 9 additions & 0 deletions open-policy-agent/trusted-image-sources/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./disallowed-registries/template.yaml
- ./disallowed-registries/constraint.yaml
- ./disallowed-tags/template.yaml
- ./disallowed-tags/constraint.yaml
commonLabels:
policy-group: trusted-image-sources