# Kubernetes外部シークレットオペレータ

<figure><img src="https://docs.keeper.io/~gitbook/image?url=https%3A%2F%2F762006384-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-MJXOXEifAmpyvNVL1to%252Fuploads%252FTuTzLzBatbwf6hlcKEGt%252Fksm-externalsecrets-kubernetes.jpg%3Falt%3Dmedia%26token%3D16302139-00a4-4e61-8c25-8d24f83c911b&#x26;width=768&#x26;dpr=4&#x26;quality=100&#x26;sign=15e8749c&#x26;sv=1" alt=""><figcaption></figcaption></figure>

## 概要 <a href="#overview" id="overview"></a>

[Kubernetes外部シークレットオペレータ](https://external-secrets.io/latest/)は、さまざまな外部APIからシークレットを同期することで、Kubernetesにシークレットを挿入します。本ページでは主に外部シークレットのセットアップに焦点を当て、KeeperボルトからKubernetesへのシークレットを同期します。

## 特徴 <a href="#features" id="features"></a>

* 外部シークレットを介して、KeeperボルトからKubernetesへシークレットをシームレスに同期します。
* Keeperボルトからすべてのポッドに渡ってシークレットにリアルタイムでアクセスできます。

## 要件 <a href="#prerequisites" id="prerequisites"></a>

この統合を利用するには、以下が必要となります。

* Keeperシークレットマネージャーへのアクセス (詳細については[クイックスタートガイド](/keeperpam/jp/secrets-manager/quick-start-guide.md)のページをご参照ください)
  * Keeperアカウントでシークレットマネージャーのアドオンが有効である
  * シークレットマネージャー強制適用ポリシーが有効になっているロールのメンバーシップ
* シークレットが共有された[Keeperシークレットマネージャーアプリケーション](/keeperpam/jp/secrets-manager/about/terminology.md)
  * アプリケーションの作成手順については、[クイックスタートガイド](/keeperpam/jp/secrets-manager/quick-start-guide.md)をご参照ください。
  * シークレットを読み書きできるように、デバイスに適切な権限を追加してください。
* 初期化された[Keeperシークレットマネージャー構成](/keeperpam/jp/secrets-manager/about/secrets-manager-configuration.md)
* 以下のコマンドがインストール済み
  * [`helm`](https://helm.sh/)- Kubernetesのパッケージマネージャー
  * [`kubectl`](https://kubernetes.io/docs/reference/kubectl/)- Kubernetesのコマンドライン ツール

## セットアップ <a href="#setup" id="setup"></a>

### Helmで外部シークレットをインストールする <a href="#install-external-secrets-with-helm" id="install-external-secrets-with-helm"></a>

以下のコマンドを実行します。

```sh
helm repo add external-secrets https://charts.external-secrets.io

helm install external-secrets \
    external-secrets/external-secrets \
    -n external-secrets \
    --create-namespace
```

### Base64 KSM 構成を保存するためのKubernetesシークレットを作成 <a href="#create-kubernetes-secret-to-store-base64-ksm-config" id="create-kubernetes-secret-to-store-base64-ksm-config"></a>

ご利用のデバイス用に[Secrets Manager構成](/keeperpam/jp/secrets-manager/about/secrets-manager-configuration.md)を作成すると、KeeperシークレットマネージャーAPI からのデータの認証と復号化に使用される接続トークン、暗号化キー、識別子、ドメイン情報を含む Base64 JSON 文字列が作成されます。

Base64 JSON構成文字列は外部シークレットによって設定されKeeper Securityに対して認証し、通常のKubernetesシークレット内で定義されます。

以下のコマンドを呼び出すと、Keeperシークレットマネージャーへの認証に使用される Kubernetesシークレットが作成されます。

{% code lineNumbers="true" %}

```
kubectl apply -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
  name: ksm-config-secret # name of the k8s Secret where KSM config is stored
type: Opaque
data:
  ksm_config: "[REPLACE WITH YOUR BASE64 JSON string]"
EOF
```

{% endcode %}

**注:** 上記のコード例の2行目から8行目は 、YAMLファイルに保存して`kubectl apply`コマンドで適用できます。たとえば、2行目から8行目を`secrets.yaml`に保存して、以下のコマンドを実行します。

```
kubectl apply -f secret.yaml
```

### SecretStoreを作成 <a href="#create-secretstore" id="create-secretstore"></a>

Base64 JSON文字列に定義した`ksm_config`でKubernetesシークレットを作成すると、SecretStoreを作成できます。

以下のコマンドを呼び出すと、SecretStoreが作成されます。

{% code lineNumbers="true" %}

```
kubectl apply -f - <<EOF
apiVersion: external-secrets.io/v1beta1   
kind: SecretStore
metadata:
  name: my-external-secrets-secretstore   # name of the SecretStore where retrieved secrets will be stored once fetched from Keeper Secrets Manager (KSM)
spec:
  provider:
    keepersecurity:                       # name of the SecretStore provider, in this case KeeperSecurity
      authRef:
        name: ksm-config-secret           # name of the k8s Secret where KSM config is stored
        key: ksm_config                   # key in the k8s Secret where KSM config is stored
      folderID: "[SHARED FOLDER UID]"     # UID of the shared folder in KeeperSecurity where the records 
                                          #   are stored. Make sure the folder is shared into the KSM Application
EOF
```

{% endcode %}

{% hint style="info" %}
上記の例では、ボルト内のレコードが保存されている共有フォルダのUIDで`folderID`を定義します。
{% endhint %}

{% hint style="info" %}
`ClusterSecretStore`の場合は、作成したシークレットの名前空間で `SecretAccessKeyRefのnamespace`を指定するようにしてください。
{% endhint %}

**注:** 上記のコード2行目から13行目は 、YAMLファイルに保存して、`kubectl apply`コマンドで適用できます。たとえば、2行目から13行目を`secretstore.yaml`に保存して、以下のコマンドを実行します。

```
kubectl apply -f secretstore.yaml
```

### ExternalSecretを作成 <a href="#create-externalsecret" id="create-externalsecret"></a>

次に、ExternalSecretを作成します。

以下のコードでは、外部シークレットを作成し、指定したレコードのログインとパスワードフィールドの値をKubernetesシークレットに保存します。これらのフィールドは`target.template.data`セクションで定義され、30秒ごとに更新されます。サポートされているフィールドのリストについては、[こちらのページ](/keeperpam/jp/secrets-manager/about/field-record-types.md)をご覧ください。

{% code lineNumbers="true" %}

```
kubectl apply -f - <<EOF
apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
 name: ksm-external-secret
spec:
 refreshInterval: 12h               # rate how often SecretManager pulls KeeperSecurity. 
                                    #   In this case every 12 hours, for the example. 
                                    #   We recommend this value to be 12h or more.
 secretStoreRef:                    # reference to the SecretStore defined above to authenticate against Keeper Security
   kind: SecretStore                # tells External Secrets the type of the secret store, should be same as the one defined above
   name: my-external-secrets-secretstore  

 dataFrom:                          # tells External Secrets which record to use to fetch from Keeper Secrets Manager (KSM)
   - extract:
       key: "[RECORD UID]"          # UID of the record in Keeper where the secrets are going to be fetched from
 target:                            # tells External Secrets the target location where to store the secrets once fetched from Keeper Security
   name: my-external-secrets-values # name of the k8s Secret to be created
   creationPolicy: Owner            # tells External Secrets to create the k8s Secret if it doesn't exist
   template:
     engineVersion: v2          
     data:
       username: "{{ .login }}"     # tells External Secrets to store the value of 
                                    # the login field in Keeper Security into the k8s Secret under the key username
       password: "{{ .password }}"  # tells External Secrets to store the value of 
                                    # the password field in Keeper Security into the k8s Secret under the key password
       name: "{{  (fromJson .name).first }} {{  (fromJson .name).middle }} {{  (fromJson .name).last }}" # decode json string into vars
EOF
```

{% endcode %}

{% hint style="info" %}
上記のコード例で、\[RECORD UID]を目的のレコードUIDに置き換えます。
{% endhint %}

{% hint style="info" %}
`name`、`phone`、`bankAccount`などの複雑な[タイプ](/keeperpam/jp/secrets-manager/about/field-record-types.md)は単一の文字列値と一致しないため、外部シークレットは完全なJSON文字列を返します。JSONテンプレート関数を使用してデコードします。
{% endhint %}

**注:** 上記のコード例の2行目から27行目は 、YAMLファイルに保存して、`kubectl apply`コマンドで適用できます。たとえば、2行目から27行目を`externalsecret.yaml`に保存して、以下のコマンドを実行します。

```
kubectl apply -f externalsecret.yaml
```

**動作**

* RecordがExternalSecretがマッピングされる方法
  * `remoteRef.key`がレコードIDに一致
  * `remoteRef.property`が以下のいずれかに一致
    * フィールド: レコードのフィールドのタイプ
    * CustomFields: レコードのフィールドのラベル
    * ファイル: レコードのファイル名
    * 空白の場合、レコード全体がJSON形式で返されます。
  * `remoteRef.version`は現在サポートされていません。
* `dataFrom`:
  * `find.path`は現在サポートされていません。
  * `find.name.regexp`が以下のいずれかに一致
    * フィールド: レコードのフィールドのタイプ
    * CustomFields: レコードのフィールドのラベル
    * ファイル: レコードのファイル名
  * `find.tags`は現時点ではサポートされていません。

### 制限事項 <a href="#limitations" id="limitations"></a>

このプロバイダの使用にはいくつかの制限があります。

* Keeperシークレットマネージャーは、レガシーのタイプなしのレコードでは動作しません。
* Keeperシークレットマネージャーでは`find.tags`タグの使用はサポートされていません。
* `find.path`は現在サポートされていません。

### シークレットをプッシュ <a href="#push-secrets" id="push-secrets"></a>

プッシュシークレットは、カスタムKeeperSecurityレコードタイプの`ExternalSecrets`でのみ機能します。

**動作**

* `selector`:
* `secret.name`: プッシュするKubernetes シークレットの名前
* `data.match`:
* `secretKey`: プッシュする選択済みシークレットのキー
* `remoteRef.remoteKey`: リモートプロバイダで作成されるシークレットとキー
  * 形式: SecretName/SecretKey

### PushSecretを作成 <a href="#create-pushsecret" id="create-pushsecret"></a>

KubernetesからKeeper Securityレコードを作成するには、`Kind=PushSecret`が必要となります。

{% code lineNumbers="true" %}

```
kubectl apply -f - <<EOF
apiVersion: external-secrets.io/v1alpha1
kind: PushSecret
metadata:
  name: example
spec:
  secretStoreRefs:
    - name: keeper
      kind: SecretStore
  refreshInterval: "1h"
  deletionPolicy: Delete
  selector:
    secret:
      name: secret-name # k8s secret to be pushed
  data:
    - match:
        secretKey: secret-key # k8s key within the secret to be pushed
        remoteRef:
          remoteKey: remote-secret-name/remote-secret-key # This will create a record called "remote-secret-name" with a key "remote-secret-key"
EOF
```

{% endcode %}

**注:** 上記のコード例の2行目から19行目は 、YAMLファイルに保存して、`kubectl apply`コマンドで適用できます。たとえば、 2行目から19行目を`pushsecret.yaml`に保存して、以下のコマンドを実行します。

```
kubectl apply -f pushsecret.yaml
```

{% hint style="warning" %}
KSMアプリケーションで、使用中の`remote-secret-name`というタイトルの付いた記録は1つだけであるようにします。
{% endhint %}

#### **制限事項**

* 現時点では、シークレットごとに 1 つのキーのみプッシュできます。
* 選択した名前のレコードは存在するが、キーが存在しない場合は、レコードを更新できません。

## セットアップの確認 <a href="#verifying-setup" id="verifying-setup"></a>

[Kubernetesシークレット](#create-kubernetes-secret-to-store-base64-ksm-config)、[SecretStore](#create-secretstore)、[ExternalSecret](#create-externalsecret)を設定後、`kubectl get secrets`コマンドでシークレットを抽出できます。

上記のコード例では、シークレットの名前はmy-external-secrets-valuesであり、以下のレコード値が保存されます。

```
data:
       username: "{{ .login }}"                                      
       password: "{{ .password }}"
```

ログインとパスワードの値を取得するには、以下のコマンドを呼び出します。

```
$ kubectl get secret my-external-secrets-values -o jsonpath="{.data}"
{"password":"a2lsbCB5b3U=","username":"SSB3aWxs"}
```

上記の応答はエンコードいるので、デコードするには以下を呼び出します。

```
$ kubectl get secret my-external-secrets-values -o jsonpath="{.data.password}" | base64 --decode
pAs$w0rd
```

## 結論 <a href="#conclusion" id="conclusion"></a>

本ページでは、Kubernetes外部シークレットオペレータを介して KeeperシークレットマネージャーをKubernetesに統合するための手順を詳しく説明しました。上記の手順で、Keeperボルトに保存されているシークレットをKubernetes環境にシームレスに同期できるようになります。これにより、シークレットを安全に管理できるだけでなく、すべてのポッド間でリアルタイムのアクセスも容易になります。

概説したプロセス (外部シークレットオペレータの設定、Kubernetesシークレット、SecretStore、ExternalSecret の作成など) は、この統合の鍵となります。設定が成功すると、コマンドを使用して統合を確認し、保存されているシークレットを簡単に取得できるようになります。

KeeperシークレットマネージャーとKubernetesの統合により、Kubernetes環境で実行されるアプリケーションのセキュリティインフラストラクチャが強化されます。シークレットを管理するための強固な基盤となり、全体的な運用効率とセキュリティ体制が向上します。

コマンド例のすべてのプレースホルダ部分を実際の情報に置き換えるようにしましょう。Kubernetesで外部シークレットを設定または管理する必要があるときはいつでも、本ページをご参照ください。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keeper.io/keeperpam/jp/secrets-manager/integrations/kubernetes-external-secrets-operator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
