惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Ogenki

Self-hosted LLM stack: a solid foundation for an open-weight platform built to evolve A few months with `Claude Code`: tips and workflows that helped me `Agentic Coding`: concepts and hands-on Platform Engineering use cases `PostgreSQL`: From Metrics to Query Plan Analysis `VictoriaLogs`: What if logs management became simple and performant? `VictoriaMetrics` : Effective alerts, from theory to practice 🛠️ Harness the Power of `VictoriaMetrics` and `Grafana` Operators for Metrics Management `Dagger`: The missing piece of the developer experience? `TLS` with Gateway API: Efficient and Secure Management of Public and Private Certificates Going Further with `Crossplane`: Compositions and Functions Beyond Traditional VPNs: Simplifying Cloud Access with `Tailscale` `Gateway API`: Can I replace my Ingress Controller with `Cilium`? Applying GitOps Principles to Infrastructure: An overview of `tf-controller` `CloudNativePG`: An easy way to run PostgreSQL on Kubernetes 100% `GitOps` using Flux My Kubernetes cluster (GKE) with `Crossplane` Manage tools versions with `asdf` Helm workshop: Templating exercises Helm workshop: Build your first chart Helm workshop: Lifecycle operations Helm workshop: Ecosystem Helm workshop: Third party charts Helm workshop Kubernetes workshop: Troubleshooting Kubernetes workshop: Resources allocation and autoscaling Kubernetes workshop: Complete application stack Kubernetes workshop: Local environment Run an application on Kubernetes Kubernetes workshop
Kubernetes workshop: Manage permissions in Kubernetes
2021-05-08 · via Ogenki

RBAC is the method used by Kubernetes to authorize access to API resources.

ℹ️ When it makes sense you can use the default roles that are available in all Kubernetes installation instead of having to maintain custom ones.

For this lab what we want to achieve is to give permissions the following permissions to an application myapp:

  • read the configmaps in the namespace foo
  • List the pods in all the namespaces

It is a good practice to configure your pod to make use of a serviceaccount. A serviceaccount are used to identify applications and give them permissions if necessary.

Create a service account

1kubectl create serviceaccount myapp
2serviceaccount/myapp created

When a service account is created, a token is automatically generated and stored in a secret.

1kubectl describe sa myapp | grep -i token
2Mountable secrets:   myapp-token-bz2zq
3Tokens:              myapp-token-bz2zq
4
5kubectl get secret myapp-token-bz2zq --template={{.data.token}} | base64 -d
6eyJhb...EYxhjI_ckZ74A

Using a tool to decode the JWT token you should see the following content

 1kubectl get secret myapp-token-bz2zq --template={{.data.token}} | base64 -d | jwt decode -
 2
 3Token header
 4------------
 5{
 6  "alg": "RS256",
 7  "kid": "IdsXYO6E93xozgJg-LY2oETTPEHBJjydTU4vF2wy-wg"
 8}
 9
10Token claims
11------------
12{
13  "iss": "kubernetes/serviceaccount",
14  "kubernetes.io/serviceaccount/namespace": "foo",
15  "kubernetes.io/serviceaccount/secret.name": "myapp-token-bz2zq",
16  "kubernetes.io/serviceaccount/service-account.name": "myapp",
17  "kubernetes.io/serviceaccount/service-account.uid": "eb606bdc-b713-4b7c-8da8-c4f71075995e",
18  "sub": "system:serviceaccount:foo:myapp"
19}

We're going to create a deployment that will be configured to used this serviceaccount. In the yaml you'll notice that we defined the serviceAccountName.

1kubectl apply -f content/resources/kubernetes_workshop/rbac/deployment.yaml
2deployment.apps/myapp created

As we didn't assigned any permissions to this serviceaccount, our application won't be able to call any of the API endpoints

 1POD_NAME=$(kubectl get po -l app=myapp -o jsonpath='{.items[0].metadata.name}')
 2
 3kubectl exec ${POD_NAME} -- kubectl auth can-i -n foo --list
 4Resources                                       Non-Resource URLs                     Resource Names   Verbs
 5selfsubjectaccessreviews.authorization.k8s.io   []                                    []               [create]
 6selfsubjectrulesreviews.authorization.k8s.io    []                                    []               [create]
 7                                                [/.well-known/openid-configuration]   []               [get]
 8                                                [/api/*]                              []               [get]
 9                                                [/api]                                []               [get]
10                                                [/apis/*]                             []               [get]
11                                                [/apis]                               []               [get]
12                                                [/healthz]                            []               [get]
13                                                [/healthz]                            []               [get]
14                                                [/livez]                              []               [get]
15                                                [/livez]                              []               [get]
16                                                [/openapi/*]                          []               [get]
17                                                [/openapi]                            []               [get]
18                                                [/openid/v1/jwks]                     []               [get]
19                                                [/readyz]                             []               [get]
20                                                [/readyz]                             []               [get]
21                                                [/version/]                           []               [get]
22                                                [/version/]                           []               [get]
23                                                [/version]                            []               [get]
24                                                [/version]                            []               [get]

In order to allow it to read configmaps in the namespace foo, we're going to create 2 resources:

  • A role which will describe the permissions and which is bounded to a namespace
  • A rolebinding to assign this role to our application (serviceaccount)

Create the role

1kubectl apply -f content/resources/kubernetes_workshop/rbac/role.yaml
2role.rbac.authorization.k8s.io/read-configmaps created

And assign it to the serviceaccount we've created previously

1kubectl create rolebinding -n foo myapp-configmap --serviceaccount=foo:myapp --role=read-configmaps
2rolebinding.rbac.authorization.k8s.io/myapp-configmap created

Note that in the above command the serviceaccount must be specified with the namespace as a prefix and separated by a semicolon.

You don't have to restart the pod to get the permissions enabled.

1kubectl exec ${POD_NAME} -- kubectl auth can-i get configmaps -n foo
2yes
3
4kubectl exec ${POD_NAME} -- kubectl get cm
5NAME               DATA   AGE
6kube-root-ca.crt   1      3d20h
7helloworld         2      2d2h

This is possible thanks to the token mounted within the container

 1kubectl exec -ti ${POD_NAME} -- bash -c 'curl -skH "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" https://kubernetes.default/api/v1/namespaces/foo/configmaps'
 2{
 3  "kind": "ConfigMapList",
 4  "apiVersion": "v1",
 5  "metadata": {
 6    "resourceVersion": "76334"
 7  },
 8  "items": [
 9    {
10      "metadata": {
11        "name": "kube-root-ca.crt",
12        "namespace": "foo",
13        "uid": "c352e4cd-3b88-4400-80a0-cbba318794e4",
14...

Finally we want to list the pods in all the namespaces of our cluster. we need:

  • A clusterrole which will describe the permissions that are cluster wide.
  • A clusterrolebinding to assign this clusterrole to our application (serviceaccount)
1$ kubectl apply -f content/resources/kubernetes_workshop/rbac/clusterrole.yaml
2clusterrole.rbac.authorization.k8s.io/list-pods created
1kubectl create clusterrolebinding -n foo myapp-pods --serviceaccount=foo:myapp --clusterrole=list-pods
2clusterrolebinding.rbac.authorization.k8s.io/myapp-pods created

Now lets have a look to the permissions our applications has in the namespace foo

 1kubectl exec ${POD_NAME} -- kubectl auth can-i -n foo --list
 2Resources                                       Non-Resource URLs                     Resource Names   Verbs
 3selfsubjectaccessreviews.authorization.k8s.io   []                                    []               [create]
 4selfsubjectrulesreviews.authorization.k8s.io    []                                    []               [create]
 5pods                                            []                                    []               [get list]
 6configmaps                                      []                                    []               [get watch list]
 7                                                [/.well-known/openid-configuration]   []               [get]
 8                                                [/api/*]                              []               [get]
 9                                                [/api]                                []               [get]
10                                                [/apis/*]                             []               [get]
11...