Skip to content

Commit 6539c76

Browse files
committed
advances NP section
1 parent b071583 commit 6539c76

File tree

2 files changed

+85
-17
lines changed

2 files changed

+85
-17
lines changed

docs/img/kube-networking.png

152 KB
Loading

docs/policies.md

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,140 @@
22

33
One important tool in the defense in depth strategy are policies. These define
44
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.
57

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.
810

911
## Least privileges
1012

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+
1120
First off we start with the simple case of a Kubernetes security context,
1221
allowing you to specify runtime policies around privileges and access control.
1322

23+
### Preparation
24+
1425
Let's create the cluster for it:
1526

1627
```
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
1830
```
1931

32+
### Using a security context
33+
2034
We will be using an [example from the Kubernetes docs](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/)
2135
so that you can read up on the details later on.
2236

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+
2339
```
2440
kubectl apply -f https://raw.githubusercontent.com/k8s-sec/cloud-native-security-tutorial/master/res/pod-security-context.yaml
41+
```
2542

26-
kubectl exec -it security-context -- sh
43+
Next, enter the pod:
2744

28-
echo something > /data/content
45+
```
46+
kubectl exec -it security-context -- sh
47+
```
2948

30-
cd /data && id
49+
And, in the pod, create a file as shown:
3150

3251
```
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/)
3367

34-
See more at http://canihaznonprivilegedcontainers.info/
68+
Clean up with `kind delete cluster --name cnsectut` when you're done exploring
69+
this topic.
3570

3671
## Network policies
3772

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+
![Kubernetes networking overview](img/kube-networking.png)
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):
4191

4292
```
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):
4593
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):
4698

47-
# install Calico controller and custom resources, patch for testing:
99+
```
48100
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+
```
49108
kubectl -n kube-system set env daemonset/calico-node FELIX_IGNORELOOSERPF=true
109+
```
50110

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+
```
52115
kubectl -n kube-system get pods | grep calico-node
53116
```
54117

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:
56121

57122
```
58123
#TBD: deploy pods in two namespaces that can talk to each other
59124
# and two that can not talk to each other (or ingress/egress)
60125
```
61126

62-
See more at:
127+
Learn more about network policies via:
63128

64129
- [Exploring Network Policies in Kubernetes](https://banzaicloud.com/blog/network-policy/)
65130
- [Best Practices for Kubernetes Network Policies](https://medium.com/@tufin/best-practices-for-kubernetes-network-policies-2b643c4b1aa)
66131
- [Securing Kubernetes Cluster Networking](https://ahmet.im/blog/kubernetes-network-policy/)
67132

133+
Clean up with `kind delete cluster --name cnnp` when you're done exploring
134+
this topic.
135+
68136
## OPA in action
69137

70-
what is it
138+
The Open Policy Agent (OPA) project is a
71139

72140
mini example via https://play.openpolicyagent.org/
73141

0 commit comments

Comments
 (0)