首页 > 其他分享 >KubeSphere 使用 OpenLDAP 进行统一认证完全指南

KubeSphere 使用 OpenLDAP 进行统一认证完全指南

时间:2023-01-05 14:05:11浏览次数:84  
标签:iam name KubeSphere 认证 OpenLDAP customuser001 io LDAP kubesphere

作者:申红磊,青云QingCloud 容器解决方案架构师,开源项目爱好者,KubeSphere Member。

背景

在实际使用中,会有一些用户,在不同场景中经常碰到 OpenLDAP 对接问题:

  • 能否对接 LDAP?
  • 对接方式都有什么,有界面吗?
  • 能否按自己要求来对接指定账户,而非全部账户都能访问?
  • 能否批量导入账户?
  • 默认角色如何绑定?

在这里可以简单操作一下,以便来抛砖引玉,主要思路为:在 KubeSphere 中直接运行一个 LDAP Server,用 ApacheDirectoryStudio 来验证,然后使用 KubeSphere 进行 LDAP 对接验证。

前置条件

您需要部署一个 K8s 集群,并在集群中安装 KubeSphere。有关详细信息,请参阅​​在 Linux 上安装​​和​​在 Kubernetes 上安装​​。

KubeSphere 中部署 LDAP

这里通过应用为用户提供完整的业务功能,由一个或多个特定功能的组件组成。来部署 OpenLDAP

部署 LDAP 应用

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_Kubernetes

创建无状态服务(演示使用)

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_OpenLDAP_02

这里使用的镜像为:​​bitnami/openldap:latest​

docker pull bitnami/openldap:latest
# 参考 the OpenLDAP server instance 可以配置 env 在后面使用
$ docker run --detach --rm --name openldap \
--network my-network \
--env LDAP_ADMIN_USERNAME=admin \
--env LDAP_ADMIN_PASSWORD=adminpassword \
--env LDAP_USERS=customuser \
--env LDAP_PASSWORDS=custompassword \
bitnami/openldap:latest

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_Kubernetes_03

配置 Resources 的 Request 和 Limit ,并使用默认端口:

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_KubeSphere_04

上面所说的环境变量,可以进行批量引用(KubeSphere 很多功能,明亮简洁又好用)。

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_OpenLDAP_05

外部访问,这里使用模式为:NodePort。

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_OpenLDAP_06

服务如下,通过 1389 对应的 NodePort 30598 来访问。

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_KubeSphere_07

ApacheDirectoryStudio 验证 LDAP

通过 ApacheDirectoryStudio 来直接验证,确认 OpenLDAP server instance 无问题后,再进行 KubeSphere 的对接。首先先下载 Mac 版 ApacheDirectoryStudio。

下载部署 ApacheDirectoryStudio

​ApacheDirectoryStudio-2.0.0.v20210717-M17-macosx.cocoa.x86_64.dmg​

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_OpenLDAP_08

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_OpenLDAP_09

安装部署后 ApacheDirectoryStudio 如下:

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_KubeSphere_10

ApacheDirectoryStudio 测试 LDAP

创建 CONNECTIONS:

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_Kubernetes_11

Connection Name: LDAP4KS-admin

NetWork Parameter
HostName: 192.168.100.2
Port: 30598
Connection timeout(s): 30
Encryption method: no encryption

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_Kubernetes_12

Authentication:

Authentication Method: Simple authentication
#参考上面时设置的 the OpenLDAP server instance 可以配置 env 在后面使用
Authentication Parameter:
Bind DB or User: cn=admin,dc=example,dc=org
Bind password: adminpassword

在 the OpenLDAP server instance 可以配置 env 在后面使用
--env LDAP_ADMIN_USERNAME=admin \
--env LDAP_ADMIN_PASSWORD=adminpassword \
--env LDAP_USERS=customuser \
--env LDAP_PASSWORDS=custompassword \

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_K8s_13

Browser Options:

# 这里可以通过 FetchBase DNs 获取 Base DN: dc=example,dc=org

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_KubeSphere_14

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_KubeSphere_15

这里有一个小细节,如果对接认证账户不是 admin, 在 ApacheDirectoryStudio 中添加 Entry 时会显示无权限添加,所以 Studio 对接时可以选择使用 admin , 查询对接时可以使用其它账户。

创建 Ldap Search ,KS 对接时可选择使用

这里举例展示,userSearchFilter 的结果集,可以根据自己业务需求,将符合条件的用户,显示这个 Ldap Search 中,这里只是在 filter 中添加一个条件为:(uid=customuser001)

dc=example,dc=org  -  Filter: (uid=customuser001)
#在 KubeSphere 对接中,这里显示为:
userSearchFilter: (uid=customuser001)

KubeSphere 对接 LDAP

Harbor 的对接界面是酱紫

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_Kubernetes_16

KubeSphere 的对接方式是酱紫

上述 Harbor 对接 LDAP 界面功能,这里同样可以实现。

暂时没有界面,CRD ClusterConfiguration 中的 ks-installer:

kubectl -n kubesphere-system edit cc ks-installer

​内容如下,请参阅官方文档​​。

spec:
alerting:
enabled: true
auditing:
enabled: true
authentication:
jwtSecret: ""
maximumClockSkew: 10s
multipleLogin: true
oauthOptions:
accessTokenInactivityTimeout: 30m
accessTokenMaxAge: 1h
identityProviders:
- mappingMethod: auto
name: LDAP
provider:
host: ldap.middleware.svc:1389 #这里我填写的为 KS 集群内部访问方式
loginAttribute: uid
mailAttribute: mail
managerDN: cn=admin,dc=example,dc=org
managerPassword: adminpassword
userSearchBase: dc=example,dc=org
userSearchFilter: (uid=customuser001) #这里配置userSearchFilter可根业务灵活配置
type: LDAPIdentityProvider

排错或者查看日志情况:

#在 cc 里配置好,等待 ks-installer 自动更新;或者手动重启 ks-apiserver
#编辑ks-installer
kubectl edit cc -n kubesphere-system ks-installer
#重启ks-installer
kubectl rollout restart -n kubesphere-system deploy ks-installer
#重启ks-apiserver
kubectl rollout restart -n kubesphere-system deploy ks-apiserver
#查看ks-apiserver日志
kubectl -n kubesphere-system logs ks-apiserver-xxxxxxxxx

KubeSphere 配置验证

​http://192.168.100.2:30880/kapis/config.kubesphere.io/v1alpha2/configs/oauth​

验证配置内容已经正确更新:

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_OpenLDAP_17

如果 mappingMethod 设置为 lookup,可以运行以下命令并添加标签来进行帐户关联。如果 mappingMethod 是 auto 可以跳过这个部分。

kubectl edit user <KubeSphere username>

labels:
iam.kubesphere.io/identify-provider: <LDAP service name>
iam.kubesphere.io/origin-uid: <LDAP username>

测试使用 OpenLDAP

首先我在 OpenLDAP 中 ldap search 结果显示只有一个用户 customuser001。

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_K8s_18

那么就用 customuser001 这个用户来尝试登录试试:

#这个用户是我在 studio 中通过界面添加的一个新用户,customuser001,密码为:custompassword001
customuser001
custompassword001

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_KubeSphere_19

切换回管理员,查看账户同步内容:

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_Kubernetes_20

目前来看,默认同步的账户可以登录,但是没有赋予角色,需要再人为分配下或者设置成默认角色进行导入。

批量导出导入用户数据到 KubeSphere

这里只提供一下思路,批量脚本可自行编写:

#可以在 KubeSphere 中提前导入第三方(eg.ldap)账户并关联平台角色、企业空间
cat << EOF | kubectl apply -f -
apiVersion: iam.kubesphere.io/v1alpha2
kind: User
metadata:
labels:
iam.kubesphere.io/identify-provider: <IDENTITY_PROVIDER_NAME> #这里指配置名称为:ldap
iam.kubesphere.io/origin-uid: <UID> #第三方账户的 UID
name: <USERNAME> # 关联的 KubeSphere 账户名称
spec:
email: <EMAIL> # 关联的 KubeSphere 账户email
EOF

导入账号到 KubeSphere

#eg.账户数据如下
identify-provider:ldap
origin-uid:customuser002
email:[email protected]

cat << EOF | kubectl apply -f -
apiVersion: iam.kubesphere.io/v1alpha2
kind: User
metadata:
labels:
iam.kubesphere.io/identify-provider: ldap
iam.kubesphere.io/origin-uid: customuser002
name: customuser002
spec:
email: [email protected]
EOF

root@i-9z8gtfm9:/home/ubuntu# kubectl get user
NAME EMAIL STATUS
admin [email protected] Active
customuser [email protected] Active
customuser001 [email protected] Active
customuser002 [email protected] Active
shl [email protected] Active

为导入账号关联平台角色

这里将用户 customuser001 关联成 platform-regular 角色:

cat << EOF | kubectl apply -f -
apiVersion: iam.kubesphere.io/v1alpha2
kind: GlobalRoleBinding
metadata:
labels:
iam.kubesphere.io/user-ref: customuser001
name: customuser001-platform-regular
roleRef:
apiGroup: iam.kubesphere.io
kind: GlobalRole
name: platform-regular #查询平台已有的角色
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: customuser001
EOF

执行后,查询结果:

root@i-9z8gtfm9:/home/ubuntu# kubectl get globalrolebinding
NAME AGE
admin 139d
anonymous 139d
authenticated 139d
customuser001-platform-regular 93s
pre-registration 139d
shl-platform-regular 139d
root@i-9z8gtfm9:/home/ubuntu# kubectl get globalrolebinding customuser001-platform-regular -oyaml
apiVersion: iam.kubesphere.io/v1alpha2
kind: GlobalRoleBinding
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"iam.kubesphere.io/v1alpha2","kind":"GlobalRoleBinding","metadata":{"annotations":{},"labels":{"iam.kubesphere.io/user-ref":"customuser001"},"name":"customuser001-platform-regular"},"roleRef":{"apiGroup":"iam.kubesphere.io","kind":"GlobalRole","name":"platform-regular"},"subjects":[{"apiGroup":"rbac.authorization.k8s.io","kind":"User","name":"customuser001"}]}
creationTimestamp: "2023-01-03T08:34:23Z"
generation: 1
labels:
iam.kubesphere.io/user-ref: customuser001
kubefed.io/managed: "false"
name: customuser001-platform-regular
resourceVersion: "3436734"
uid: 9d7a18a6-b4c9-40f8-8186-7029575e5d2d
roleRef:
apiGroup: iam.kubesphere.io
kind: GlobalRole
name: platform-regular
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: customuser001

KubeSphere 使用 OpenLDAP 进行统一认证完全指南_OpenLDAP_21

已有企业空间 / 创建企业空间

#这里注意多集群的企业空间差异
cat << EOF | kubectl apply -f -
apiVersion: tenant.kubesphere.io/v1alpha2
kind: WorkspaceTemplate
metadata:
name: app
spec:
# placement: {}
placement:
clusters:
- name: <CLUSTER_NAME>
template:
spec:
manager: <USERNAME>
EOF

将用户关联企业空间角色

cat << EOF | kubectl apply -f -
apiVersion: iam.kubesphere.io/v1alpha2
kind: WorkspaceRoleBinding
metadata:
labels:
iam.kubesphere.io/user-ref: <USERNAME>
kubesphere.io/workspace: <WORKSPACE_NAME>
name: <USERNAME>-<WORKSPACE_ROLE_NAME>
roleRef:
apiGroup: iam.kubesphere.io
kind: WorkspaceRole
name: <WORKSPACE_ROLE_NAME>
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: <USERNAME>
EOF

将用户和上述方法进行批量化脚本即可

# 更为高级灵活的使用方法,可以升级为定期、定时、触发式、差异化增量/全量同步账户信息

标签:iam,name,KubeSphere,认证,OpenLDAP,customuser001,io,LDAP,kubesphere
From: https://blog.51cto.com/u_15533008/5991015

相关文章

  • KubeSphere 使用 OpenLDAP 进行统一认证完全指南
    作者:申红磊,青云QingCloud容器解决方案架构师,开源项目爱好者,KubeSphereMember。背景在实际使用中,会有一些用户,在不同场景中经常碰到OpenLDAP对接问题:能否对接LDA......
  • 欧盟儿童玩具CE认证,EN71测试报告详情
    CE认证是一种安全认证标志,代表欧盟认可的,可以进入欧盟市场销售的许可证。没有CE认证无法进入欧盟市场销售,或者缺少CE认证的商品会被下架处理。EN71测试是欧盟市场针对玩具类......
  • 欧盟电动自行车CE认证EN15194标准测试详情
    2009年欧盟推出了新的电动助力自行车标准EN15194,EN15194标准为国际第一个针对电动助力自行车的安全标准,产品通过EN15194检测可以证明产品符合国际一流水平,并且对企业开拓市......
  • NGINX 配置 SSL 双向认证
    NGINX配置SSL双向认证1.1.生成一个CA私钥:ca.keyopensslgenrsa-outca.key40961.2.生成一个CA的数字证书:ca.crt(CommonName随意填写;其它可以填”.”)op......
  • csrf相关知识、auth认证模块、扩展auth_user表
    csrf相关知识、auth认证模块、扩展auth_user表目录csrf相关知识、auth认证模块、扩展auth_user表csrf跨站请求伪造csrf攻击原理图解csrf校验策略csrf相关装饰器auth认证模......
  • KubeSphere平台安装
    记录自尚硅谷的k8s一、Kubernetes上安装KubeSphere安装步骤选择4核8G(master)、8核16G(node1)、8核16G(node2)三台机器CentOS7.9安装Docker安装Kubernetes安装......
  • 本地python环境安装kylin项目依赖时报认证错误信息
    问题描述:项目需要连接kylin数据库查询数据,本地安装kylin项目的依赖环境后报认证错误python版本3.732位pycharm版本2022版成功方法:更换解释器选择无解释器,创建虚......
  • 首款通过! 机器学习服务活体检测算法荣获CFCA权威安全认证
    随着人脸识别技术在金融、医疗等多个领域的加速落地,网络安全、信息泄露等问题愈为突出,用户对应用稳定性和安全性的要求也更为严格。为保障各行业高效稳定的开展业务,提前发现......
  • 首款通过! 机器学习服务活体检测算法荣获CFCA权威安全认证
    随着人脸识别技术在金融、医疗等多个领域的加速落地,网络安全、信息泄露等问题愈为突出,用户对应用稳定性和安全性的要求也更为严格。为保障各行业高效稳定的开展业务,提前发......
  • ceph 集群维护 存储池管理 用户认证
    添加主机 添加磁盘  删除磁盘ceph集群维护:http://docs.ceph.org.cn/rados/#ceph集群配置、部署与运维通过套接字进行单机管理每个节点上,每个osd都会生成 soc......