1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Sealed SecretsでGCPのサービスアカウントキーを扱う

Last updated at Posted at 2021-10-30

背景

  • Sealed Secretsを利用している環境でGCPのサービスアカウントキーSecretリソースとして扱う方法について記載する
  • KubernetesでのGCP認証はWorkload Identityを使用することが推奨されている
    • Workload Identityを使用すればSecretリソースを払い出す必要がない
    • しかし、オンプレのKubernetes環境など、Workload Identityが使用できない場合もある

Sealed Secretsとは

  • Bitnamiが開発しているOSS
  • Sealed SecretsをKubernetesクラスタにデプロイすることで、クラスタ内に公開鍵と秘密鍵が作成される
    • 開発者は公開鍵で機密情報を暗号化した文字列をSealed Secretリソースとしてデプロイする(クラスタで保持している秘密鍵でした複合できないためGithubなどのリポジトリで管理できる)
    • Sealed SecretリソースがデプロイされるとSealed Secrets Controllerが秘密鍵で複合し、クラスタ内にSecretリソースが払い出される
  • 合わせてkubesealというコマンドラインツールも提供されており、SecretリソースからSealed Secretリソースを生成する際に使用する

環境

  • macOS Big Sur 11.4
  • Kubernetes 1.21.3 (Docker Desktop)
  • Sealed Secrets [CHART:sealed-secrets-1.16.1, APP:v0.16.0]
  • kubeseal v0.16.0

事前準備

Sealed Secretsのinstall(Helmを使用)

$ helm repo add sealed-secrets https://bitnami-labs.github.io/sealed-secrets
$ helm install --namespace kube-system my-release sealed-secrets/sealed-secrets 
  • installすると以下のようにSealed SecretのControllerがデプロイされる
$ kubectl get pod
NAME                                        READY   STATUS    RESTARTS   AGE
my-release-sealed-secrets-dd998d6cd-bvdld   1/1     Running   0          2m13s

kubesealのinstall

  • brewでkubesealをインストールする
$ brew install kubeseal

サービスアカウントと鍵の作成

サービスアカウントの鍵をSealedSecretsとして扱う

  • ダウンロードしたjsonを基にSecretリソースを作成
    • PATH-TO-KEY-FILE.jsonをダウンロードされたパスに変更する
$ kubectl create secret generic sample-key --from-file=key.json=PATH-TO-KEY-FILE.json
secret/sample-key created
  • kubesealでSealedSecretリソースを払い出すためにはmanifest形式でSecretの情報が必要になる
  • そのため、$ kubectl edit secret sample-keyで先程作成したsecretの情報を表示し、それを以下のようにyamlファイルとして保存する
secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: sample-key
  namespace: kube-system
type: Opaque
data:
  key.json: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  • Secretリソースは不要なので削除する
$ kubectl delete secret sample-key
secret "sample-key" deleted
  • このSecretリソースの内容を基に、kubesealからSealedSecretリソースを作成する
    • controller-name はHelmで作成されたDeploymentの名前に対応させる
    • 基になるSecretファイルは<>で指定し、出力するファイル名をその後に指定する
$ kubeseal --format=yaml --controller-name my-release-sealed-secrets <secrets.yaml> sealedsecret.yaml
  • 以下のようなファイルが作成される(クラスタ内に保存されている鍵でしかdecryptできない)
sealedsecret.yaml
apiVersion: bitnami.com/v1alpha1
kind: SealedSecret
metadata:
  creationTimestamp: null
  name: sample-key
  namespace: kube-system
spec:
  encryptedData:
    key.json: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  template:
    data: null
    metadata:
      creationTimestamp: null
      name: sample-key
      namespace: kube-system
    type: Opaque
  • このファイルをapplyすればSealedSecretリソースが作成される
$ kubectl apply -f sealedsecret.yaml                                             
sealedsecret.bitnami.com/sample-key created
  • これを基にSecretリソースが払い出されているのが確認できればOK
$ kubectl get secret
NAME               TYPE              DATA   AGE
sample-key         Opaque            1      30s
  • あとは一般的な方法でPodからこのSecretを読み取らせる

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?