当前位置:网站首页>OpenShift 4 - 提升客户端访问 API Server 安全
OpenShift 4 - 提升客户端访问 API Server 安全
2022-04-21 10:38:00 【dawnsky.liu】
《OpenShift / RHEL / DevSecOps 汇总目录》
文章目录
kubeconfig 文件的作用
OpenShift 或 Kubernetes 的客户端每次向 OpenShift或 Kubernetes 发出命令,Kubernetes 都需要对其身份进行识别。如果 Kubernetes 识别出客户端没有认证登录,那么将无法执行指令。

为了简化客户端操作,我们并不需要在每次发出的命令中都提供身份认证信息,而是使用 kubeconfig 文件保存客户端的会话。通常当客户端首次通过命令行登录 OpenShift 后会自动在 $HOME/.kube/ 目录下创建 config 文件,它除了保存当前用户身份认证信息外还会保存曾经访问过的集群 API Server 地址、当前默认操作的 namespace 等信息。这样客户端就可以决定使用哪个身份、访问哪个集群、在哪个项目中执行命令来操作。
OpenShift 客户端除了默认使用 $HOME/.kube/config 文件,也可以用 KUBECONIFG 变量指定默认的文件位置,或者每次在命令中使用 --kubeconfig 参数指定该文件位置。
kubeconfig 文件构成
在 kubeconfig 文件中包括三部分:
- clusters:记录访问过的集群
- contexts:记录访问过的每个集群当前使用的 namespace
- users:记录访问过的每个集群的用户认证信息。
其中 users 部分可以使用以下几种方式来记录用户的认证信息:证书/秘钥、用户名/密码、Token。
users:
- name: admin1
user:
client-certificate: your-cert-file
client-key: your-key-file
- name: admin2
user:
client-certificate-data: cert-string-base64
client-key-data: key-string-base64
- name: admin3
user:
password: mypassword
username: admin
- name: admin4
user:
token: sha256~tqccPtgknX99ZwK-Sc5umeXz8-b_Hqgxrb4vQ3VWPgs
用户认证 Token
通常,OpenShift 客户端会将认证过用户的会话 Token 保存在 kubeconfig 文件中。默认情况 OpenShift 的 Token 仅在 24 小时内有效,现有的会话会在此时间过后到期。由于 Token 具备有效时限,因此要比长时间有效的“用户名/密码”或证书更安全。
- 退出登录,删除已有的 kubeconfig 文件。
$ oc logou
$ rm $HOME/.kube/config
- 在未登录的时候执行 oc 命令,客户端会提示找不到 kubeconfig 文件。
$ oc whoami
error: Missing or incomplete configuration info. Please point to an existing, complete config file:
1. Via the command-line flag --kubeconfig
2. Via the KUBECONFIG environment variable
3. In your home directory as ~/.kube/config
To view or setup config directly use the 'config' command.
- 登录 OpenShift 的 OCP-API-SERVER。
$ oc login OCP-API-SERVER -u USERNAME -p PASSWORD
- 确认生成 $HOME/.kube/config 文件。
$ cat $HOME/.kube/config
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://api.crc.testing:6443
name: api-crc-testing:6443
contexts:
- context:
cluster: api-crc-testing:6443
namespace: default
user: admin/api-crc-testing:6443
name: default/api-crc-testing:6443/admin
current-context: default/api-crc-testing:6443/admin
kind: Config
preferences: {
}
users:
- name: admin/api-crc-testing:6443
user:
token: sha256~tqccPtgknX99ZwK-Sc5umeXz8-b_Hqgxrb4vQ3VWPgs
了解 OpenShift 认证和 Token 关系
- 执行命令查看已经登录的用户 oauthaccesstokens。以下有3个客户端登录了 OpenShift,其中2个是通过 console 认证的身份,1个是通过 oc 命令认证的身份。
$ oc get oauthaccesstokens
NAME USER NAME CLIENT NAME CREATED EXPIRES REDIRECT URI SCOPES
sha256~6Bnlp-r9GRe-oTH2_HOf0PHb8dSDukxK94psf0ZO7Vo admin console 2022-04-19T13:48:21Z 2022-04-20 13:48:21 +0000 UTC https://console-openshift-console.crc-dzk9v-master-0.crc.t2nlpquzh82q.instruqt.io/auth/callback user:full
sha256~lMj713Q6LUVBrFtjMUwpLFUn7Qi_pSijm9LFS7rzmV4 admin openshift-challenging-client 2022-04-19T13:48:38Z 2022-04-20 13:48:38 +0000 UTC https://oauth-openshift.crc-dzk9v-master-0.crc.t2nlpquzh82q.instruqt.io/oauth/token/implicit user:full
sha256~q_0BM9by-I7YRmTeFItdD5laF8MbYdNV5icZlFYZkd4 admin console 2022-04-19T13:47:47Z 2022-04-20 13:47:47 +0000 UTC https://console-openshift-console.crc-dzk9v-master-0.crc.t2nlpquzh82q.instruqt.io/auth/callback user:full
- 查看其中一个 oauthaccesstoken,可以看到登录用户是 admin,用户是在 “2 minutes ago” 登录成功,Token 有效时间是 “24 hours”。
$ oc describe oauthaccesstokens sha256~6Bnlp-r9GRe-oTH2_HOf0PHb8dSDukxK94psf0ZO7Vo
Name: sha256~6Bnlp-r9GRe-oTH2_HOf0PHb8dSDukxK94psf0ZO7Vo
Created: 2 minutes ago
Labels: <none>
Annotations: <none>
Scopes: [user:full]
Expires In: 24 hours
User Name: admin
User UID: f2736e81-192b-45d5-92a9-910464451fba
Client Name: console
- 如果删除一个 oauthaccesstoken,对应已被认证的用户需要重新登录才能再次访问 OpenShift。
$ oc delete oauthaccesstokens sha256~lMj713Q6LUVBrFtjMUwpLFUn7Qi_pSijm9LFS7rzmV4
$ oc whoami
error: You must be logged in to the server (Unauthorized)
更改 Token 有效时间,增强客户端访问安全
Token 是 OpenShift 服务器端生成用来为客户端认证用的。Token 默认 24 小时有效,可以修改 OAuth 的 accessTokenMaxAgeSeconds 参数来修改默认 Token 的有效时间。
执行命令,将 Token 的有效时间改为 172800 秒(2 天)。
$ oc edit OAuth cluster
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
annotations:
...
...
spec:
tokenConfig:
accessTokenMaxAgeSeconds: 172800
另外,当 OpenShift 客户端在一段时间没有访问 API Server 后客户端会进入 InactivityTimeout,其保存在本地 kubeconfig 中的 Token 也会失效。这时客户端也需要重新登录才可以继续访问 OpenShift。
执行以下命令,修改名为 openshift-challenging-client 的 oauthclient 中的 accessTokenInactivityTimeoutSeconds 参数为 300 秒。
$ oc edit oauthclient openshift-challenging-client
apiVersion: oauth.openshift.io/v1
grantMethod: auto
kind: OAuthClient
metadata:
...
accessTokenInactivityTimeoutSeconds: 300
完成上一步修改后,然后至少 5 分钟后再使用客户端访问 OpenShift。确认此时认证已经失效,需要再次登录才可以访问 OpenShift。
$ oc whoami
error: You must be logged in to the server (Unauthorized)
参考
https://access.redhat.com/documentation/zh-cn/openshift_container_platform/4.10/html-single/authentication_and_authorization/index
版权声明
本文为[dawnsky.liu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43902588/article/details/124283020
边栏推荐
- 背包问题小结(0-1,完全,多重背包问题)
- 使用对数器验证简单排序
- Pytoch learning notes (3) torch Autograd, logistic regression model, training
- 趣丸集团招股书“失效”,旗下TT语音已下架,如何实现稳定增长?
- Design a high-quality API interface
- 2022信息与未来预备刷题2《New Online Judge 1113: 位数问题》
- Uniapp wechat applet clicks the button to call wechat payment
- Opencv - contour detection
- Oracle密码过期问题,设置永不过期
- Mysql 002 DDL
猜你喜欢

Mysql 002 DDL

00000000000000000000000

Opencv -- template matching

数字经济&绿色创新匹配:全国3169公司数字金融企业绿色专利匹配数据 2011-2019年

塔米狗知识|上市公司收购的基本原则

Local IP addresses are accessed using domain names

24 pictures to conquer border image

Can Jingdong Logistics, ririshun supply chain and Shunfeng find the optimal solution of logistics under the "epidemic"?

C#入门-利用正则表达式校验身份证号

Vulnhub PRIME: 1
随机推荐
基于润和大禹开发板的导购系统项目方案
Impact of AOT and single file release on program performance
openCV——模板匹配
C语言 字符串
"Air washing" meets the iteration again, and the imitator has a new goal
利用随机函数实现随机范围的改变
Better than SQL, another domestic database language was born
openCV——轮廓检测
摩尔线程与Ampere Computing达成合作
C#入门-利用正则表达式校验身份证号
力扣(LeetCode)110. 平衡二叉树(2022.04.20)
24张图攻克border-image
塔米狗知识|上市公司收购的基本原则
MySQL8.0学习记录07 - 数据类型之JSON
Pytoch learning notes (2) examples of univariate linear regression and calculation diagram
Wxml template syntax - data binding
Mysql 002 DDL
润和大禹学习方案
SAP ABAP FOR ALL ENTRIES 的用法
Why programming is so difficult: starting from the deduction of stored value card