---
title: CredHub Service BrokerでCloud Foundry上のアプリケーションへセキュアに環境変数を設定
tags: ["Cloud Foundry", "CredHub", "Pivotal Cloud Foundry"]
categories: ["Dev", "PaaS", "CloudFoundry", "PCF"]
date: 2018-07-31T18:12:37Z
updated: 2020-02-05T08:54:31Z
---

PCF 2.2でGAになったCredHub Service Brokerについて。([関連記事](https://content.pivotal.io/blog/how-pivotal-cloud-foundry-2-2-helps-you-improve-the-metrics-that-matter#CredHub))


### User Provided Serviceの場合

CredHub Service Brokerは一言で言うとセキュアな[User Provided Service](https://docs.cloudfoundry.org/devguide/services/user-provided.html)です。

違いを説明するためにまずはUser Provided Serviceを使ってみます。

`foo`ディレクトリを作成して、

```
mdkir foo
cd foo
```

`phpinfo()`を表示するだけのPHPアプリケーションを作成します。

```php
cat <<EOF > index.php
<?php
phpinfo();
EOF
```

`cf push`でデプロイします。

```
cf push hello-php -b php_buildpack -m 32m
```

このアプリケーションに何らかのAPIキーを渡すために、User Provided Serviceを使ってみます。

`cf create-user-provided-service` (または`cf cups`)で次のようにサービスインスタンスを作成します。

```
cf create-user-provided-service demo-ups -p '{"api-key":"THIS_IS_A_SECRET_KEY"}'
```

これを`cf bind-service`でアプリケーションにバインドして、リスタートすると、

```
cf bind-service hello-php demo-ups
cf restart hello-php
```

環境変数`VCAP_SERVICES`のJSON文字列の中に`{"api-key":"THIS_IS_A_SECRET_KEY"}`が含まれます。

![image](https://user-images.githubusercontent.com/106908/43475764-bbd193ba-9531-11e8-8f3a-d1f544436417.png)

例えばPHPでは次のようにAPIキーにアクセスできます。

```php
$vcap_services = json_decode(getenv('VCAP_SERVICES'));
$credentials = $vcap_services->{'user-provided'}[0]->credentials;
$api_key = $credentials->{'api-key'};
```

Spring Bootの場合は`application.properties`に次のように設定すれば良いです。

```properties
api-key=${vcap.services.demo-ups.credentials.api-key}
```

アプリケーションに環境変数を設定する方法としてUser Provided Serviceは便利なのですが、一つ課題があり、
`cf env`で環境変数を確認する際に次のように設定内容がターミナル上で表示されてしまいます。

```
cf env hello-php
```

![image](https://user-images.githubusercontent.com/106908/43475768-bf662da6-9531-11e8-8317-c8b73595671b.png)

これを嫌がる方は多いです。`cf set-env`で環境変数を設定する場合も同じです。

### CredHub Service Brokerの場合

CredHub Service Brokerは設定したCredentialsをCredHubに保存します。CredHubに保存されるデータは暗号化されますし、HSM(Hardware Security Module)と連携することもできます。

CredHub Service Brokerが有効になっていれば`cf marketplace`から確認できます。

![image](https://user-images.githubusercontent.com/106908/43475831-e90670ee-9531-11e8-9f7a-539c2eeb1dd0.png)

まずは先ほど作成した`demo-ups`サービスインスタンスをアンバインドして削除します。

```
cf unbind-service hello-php demo-ups
cf delete-service demo-ups
```

次のCredHub Service Brokerのサービスインスタンスを作成します。作成方法は次の通りで、User Provided Serviceと似ています。

```
cf create-service credhub default demo-credhub -c '{"api-key":"THIS_IS_A_SECRET_KEY"}'
```

これを`cf bind-service`でアプリケーションにバインドして、リスタートすると、

```
cf bind-service hello-php demo-credhub
cf restart hello-php
```

`cf env`で環境変数を確認する際はCredentialsが直接表示される代わりに、次のようにCredHubへの参照キーのみが表示されます。

```
cf env hello-php
```

![image](https://user-images.githubusercontent.com/106908/43476117-b5773276-9532-11e8-8a00-6e6801d1a121.png)

ではアプリケーションからわざわざCredHubにアクセスしにいけないかというとそうではなく、アプリケーションからはUser Provided Serviceと同じように`VCAP_SERVICES`内にCredentialsが見えます。

![image](https://user-images.githubusercontent.com/106908/43476040-7cf0b698-9532-11e8-91a7-111ee99c5a61.png)

アプリケーションは環境変数を見れば良いだけです。
PHPの場合は次のようにアクセスできます。

```php
cat <<'EOF' > apikey.php
<?php
$vcap_services = json_decode(getenv('VCAP_SERVICES'));
$credentials = $vcap_services->credhub[0]->credentials;
echo "API_KEY = " . $credentials->{'api-key'};
EOF
```

![image](https://user-images.githubusercontent.com/106908/43477160-705822ec-9535-11e8-8896-c982659b6bcf.png)

Spring Bootの場合は`application.properties`に次のように設定すれば良いです。

```properties
api-key=${vcap.services.demo-credhub.credentials.api-key}
```

環境変数`VCAP_SERVICES`からCredentialsにアクセスできる点はUser Provided Serviceと同じですが、

* `cf env`でCredentialsが表示されない
* Credentialsの保存場所がCredHub

という点でよりセキュアです。

内部の仕組みについては[こちらの記事](https://content.pivotal.io/pivotal-blog/enterprise-architects-its-time-to-learn-how-the-credhub-service-broker-applies-the-principle-of-least-privilege-to-your-secrets)を参照してください。

また、CredHub Service Brokerのインストール方法は[こちら](https://docs.pivotal.io/credhub-service-broker/)。
OSS版は[こちら](https://github.com/cloudfoundry/secure-credentials-broker)です。
