|
2 | 2 |
|
3 | 3 | One important tool in the defense in depth strategy are policies. These define
|
4 | 4 | what is or is not allowed and part of it is usually an enforcement component.
|
| 5 | +In this section we will have a look at a number of different policies and how |
| 6 | +you can apply them. |
5 | 7 |
|
6 |
| -In the context of policies, the concept of least privileges is an important one. |
7 |
| -So let's have a look at this. |
| 8 | +In the context of policies, the concept of least privileges is an important one, |
| 9 | +so let's have a look at this for starters. |
8 | 10 |
|
9 | 11 | ## Least privileges
|
10 | 12 |
|
| 13 | +With least privileges we mean to equip someone or something with exactly the |
| 14 | +rights to carry out their or its task but not more. For example, if you consider |
| 15 | +a program that needs to read from a specific location in the filesystem then |
| 16 | +the operation (`read`) and the location (say, `/data`) would determine what |
| 17 | +permissions are necessary. Conversely, said program would _not_ need `write` access |
| 18 | +in addition and hence this would violate the least privileges principle. |
| 19 | + |
11 | 20 | First off we start with the simple case of a Kubernetes security context,
|
12 | 21 | allowing you to specify runtime policies around privileges and access control.
|
13 | 22 |
|
| 23 | +### Preparation |
| 24 | + |
14 | 25 | Let's create the cluster for it:
|
15 | 26 |
|
16 | 27 | ```
|
17 |
| -kind create cluster --name cnsectut --config res/security-context-cluster-config.yaml |
| 28 | +kind create cluster --name cnsectut \ |
| 29 | + --config res/security-context-cluster-config.yaml |
18 | 30 | ```
|
19 | 31 |
|
| 32 | +### Using a security context |
| 33 | + |
20 | 34 | We will be using an [example from the Kubernetes docs](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/)
|
21 | 35 | so that you can read up on the details later on.
|
22 | 36 |
|
| 37 | +First, launch the [pod with a security context](https://github.com/k8s-sec/cloud-native-security-tutorial/blob/master/res/pod-security-context.yaml) defined like so: |
| 38 | + |
23 | 39 | ```
|
24 | 40 | kubectl apply -f https://raw.githubusercontent.com/k8s-sec/cloud-native-security-tutorial/master/res/pod-security-context.yaml
|
| 41 | +``` |
25 | 42 |
|
26 |
| -kubectl exec -it security-context -- sh |
| 43 | +Next, enter the pod: |
27 | 44 |
|
28 |
| -echo something > /data/content |
| 45 | +``` |
| 46 | +kubectl exec -it security-context -- sh |
| 47 | +``` |
29 | 48 |
|
30 |
| -cd /data && id |
| 49 | +And, in the pod, create a file as shown: |
31 | 50 |
|
32 | 51 | ```
|
| 52 | +echo something > /data/content ; cd /data && id |
| 53 | +``` |
| 54 | + |
| 55 | +What you see here is how the security context defined in the pod enforces file |
| 56 | +and group ownership. But who enforces the definition of security contexts? That |
| 57 | +is, how can you make sure that a developer creating a pod spec in fact thinks |
| 58 | +of this? Enter [Pod Security |
| 59 | +Policies](https://kubernetes.io/docs/concepts/policy/pod-security-policy/) or |
| 60 | +PSP for short. |
| 61 | + |
| 62 | + |
| 63 | +Learn more about least privileges practices in the container runtime context via: |
| 64 | + |
| 65 | +- [rootlesscontaine.rs](https://rootlesscontaine.rs/) |
| 66 | +- [canihaznonprivilegedcontainers.info](http://canihaznonprivilegedcontainers.info/) |
33 | 67 |
|
34 |
| -See more at http://canihaznonprivilegedcontainers.info/ |
| 68 | +Clean up with `kind delete cluster --name cnsectut` when you're done exploring |
| 69 | +this topic. |
35 | 70 |
|
36 | 71 | ## Network policies
|
37 | 72 |
|
38 |
| -Let's create the cluster for the network policies walkthrough |
39 |
| -(kudos to [Alex](https://alexbrand.dev/post/creating-a-kind-cluster-with-calico-networking/) |
40 |
| -for the patch instructions): |
| 73 | +So runtime policies are fun, but not the only thing that matters: next up, we |
| 74 | +have a look at network policies. These kind of policies allow you to control |
| 75 | +the communication patterns in-cluster (between pods) and concerning the outside |
| 76 | +world (ingress and egress traffic): |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +You might be surprised to learn that in Kubernetes by default all traffic |
| 81 | +(in-cluster and to/from the outside world) is allowed. That is, any pod can see |
| 82 | +and talk to any other pod by default as well as any connection to a pod running in your |
| 83 | +Kubernetes cluster. |
| 84 | + |
| 85 | +### Preparation |
| 86 | + |
| 87 | +Let's create the cluster for the network policies walkthrough. |
| 88 | + |
| 89 | +The following can take a minute or two, depending on if you've pulled the |
| 90 | +container images before or doing it the first time (then it can take 10min or more): |
41 | 91 |
|
42 | 92 | ```
|
43 |
| -# can take a minute or two, depending on if you've pulled the container images |
44 |
| -# before or doing it the first time (then it can take 10min or more): |
45 | 93 | kind create cluster --name cnnp --config res/network-policy-cluster-config.yaml
|
| 94 | +``` |
| 95 | + |
| 96 | +Next, install the Calico controller and custom resources |
| 97 | +(this is the CNI plugin that allows us to enforce the network policies): |
46 | 98 |
|
47 |
| -# install Calico controller and custom resources, patch for testing: |
| 99 | +``` |
48 | 100 | kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
|
| 101 | +``` |
| 102 | + |
| 103 | +Then, we need to patch the setup due to the fact we're using `kind` here |
| 104 | +(kudos to [Alex](https://alexbrand.dev/post/creating-a-kind-cluster-with-calico-networking/) |
| 105 | +for the patch instructions): |
| 106 | + |
| 107 | +``` |
49 | 108 | kubectl -n kube-system set env daemonset/calico-node FELIX_IGNORELOOSERPF=true
|
| 109 | +``` |
50 | 110 |
|
51 |
| -# verify setup, can take some 5 min until you see all in the 'Running' state: |
| 111 | +Now you can verify the setup, and note that it can take some 5 min until |
| 112 | +you see all pods in the `Running` state: |
| 113 | + |
| 114 | +``` |
52 | 115 | kubectl -n kube-system get pods | grep calico-node
|
53 | 116 | ```
|
54 | 117 |
|
55 |
| -Now let's create workloads and define communication paths: |
| 118 | +### Limit ingress traffic |
| 119 | + |
| 120 | +Now let's create a workload and define communication paths: |
56 | 121 |
|
57 | 122 | ```
|
58 | 123 | #TBD: deploy pods in two namespaces that can talk to each other
|
59 | 124 | # and two that can not talk to each other (or ingress/egress)
|
60 | 125 | ```
|
61 | 126 |
|
62 |
| -See more at: |
| 127 | +Learn more about network policies via: |
63 | 128 |
|
64 | 129 | - [Exploring Network Policies in Kubernetes](https://banzaicloud.com/blog/network-policy/)
|
65 | 130 | - [Best Practices for Kubernetes Network Policies](https://medium.com/@tufin/best-practices-for-kubernetes-network-policies-2b643c4b1aa)
|
66 | 131 | - [Securing Kubernetes Cluster Networking](https://ahmet.im/blog/kubernetes-network-policy/)
|
67 | 132 |
|
| 133 | +Clean up with `kind delete cluster --name cnnp` when you're done exploring |
| 134 | +this topic. |
| 135 | + |
68 | 136 | ## OPA in action
|
69 | 137 |
|
70 |
| -what is it |
| 138 | +The Open Policy Agent (OPA) project is a |
71 | 139 |
|
72 | 140 | mini example via https://play.openpolicyagent.org/
|
73 | 141 |
|
|
0 commit comments