Slide 1

Slide 1 text

1 #GitLabCommit Bringing Kubernetes Policy Enforcement to GitLab

Slide 2

Slide 2 text

2 #GitLabCommit Sandeep Parikh DevRel Engineer, Google Cloud @crcsmnky @crcsmnky

Slide 3

Slide 3 text

3 #GitLabCommit Policies and Enforcement

Slide 4

Slide 4 text

4 #GitLabCommit Policies Definitions Policy management Policy enforcement Rules that tell us how we can configure a resource The practice of developing, deploying, and applying rules The scope and actions taken, based on defined rules Constraints & Templates Open Policy Agent OPA Gatekeeper

Slide 5

Slide 5 text

5 #GitLabCommit Open Policy Agent Decouple policy decisions from services to achieve unified control across the entire stack. Unified Express policies in a high-level declarative language that promotes safe, fine-grained logic. Declarative Leverage arbitrary external data in policies to ensure that important requirements are enforced. Context Aware

Slide 6

Slide 6 text

6 #GitLabCommit #GitLabCommit Gatekeeper OPA Gatekeeper brings Open Policy Agent to Kubernetes as an admission controller Gatekeeper turns OPA policies into Kubernetes custom resources Resources are managed using the standard Kubernetes Resource Model (KRM) Gatekeeper Kubernetes kubectl AdmissionReview (request) AdmissionReview (response)

Slide 7

Slide 7 text

7 #GitLabCommit Templates apiVersion: templates.gatekeeper.sh/v1beta1 kind: ConstraintTemplate metadata: name: k8sblocknodeport spec: crd: spec: names: kind: K8sBlockNodePort targets: - target: admission.k8s.gatekeeper.sh rego: | package k8sblocknodeport violation[{"msg": msg}] { input.review.kind.kind == "Service" input.review.object.spec.type == "NodePort" msg := "Cannot create service of type NodePort" } Policies are written in Rego and packaged as parameterized ConstraintTemplate objects.

Slide 8

Slide 8 text

8 #GitLabCommit apiVersion: templates.gatekeeper.sh/v1beta1 kind: ConstraintTemplate metadata: name: k8sblocknodeport spec: crd: spec: names: kind: K8sBlockNodePort targets: - target: admission.k8s.gatekeeper.sh rego: | package k8sblocknodeport violation[{"msg": msg}] { input.review.kind.kind == "Service" input.review.object.spec.type == "NodePort" msg := "Cannot create service of type NodePort" } Templates Policies are written in Rego and packaged as parameterized ConstraintTemplate objects. The ConstraintTemplate extends Gatekeeper by adding a new policy that can be invoked via a new CR.

Slide 9

Slide 9 text

9 #GitLabCommit Constraints apiVersion: constraints.gatekeeper.sh/v1beta1 kind: K8sBlockNodePort metadata: name: block-node-port spec: enforcementAction: deny match: kinds: - apiGroups: [""] kinds: ["Service"] --- apiVersion: v1 kind: Service metadata: name: my-service-disallowed spec: type: NodePort ports: - port: 80 nodePort: 30007 Constraints are instantiations of a ConstraintTemplate CR and can be optionally scoped to specific objects and/or namespaces.

Slide 10

Slide 10 text

10 #GitLabCommit Constraints apiVersion: constraints.gatekeeper.sh/v1beta1 kind: K8sBlockNodePort metadata: name: block-node-port spec: enforcementAction: deny match: kinds: - apiGroups: [""] kinds: ["Service"] --- apiVersion: v1 kind: Service metadata: name: my-service-disallowed spec: type: NodePort ports: - port: 80 nodePort: 30007 Constraints are instantiations of a ConstraintTemplate CR and can be optionally scoped to specific objects and/or namespaces. When violated, Constraints can either deny admission or allow entry, and audit the violation in the status field.

Slide 11

Slide 11 text

11 #GitLabCommit #GitLabCommit Enforcement Gatekeeper reviews the request then denies admission or issues warnings, based on violations. But this only happens when resources are deployed. Gatekeeper Kubernetes Evaluate policy for incoming object Reject object as it violates policy apiVersion: v1 kind: Service metadata: name: my-service-disallowed spec: type: NodePort ports: - port: 80 nodePort: 30007

Slide 12

Slide 12 text

12 #GitLabCommit #GitLabCommit Runtime enforcement If resources violate any policies they will be rejected. But with GitOps, the controller will continually fail* to sync resources with clusters. Kubernetes Repo Gatekeeper GitOps * pending baked-in backoff, depends on your controller, YMMV, etc.

Slide 13

Slide 13 text

13 #GitLabCommit Workflows

Slide 14

Slide 14 text

14 #GitLabCommit Validation workflows Commits are pushed MRs are submitted Push Deploy ↺ Enforcement Push Review & Enforcement Deploy Merge

Slide 15

Slide 15 text

15 #GitLabCommit Validation tools googlecontainertools.github.io/kpt Kpt is an OSS tool for building declarative workflows on top of resource configuration. conftest.dev Conftest is a utility to help you write tests against structured configuration data. $ conftest test deployment.yaml --policy ./policy $ docker run -i gcr.io/kpt-functions/gatekeeper-validate

Slide 16

Slide 16 text

16 #GitLabCommit GitLab CI + conftest From the Open Policy Agent team Evaluates single or combined files Support for policy namespaces conftest-policy-validation: stage: test image: openpolicyagent/conftest:latest script: - echo "validating k8s-manifests/ against k8s-policies/" - /conftest test k8s-manifests --policy k8s-policies

Slide 17

Slide 17 text

17 #GitLabCommit Concat policies and manifests Validate to find violations Package and export workflows GitLab CI + kpt kpt-prep-manifests: stage: test image: gcr.io/kpt-dev/kpt:v0.4.0 script: - /kpt fn source k8s-manifests/ k8s-policies/ > kpt-manifests.yaml kpt-validate-manifests: stage: test image: gcr.io/kpt-functions/gatekeeper-validate script: - /app/gatekeeper_validate --input kpt-manifests.yaml

Slide 18

Slide 18 text

18 #GitLabCommit Infrastructure repos and clusters Infra Repo Kubernetes GitOps Gatekeeper Kubernetes GitOps Gatekeeper Kubernetes GitOps Gatekeeper Validation workflow

Slide 19

Slide 19 text

19 #GitLabCommit App repos and clusters App Repo Kubernetes GitOps Gatekeeper Kubernetes GitOps Gatekeeper Kubernetes GitOps Gatekeeper Validation workflow Infra Repo

Slide 20

Slide 20 text

20 #GitLabCommit Working Example

Slide 21

Slide 21 text

21 #GitLabCommit #GitLabCommit Modern CI/CD Prescriptive approach for software delivery Platform admins create and update best practices App developers iterate independently Infra teams implement policy across platform Devs Ops Infra Dev Prod

Slide 22

Slide 22 text

22 #GitLabCommit Devs Ops Infra Continuous Integration Container Registry Application Config Continuous Deployment Infrastructure Config & Policy Kubernetes App Repo Shared Config Infra Repo Env Repo CI/CD Workflow

Slide 23

Slide 23 text

23 #GitLabCommit Devs Ops Infra Continuous Integration Container Registry Application Config Continuous Deployment Infrastructure Config & Policy Kubernetes App Repo Shared Config Infra Repo Env Repo CI/CD Workflow + Policy Enforcement

Slide 24

Slide 24 text

24 #GitLabCommit #GitLabCommit Configuration Terraform + Cloud Build to deploy solution GitLab deployed on GKE Dev, Staging, and multi-region Prod clusters GitOps for app and config delivery Starter repos for CI, CD, and Kustomize Example repo for Go application Devs Ops Infra Dev Prod bit.ly/modern-cicd-repo bit.ly/modern-cicd-guide

Slide 25

Slide 25 text

25 #GitLabCommit Getting Started

Slide 26

Slide 26 text

26 #GitLabCommit #GitLabCommit Authoring – Starter The Rego Playground provides a solid editor to get started with OPA and share policies. Try it out at play.openpolicyagent.org

Slide 27

Slide 27 text

27 #GitLabCommit #GitLabCommit Authoring – IDE OPA has integrations for several editors and IDEs → VS Code, Sublime Text, IntelliJ, Emacs, VIM. Integrations differ depending on the tools but many offer syntax highlighting, query eval, policy coverage, and more.

Slide 28

Slide 28 text

28 #GitLabCommit Testing – Dev loop Use skaffold configs to add custom tests Build images with conftest and script execution apiVersion: skaffold/v2beta13 kind: Config metadata: name: test build: artifacts: - image: validate-image context: .. docker: dockerfile: validate/Dockerfile test: - image: validate-image custom: - command: docker run --entrypoint bash $IMAGE ./validate.sh

Slide 29

Slide 29 text

29 #GitLabCommit Donʼt forget to test against Gatekeeper itself Especially to understand the failure scenarios Testing – Gatekeeper

Slide 30

Slide 30 text

30 #GitLabCommit Example policies ● open-policy-agent/gatekeeper ○ Getting started examples ○ Includes required labels, allowed repos, container limits, unique service selector ● open-policy-agent/gatekeeper-library ○ Community-owned library of policies ○ General examples (see above) plus others (https-only, disallowed tags, unique ingress) ○ Pod Security Policies implemented as Constraints and ConstraintTemplates

Slide 31

Slide 31 text

31 #GitLabCommit Example policies ● crcsmnky/gatekeeper-istio ○ Gatekeeper policies for Istio resources ○ Require mTLS activation, disallow all inbound sources, port-naming conventions ● GoogleCloudPlatform/acm-policy-controller-library ○ Anthos Service Mesh (Istio) policies, for use with Anthos Config Management (GitOps) Policy Controller (Gatekeeper) ○ Multiple authz controls, peer authentication, mTLS traffic policies

Slide 32

Slide 32 text

32 #GitLabCommit Closing Thoughts

Slide 33

Slide 33 text

33 #GitLabCommit Complexity ● Scoping enforcement to the correct ○ Resources ○ Namespaces ○ Labels, etc. ● Understanding “fail open” vs “fail closed” ● Synchronizing resources to Gatekeeper for policy inputs ● RBAC for administering Constraints and ConstraintTemplates

Slide 34

Slide 34 text

34 #GitLabCommit Defense in depth Policy enforcement Signed images Vulnerability scanning Signature validation Audit controls Encrypted images

Slide 35

Slide 35 text

35 #GitLabCommit

Slide 36

Slide 36 text

36 #GitLabCommit Thank you! Questions or comments? Find me at @crcsmnky on Twitter.