Skill files3 files

assets/

references/

SKILL.md348 lines
1
---
2
name: k8s-security-policies
3
description: Implement Kubernetes security policies including NetworkPolicy, PodSecurityPolicy, and RBAC for production-grade security. Use when securing Kubernetes clusters, implementing network isolation, or enforcing pod security standards.
4
---
5
6
# Kubernetes Security Policies
7
8
Comprehensive guide for implementing NetworkPolicy, PodSecurityPolicy, RBAC, and Pod Security Standards in Kubernetes.
9
10
## Purpose
11
12
Implement defense-in-depth security for Kubernetes clusters using network policies, pod security standards, and RBAC.
13
14
## When to Use This Skill
15
16
- Implement network segmentation
17
- Configure pod security standards
18
- Set up RBAC for least-privilege access
19
- Create security policies for compliance
20
- Implement admission control
21
- Secure multi-tenant clusters
22
23
## Pod Security Standards
24
25
### 1. Privileged (Unrestricted)
26
27
```yaml
28
apiVersion: v1
29
kind: Namespace
30
metadata:
31
name: privileged-ns
32
labels:
33
pod-security.kubernetes.io/enforce: privileged
34
pod-security.kubernetes.io/audit: privileged
35
pod-security.kubernetes.io/warn: privileged
36
```
37
38
### 2. Baseline (Minimally restrictive)
39
40
```yaml
41
apiVersion: v1
42
kind: Namespace
43
metadata:
44
name: baseline-ns
45
labels:
46
pod-security.kubernetes.io/enforce: baseline
47
pod-security.kubernetes.io/audit: baseline
48
pod-security.kubernetes.io/warn: baseline
49
```
50
51
### 3. Restricted (Most restrictive)
52
53
```yaml
54
apiVersion: v1
55
kind: Namespace
56
metadata:
57
name: restricted-ns
58
labels:
59
pod-security.kubernetes.io/enforce: restricted
60
pod-security.kubernetes.io/audit: restricted
61
pod-security.kubernetes.io/warn: restricted
62
```
63
64
## Network Policies
65
66
### Default Deny All
67
68
```yaml
69
apiVersion: networking.k8s.io/v1
70
kind: NetworkPolicy
71
metadata:
72
name: default-deny-all
73
namespace: production
74
spec:
75
podSelector: {}
76
policyTypes:
77
- Ingress
78
- Egress
79
```
80
81
### Allow Frontend to Backend
82
83
```yaml
84
apiVersion: networking.k8s.io/v1
85
kind: NetworkPolicy
86
metadata:
87
name: allow-frontend-to-backend
88
namespace: production
89
spec:
90
podSelector:
91
matchLabels:
92
app: backend
93
policyTypes:
94
- Ingress
95
ingress:
96
- from:
97
- podSelector:
98
matchLabels:
99
app: frontend
100
ports:
101
- protocol: TCP
102
port: 8080
103
```
104
105
### Allow DNS
106
107
```yaml
108
apiVersion: networking.k8s.io/v1
109
kind: NetworkPolicy
110
metadata:
111
name: allow-dns
112
namespace: production
113
spec:
114
podSelector: {}
115
policyTypes:
116
- Egress
117
egress:
118
- to:
119
- namespaceSelector:
120
matchLabels:
121
name: kube-system
122
ports:
123
- protocol: UDP
124
port: 53
125
```
126
127
**Reference:** See `assets/network-policy-template.yaml`
128
129
## RBAC Configuration
130
131
### Role (Namespace-scoped)
132
133
```yaml
134
apiVersion: rbac.authorization.k8s.io/v1
135
kind: Role
136
metadata:
137
name: pod-reader
138
namespace: production
139
rules:
140
- apiGroups: [""]
141
resources: ["pods"]
142
verbs: ["get", "watch", "list"]
143
```
144
145
### ClusterRole (Cluster-wide)
146
147
```yaml
148
apiVersion: rbac.authorization.k8s.io/v1
149
kind: ClusterRole
150
metadata:
151
name: secret-reader
152
rules:
153
- apiGroups: [""]
154
resources: ["secrets"]
155
verbs: ["get", "watch", "list"]
156
```
157
158
### RoleBinding
159
160
```yaml
161
apiVersion: rbac.authorization.k8s.io/v1
162
kind: RoleBinding
163
metadata:
164
name: read-pods
165
namespace: production
166
subjects:
167
- kind: User
168
name: jane
169
apiGroup: rbac.authorization.k8s.io
170
- kind: ServiceAccount
171
name: default
172
namespace: production
173
roleRef:
174
kind: Role
175
name: pod-reader
176
apiGroup: rbac.authorization.k8s.io
177
```
178
179
**Reference:** See `references/rbac-patterns.md`
180
181
## Pod Security Context
182
183
### Restricted Pod
184
185
```yaml
186
apiVersion: v1
187
kind: Pod
188
metadata:
189
name: secure-pod
190
spec:
191
securityContext:
192
runAsNonRoot: true
193
runAsUser: 1000
194
fsGroup: 1000
195
seccompProfile:
196
type: RuntimeDefault
197
containers:
198
- name: app
199
image: myapp:1.0
200
securityContext:
201
allowPrivilegeEscalation: false
202
readOnlyRootFilesystem: true
203
capabilities:
204
drop:
205
- ALL
206
```
207
208
## Policy Enforcement with OPA Gatekeeper
209
210
### ConstraintTemplate
211
212
```yaml
213
apiVersion: templates.gatekeeper.sh/v1
214
kind: ConstraintTemplate
215
metadata:
216
name: k8srequiredlabels
217
spec:
218
crd:
219
spec:
220
names:
221
kind: K8sRequiredLabels
222
validation:
223
openAPIV3Schema:
224
type: object
225
properties:
226
labels:
227
type: array
228
items:
229
type: string
230
targets:
231
- target: admission.k8s.gatekeeper.sh
232
rego: |
233
package k8srequiredlabels
234
violation[{"msg": msg, "details": {"missing_labels": missing}}] {
235
provided := {label | input.review.object.metadata.labels[label]}
236
required := {label | label := input.parameters.labels[_]}
237
missing := required - provided
238
count(missing) > 0
239
msg := sprintf("missing required labels: %v", [missing])
240
}
241
```
242
243
### Constraint
244
245
```yaml
246
apiVersion: constraints.gatekeeper.sh/v1beta1
247
kind: K8sRequiredLabels
248
metadata:
249
name: require-app-label
250
spec:
251
match:
252
kinds:
253
- apiGroups: ["apps"]
254
kinds: ["Deployment"]
255
parameters:
256
labels: ["app", "environment"]
257
```
258
259
## Service Mesh Security (Istio)
260
261
### PeerAuthentication (mTLS)
262
263
```yaml
264
apiVersion: security.istio.io/v1beta1
265
kind: PeerAuthentication
266
metadata:
267
name: default
268
namespace: production
269
spec:
270
mtls:
271
mode: STRICT
272
```
273
274
### AuthorizationPolicy
275
276
```yaml
277
apiVersion: security.istio.io/v1beta1
278
kind: AuthorizationPolicy
279
metadata:
280
name: allow-frontend
281
namespace: production
282
spec:
283
selector:
284
matchLabels:
285
app: backend
286
action: ALLOW
287
rules:
288
- from:
289
- source:
290
principals: ["cluster.local/ns/production/sa/frontend"]
291
```
292
293
## Best Practices
294
295
1. **Implement Pod Security Standards** at namespace level
296
2. **Use Network Policies** for network segmentation
297
3. **Apply least-privilege RBAC** for all service accounts
298
4. **Enable admission control** (OPA Gatekeeper/Kyverno)
299
5. **Run containers as non-root**
300
6. **Use read-only root filesystem**
301
7. **Drop all capabilities** unless needed
302
8. **Implement resource quotas** and limit ranges
303
9. **Enable audit logging** for security events
304
10. **Regular security scanning** of images
305
306
## Compliance Frameworks
307
308
### CIS Kubernetes Benchmark
309
310
- Use RBAC authorization
311
- Enable audit logging
312
- Use Pod Security Standards
313
- Configure network policies
314
- Implement secrets encryption at rest
315
- Enable node authentication
316
317
### NIST Cybersecurity Framework
318
319
- Implement defense in depth
320
- Use network segmentation
321
- Configure security monitoring
322
- Implement access controls
323
- Enable logging and monitoring
324
325
## Troubleshooting
326
327
**NetworkPolicy not working:**
328
329
```bash
330
# Check if CNI supports NetworkPolicy
331
kubectl get nodes -o wide
332
kubectl describe networkpolicy <name>
333
```
334
335
**RBAC permission denied:**
336
337
```bash
338
# Check effective permissions
339
kubectl auth can-i list pods --as system:serviceaccount:default:my-sa
340
kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-sa
341
```
342
343
344
## Related Skills
345
346
- `k8s-manifest-generator` - For creating secure manifests
347
- `gitops-workflow` - For automated policy deployment
348

© Seth Hobson · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…