---
title: Pinnipedを使ってKindで簡単に認証を有効にしてRBACの検証を行うメモ
tags: ["Kubernetes", "Kind", "Pinniped"]
categories: ["Dev", "CaaS", "Kubernetes", "kind"]
date: 2022-04-19T13:27:44Z
updated: 2022-04-19T13:27:44Z
---

[Pinniped](https://pinniped.dev) を使うと簡単に既存のK8sクラスタに認証の仕組みを追加できます。

Production用途では [Supervisor](https://pinniped.dev/docs/tutorials/concierge-and-supervisor-demo/) を使って外部IDPと連携すべきですが、
今回はKindでRBACの検証をするために認証の仕組みが簡単にほしいという用途で、 [Webhook](https://pinniped.dev/docs/tutorials/concierge-only-demo/) を使った設定を行います。
また、Webhookを受け付けるサーバーとして [Local User Authenticator](https://github.com/vmware-tanzu/pinniped/blob/main/deploy/local-user-authenticator/README.md) を使うことでSecretでUserを管理できます。
Kindなどでちょっとした検証をしたいときに便利です。

以下の作業内容はほぼ [ドキュメント](https://pinniped.dev/docs/tutorials/concierge-only-demo/) の通りです。

**目次**
<!-- toc -->

### Kindクラスタの作成

```
kind create cluster
```

### Local User Authenticatorのインストール

```
kubectl apply -f https://get.pinniped.dev/latest/install-local-user-authenticator.yaml
```

### Userの登録

```yaml
cat <<EOF > pinny-the-seal.yaml
apiVersion: v1
kind: Secret
metadata:
  namespace: local-user-authenticator
  name: pinny-the-seal
stringData:
  groups: group1,group2
  passwordHash: $(htpasswd -nbBC 10 x password123 | sed -e "s/^x://")
EOF
kubectl apply -f pinny-the-seal.yaml
```


### Pinniped Conciergeのデプロイ

```
kubectl apply -f https://get.pinniped.dev/latest/install-pinniped-concierge-crds.yaml
kubectl apply -f https://get.pinniped.dev/latest/install-pinniped-concierge-resources.yaml
```

### WebhookAuthenticatorの設定

```
kubectl get secret local-user-authenticator-tls-serving-certificate --namespace local-user-authenticator -o template='{{.data.caCertificate}}' > local-user-authenticator-ca-base64-encoded
```

```yaml
cat <<EOF > local-user-authenticator.yaml
apiVersion: authentication.concierge.pinniped.dev/v1alpha1
kind: WebhookAuthenticator
metadata:
  name: local-user-authenticator
spec:
  endpoint: https://local-user-authenticator.local-user-authenticator.svc/authenticate
  tls:
    certificateAuthorityData: $(cat local-user-authenticator-ca-base64-encoded)
EOF
kubectl apply -f local-user-authenticator.yaml
```

### kubeconfigの取得

```
brew install vmware-tanzu/pinniped/pinniped-cli
```

```
pinniped get kubeconfig \
  --static-token "pinny-the-seal:password123" \
  --concierge-authenticator-type webhook \
  --concierge-authenticator-name local-user-authenticator > pinniped-kubeconfig
```



### RBACの検証

デフォルトでは当然Podの情報を読み取ることはできません。

```
$ kubectl get pod --kubeconfig pinniped-kubeconfig 
Error from server (Forbidden): pods is forbidden: User "pinny-the-seal" cannot list resource "pods" in API group "" in the namespace "default"
```

このUserに対して `view` ClusterRoleをClusteRroleBindingでバインドします。

```
kubectl create clusterrolebinding pinny-can-read --clusterrole view --user pinny-the-seal
```

これでアクセスできるようになりました。

```
$ kubectl get pod --kubeconfig pinniped-kubeconfig -A
NAMESPACE                  NAME                                                 READY   STATUS    RESTARTS   AGE
kube-system                coredns-64897985d-jcvjq                              1/1     Running   0          14m
kube-system                coredns-64897985d-wd4xr                              1/1     Running   0          14m
kube-system                etcd-kind-control-plane                              1/1     Running   0          14m
kube-system                kindnet-m7jm6                                        1/1     Running   0          14m
kube-system                kube-apiserver-kind-control-plane                    1/1     Running   0          14m
kube-system                kube-controller-manager-kind-control-plane           1/1     Running   0          14m
kube-system                kube-proxy-fswl4                                     1/1     Running   0          14m
kube-system                kube-scheduler-kind-control-plane                    1/1     Running   0          14m
local-path-storage         local-path-provisioner-5ddd94ff66-dbg24              1/1     Running   0          14m
local-user-authenticator   local-user-authenticator-7556f9c5-26bn9              1/1     Running   0          13m
pinniped-concierge         pinniped-concierge-8b767c49-kblwx                    1/1     Running   0          13m
pinniped-concierge         pinniped-concierge-8b767c49-rkpcv                    1/1     Running   0          13m
pinniped-concierge         pinniped-concierge-kube-cert-agent-b7488ddb4-49jw9   1/1     Running   0          13m
```

Podの削除などは行えません。

```
$ kubectl delete pod --kubeconfig pinniped-kubeconfig -n local-user-authenticator   local-user-authenticator-7556f9c5-26bn9 
Error from server (Forbidden): pods "local-user-authenticator-7556f9c5-26bn9" is forbidden: User "pinny-the-seal" cannot delete resource "pods" in API group "" in the namespace "local-user-authenticator"
```
