|
| 1 | +# OPA Policies on GKE |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +[Policy Controller benefits](https://cloud.google.com/kubernetes-engine/enterprise/policy-controller/docs/overview#benefits): |
| 6 | + |
| 7 | +- Integrated with Google Cloud: Platform admins can install Policy Controller by using the Google Cloud console, by using Terraform, or by using Google Cloud CLI on any cluster connected to your fleet. Policy Controller works with other Google Cloud services like Config Sync, metrics, and Cloud Monitoring. |
| 8 | +- Supports multiple enforcement points: In addition to both audit and admission control for your cluster, Policy Controller can optionally enable a shift-left approach to analyse and catch non-compliant changes prior to application. |
| 9 | +- Pre-built policy bundles: Policy Controller comes with a full library of pre-built policies for common security and compliance controls. These include both Policy bundles, which are built and maintained by Google, and the constraint template library. |
| 10 | +- Supports custom policies: If policy customization is required beyond what is available using the constraint template library, Policy Controller additionally supports the development of custom constraint templates. |
| 11 | +- Built-in observability: Policy Controller includes a Google Cloud console dashboard, providing an overview for the state of all the policies applied to your fleet (including unregistered clusters). From the dashboard, view compliance and enforcement status to help you troubleshoot, and get opinionated recommendations to resolve policy violations. |
| 12 | + |
| 13 | +Policy Controller is installed in this repository on the 3-fleetscope stage. |
| 14 | + |
| 15 | +For more information on the dashboard, see [Policy Controller Status](https://cloud.google.com/kubernetes-engine/enterprise/policy-controller/docs/how-to/policy-controller-status). |
| 16 | + |
| 17 | +## Installed Bundles |
| 18 | + |
| 19 | +In this repository, two policies bundles are installed by default on the cluster: |
| 20 | + |
| 21 | +Bundle Code | Name | Description | |
| 22 | +--- | -- | -- | |
| 23 | +pss-baseline-v2022 | Pod Security Standards Baseline | Apply protections based on the Kubernetes Pod Security Standards (PSS) Baseline policy. | |
| 24 | +policy-essentials-v2022 | Policy Essentials | Apply best practices to your cluster resources | |
| 25 | + |
| 26 | +### `pss-baseline-v2022` details |
| 27 | + |
| 28 | +| Constraint Name | Description | Control Name | |
| 29 | +|---------------------------------------------------|------------------------------------------------------|-------------------------------| |
| 30 | +| pss-baseline-v2022-apparmor | The AppArmor profile used by containers | AppArmor | |
| 31 | +| pss-baseline-v2022-capabilities | Linux capabilities | Capabilities | |
| 32 | +| pss-baseline-v2022-host-namespaces-host-pid-ipc | Usage of host namespaces | Host Namespaces | |
| 33 | +| pss-baseline-v2022-host-namespaces-hostnetwork | Use of host networking | Host Networking | |
| 34 | +| pss-baseline-v2022-host-ports | Usage of host ports | Host Ports (configurable) | |
| 35 | +| pss-baseline-v2022-hostpath-volumes | Usage of the host filesystem | HostPath Volumes | |
| 36 | +| pss-baseline-v2022-hostprocess | Usage of Windows HostProcess | HostProcess | |
| 37 | +| pss-baseline-v2022-privileged-containers | Running of privileged containers | Privileged Containers | |
| 38 | +| pss-baseline-v2022-proc-mount-type | The Allowed Proc Mount types for the container | /proc Mount Type | |
| 39 | +| pss-baseline-v2022-seccomp | The seccomp profile used by containers | Seccomp | |
| 40 | +| pss-baseline-v2022-selinux | The SELinux context of the container | SELinux | |
| 41 | +| pss-baseline-v2022-sysctls | The sysctl profile used by containers | Sysctls | |
| 42 | + |
| 43 | +### `policy-essentials-v2022` details |
| 44 | + |
| 45 | +This bundle of constraints addresses and enforces policies in the following domains: |
| 46 | + |
| 47 | +- RBAC and service accounts |
| 48 | +- Pod Security Policies |
| 49 | +- Container Network Interface (CNI) |
| 50 | +- Secrets management |
| 51 | +- General policies |
| 52 | + |
| 53 | +Constraint | Description | |
| 54 | +| - | - | |
| 55 | +policy-essentials-v2022-no-secrets-as-env-vars | Prefer using Secrets as files over Secrets as environment variables |
| 56 | +policy-essentials-v2022-pods-require-security-context | Apply Security Context to your Pods and containers |
| 57 | +policy-essentials-v2022-prohibit-role-wildcard-access | Minimize the use of wildcards in Roles and ClusterRoles. |
| 58 | +policy-essentials-v2022-psp-allow-privilege-escalation-container | Minimize the admission of containers with allowPrivilegeEscalation |
| 59 | +policy-essentials-v2022-psp-capabilities | Containers must drop the `NET_RAW` capability and aren't permitted to add back any capabilities. |
| 60 | +policy-essentials-v2022-psp-host-namespace | Minimize the admission of containers with `hostPID` or `hostIPC` set to `true`. |
| 61 | +policy-essentials-v2022-psp-host-network-ports | Minimize the admission of containers wanting to share the host network namespace |
| 62 | +policy-essentials-v2022-psp-pods-must-run-as-nonroot | Minimize the admission of root containers |
| 63 | +policy-essentials-v2022-psp-privileged-container | Minimize the admission of privileged containers |
| 64 | +policy-essentials-v2022-psp-seccomp-default | Ensure that the seccomp profile is set to `runtime/default` or `docker/default` in your Pod definitions |
| 65 | +policy-essentials-v2022-restrict-clusteradmin-rolebindings | Minimize the use of the cluster-admin role. |
| 66 | + |
| 67 | +## Validating Correct Installation |
| 68 | + |
| 69 | +After installing Policy Controller, you can verify that it completed successfully by running the following command: |
| 70 | + |
| 71 | +```bash |
| 72 | +gcloud container fleet policycontroller describe --memberships=MEMBERSHIP_NAME |
| 73 | +``` |
| 74 | + |
| 75 | +You can also validate the installed constraints by running: |
| 76 | + |
| 77 | +```bash |
| 78 | +kubectl get constrainttemplates |
| 79 | +``` |
| 80 | + |
| 81 | +## Violations Validation Example |
| 82 | + |
| 83 | +If you want to validate the violations for the `pss-baseline-v2022` policy bundle, for example, you can run the following command: |
| 84 | + |
| 85 | +```bash |
| 86 | +kubectl get constraint -l policycontroller.gke.io/bundleName=pss-baseline-v2022 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]' |
| 87 | +``` |
| 88 | + |
| 89 | +You can also see policies summary on GKE Dashboard, for example: |
| 90 | + |
| 91 | + |
| 92 | +### Enforcing policy to `warn` or `deny` |
| 93 | + |
| 94 | +Once you've reviewed policy violations on your cluster, you can consider changing the enforcement mode so the Admission Controller will either warn on or even deny block non-compliant resource from getting applied to the cluster. Here is an example on how to update it to warn: |
| 95 | + |
| 96 | +```bash |
| 97 | +kubectl get constraint -l policycontroller.gke.io/bundleName=pss-baseline-v2022 -o name | xargs -I {} kubectl patch {} --type='json' -p='[{"op":"replace","path":"/spec/enforcementAction","value":"warn"}]' |
| 98 | +``` |
| 99 | + |
| 100 | +> **Warning**: The deny enforcement action should be used with care as it can potentially block required changes resulting in interruption to critical workloads or the cluster. |
| 101 | +
|
| 102 | +## Additional Policies |
| 103 | + |
| 104 | +You have three options for adding new policies: |
| 105 | + |
| 106 | +1. **Add a Bundle of Policies**: Deploy a comprehensive set of policies as a bundle with common security and compliance controls. |
| 107 | + |
| 108 | +2. **Add Individual Constraints**: Create policies by adding individual constraints based on the available constraint templates. |
| 109 | + |
| 110 | +3. **Write Custom Policies**: Develop tailored policies by writing custom constraints to meet specific requirements. |
| 111 | + |
| 112 | +### Adding a Bundle of Policies |
| 113 | + |
| 114 | +You can add additional Bundles by editing the policy baseline on [3-fleetscope/modules/env_baseline/policy.tf](../3-fleetscope/modules/env_baseline/policy.tf). |
| 115 | + |
| 116 | +For all the available bundles, see [Policy Controller Bundles on GKE](https://cloud.google.com/kubernetes-engine/enterprise/policy-controller/docs/concepts/policy-controller-bundles) |
| 117 | + |
| 118 | +### Adding Individual Constraints Policies |
| 119 | + |
| 120 | +To explore the complete library of available constraint templates, please visit: |
| 121 | + |
| 122 | +[Constraint Template Library](https://cloud.google.com/kubernetes-engine/enterprise/policy-controller/docs/latest/reference/constraint-template-library) |
| 123 | + |
| 124 | +For detailed instructions on how to utilize the constraint library, refer to the following guide: |
| 125 | + |
| 126 | +[Creating Policy Controller Constraints](https://cloud.google.com/kubernetes-engine/enterprise/policy-controller/docs/how-to/creating-policy-controller-constraints) |
| 127 | + |
| 128 | +The recommended way of adding new individual policies, is to clone the `config-sync` repository and add the individual policies, for example: |
| 129 | + |
| 130 | +### Example for Config Sync with Gitlab Repository |
| 131 | + |
| 132 | +1. Clone config sync repository: |
| 133 | + |
| 134 | +```bash |
| 135 | +git clone https://GIT_URL/namespace/config-sync-development.git && cd config-sync-development |
| 136 | +``` |
| 137 | + |
| 138 | +1. Create a file for your policy, here is an example for `K8sRequiredLabels`: |
| 139 | + |
| 140 | +```YAML |
| 141 | +apiVersion: constraints.gatekeeper.sh/v1beta1 |
| 142 | +kind: K8sRequiredLabels |
| 143 | +metadata: |
| 144 | + name: ns-must-have-geo |
| 145 | +spec: |
| 146 | + match: |
| 147 | + kinds: |
| 148 | + - apiGroups: [""] |
| 149 | + kinds: ["Namespace"] |
| 150 | + parameters: |
| 151 | + labels: |
| 152 | + - key: "geo" |
| 153 | +``` |
| 154 | +
|
| 155 | +1. Commit to sync branch and wait for the policy to be applied on the cluster |
| 156 | +
|
| 157 | +```bash |
| 158 | +git add . |
| 159 | +git commit -am "require 'geo' label policy" |
| 160 | +git push origin master |
| 161 | +``` |
| 162 | + |
| 163 | +1. Wait for sync: |
| 164 | + |
| 165 | +```bash |
| 166 | +kubectl get rootsyncs.configsync.gke.io -n config-management-system root-sync -o yaml |
| 167 | +``` |
| 168 | + |
| 169 | +### Writing Custom Policies |
| 170 | + |
| 171 | +For guidance on creating custom policies, please refer to the official documentation available at the following link: |
| 172 | + |
| 173 | +[How to Write Custom Constraint Templates](https://cloud.google.com/kubernetes-engine/enterprise/policy-controller/docs/how-to/write-custom-constraint-templates) |
0 commit comments