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

背景

在理论应用中,会有一些用户,在不同场景中常常碰到 OpenLDAP 对接问题:

  • 是否对接 LDAP?
  • 对接形式都有什么,有界面吗?
  • 是否按本人要求来对接指定账户,而非全副账户都能拜访?
  • 是否批量导入账户?
  • 默认角色如何绑定?

在这里能够简略操作一下,以便来抛砖引玉,次要思路为:在 KubeSphere 中间接运行一个 LDAP Server,用 ApacheDirectoryStudio 来验证,而后应用 KubeSphere 进行 LDAP 对接验证。

前置条件

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

KubeSphere 中部署 LDAP

这里通过利用为用户提供残缺的业务性能,由一个或多个特定性能的组件组成。来部署 OpenLDAP

部署 LDAP 利用

创立无状态服务(演示应用)

这里应用的镜像为: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

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

下面所说的环境变量,能够进行批量援用(KS 很多性能,亮堂简洁又好用)。

内部拜访,这里应用模式为:NodePort。

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

ApacheDirectoryStudio 验证 LDAP

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

下载部署 ApacheDirectoryStudio

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

装置部署后 ApacheDirectoryStudio 如下:

ApacheDirectoryStudio 测试 LDAP

创立 CONNECTIONS:

Connection Name: LDAP4KS-adminNetWork ParameterHostName: 192.168.100.2Port: 30598Connection timeout(s): 30Encryption method: no encryption

Authentication:

Authentication Method: Simple authentication#参考下面时设置的 the OpenLDAP server instance 能够配置 env 在前面应用Authentication Parameter:Bind DB or User: cn=admin,dc=example,dc=orgBind 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 \

Browser Options:

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

这里有一个小细节,如果对接认证账户不是 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 的对接形式是酱紫

上述 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-installerkubectl edit cc -n kubesphere-system ks-installer#重启ks-installerkubectl rollout restart -n kubesphere-system deploy ks-installer#重启ks-apiserverkubectl 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

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

如果 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。

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

#这个用户是我在 studio 中通过界面增加的一个新用户,customuser001,明码为:custompassword001customuser001custompassword001

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

目前来看,默认同步的账户能够登录,然而没有赋予角色,须要再人为调配下或者设置成默认角色进行导入。

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

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

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

导入账号到 KubeSphere

#eg.账户数据如下identify-provider:ldaporigin-uid:customuser002email:custompasswordaa@ks.comcat << EOF | kubectl apply -f -apiVersion: iam.kubesphere.io/v1alpha2kind: Usermetadata:  labels:    iam.kubesphere.io/identify-provider: ldap    iam.kubesphere.io/origin-uid: customuser002  name: customuser002spec:  email: custompasswordaa@ks.comEOFroot@i-9z8gtfm9:/home/ubuntu# kubectl get userNAME            EMAIL                     STATUSadmin           admin@kubesphere.io       Activecustomuser      customuser@ks.com         Activecustomuser001   customusera@example.com   Activecustomuser002   custompasswordaa@ks.com   Activeshl             shl@163.com               Active

为导入账号关联平台角色

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

cat << EOF | kubectl apply -f -apiVersion: iam.kubesphere.io/v1alpha2kind: GlobalRoleBindingmetadata:  labels:    iam.kubesphere.io/user-ref: customuser001  name: customuser001-platform-regularroleRef:  apiGroup: iam.kubesphere.io  kind: GlobalRole  name: platform-regular #查问平台已有的角色subjects:- apiGroup: rbac.authorization.k8s.io  kind: User  name: customuser001EOF

执行后,查问后果:

root@i-9z8gtfm9:/home/ubuntu# kubectl get globalrolebindingNAME                             AGEadmin                            139danonymous                        139dauthenticated                    139dcustomuser001-platform-regular   93spre-registration                 139dshl-platform-regular             139droot@i-9z8gtfm9:/home/ubuntu# kubectl get globalrolebinding customuser001-platform-regular -oyamlapiVersion: iam.kubesphere.io/v1alpha2kind: GlobalRoleBindingmetadata:  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-7029575e5d2droleRef:  apiGroup: iam.kubesphere.io  kind: GlobalRole  name: platform-regularsubjects:- apiGroup: rbac.authorization.k8s.io  kind: User  name: customuser001

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

#这里留神多集群的企业空间差别cat << EOF | kubectl apply -f -apiVersion: tenant.kubesphere.io/v1alpha2kind: WorkspaceTemplatemetadata:  name: appspec:  # placement: {}  placement:    clusters:    - name: <CLUSTER_NAME>  template:    spec:      manager: <USERNAME>EOF

将用户关联企业空间角色

cat << EOF | kubectl apply -f -apiVersion: iam.kubesphere.io/v1alpha2kind: WorkspaceRoleBindingmetadata:  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

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

# 更为高级灵便的应用办法,能够降级为定期、定时、触发式、差异化增量/全量同步账户信息
本文由博客一文多发平台 OpenWrite 公布!