1
0

More than 1 year has passed since last update.

OpenShift の Identity Provider に htpasswdファイルを使用してみる。

Last updated at Posted at 2021-03-24

OpenShift を実運用で使用する場合、cluster-admin権限(最強の権限)を持ったkubeadmin以外にも、一般ユーザーも登録する必要があるので、LDAP 等のクラスター外部の Identity Provider が必要になります。

LDAP等を立てるのも大変なので、ここではテスト環境用にhtpasswdファイルに登録した useridpasswordOpenShiftのログインユーザーに使用してみます。

1. htpasswd ファイルを作る

user名とPasswordが入ったhtpasswdファイルを作成します。
jonh (パスワードは passw0rd) を登録した htpasswd 形式の $HOME/htpaswd というファイルを作成します。

[root@bastion ~]# htpasswd -c -Bb $HOME/htpasswd john passw0rd
Adding password for user john
[root@bastion ~]#
-b : Use the password from the command line rather than prompting for it.
-B : Force bcrypt aencryption of the password (very secure).
-c : 新規にファイルを作成

他にもユーザーを追加しておきます。
ここでは、paulringogeorge を追加します。

[root@bastion ~]#  htpasswd -Bb $HOME/htpasswd  paul passw0rd
Adding password for user paul
[root@bastion ~]#  htpasswd -Bb $HOME/htpasswd  ringo passw0rd
Adding password for user ringo
[root@bastion ~]#  htpasswd -Bb $HOME/htpasswd  george passw0rd
Adding password for user george
[root@bastion ~]#

2. 作成した htpasswd のファイルを元にした Secret リソースを作成します。

openshift-config namespace に、htpasswd という名前の secret リソースを作成します。

[root@bastion ~]# oc create secret generic htpasswd --from-file=$HOME/htpasswd -n openshift-config
secret/htpasswd created
[root@bastion ~]#

3. OAuth リソースを編集して前述のSecretリソースを参照させます。

デフォルトで clusterという名前の OAuth リソースがあります。

[root@bastion ~]# oc get oauth
NAME      AGE
cluster   21d
[root@bastion ~]# oc get oauth cluster -o yaml
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
  annotations:
    release.openshift.io/create-only: "true"
  creationTimestamp: "2021-03-03T08:57:12Z"
  generation: 1
  managedFields:
  - apiVersion: config.openshift.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:release.openshift.io/create-only: {}
      f:spec: {}
    manager: cluster-version-operator
    operation: Update
    time: "2021-03-03T08:57:12Z"
  name: cluster
  resourceVersion: "1728"
  selfLink: /apis/config.openshift.io/v1/oauths/cluster
  uid: 1001967e-401e-41db-8841-4fca3ac4ad7b
spec: {}
[root@bastion ~]#

これを、先ほど作成した Secretリソースのhtpasswdを参照するように変更します。

[root@bastion ~]# cat oauth-cluster.yaml
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
  name: cluster
spec:
  identityProviders:
  - name: Local Password
    mappingMethod: claim
    type: HTPasswd
    htpasswd:
      fileData:
        name: htpasswd   # これがさっき htpasswd ファイルから作成した Secret
[root@bastion ~]# oc apply -f oauth-cluster.yaml
Warning: oc apply should be used on resource created by either oc create --save-config or oc apply
oauth.config.openshift.io/cluster configured
[root@bastion ~]#

新しい設定で openshift-authentication namespaceoauth-openshift-xxxx という Podが再起動するので観察します。

[root@bastion ~]# oc get pods -n openshift-authentication
NAME                               READY   STATUS        RESTARTS   AGE
oauth-openshift-6456c879c9-5dlsz   1/1     Running       0          19d
oauth-openshift-6456c879c9-b2gcs   1/1     Running       0          19d
oauth-openshift-6b44f6844f-hkjpb   1/1     Terminating   0          14s
oauth-openshift-7dcb9cf497-h9vbz   0/1     Running       0          10s
[root@bastion ~]# oc get pods -n openshift-authentication
NAME                               READY   STATUS        RESTARTS   AGE
oauth-openshift-6456c879c9-5dlsz   1/1     Terminating   0          19d
oauth-openshift-6456c879c9-b2gcs   1/1     Terminating   0          19d
oauth-openshift-6b44f6844f-hkjpb   0/1     Terminating   0          39s
oauth-openshift-7dcb9cf497-ckz6b   1/1     Running       0          25s
oauth-openshift-7dcb9cf497-h9vbz   1/1     Running       0          35s
[root@bastion ~]# oc get pods -n openshift-authentication
NAME                               READY   STATUS    RESTARTS   AGE
oauth-openshift-7dcb9cf497-ckz6b   1/1     Running   0          41s
oauth-openshift-7dcb9cf497-h9vbz   1/1     Running   0          51s
[root@bastion ~]#

4. ログインの確認

Pod の再起動が完了したら、htpasswd のファイルに登録した userid / password でログインできるか試して見ます。

[root@bastion ~]# oc login -u john -p passw0rd $(oc whoami --show-server)
Login successful.

You don't have any projects. You can try to create a new project, by running

    oc new-project <projectname>

[root@bastion ~]#

syst

[root@bastion ~]# oc login -u system:admin
Logged into "https://api.ocp46.example.localdomain:6443" as "system:admin" using existing credentials.

You have access to 63 projects, the list has been suppressed. You can list all projects with ' projects'

Using project "default".
[root@bastion ~]# oc get users
NAME     UID                                    FULL NAME   IDENTITIES
john     3d81b831-fcb7-464f-a5b0-bb45e5014e29               Local Password:john
[root@bastion ~]#
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0