specweave 0.3.13 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +17 -1
- package/README.md +1 -1
- package/bin/install-all.sh +9 -2
- package/bin/install-hooks.sh +57 -0
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +55 -0
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/core/agent-model-manager.d.ts +52 -0
- package/dist/core/agent-model-manager.d.ts.map +1 -0
- package/dist/core/agent-model-manager.js +120 -0
- package/dist/core/agent-model-manager.js.map +1 -0
- package/dist/core/cost-tracker.d.ts +108 -0
- package/dist/core/cost-tracker.d.ts.map +1 -0
- package/dist/core/cost-tracker.js +281 -0
- package/dist/core/cost-tracker.js.map +1 -0
- package/dist/core/model-selector.d.ts +57 -0
- package/dist/core/model-selector.d.ts.map +1 -0
- package/dist/core/model-selector.js +115 -0
- package/dist/core/model-selector.js.map +1 -0
- package/dist/core/phase-detector.d.ts +62 -0
- package/dist/core/phase-detector.d.ts.map +1 -0
- package/dist/core/phase-detector.js +229 -0
- package/dist/core/phase-detector.js.map +1 -0
- package/dist/types/cost-tracking.d.ts +43 -0
- package/dist/types/cost-tracking.d.ts.map +1 -0
- package/dist/types/cost-tracking.js +8 -0
- package/dist/types/cost-tracking.js.map +1 -0
- package/dist/types/model-selection.d.ts +53 -0
- package/dist/types/model-selection.d.ts.map +1 -0
- package/dist/types/model-selection.js +12 -0
- package/dist/types/model-selection.js.map +1 -0
- package/dist/utils/cost-reporter.d.ts +58 -0
- package/dist/utils/cost-reporter.d.ts.map +1 -0
- package/dist/utils/cost-reporter.js +224 -0
- package/dist/utils/cost-reporter.js.map +1 -0
- package/dist/utils/pricing-constants.d.ts +70 -0
- package/dist/utils/pricing-constants.d.ts.map +1 -0
- package/dist/utils/pricing-constants.js +71 -0
- package/dist/utils/pricing-constants.js.map +1 -0
- package/package.json +1 -1
- package/src/agents/architect/AGENT.md +3 -0
- package/src/agents/code-reviewer.md +156 -0
- package/src/agents/data-scientist/AGENT.md +181 -0
- package/src/agents/database-optimizer/AGENT.md +147 -0
- package/src/agents/devops/AGENT.md +3 -0
- package/src/agents/diagrams-architect/AGENT.md +3 -0
- package/src/agents/docs-writer/AGENT.md +3 -0
- package/src/agents/kubernetes-architect/AGENT.md +142 -0
- package/src/agents/ml-engineer/AGENT.md +150 -0
- package/src/agents/mlops-engineer/AGENT.md +201 -0
- package/src/agents/network-engineer/AGENT.md +149 -0
- package/src/agents/observability-engineer/AGENT.md +213 -0
- package/src/agents/payment-integration/AGENT.md +35 -0
- package/src/agents/performance/AGENT.md +3 -0
- package/src/agents/performance-engineer/AGENT.md +153 -0
- package/src/agents/pm/AGENT.md +3 -0
- package/src/agents/qa-lead/AGENT.md +3 -0
- package/src/agents/security/AGENT.md +3 -0
- package/src/agents/sre/AGENT.md +3 -0
- package/src/agents/tdd-orchestrator/AGENT.md +169 -0
- package/src/agents/tech-lead/AGENT.md +3 -0
- package/src/commands/specweave.costs.md +261 -0
- package/src/commands/specweave.ml-pipeline.md +292 -0
- package/src/commands/specweave.monitor-setup.md +501 -0
- package/src/commands/specweave.slo-implement.md +1055 -0
- package/src/commands/specweave.sync-github.md +1 -1
- package/src/commands/specweave.tdd-cycle.md +199 -0
- package/src/commands/specweave.tdd-green.md +842 -0
- package/src/commands/specweave.tdd-red.md +135 -0
- package/src/commands/specweave.tdd-refactor.md +165 -0
- package/src/skills/SKILLS-INDEX.md +18 -10
- package/src/skills/billing-automation/SKILL.md +559 -0
- package/src/skills/distributed-tracing/SKILL.md +438 -0
- package/src/skills/e2e-playwright/README.md +1 -1
- package/src/skills/e2e-playwright/package.json +1 -1
- package/src/skills/gitops-workflow/SKILL.md +285 -0
- package/src/skills/gitops-workflow/references/argocd-setup.md +134 -0
- package/src/skills/gitops-workflow/references/sync-policies.md +131 -0
- package/src/skills/grafana-dashboards/SKILL.md +369 -0
- package/src/skills/helm-chart-scaffolding/SKILL.md +544 -0
- package/src/skills/helm-chart-scaffolding/assets/Chart.yaml.template +42 -0
- package/src/skills/helm-chart-scaffolding/assets/values.yaml.template +185 -0
- package/src/skills/helm-chart-scaffolding/references/chart-structure.md +500 -0
- package/src/skills/helm-chart-scaffolding/scripts/validate-chart.sh +244 -0
- package/src/skills/k8s-manifest-generator/SKILL.md +511 -0
- package/src/skills/k8s-manifest-generator/assets/configmap-template.yaml +296 -0
- package/src/skills/k8s-manifest-generator/assets/deployment-template.yaml +203 -0
- package/src/skills/k8s-manifest-generator/assets/service-template.yaml +171 -0
- package/src/skills/k8s-manifest-generator/references/deployment-spec.md +753 -0
- package/src/skills/k8s-manifest-generator/references/service-spec.md +724 -0
- package/src/skills/k8s-security-policies/SKILL.md +334 -0
- package/src/skills/k8s-security-policies/assets/network-policy-template.yaml +177 -0
- package/src/skills/k8s-security-policies/references/rbac-patterns.md +187 -0
- package/src/skills/ml-pipeline-workflow/SKILL.md +245 -0
- package/src/skills/paypal-integration/SKILL.md +467 -0
- package/src/skills/pci-compliance/SKILL.md +466 -0
- package/src/skills/prometheus-configuration/SKILL.md +392 -0
- package/src/skills/slo-implementation/SKILL.md +329 -0
- package/src/skills/stripe-integration/SKILL.md +442 -0
- package/src/skills/tdd-workflow/SKILL.md +378 -0
- package/src/templates/README.md.template +1 -1
- package/src/skills/bmad-method-expert/SKILL.md +0 -626
- package/src/skills/bmad-method-expert/scripts/analyze-project.js +0 -318
- package/src/skills/bmad-method-expert/scripts/check-setup.js +0 -208
- package/src/skills/bmad-method-expert/scripts/generate-template.js +0 -1149
- package/src/skills/bmad-method-expert/scripts/validate-documents.js +0 -340
- package/src/skills/context-optimizer/SKILL.md +0 -588
- package/src/skills/figma-designer/SKILL.md +0 -149
- package/src/skills/figma-implementer/SKILL.md +0 -148
- package/src/skills/figma-mcp-connector/SKILL.md +0 -136
- package/src/skills/figma-to-code/SKILL.md +0 -128
- package/src/skills/spec-kit-expert/SKILL.md +0 -1010
|
@@ -0,0 +1,334 @@
|
|
|
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
|
+
```yaml
|
|
27
|
+
apiVersion: v1
|
|
28
|
+
kind: Namespace
|
|
29
|
+
metadata:
|
|
30
|
+
name: privileged-ns
|
|
31
|
+
labels:
|
|
32
|
+
pod-security.kubernetes.io/enforce: privileged
|
|
33
|
+
pod-security.kubernetes.io/audit: privileged
|
|
34
|
+
pod-security.kubernetes.io/warn: privileged
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Baseline (Minimally restrictive)
|
|
38
|
+
```yaml
|
|
39
|
+
apiVersion: v1
|
|
40
|
+
kind: Namespace
|
|
41
|
+
metadata:
|
|
42
|
+
name: baseline-ns
|
|
43
|
+
labels:
|
|
44
|
+
pod-security.kubernetes.io/enforce: baseline
|
|
45
|
+
pod-security.kubernetes.io/audit: baseline
|
|
46
|
+
pod-security.kubernetes.io/warn: baseline
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 3. Restricted (Most restrictive)
|
|
50
|
+
```yaml
|
|
51
|
+
apiVersion: v1
|
|
52
|
+
kind: Namespace
|
|
53
|
+
metadata:
|
|
54
|
+
name: restricted-ns
|
|
55
|
+
labels:
|
|
56
|
+
pod-security.kubernetes.io/enforce: restricted
|
|
57
|
+
pod-security.kubernetes.io/audit: restricted
|
|
58
|
+
pod-security.kubernetes.io/warn: restricted
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Network Policies
|
|
62
|
+
|
|
63
|
+
### Default Deny All
|
|
64
|
+
```yaml
|
|
65
|
+
apiVersion: networking.k8s.io/v1
|
|
66
|
+
kind: NetworkPolicy
|
|
67
|
+
metadata:
|
|
68
|
+
name: default-deny-all
|
|
69
|
+
namespace: production
|
|
70
|
+
spec:
|
|
71
|
+
podSelector: {}
|
|
72
|
+
policyTypes:
|
|
73
|
+
- Ingress
|
|
74
|
+
- Egress
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Allow Frontend to Backend
|
|
78
|
+
```yaml
|
|
79
|
+
apiVersion: networking.k8s.io/v1
|
|
80
|
+
kind: NetworkPolicy
|
|
81
|
+
metadata:
|
|
82
|
+
name: allow-frontend-to-backend
|
|
83
|
+
namespace: production
|
|
84
|
+
spec:
|
|
85
|
+
podSelector:
|
|
86
|
+
matchLabels:
|
|
87
|
+
app: backend
|
|
88
|
+
policyTypes:
|
|
89
|
+
- Ingress
|
|
90
|
+
ingress:
|
|
91
|
+
- from:
|
|
92
|
+
- podSelector:
|
|
93
|
+
matchLabels:
|
|
94
|
+
app: frontend
|
|
95
|
+
ports:
|
|
96
|
+
- protocol: TCP
|
|
97
|
+
port: 8080
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Allow DNS
|
|
101
|
+
```yaml
|
|
102
|
+
apiVersion: networking.k8s.io/v1
|
|
103
|
+
kind: NetworkPolicy
|
|
104
|
+
metadata:
|
|
105
|
+
name: allow-dns
|
|
106
|
+
namespace: production
|
|
107
|
+
spec:
|
|
108
|
+
podSelector: {}
|
|
109
|
+
policyTypes:
|
|
110
|
+
- Egress
|
|
111
|
+
egress:
|
|
112
|
+
- to:
|
|
113
|
+
- namespaceSelector:
|
|
114
|
+
matchLabels:
|
|
115
|
+
name: kube-system
|
|
116
|
+
ports:
|
|
117
|
+
- protocol: UDP
|
|
118
|
+
port: 53
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**Reference:** See `assets/network-policy-template.yaml`
|
|
122
|
+
|
|
123
|
+
## RBAC Configuration
|
|
124
|
+
|
|
125
|
+
### Role (Namespace-scoped)
|
|
126
|
+
```yaml
|
|
127
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
128
|
+
kind: Role
|
|
129
|
+
metadata:
|
|
130
|
+
name: pod-reader
|
|
131
|
+
namespace: production
|
|
132
|
+
rules:
|
|
133
|
+
- apiGroups: [""]
|
|
134
|
+
resources: ["pods"]
|
|
135
|
+
verbs: ["get", "watch", "list"]
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### ClusterRole (Cluster-wide)
|
|
139
|
+
```yaml
|
|
140
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
141
|
+
kind: ClusterRole
|
|
142
|
+
metadata:
|
|
143
|
+
name: secret-reader
|
|
144
|
+
rules:
|
|
145
|
+
- apiGroups: [""]
|
|
146
|
+
resources: ["secrets"]
|
|
147
|
+
verbs: ["get", "watch", "list"]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### RoleBinding
|
|
151
|
+
```yaml
|
|
152
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
153
|
+
kind: RoleBinding
|
|
154
|
+
metadata:
|
|
155
|
+
name: read-pods
|
|
156
|
+
namespace: production
|
|
157
|
+
subjects:
|
|
158
|
+
- kind: User
|
|
159
|
+
name: jane
|
|
160
|
+
apiGroup: rbac.authorization.k8s.io
|
|
161
|
+
- kind: ServiceAccount
|
|
162
|
+
name: default
|
|
163
|
+
namespace: production
|
|
164
|
+
roleRef:
|
|
165
|
+
kind: Role
|
|
166
|
+
name: pod-reader
|
|
167
|
+
apiGroup: rbac.authorization.k8s.io
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**Reference:** See `references/rbac-patterns.md`
|
|
171
|
+
|
|
172
|
+
## Pod Security Context
|
|
173
|
+
|
|
174
|
+
### Restricted Pod
|
|
175
|
+
```yaml
|
|
176
|
+
apiVersion: v1
|
|
177
|
+
kind: Pod
|
|
178
|
+
metadata:
|
|
179
|
+
name: secure-pod
|
|
180
|
+
spec:
|
|
181
|
+
securityContext:
|
|
182
|
+
runAsNonRoot: true
|
|
183
|
+
runAsUser: 1000
|
|
184
|
+
fsGroup: 1000
|
|
185
|
+
seccompProfile:
|
|
186
|
+
type: RuntimeDefault
|
|
187
|
+
containers:
|
|
188
|
+
- name: app
|
|
189
|
+
image: myapp:1.0
|
|
190
|
+
securityContext:
|
|
191
|
+
allowPrivilegeEscalation: false
|
|
192
|
+
readOnlyRootFilesystem: true
|
|
193
|
+
capabilities:
|
|
194
|
+
drop:
|
|
195
|
+
- ALL
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Policy Enforcement with OPA Gatekeeper
|
|
199
|
+
|
|
200
|
+
### ConstraintTemplate
|
|
201
|
+
```yaml
|
|
202
|
+
apiVersion: templates.gatekeeper.sh/v1
|
|
203
|
+
kind: ConstraintTemplate
|
|
204
|
+
metadata:
|
|
205
|
+
name: k8srequiredlabels
|
|
206
|
+
spec:
|
|
207
|
+
crd:
|
|
208
|
+
spec:
|
|
209
|
+
names:
|
|
210
|
+
kind: K8sRequiredLabels
|
|
211
|
+
validation:
|
|
212
|
+
openAPIV3Schema:
|
|
213
|
+
type: object
|
|
214
|
+
properties:
|
|
215
|
+
labels:
|
|
216
|
+
type: array
|
|
217
|
+
items:
|
|
218
|
+
type: string
|
|
219
|
+
targets:
|
|
220
|
+
- target: admission.k8s.gatekeeper.sh
|
|
221
|
+
rego: |
|
|
222
|
+
package k8srequiredlabels
|
|
223
|
+
violation[{"msg": msg, "details": {"missing_labels": missing}}] {
|
|
224
|
+
provided := {label | input.review.object.metadata.labels[label]}
|
|
225
|
+
required := {label | label := input.parameters.labels[_]}
|
|
226
|
+
missing := required - provided
|
|
227
|
+
count(missing) > 0
|
|
228
|
+
msg := sprintf("missing required labels: %v", [missing])
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Constraint
|
|
233
|
+
```yaml
|
|
234
|
+
apiVersion: constraints.gatekeeper.sh/v1beta1
|
|
235
|
+
kind: K8sRequiredLabels
|
|
236
|
+
metadata:
|
|
237
|
+
name: require-app-label
|
|
238
|
+
spec:
|
|
239
|
+
match:
|
|
240
|
+
kinds:
|
|
241
|
+
- apiGroups: ["apps"]
|
|
242
|
+
kinds: ["Deployment"]
|
|
243
|
+
parameters:
|
|
244
|
+
labels: ["app", "environment"]
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Service Mesh Security (Istio)
|
|
248
|
+
|
|
249
|
+
### PeerAuthentication (mTLS)
|
|
250
|
+
```yaml
|
|
251
|
+
apiVersion: security.istio.io/v1beta1
|
|
252
|
+
kind: PeerAuthentication
|
|
253
|
+
metadata:
|
|
254
|
+
name: default
|
|
255
|
+
namespace: production
|
|
256
|
+
spec:
|
|
257
|
+
mtls:
|
|
258
|
+
mode: STRICT
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### AuthorizationPolicy
|
|
262
|
+
```yaml
|
|
263
|
+
apiVersion: security.istio.io/v1beta1
|
|
264
|
+
kind: AuthorizationPolicy
|
|
265
|
+
metadata:
|
|
266
|
+
name: allow-frontend
|
|
267
|
+
namespace: production
|
|
268
|
+
spec:
|
|
269
|
+
selector:
|
|
270
|
+
matchLabels:
|
|
271
|
+
app: backend
|
|
272
|
+
action: ALLOW
|
|
273
|
+
rules:
|
|
274
|
+
- from:
|
|
275
|
+
- source:
|
|
276
|
+
principals: ["cluster.local/ns/production/sa/frontend"]
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## Best Practices
|
|
280
|
+
|
|
281
|
+
1. **Implement Pod Security Standards** at namespace level
|
|
282
|
+
2. **Use Network Policies** for network segmentation
|
|
283
|
+
3. **Apply least-privilege RBAC** for all service accounts
|
|
284
|
+
4. **Enable admission control** (OPA Gatekeeper/Kyverno)
|
|
285
|
+
5. **Run containers as non-root**
|
|
286
|
+
6. **Use read-only root filesystem**
|
|
287
|
+
7. **Drop all capabilities** unless needed
|
|
288
|
+
8. **Implement resource quotas** and limit ranges
|
|
289
|
+
9. **Enable audit logging** for security events
|
|
290
|
+
10. **Regular security scanning** of images
|
|
291
|
+
|
|
292
|
+
## Compliance Frameworks
|
|
293
|
+
|
|
294
|
+
### CIS Kubernetes Benchmark
|
|
295
|
+
- Use RBAC authorization
|
|
296
|
+
- Enable audit logging
|
|
297
|
+
- Use Pod Security Standards
|
|
298
|
+
- Configure network policies
|
|
299
|
+
- Implement secrets encryption at rest
|
|
300
|
+
- Enable node authentication
|
|
301
|
+
|
|
302
|
+
### NIST Cybersecurity Framework
|
|
303
|
+
- Implement defense in depth
|
|
304
|
+
- Use network segmentation
|
|
305
|
+
- Configure security monitoring
|
|
306
|
+
- Implement access controls
|
|
307
|
+
- Enable logging and monitoring
|
|
308
|
+
|
|
309
|
+
## Troubleshooting
|
|
310
|
+
|
|
311
|
+
**NetworkPolicy not working:**
|
|
312
|
+
```bash
|
|
313
|
+
# Check if CNI supports NetworkPolicy
|
|
314
|
+
kubectl get nodes -o wide
|
|
315
|
+
kubectl describe networkpolicy <name>
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
**RBAC permission denied:**
|
|
319
|
+
```bash
|
|
320
|
+
# Check effective permissions
|
|
321
|
+
kubectl auth can-i list pods --as system:serviceaccount:default:my-sa
|
|
322
|
+
kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-sa
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Reference Files
|
|
326
|
+
|
|
327
|
+
- `assets/network-policy-template.yaml` - Network policy examples
|
|
328
|
+
- `assets/pod-security-template.yaml` - Pod security policies
|
|
329
|
+
- `references/rbac-patterns.md` - RBAC configuration patterns
|
|
330
|
+
|
|
331
|
+
## Related Skills
|
|
332
|
+
|
|
333
|
+
- `k8s-manifest-generator` - For creating secure manifests
|
|
334
|
+
- `gitops-workflow` - For automated policy deployment
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# Network Policy Templates
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
# Template 1: Default Deny All (Start Here)
|
|
5
|
+
apiVersion: networking.k8s.io/v1
|
|
6
|
+
kind: NetworkPolicy
|
|
7
|
+
metadata:
|
|
8
|
+
name: default-deny-all
|
|
9
|
+
namespace: <namespace>
|
|
10
|
+
spec:
|
|
11
|
+
podSelector: {}
|
|
12
|
+
policyTypes:
|
|
13
|
+
- Ingress
|
|
14
|
+
- Egress
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
# Template 2: Allow DNS (Essential)
|
|
18
|
+
apiVersion: networking.k8s.io/v1
|
|
19
|
+
kind: NetworkPolicy
|
|
20
|
+
metadata:
|
|
21
|
+
name: allow-dns
|
|
22
|
+
namespace: <namespace>
|
|
23
|
+
spec:
|
|
24
|
+
podSelector: {}
|
|
25
|
+
policyTypes:
|
|
26
|
+
- Egress
|
|
27
|
+
egress:
|
|
28
|
+
- to:
|
|
29
|
+
- namespaceSelector:
|
|
30
|
+
matchLabels:
|
|
31
|
+
name: kube-system
|
|
32
|
+
ports:
|
|
33
|
+
- protocol: UDP
|
|
34
|
+
port: 53
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
# Template 3: Frontend to Backend
|
|
38
|
+
apiVersion: networking.k8s.io/v1
|
|
39
|
+
kind: NetworkPolicy
|
|
40
|
+
metadata:
|
|
41
|
+
name: allow-frontend-to-backend
|
|
42
|
+
namespace: <namespace>
|
|
43
|
+
spec:
|
|
44
|
+
podSelector:
|
|
45
|
+
matchLabels:
|
|
46
|
+
app: backend
|
|
47
|
+
tier: backend
|
|
48
|
+
policyTypes:
|
|
49
|
+
- Ingress
|
|
50
|
+
ingress:
|
|
51
|
+
- from:
|
|
52
|
+
- podSelector:
|
|
53
|
+
matchLabels:
|
|
54
|
+
app: frontend
|
|
55
|
+
tier: frontend
|
|
56
|
+
ports:
|
|
57
|
+
- protocol: TCP
|
|
58
|
+
port: 8080
|
|
59
|
+
- protocol: TCP
|
|
60
|
+
port: 9090
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
# Template 4: Allow Ingress Controller
|
|
64
|
+
apiVersion: networking.k8s.io/v1
|
|
65
|
+
kind: NetworkPolicy
|
|
66
|
+
metadata:
|
|
67
|
+
name: allow-ingress-controller
|
|
68
|
+
namespace: <namespace>
|
|
69
|
+
spec:
|
|
70
|
+
podSelector:
|
|
71
|
+
matchLabels:
|
|
72
|
+
app: web
|
|
73
|
+
policyTypes:
|
|
74
|
+
- Ingress
|
|
75
|
+
ingress:
|
|
76
|
+
- from:
|
|
77
|
+
- namespaceSelector:
|
|
78
|
+
matchLabels:
|
|
79
|
+
name: ingress-nginx
|
|
80
|
+
ports:
|
|
81
|
+
- protocol: TCP
|
|
82
|
+
port: 80
|
|
83
|
+
- protocol: TCP
|
|
84
|
+
port: 443
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
# Template 5: Allow Monitoring (Prometheus)
|
|
88
|
+
apiVersion: networking.k8s.io/v1
|
|
89
|
+
kind: NetworkPolicy
|
|
90
|
+
metadata:
|
|
91
|
+
name: allow-prometheus-scraping
|
|
92
|
+
namespace: <namespace>
|
|
93
|
+
spec:
|
|
94
|
+
podSelector:
|
|
95
|
+
matchLabels:
|
|
96
|
+
prometheus.io/scrape: "true"
|
|
97
|
+
policyTypes:
|
|
98
|
+
- Ingress
|
|
99
|
+
ingress:
|
|
100
|
+
- from:
|
|
101
|
+
- namespaceSelector:
|
|
102
|
+
matchLabels:
|
|
103
|
+
name: monitoring
|
|
104
|
+
ports:
|
|
105
|
+
- protocol: TCP
|
|
106
|
+
port: 9090
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
# Template 6: Allow External HTTPS
|
|
110
|
+
apiVersion: networking.k8s.io/v1
|
|
111
|
+
kind: NetworkPolicy
|
|
112
|
+
metadata:
|
|
113
|
+
name: allow-external-https
|
|
114
|
+
namespace: <namespace>
|
|
115
|
+
spec:
|
|
116
|
+
podSelector:
|
|
117
|
+
matchLabels:
|
|
118
|
+
app: api-client
|
|
119
|
+
policyTypes:
|
|
120
|
+
- Egress
|
|
121
|
+
egress:
|
|
122
|
+
- to:
|
|
123
|
+
- ipBlock:
|
|
124
|
+
cidr: 0.0.0.0/0
|
|
125
|
+
except:
|
|
126
|
+
- 169.254.169.254/32 # Block metadata service
|
|
127
|
+
ports:
|
|
128
|
+
- protocol: TCP
|
|
129
|
+
port: 443
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
# Template 7: Database Access
|
|
133
|
+
apiVersion: networking.k8s.io/v1
|
|
134
|
+
kind: NetworkPolicy
|
|
135
|
+
metadata:
|
|
136
|
+
name: allow-app-to-database
|
|
137
|
+
namespace: <namespace>
|
|
138
|
+
spec:
|
|
139
|
+
podSelector:
|
|
140
|
+
matchLabels:
|
|
141
|
+
app: postgres
|
|
142
|
+
tier: database
|
|
143
|
+
policyTypes:
|
|
144
|
+
- Ingress
|
|
145
|
+
ingress:
|
|
146
|
+
- from:
|
|
147
|
+
- podSelector:
|
|
148
|
+
matchLabels:
|
|
149
|
+
tier: backend
|
|
150
|
+
ports:
|
|
151
|
+
- protocol: TCP
|
|
152
|
+
port: 5432
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
# Template 8: Cross-Namespace Communication
|
|
156
|
+
apiVersion: networking.k8s.io/v1
|
|
157
|
+
kind: NetworkPolicy
|
|
158
|
+
metadata:
|
|
159
|
+
name: allow-from-prod-namespace
|
|
160
|
+
namespace: <namespace>
|
|
161
|
+
spec:
|
|
162
|
+
podSelector:
|
|
163
|
+
matchLabels:
|
|
164
|
+
app: api
|
|
165
|
+
policyTypes:
|
|
166
|
+
- Ingress
|
|
167
|
+
ingress:
|
|
168
|
+
- from:
|
|
169
|
+
- namespaceSelector:
|
|
170
|
+
matchLabels:
|
|
171
|
+
environment: production
|
|
172
|
+
podSelector:
|
|
173
|
+
matchLabels:
|
|
174
|
+
app: frontend
|
|
175
|
+
ports:
|
|
176
|
+
- protocol: TCP
|
|
177
|
+
port: 8080
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# RBAC Patterns and Best Practices
|
|
2
|
+
|
|
3
|
+
## Common RBAC Patterns
|
|
4
|
+
|
|
5
|
+
### Pattern 1: Read-Only Access
|
|
6
|
+
```yaml
|
|
7
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
8
|
+
kind: ClusterRole
|
|
9
|
+
metadata:
|
|
10
|
+
name: read-only
|
|
11
|
+
rules:
|
|
12
|
+
- apiGroups: ["", "apps", "batch"]
|
|
13
|
+
resources: ["*"]
|
|
14
|
+
verbs: ["get", "list", "watch"]
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Pattern 2: Namespace Admin
|
|
18
|
+
```yaml
|
|
19
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
20
|
+
kind: Role
|
|
21
|
+
metadata:
|
|
22
|
+
name: namespace-admin
|
|
23
|
+
namespace: production
|
|
24
|
+
rules:
|
|
25
|
+
- apiGroups: ["", "apps", "batch", "extensions"]
|
|
26
|
+
resources: ["*"]
|
|
27
|
+
verbs: ["*"]
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Pattern 3: Deployment Manager
|
|
31
|
+
```yaml
|
|
32
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
33
|
+
kind: Role
|
|
34
|
+
metadata:
|
|
35
|
+
name: deployment-manager
|
|
36
|
+
namespace: production
|
|
37
|
+
rules:
|
|
38
|
+
- apiGroups: ["apps"]
|
|
39
|
+
resources: ["deployments"]
|
|
40
|
+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
|
|
41
|
+
- apiGroups: [""]
|
|
42
|
+
resources: ["pods"]
|
|
43
|
+
verbs: ["get", "list", "watch"]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Pattern 4: Secret Reader (ServiceAccount)
|
|
47
|
+
```yaml
|
|
48
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
49
|
+
kind: Role
|
|
50
|
+
metadata:
|
|
51
|
+
name: secret-reader
|
|
52
|
+
namespace: production
|
|
53
|
+
rules:
|
|
54
|
+
- apiGroups: [""]
|
|
55
|
+
resources: ["secrets"]
|
|
56
|
+
verbs: ["get"]
|
|
57
|
+
resourceNames: ["app-secrets"] # Specific secret only
|
|
58
|
+
---
|
|
59
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
60
|
+
kind: RoleBinding
|
|
61
|
+
metadata:
|
|
62
|
+
name: app-secret-reader
|
|
63
|
+
namespace: production
|
|
64
|
+
subjects:
|
|
65
|
+
- kind: ServiceAccount
|
|
66
|
+
name: my-app
|
|
67
|
+
namespace: production
|
|
68
|
+
roleRef:
|
|
69
|
+
kind: Role
|
|
70
|
+
name: secret-reader
|
|
71
|
+
apiGroup: rbac.authorization.k8s.io
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Pattern 5: CI/CD Pipeline Access
|
|
75
|
+
```yaml
|
|
76
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
77
|
+
kind: ClusterRole
|
|
78
|
+
metadata:
|
|
79
|
+
name: cicd-deployer
|
|
80
|
+
rules:
|
|
81
|
+
- apiGroups: ["apps"]
|
|
82
|
+
resources: ["deployments", "replicasets"]
|
|
83
|
+
verbs: ["get", "list", "create", "update", "patch"]
|
|
84
|
+
- apiGroups: [""]
|
|
85
|
+
resources: ["services", "configmaps"]
|
|
86
|
+
verbs: ["get", "list", "create", "update", "patch"]
|
|
87
|
+
- apiGroups: [""]
|
|
88
|
+
resources: ["pods"]
|
|
89
|
+
verbs: ["get", "list"]
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## ServiceAccount Best Practices
|
|
93
|
+
|
|
94
|
+
### Create Dedicated ServiceAccounts
|
|
95
|
+
```yaml
|
|
96
|
+
apiVersion: v1
|
|
97
|
+
kind: ServiceAccount
|
|
98
|
+
metadata:
|
|
99
|
+
name: my-app
|
|
100
|
+
namespace: production
|
|
101
|
+
---
|
|
102
|
+
apiVersion: apps/v1
|
|
103
|
+
kind: Deployment
|
|
104
|
+
metadata:
|
|
105
|
+
name: my-app
|
|
106
|
+
spec:
|
|
107
|
+
template:
|
|
108
|
+
spec:
|
|
109
|
+
serviceAccountName: my-app
|
|
110
|
+
automountServiceAccountToken: false # Disable if not needed
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Least-Privilege ServiceAccount
|
|
114
|
+
```yaml
|
|
115
|
+
apiVersion: rbac.authorization.k8s.io/v1
|
|
116
|
+
kind: Role
|
|
117
|
+
metadata:
|
|
118
|
+
name: my-app-role
|
|
119
|
+
namespace: production
|
|
120
|
+
rules:
|
|
121
|
+
- apiGroups: [""]
|
|
122
|
+
resources: ["configmaps"]
|
|
123
|
+
verbs: ["get"]
|
|
124
|
+
resourceNames: ["my-app-config"]
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Security Best Practices
|
|
128
|
+
|
|
129
|
+
1. **Use Roles over ClusterRoles** when possible
|
|
130
|
+
2. **Specify resourceNames** for fine-grained access
|
|
131
|
+
3. **Avoid wildcard permissions** (`*`) in production
|
|
132
|
+
4. **Create dedicated ServiceAccounts** for each app
|
|
133
|
+
5. **Disable token auto-mounting** if not needed
|
|
134
|
+
6. **Regular RBAC audits** to remove unused permissions
|
|
135
|
+
7. **Use groups** for user management
|
|
136
|
+
8. **Implement namespace isolation**
|
|
137
|
+
9. **Monitor RBAC usage** with audit logs
|
|
138
|
+
10. **Document role purposes** in metadata
|
|
139
|
+
|
|
140
|
+
## Troubleshooting RBAC
|
|
141
|
+
|
|
142
|
+
### Check User Permissions
|
|
143
|
+
```bash
|
|
144
|
+
kubectl auth can-i list pods --as john@example.com
|
|
145
|
+
kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-app
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### View Effective Permissions
|
|
149
|
+
```bash
|
|
150
|
+
kubectl describe clusterrole cluster-admin
|
|
151
|
+
kubectl describe rolebinding -n production
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Debug Access Issues
|
|
155
|
+
```bash
|
|
156
|
+
kubectl get rolebindings,clusterrolebindings --all-namespaces -o wide | grep my-user
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Common RBAC Verbs
|
|
160
|
+
|
|
161
|
+
- `get` - Read a specific resource
|
|
162
|
+
- `list` - List all resources of a type
|
|
163
|
+
- `watch` - Watch for resource changes
|
|
164
|
+
- `create` - Create new resources
|
|
165
|
+
- `update` - Update existing resources
|
|
166
|
+
- `patch` - Partially update resources
|
|
167
|
+
- `delete` - Delete resources
|
|
168
|
+
- `deletecollection` - Delete multiple resources
|
|
169
|
+
- `*` - All verbs (avoid in production)
|
|
170
|
+
|
|
171
|
+
## Resource Scope
|
|
172
|
+
|
|
173
|
+
### Cluster-Scoped Resources
|
|
174
|
+
- Nodes
|
|
175
|
+
- PersistentVolumes
|
|
176
|
+
- ClusterRoles
|
|
177
|
+
- ClusterRoleBindings
|
|
178
|
+
- Namespaces
|
|
179
|
+
|
|
180
|
+
### Namespace-Scoped Resources
|
|
181
|
+
- Pods
|
|
182
|
+
- Services
|
|
183
|
+
- Deployments
|
|
184
|
+
- ConfigMaps
|
|
185
|
+
- Secrets
|
|
186
|
+
- Roles
|
|
187
|
+
- RoleBindings
|