iT邦幫忙

2025 iThome 鐵人賽

DAY 7
0
Cloud Native

駕馭商用容器叢集,汪洋漂流術系列 第 7

【Day 7】 叢集的身份驗證 / Authentication

  • 分享至 

  • xImage
  •  

前言

叢集這麼貴,不太可能作為個人使用,團隊共用才是最常見合理。 多人共用叢集,必定會有權限分配的議題,允許誰可以做什麼事情,就是本篇要探討的內容。

認證(Authentication)是驗證「你是誰」的過程,驗證通過後才會再進入 RBAC 授權(Authorization)。
這篇先寫 認證(Authentication)的部分

管理與設定 OAuth 方式

  1. 先看看 OCP 的 OAuth 設定

    oc get oauth cluster -o yaml
    
  2. 這會顯示目前叢集的 OAuth 設定,包括:

    • 登入畫面資訊(branding)
    • Identity Providers(如 GitHub、LDAP、htpasswd 等)
    • Token 有效期限(Access token / Refresh token)
    • 登入提示與提示網址
    apiVersion: config.openshift.io/v1
    kind: OAuth
    metadata:
      name: cluster
    spec:
      identityProviders:
      - name: ldap_provider
        mappingMethod: claim
        type: LDAP
        ldap:
          url: "ldaps://ldap.example.com:636/ou=users,dc=example,dc=com"
          bindDN: "cn=admin,dc=example,dc=com"
          bindPassword:
            name: ldap-secret
          attributes:
            id: ["dn"]
            email: ["mail"]
            name: ["cn"]
    
    
  3. 修改設定方式

    oc edit oauth cluster
    

OAuth 選擇

以下列出常見的 OAuth 選項及適用的場景或理由。

加入 LDAP + Secret (密碼) 方案

  • OAuth 加入 LDAP。 如果企業已經有在使用目錄系統管控員工帳號,那麼透過 LDAP 處理身份驗證也是很理所當然的,把身份驗證的工作交給本來就應該拿來處理這項工作的主機,避免用一堆主機做一樣的工作,還有可能使用者發生管理一堆密碼的負面影響。
    identityProviders:
    - name: ldap_provider
      mappingMethod: claim
      type: LDAP
      ldap:
        url: "ldaps://ldap.example.com:636/ou=users,dc=example,dc=com"
        bindDN: "cn=admin,dc=example,dc=com"
        bindPassword:
          name: ldap-secret  # 事先建立的 secret,內容為 bind 密碼
        insecure: false
        attributes:
          id: ["dn"]
          preferredUsername: ["uid"]
          name: ["cn"]
          email: ["mail"]
    
  • 加入密碼要事先在叢集中建立 Secret
    oc create secret generic ldap-secret \
      --from-literal=bindPassword='你的密碼' -n openshift-config
    

GitHub OAuth 身份提供者

  • 這個選項特別適合開給「開發者」,畢竟開發者(應該)都有 GitHub 帳號。
    identityProviders:
    - name: github
      mappingMethod: claim
      type: GitHub
      github:
        clientID: your-client-id
        clientSecret:
          name: github-client-secret  # Secret 中包含 clientSecret
        hostname: ""  # 空代表 github.com
        organizations:
        - your-github-org
    
    
  • 須事先建立 secret,包含 GitHub OAuth App 的 secret
    oc create secret generic github-client-secret \
      --from-literal=clientSecret='你的 GitHub secret' -n openshift-config
    

htpassword

  • 就是用密碼登錄的做法,比較建議是用在 POC環境內部帳號管理。 如果這個系統需要快速驗證概念,或者你可以確保他不會被惡意的外部使用者亂嘗試密碼登入的話,那 htpassword 就很適用,才不會說只是要測試功能,還要先綁定一堆有的沒的驗證機制。
    identityProviders:
    - name: localusers
      mappingMethod: claim
      type: HTPasswd
      htpasswd:
        fileData:
          name: htpasswd-secret
    
    
  • 密碼可以由 htpassword 工具產生
    htpasswd -c -B -b users.htpasswd user1 password123
    oc create secret generic htpasswd-secret \
      --from-file=htpasswd=users.htpasswd -n openshift-config
    

確認修改是否生效

  • 這個修改是馬上生效的,所以在修改完 oc edit oauth cluster 後,OpenShift 會自動重新部署 oauth-openshift pods,不用手動重啟。
  • 可以用確認 oauth-openshift Pods 的狀態,檢查啟用時間。
    oc get pods -n openshift-authentication
    

結論

  • 此時新增的使用者可以登入,但尚無權限。
  • 因為目前我所屬團隊沒有在使用 GitHub 組織,所以先完成上述的 LDAPHTPassword 設定
  • oc get oauth cluster -o yaml 拿回來的內容如下:
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
  annotations:
    include.release.openshift.io/ibm-cloud-managed: "true"
    include.release.openshift.io/self-managed-high-availability: "true"
    include.release.openshift.io/single-node-developer: "true"
    kubectl.kubernetes.io/last-applied-configuration: |
      {}
    release.openshift.io/create-only: "true"
  creationTimestamp: 
  generation: 4
  name: cluster
  resourceVersion: "773055770"
  uid: xxxxxxxx
spec:
  identityProviders:
  - htpasswd:
      fileData:
        name: htpass-secret
    mappingMethod: claim
    name: ocp_htpasswd_provider
    type: HTPasswd
  - ldap:
      attributes:
        email:
        - mail
        id:
        - sAMAccountName
        name:
        - cn
        preferredUsername:
        - sAMAccountName
      bindDN: CN=admin xxxxx,CN=Users,DC=xxxxxxxxx,DC=tw
      bindPassword:
        name: ldap-secret
      insecure: true
      url: ldap://xxxxxxxxxxx
    mappingMethod: claim
    name: xxxxx-ldap
    type: LDAP
  tokenConfig:
    accessTokenMaxAgeSeconds: 172800

比較總覽:openshift-authentication vs K8s 原生身份驗證

功能面向 Kubernetes 原生身份驗證 OpenShift 的 openshift-authentication
身份驗證元件 無獨立模組,交由 kube-apiserver 處理 專屬模組:openshift-authentication(OAuth server + console login)
支援 OAuth (需 OIDC 參數設定) (開箱即用,完整流程)
SSO 整合能力 手動設定 OIDC、Webhook 內建支援 GitHub / LDAP / OIDC / htpasswd 等
登入頁面 無 web login(需額外套件) 提供網頁登入介面
使用者註冊 / 角色管理 不提供 搭配 oc + RoleBinding 提供完整權限控管
身份提供者(IdP)設定方式 修改 kube-apiserver 參數 透過 oc edit oauth cluster 管理 CR
群組支援 僅支援 OIDC 群組 claim 支援 IdP 群組(LDAP, GitHub orgs)+ Group Mapping
組織式身份管理 無中央元件 提供 centralized OAuth server,集中管理

參考資料

  1. K8s 官方文件 / Using RBAC Authorization
  2. OCP 官方文件 / Understanding authentication

上一篇
【Day 6】 簡介 Operator
下一篇
【Day 8】 Authroization / RBAC
系列文
駕馭商用容器叢集,汪洋漂流術15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言