为OKDOpenshift集群配置OpenLDAP认证

47次阅读

共计 5266 个字符,预计需要花费 14 分钟才能阅读完成。

前言

如同 Linux 操作系统安装完成后,管理员需为应用创建不同的用户,那么,K8S/OKD/Openshift集群同样也需如此,而在 OKD/Openshift 集群里,我们可集成 OpenLDAP 目录系统,方法如下所示。

OpenLDAP安装

本文使用 helm 安装 openldap,首先将chars 下载下来以方便查看:

git clone https://github.com/helm/charts

可选 。镜像可先推送到私有仓库(PS:测试发现latest 镜像有问题):

docker pull osixia/openldap:1.2.1
docker tag docker.io/osixia/openldap:1.2.1 okd-lr.zyl.io:5001/osixia/openldap:1.2.1
docker push okd-lr.zyl.io:5001/osixia/openldap:1.2.1

镜像以 root 用户运行(gosudo切换),赋权:

oc new-project auth-openshift
oc adm policy add-scc-to-user anyuid -z default

openldap char 参数做定制:

cd charts/stable/openldap
cp values.yaml values_cs.yaml
vi values_cs.yaml
...

env:
  # LDAP 将创建 dc=zyl,dc=io 域,组织名称为 Zyl Inc.
  LDAP_ORGANISATION: "Zyl Inc."
  LDAP_DOMAIN: "zyl.io"
...

# Ldap 域管理员(cn=admin,dc=zyl,dc=io)及 config 管理员(cn=admin,cn=config)密码
adminPassword: admin
configPassword: config

# 持久化存储,本例使用已创建好的 glusterfs 存储系统,其支持动态提供。persistence:
  enabled: true
  storageClass: "glusterfs-app"
  accessMode: ReadWriteOnce
  size: 8Gi

执行 helm 命令安装:

helm install --name openldap -f values_cs.yaml .

Ldap启动后,创建了域 dc=zyl,dc=iohdb管理员账户cn=admin,dc=zyl,dc=io。如下所示,在此域下创建用户与组信息:

% oc rsh deploy/openldap
% cat > users.ldif <<EOF
dn: ou=People,dc=zyl,dc=io
ou: People
objectClass: top
objectClass: organizationalUnit

dn: ou=Group,dc=zyl,dc=io
ou: Group
objectClass: top
objectClass: organizationalUnit

dn: uid=zyl,ou=People,dc=zyl,dc=io
uid: zyl
cn: zyl
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: changeme
loginShell: /bin/bash
uidNumber: 5000
gidNumber: 5000
homeDirectory: /home/zyl

dn: uid=admin,ou=People,dc=zyl,dc=io
uid: admin
cn: admin
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: changeme
loginShell: /bin/bash
uidNumber: 5001
gidNumber: 5001
homeDirectory: /home/admin

dn: cn=zyl,ou=Group,dc=zyl,dc=io
cn: zyl
objectClass: top
objectClass: posixGroup
gidNumber: 5000
memberUid: zyl

dn: cn=admin,ou=Group,dc=zyl,dc=io
cn: admin
objectClass: top
objectClass: posixGroup
gidNumber: 5001
memberUid: admin

dn: cn=openshift_user,ou=Group,dc=zyl,dc=io
cn: openshift_user
objectClass: top
objectClass: posixGroup
gidNumber: 6000
memberUid: zyl

dn: cn=openshift_admin,ou=Group,dc=zyl,dc=io
cn: openshift_admin
objectClass: top
objectClass: posixGroup
gidNumber: 6001
memberUid: admin
EOF
% ldapadd -x -w $LDAP_ADMIN_PASSWORD -D "cn=admin,dc=zyl,dc=io" -H ldapi:/// -f users.ldif
% ldapsearch -x -D "cn=admin,dc=zyl,dc=io" -w $LDAP_ADMIN_PASSWORD \
             -b dc=zyl,dc=io
# 可使用 config 管理员检查 ldap config 配置
% ldapsearch -x -D "cn=admin,cn=config" -w $LDAP_CONFIG_PASSWORD \
             -b cn=config "olcDatabase=config"

配置 Master 使用 Ldap 认证

OKD初始安装时若未配置 openshift_master_identity_providers,则OKD 默认使用如下认证,此认证方式允许任何用户登录集群。

% vi /etc/origin/master/master-config.yaml 
...
oauthConfig:
...
  identityProviders:
  - challenge: true
    login: true
    mappingMethod: claim
    name: allow_all
    provider:
      apiVersion: v1
      kind: AllowAllPasswordIdentityProvider
...

将所有 Master 配置的如下段删除:

  - challenge: true
    login: true
    mappingMethod: claim
    name: allow_all
    provider:
      apiVersion: v1
      kind: AllowAllPasswordIdentityProvider

替换为如下段:

  - challenge: true
    login: true
    mappingMethod: claim
    name: ldap_auth
    provider:
      apiVersion: v1
      attributes:
        email:
        - mail
        id:
        - dn
        name:
        - cn
        preferredUsername:
        - uid
      bindDN: cn=admin,dc=zyl,dc=io
      bindPassword: admin
      insecure: true
      kind: LDAPPasswordIdentityProvider
      url: ldap://openldap.auth-openshift.svc.cluster.local./ou=People,dc=zyl,dc=io?uid

注意 :若启用TLS,即insecure: false,则需提供OpenLDAP 的证书,如添加 ca: my-ldap-ca.crt,而后将证书拷贝到Master 上:/etc/origin/master/my-ldap-ca.crt

Ansible配置文件中的 OSEv3.yaml 加入以下段,避免升级时被还原回去。

##### Auth
openshift_master_identity_providers:
- name: ldap_auth
  challenge: true
  login: true
  kind: LDAPPasswordIdentityProvider
  bindDN: cn=admin,dc=zyl,dc=io
  bindPassword: admin
  url: ldap://openldap.auth-openshift.svc.cluster.local./ou=People,dc=zyl,dc=io?uid
  attributes:
    id: ['dn']
    email: ['mail']
    name: ['cn']
    preferredUsername: ['uid']
  insecure: true

而后分别重启 Master 节点:

master-restart api
master-restart controllers
oc get pod -n kube-system  
master-logs api api                   # 查看日志
master-logs controllers controllers

同步 LDAP 组信息到 OKD

创建如下文件:

cat > rfc2307_config_user_defined.yaml <<EOF
---
kind: LDAPSyncConfig
apiVersion: v1
bindDN: cn=admin,dc=zyl,dc=io
bindPassword: admin
insecure: true
url: ldap://openldap.auth-openshift.svc.cluster.local
groupUIDNameMapping:
  "cn=openshift_admin,ou=Group,dc=zyl,dc=cn": openshift_admin
  "cn=openshift_user,ou=Group,dc=zyl,dc=cn": openshift_user
rfc2307:
    groupsQuery:
        baseDN: "ou=Group,dc=zyl,dc=io"
        scope: sub
        derefAliases: never
        filter: (objectClass=posixGroup)
    groupUIDAttribute: dn
    groupNameAttributes: [cn]
    groupMembershipAttributes: [memberUid] 
    usersQuery:
        baseDN: "ou=People,dc=zyl,dc=io"
        scope: sub
        derefAliases: never
        filter: (objectClass=posixAccount)
    userUIDAttribute: uid
    userNameAttributes: [cn]
EOF

执行如下命令同步:

% oc adm groups sync --sync-config=rfc2307_config_user_defined.yaml --confirm
group/zyl
group/admin
group/openshift_user
group/openshift_admin

openshift_admin作为管理员组、openshift_user为普通用户组,赋权:

oc adm policy add-cluster-role-to-group cluster-admin openshift_admin
oc adm policy add-cluster-role-to-group basic-user openshift_user

登录用户:

oc login -uadmin -pchangeme

用户登录后,OKD会生成自己的用户与 LDAP 对应:

% oc get groups
NAME              USERS
admin             admin
openshift_admin   admin
openshift_user    zyl
zyl               zyl                

% oc get users
NAME      UID                                    FULL NAME   IDENTITIES
admin     3c4ae0bf-338c-11e9-b2f8-52540042814f   admin       ldap_auth:uid=admin,ou=People,dc=zyl,dc=io

% oc get identities
NAME                                         IDP NAME    IDP USER NAME                      USER NAME   USER UID
ldap_auth:uid=admin,ou=People,dc=zyl,dc=io   ldap_auth   uid=admin,ou=People,dc=zyl,dc=io   admin       3c4ae0bf-338c-11e9-b2f8-52540042814f

参考文档

  • OpenLDAP Helm Chart:https://github.com/helm/chart…;
  • osixia/openldap:https://github.com/osixia/doc…;

正文完
 0