Security
Some of the scenario questions here are based on Kodekloud's CKA course labs.
CKAD and CKA can have similar scenario questions. It is recommended to go through the CKAD practice tests.
Shortcuts
First run the two commands below for shortcuts.
export do="--dry-run=client -o yaml"
export now="--force --grace-period=0"
Questions
-
Add a new rule in the existing role developer to grant the dev-user permissions to create deployments in the blue namespace. Remember to add api group "apps".
controlplane ~ ➜ k get -n blue roleNAME CREATED ATdeveloper 2023-12-30T16:01:43ZAnswer
controlplane ~ ➜ k get role -n blue developer -o yaml > blue-dev-role.yamlAdd a new api-group in the YAML file.
## blue-dev-role.yamlapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:creationTimestamp: "2023-12-30T15:24:38Z"name: developernamespace: blueresourceVersion: "619"uid: 994093a1-b5e4-4256-b911-533769b6eb63rules:- apiGroups:- ""resourceNames:- dark-blue-appresources:- podsverbs:- get- watch- create- delete- apiGroups:- appsresources:- deploymentsverbs:- createcontrolplane ~ ➜ k delete -f blue-dev-role.yamlrole.rbac.authorization.k8s.io "developer" deletedcontrolplane ~ ➜ k apply -f blue-dev-role.yamlrole.rbac.authorization.k8s.io/developer createdCreate a sample deployment as the dev-user.
controlplane ~ ➜ k create deployment testing-access --image nginx --namespace blue --as dev-userdeployment.apps/testing-access created -
How many ClusterRoles do you see defined in the cluster?
Answer
controlplane ~ ➜ k get clusterroles --no-headers | wc -l70 -
What user/groups are the cluster-admin role bound to?
Answer
controlplane ~ ➜ k get clusterrole | grep admincluster-admin 2023-12-30T16:00:55Zsystem:aggregate-to-admin 2023-12-30T16:00:55Zsystem:kubelet-api-admin 2023-12-30T16:00:55Zadmin 2023-12-30T16:00:55Zcontrolplane ~ ➜ k get clusterrolebinding | grep admincluster-admin ClusterRole/cluster-admin 33mkube-apiserver-kubelet-admin ClusterRole/system:kubelet-api-admin 32mhelm-kube-system-traefik-crd ClusterRole/cluster-admin 32mhelm-kube-system-traefik ClusterRole/cluster-admin 32mcontrolplane ~ ➜ k describe clusterrolebinding cluster-adminName: cluster-adminLabels: kubernetes.io/bootstrapping=rbac-defaultsAnnotations: rbac.authorization.kubernetes.io/autoupdate: trueRole:Kind: ClusterRoleName: cluster-adminSubjects:Kind Name Namespace---- ---- ---------Group system:masters -
What permissions does the clusterrole cluster-admin have?
Answer
controlplane ~ ➜ k get clusterrole | grep admincluster-admin 2023-12-30T16:00:55Zsystem:aggregate-to-admin 2023-12-30T16:00:55Zsystem:kubelet-api-admin 2023-12-30T16:00:55Zadmin 2023-12-30T16:00:55Zcontrolplane ~ ➜ k describe clusterrole cluster-adminName: cluster-adminLabels: kubernetes.io/bootstrapping=rbac-defaultsAnnotations: rbac.authorization.kubernetes.io/autoupdate: truePolicyRule:Resources Non-Resource URLs Resource Names Verbs--------- ----------------- -------------- -----*.* [] [] [*][*] [] [*] -
A new user michelle joined the team. She will be focusing on the nodes in the cluster. Create the required ClusterRoles and ClusterRoleBindings so she gets access to the nodes.
Answer
## michelle-clusterrole.yamlapiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:# "namespace" omitted since ClusterRoles are not namespacedname: nodes-accessrules:- apiGroups: [""]resources:- nodesverbs:- "*"## michelle-clusterrolebinding.yamlapiVersion: rbac.authorization.k8s.io/v1# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace.kind: ClusterRoleBindingmetadata:name: nodes-access-bindingsubjects:- kind: Username: michelle # Name is case sensitiveapiGroup: rbac.authorization.k8s.ioroleRef:kind: ClusterRolename: nodes-accessapiGroup: rbac.authorization.k8s.iocontrolplane ~ ➜ k apply -f michelle-clusterrole.yamlclusterrole.rbac.authorization.k8s.io/nodes-access createdcontrolplane ~ ➜ k apply -f michelle-clusterrolebinding.yamlclusterrolebinding.rbac.authorization.k8s.io/nodes-access-binding createdcontrolplane ~ ➜ k apply -f michelle-clusterrole.yamlclusterrole.rbac.authorization.k8s.io/nodes-access createdcontrolplane ~ ➜ k apply -f michelle-clusterrolebinding.yamlclusterrolebinding.rbac.authorization.k8s.io/nodes-access-binding createdcontrolplane ~ ➜ k get nodes --as michelleNAME STATUS ROLES AGE VERSIONcontrolplane Ready control-plane,master 39m v1.27.1+k3s1 -
User michelle's responsibilities are growing and now she will be responsible for storage as well. Create the required ClusterRoles and ClusterRoleBindings to allow her access to Storage.
-
ClusterRole: storage-admin
-
Resource: persistentvolumes
-
Resource: storageclasses
-
ClusterRoleBinding: michelle-storage-admin
-
ClusterRoleBinding Subject: michelle
-
ClusterRoleBinding Role: storage-admin
Answer
## storage-admin-clusterrole.yamlapiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:# "namespace" omitted since ClusterRoles are not namespacedname: storage-adminrules:- apiGroups:- storage.k8s.ioresources:- storageclassesverbs:- "*"- apiGroups:- ""resources:- persistentvolumesverbs:- "*"## storage-admin-clusterrolebinding.yamlapiVersion: rbac.authorization.k8s.io/v1# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace.kind: ClusterRoleBindingmetadata:name: michelle-storage-adminsubjects:- kind: Username: michelle # Name is case sensitiveapiGroup: rbac.authorization.k8s.ioroleRef:kind: ClusterRolename: storage-adminapiGroup: rbac.authorization.k8s.iocontrolplane ~ ➜ k apply -f storage-admin-clusterrole.yamlclusterrole.rbac.authorization.k8s.io/storage-admin createdcontrolplane ~ ➜ k apply -f storage-admin-clusterrolebinding.yamlclusterrolebinding.rbac.authorization.k8s.io/michelle-storage-admin createdcontrolplane ~ ➜ k get clusterrole | grep storage-adminstorage-admin 2023-12-30T16:45:41Zcontrolplane ~ ➜ k get clusterrolebinding | grep storage-adminmichelle-storage-admin ClusterRole/storage-admin 41scontrolplane ~ ➜ k get sc --as michelleNAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGElocal-path (default) rancher.io/local-path Delete WaitForFirstConsumer false 48mcontrolplane ~ ➜ k get pv --as michelleNo resources found -
-
What is the secret token used by the default service account?
controlplane ~ ➜ k get saNAME SECRETS AGEdefault 0 10mdev 0 76sAnswer
controlplane ~ ➜ k describe sa defaultName: defaultNamespace: defaultLabels: <none>Annotations: <none>Image pull secrets: <none>Mountable secrets: <none>Tokens: <none>Events: <none> -
Inspect the Dashboard Application POD and identify the Service Account mounted on it.
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEweb-dashboard-97c9c59f6-f6krx 1/1 Running 0 43sAnswer
controlplane ~ ➜ k describe po web-dashboard-97c9c59f6-f6krx | grep -i serviceService Account: default/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-jcbls (ro) -
The application needs a ServiceAccount with the Right permissions to be created to authenticate to Kubernetes. The default ServiceAccount has limited access. Create a new ServiceAccount named dashboard-sa.
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEweb-dashboard-97c9c59f6-f6krx 1/1 Running 0 43sAnswer
## dashboard-sa.yamlapiVersion: v1kind: ServiceAccountmetadata:name: dashboard-saannotations:kubernetes.io/enforce-mountable-secrets: "true"controlplane ~ ➜ k apply -f dashboard-sa.yamlserviceaccount/dashboard-sa createdcontrolplane ~ ➜ k get saNAME SECRETS AGEdefault 0 15mdev 0 5m58sdashboard-sa 0 3s -
Edit the deployment to change ServiceAccount from default to dashboard-sa.
controlplane ~ ➜ k get deployments.appsNAME READY UP-TO-DATE AVAILABLE AGEweb-dashboard 1/1 1 1 6m22scontrolplane ~ ➜ k get saNAME SECRETS AGEdefault 0 20mdev 0 10mdashboard-sa 0 4m26sAnswer
k edit deployments.apps web-dashboardapiVersion: apps/v1kind: Deploymentmetadata:annotations:deployment.kubernetes.io/revision: "1"creationTimestamp: "2023-12-30T16:53:13Z"generation: 1name: web-dashboardnamespace: defaultresourceVersion: "854"uid: 937b66d9-e256-4944-9a5b-426731eda7cespec:progressDeadlineSeconds: 600replicas: 1revisionHistoryLimit: 10selector:matchLabels:name: web-dashboardstrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:creationTimestamp: nulllabels:name: web-dashboardspec:serviceAccountName: dashboard-sa