Day29,使用Dex、OIDC为你的Kubernetes再上一道锁 (2/2)

正文

如果还没有看Day28的话,建议可以先回去看,不然接下来可能搞不清楚状况。

延续昨天的内容,我们需要去验证我们设定的OIDC/Dex server能够正常运作。

我们先去下载kubelogin的binary,将kubelogin放在目录底下,我们要先使用kubelogin验证安装的程序,使用下列指令执行

./kubelogin setup --insecure-skip-tls-verify \
--oidc-issuer-url=https://192.168.1.241 \
--oidc-client-id=kubernetes \
--oidc-client-secret=qwertasdfg

注意这边的oidc-client-secret为我们昨天设定的dex config那边的staticClients自订的secret值

kubelogin会帮我们打开浏览器8000Port,与dex server沟通做跳转,最终来到gitlab的登入页面(因为我的IdP仅有Gitlab,否则应该可以选择多种登入方式)


验证完成後回到terminal,我们会看到下列引导,我们就跟着做第3步骤,为dex server绑定cluster admin权限,之後都要透过他管理,而第4步骤则是我们昨天已经先设定过了Kube-Apiserver的部分,所以可以跳过。

## 2. Verify authentication

You got a token with the following claims:

{
  "iss": "https://192.168.1.241",
  "sub": "CgcxOTQyNDUyEgZnaXRsYWI",
  "aud": "kubernetes",
  "exp": 1632225868,
  "iat": 1632139468,
  "nonce": "iLqq7kS2VqVlYDpwWnqneJ8-j_hqo5tILpfYL4W_SAc",
  "at_hash": "UzM9v1rillKcH-JmOQ9W-w",
  "c_hash": "gcrhI9p7zFQsHyuWKMHcoQ"
}

## 3. Bind a cluster role

Run the following command:

	kubectl create clusterrolebinding oidc-cluster-admin --clusterrole=cluster-admin --user='https://192.168.1.241#CgcxOTQyNDUyEgZnaXRsYWI'

## 4. Set up the Kubernetes API server

Add the following options to the kube-apiserver:

	--oidc-issuer-url=https://192.168.1.241
	--oidc-client-id=kubernetes

## 5. Set up the kubeconfig

Run the following command:

	kubectl config set-credentials oidc \
	  --exec-api-version=client.authentication.k8s.io/v1beta1 \
	  --exec-command=kubectl \
	  --exec-arg=oidc-login \
	  --exec-arg=get-token \
	  --exec-arg=--oidc-issuer-url=https://192.168.1.241 \
	  --exec-arg=--oidc-client-id=kubernetes \
	  --exec-arg=--oidc-client-secret=qwertasdfg \
	  --exec-arg=--insecure-skip-tls-verify

## 6. Verify cluster access

Make sure you can access the Kubernetes cluster.

	kubectl --user=oidc get nodes

You can switch the default context to oidc.

	kubectl config set-context --current --user=oidc

You can share the kubeconfig to your team members for on-boarding.

在执行第五步骤前,我们需要替我们的kubectl安装kubelogin的plugin,执行

kubectl krew install oidc-login

而这边显示的第5步骤并不完全正确,因为我们在api-server中有明确指定oidc-username-claim
所以我们实际的指令执行是多了一个--oidc-extra-scope=email

 kubectl config set-credentials oidc \
  --exec-api-version=client.authentication.k8s.io/v1beta1 \
  --exec-command=kubectl \
  --exec-arg=oidc-login \
  --exec-arg=get-token \
  --exec-arg=--oidc-issuer-url=https://192.168.1.241 \
  --exec-arg=--oidc-client-id=kubernetes \
  --exec-arg=--oidc-client-secret=qwertasdfg \
  --exec-arg=--oidc-extra-scope=email \
  --exec-arg=--insecure-skip-tls-verify

执行第6步骤的验证

kubectl --user=oidc get nodes

会发现没有get nodes的权限,原因是并没有针对oidc取得的user设定kubernetes的RBAC,那我们这边快速apply一个test-rolebinding.yml

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: gurubear-rolebinding
subjects:
- kind: User
  name: [email protected] ##oidc-username-claim=email
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

现在能够正常使用啦,而这个kubeconfig就能够直接给其他具备gitlab group权限的人啦,只是还是要去apply对应的权限喔

这边补充一下kubelogin官方的flowchart,有兴趣的话可以看一下怎麽运作的,其实就是OIDC那一套标准。

图片来自官方github

那我们来看一下~/.kube/config的内容,可以看到--insecure-skip-tls-verify,是为了信任不安全的连线所用的。

但其实我们有CA可以让它变成被认定的安全连线,这边就将CA档转为base64输出

调整config

一样能够正常取得验证

最後来demo一下,在dex server那边其实有针对gitlab connector设定白名单groups,那我就一样用不属於gurubear-ithome-13th group的帐号进行验证,最後确实在dex这边被拒绝啦~

透过这样的方式应该就能更安全复杂的管理Kubernetes了吧~

闲聊

这两篇写得实在是有点太长了,精疲力尽了,应该也是这次铁人赛的最後一part了。明天估计就发些总结感言之类的凑凑篇幅XD


<<:  初探 Domain driven design

>>:  Day.21 Fibonacci

Day11 - Bmo 眼睛嘴巴动起来

设定 BMO 初始化、更新要更新什麽、画出 BMO 的方法 class Bmo { constru...

SEO营销

SEO代表搜索引擎优化(SEO),这是通过自然搜索引擎性能提高网站流量的数量和一致性的过程。它旨在改...

K8S架设ELK的E

【YC的迷路青春】 写给已经架server上Azure然後发现好像要蒐集LOG的人 新增两个yaml...

DAY 3 - 飞天鲸鲨

大家好~ 我又来乱涂乱画了~~~ 今天来尝试一下有点科幻风格的~~ 目标是画一只 能在天上飞的飞天鲸...