Lab 08: Security, RBAC and Secrets
Workflow pods run as the serviceAccountName in the spec (default: namespace’s default SA). Give workflows their own least-privilege SA.
Dedicated ServiceAccount
kubectl create serviceaccount workflow-executor -n argo
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: workflow-executor-role
namespace: argo
rules:
- apiGroups: [argoproj.io]
resources: [workflowtaskresults]
verbs: [create, patch]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: workflow-executor-binding
namespace: argo
subjects:
- kind: ServiceAccount
name: workflow-executor
namespace: argo
roleRef:
kind: Role
name: workflow-executor-role
apiGroup: rbac.authorization.k8s.io
kubectl apply -f workflow-executor-rbac.yaml
- the
waitcontainer reports results by creatingworkflowtaskresults, that is the minimum since v3.4 (older versions neededpodspatch) - without it the workflow fails with
cannot create resource "workflowtaskresults"
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: rbac-test-
spec:
serviceAccountName: workflow-executor
entrypoint: main
templates:
- name: main
container:
image: alpine:3.20
command: [sh, -c]
args: ["echo running as dedicated SA; whoami"]
argo submit -n argo rbac-workflow.yaml --watch
argo logs -n argo @latest
- can also set a default for all workflows via
workflow-controller-configmapworkflowDefaults.spec.serviceAccountName - the SA that submits workflows (UI/CLI,
--auth-mode=client) is separate from the SA the pods use
Extra permissions for resource access
If the workflow talks to the k8s API (kubectl in the container, or a resource template), add rules to the role:
rules:
- apiGroups: [argoproj.io]
resources: [workflowtaskresults]
verbs: [create, patch]
- apiGroups: [""]
resources: [configmaps]
verbs: [create, get, list]
templates:
- name: main
container:
image: bitnami/kubectl:latest
command: [sh, -c]
args:
- |
kubectl create configmap workflow-generated-config --from-literal=key1=value1 -n argo
kubectl get configmap workflow-generated-config -n argo -o yaml
kubectl auth can-i create configmaps -n argo --as=system:serviceaccount:argo:workflow-executor
kubectl get configmap workflow-generated-config -n argo
- scope by resource type and verb, no wildcards
kubectl auth can-i --as=...to check a role without running a workflow
Secrets as env vars
kubectl create secret generic my-secret -n argo \
--from-literal=mypassword='S00perS3cret' --from-literal=api-key=abc123
container:
image: alpine:3.20
command: [sh, -c]
args: ['echo "password length: ${#MYSECRETPASSWORD}"']
env:
- name: MYSECRETPASSWORD
valueFrom:
secretKeyRef:
name: my-secret
key: mypassword
- plain k8s
secretKeyRef, the secret must be in the workflow’s namespace - don’t echo the value, it ends up in the logs (and the archived logs)
- don’t pass secrets as workflow parameters, they are visible in the Workflow object and the UI
Secrets as volumes
kubectl create secret generic my-cert-secret -n argo \
--from-file=tls.crt=./tls.crt --from-file=tls.key=./tls.key
spec:
serviceAccountName: workflow-executor
entrypoint: main
volumes:
- name: certs
secret:
secretName: my-cert-secret
templates:
- name: main
container:
image: alpine:3.20
command: [sh, -c]
args: ["ls -la /secret; wc -c /secret/*"]
volumeMounts:
- name: certs
mountPath: /secret
readOnly: true
- each key becomes a file under
mountPath readOnly: true, and considerdefaultMode: 0400on the volume
Notes
- never use
defaultSA in prod, one SA per workflow type - harden pods with
securityContext(workflow-levelspec.securityContextor per container:runAsNonRoot,readOnlyRootFilesystem, drop capabilities) - restrict workflow pod traffic with NetworkPolicy (pods carry the
workflows.argoproj.io/workflowlabel) - Pod Security Policies were removed in k8s 1.25, use Pod Security Admission labels on the namespace
- RBAC for the server/UI: SSO +
workflows.argoproj.io/rbac-ruleannotations on SAs map users to SAs
Cleanup
argo delete -n argo --all
kubectl delete configmap workflow-generated-config -n argo --ignore-not-found
kubectl delete secret my-secret my-cert-secret -n argo
kubectl delete rolebinding workflow-executor-binding -n argo
kubectl delete role workflow-executor-role -n argo
kubectl delete serviceaccount workflow-executor -n argo