# Heroku

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2FVNSpK0Ucw57p3m7KKnVa%2Fimage.png?alt=media&#x26;token=73c3c7a7-103d-4aa9-ba40-ca625f9c3dcc" alt=""><figcaption></figcaption></figure>

## 機能

* シークレットのクレデンシャルをKeeperボルトに安全に保存し、Herokuで公開せずに使用できます
* Keeper ボルトからHerokuアプリにファイルをコピーします
* Herokuの他の[Secrets Manager SDK](/keeperpam/jp/secrets-manager/developer-sdk-library.md)機能 (記録の作成やTOTPコードなど) を利用できます

{% hint style="info" %}
Keeper Secrets Manager機能の完全なリストについては、[概要](/keeperpam/jp/secrets-manager/overview.md)をご参照ください。
{% endhint %}

## 概説

Herokuには、アプリケーションの設定変数を保存する機能があります。問題は、値が閲覧可能な形式で保存されることです。Keeper Secrets Managerを使用すると、機密性の高いシークレットをゼロ知識環境にある別の場所に保存できます。

以下は、Herokuと連携して使用可能なKeeper Secrets Manager SDKです。

* [Python](/keeperpam/jp/secrets-manager/developer-sdk-library/python-sdk.md)
* [GoLang](/keeperpam/jp/secrets-manager/developer-sdk-library/golang-sdk.md)
* [Java](/keeperpam/jp/secrets-manager/developer-sdk-library/java-sdk.md)
* [Javascript](/keeperpam/jp/secrets-manager/developer-sdk-library/javascript-sdk.md)

Keeper Secrets ManagerとHerokuの連携機能については、以下のサンプルプロジェクトをご参照ください

## 例 - Python SDKを使用したSlackの通知

この例では、Keeper Secrets Manager SDKを使用して、webhookトークンを取得し、HerokuウェブアプリケーションからSlackのチャネルにメッセージを送信します。

### Slackの設定

最初の手順は、Slackのインスタンスからwebhookトークンを取得することです。Slackのウェブサイトにログインし、[Your Apps](https://api.slack.com/apps)に移動します。

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2FCek8EsmTQBM1WYYeKSvc%2Fimage.png?alt=media&#x26;token=d0d6b167-20f1-4241-813d-986195ed0606" alt=""><figcaption></figcaption></figure>

Create New App (アプリの新規作成) ボタンをクリックし、ダイアログボックスからFrom scratch (最初から) を選択して、新しいアプリを作成します。この時点で、App Name (アプリ名) を入力し、アプリを開発するワークスペースを選択します。

次に「Incoming Webhooks」 (受信Webhook) ボタンをクリックします。

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2F09LH6c2PsgDMwIx45PZQ%2Fimage.png?alt=media&#x26;token=2d1c2b49-af5a-44e1-82f2-7382ccbecca7" alt=""><figcaption></figcaption></figure>

Incoming Webhookを有効にします。すると、**Webhook URLs for Your Workspace** (ワークスペースのWebhook URL) セクションが表示されます。**Add New Webhook to Workspace** (ワークスペースに新しいWebhookを追加) ボタンをクリックします。

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2F7jG29tBRi53y5v3inIz7%2Fimage.png?alt=media&#x26;token=5e42637f-e589-48de-9205-5a4c8ab58b4d" alt=""><figcaption></figcaption></figure>

次に、ワークスペースでチャンネルを選択します。

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2FJ6mMsX3RCV4SqNhk11BT%2Fimage.png?alt=media&#x26;token=0a234a76-0540-417f-93d5-2e67a2f8fbb1" alt=""><figcaption></figcaption></figure>

Incoming Webhooks **(**&#x53D7;信Webhook) ページの下部に**Webhook URL**が表示されるようになります。 この例では、そのURLを**コピー**します。これで、Keeperボルトの記録に格納されます。

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2FB5DIVcKw7UI64pyXyStA%2Fimage.png?alt=media&#x26;token=3a0e3577-5513-4a01-a484-b3120e48ee8b" alt=""><figcaption></figcaption></figure>

### Keeperボルトの設定

Keeperボルトで、共有フォルダにSlackの**Webhook URL**を格納するログイン記録を作成します。共有フォルダは、アプリケーションを使用して追加できます。詳細については、[クイックスタートガイド](/keeperpam/jp/secrets-manager/quick-start-guide.md)をご参照ください。

**Webhook URL**を**ログイン**記録&#x306E;**\[ウェブサイト アドレス]**&#x30D5;ィールドにコピーして保存します。保存したら、記録の情報アイコン <img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2F1LatmHlKOhmsFq49FYYf%2Fimage.png?alt=media&#x26;token=57ea8497-5200-4dbb-9e3d-ce1e73da83c1" alt="" data-size="line">をクリックして、記録UIDを確認します。記録UIDのURLをクリップボードにコピーして、実際の記録UIDを含めることができます。必要なのはURLの部分だけです。

### Heroku

この例では、[Herokuアカウント](https://signup.heroku.com/dc)とPython 3が必要です。次の手順では、[説明](https://devcenter.heroku.com/articles/getting-started-with-python#set-up)に従って、Heroku CLIをインストールします。

Heroku CLIをインストールすると、アプリケーションを作成できます。

```bash
$ heroku create
Creating app... done, ⬢ random-name-30564
https://random-name-30564.herokuapp.com/ | https://git.heroku.com/random-name-30564.git
```

{% hint style="info" %}
生成されたアプリケーションの名前は、この例とは異なります。
{% endhint %}

アプリケーションに使用される空のGitリポジトリも作成されます。このGitリポジトリは複製できます。空であるという警告が表示されます。その後、リポジトリディレクトリに変更します。

```bash
$ git clone https://git.heroku.com/random-name-30564.git
Cloning into 'random-name-30564'...
warning:You appear to have cloned an empty repository.

$ cd random-name-30564
```

リポジトリを複製すると、設定の変数をセットできます。セットする必要のある設定変数は2つあります。1つ目は、Webhook URLを含む記録のUID記録である**RECORD\_UID**です。2つ目は、Base64でエンコードされたSecrets Managerの設定を含む**KSM\_CONFIG**です。

{% hint style="info" %}
Base64設定の作成について詳しくは、[設定のドキュメント](/keeperpam/jp/secrets-manager/about/secrets-manager-configuration.md)をご参照ください。
{% endhint %}

```bash
$ heroku config:set RECORD_UID=XXXXXX
Setting RECORD_UID and restarting ⬢ random-name-30564... done, v11
RECORD_UID:XXXXXX

$ heroku config:set KSM_CONFIG=ewogI .....Igp9
Setting KSM_CONFIG and restarting ⬢ random-name-30564... done, v11
KSM_CONFIG: ewogI .....Igp9
```

設定変数を指定して、アプリケーションを追加できます。最初の手順は、Python 3アプリケーションの要件を定義することです。**requirements.txt**ファイルには、ウェブサイトで使用する**Flask**、Slackとの通信に使用する**slack\_sdk**、Secrets Managerとの通信する**keeper-secrets-manager-core**が含まれています。

```bash
$ cat << EOF > requirements.txt
Flask
slack_sdk
keeper-secrets-manager-core
EOF
```

任意のエディタで、以下の内容を**app.py**に保存します。

```python
import os
from flask import Flask, request
from slack_sdk.webhook import WebhookClient
from keeper_secrets_manager_core import SecretsManager

app = Flask(__name__)
secret_manager = SecretsManager()
record_id = os.environ.get("RECORD_UID")
webhook = WebhookClient(secret_manager.get_notation("{}/field/url".format(record_id)))


@app.route('/')
def hello():

    message = request.args.get("message")
    if message is not None and message != "":
        response = webhook.send(text=message)

    html = """
<html>
    <head><title>Send a Slack Message</title></head>
    <body>
        <form method="GET">
            Message:
            <input type="text" name="message" />
            <input type="submit" />
        </form>
    </body>
</html>
    """

    return html


if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

```

これは、フォーム入力ボックスと送信ボタンを表示する、単純なFlaskウェブアプリケーションです。入力ボックスに入力されたテキストはすべて、Slackチャンネルに送信されます。

最後の部分では、Procfileファイルを使用してアプリケーションを起動する方法に関する情報をHerokuに渡します。

```bash
$ cat << EOF > Procfile
web: python app.py
EOF
```

これで、アプリケーションを実行する準備ができました。Gitリポジトリにファイルを追加し、メインブランチをプッシュします。これにより、アプリケーションがビルドされて起動します。

```bash
$ git add requirements.txt app.py Procfile
$ git commit -m "Initial Commit"
[main 0f31f23] Initial Commit
 3 file changed, 0 insertion(+), 0 deletion(-)
...

$ git push origin main
...
remote:Compressing source files... done.
remote:Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/python
remote: -----> Python app detected
remote: -----> No Python version was specified.Using the same version as the last build: python-3.9.9
remote:       To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> No change in requirements detected, installing from cache
remote: -----> Using cached install of python-3.9.9
remote: -----> Installing pip 21.3.1, setuptools 57.5.0 and wheel 0.37.0
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote: -----> Discovering process types
remote:       Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:       Done:62.4M
remote: -----> Launching...
remote:       Released v13
remote:        https://random-name-30564.herokuapp.com/ deployed to Heroku
remote:
remote:Verifying deploy... done.
To https://git.heroku.com/random-name-30564.git
   89b16a2..fda1835  main -> main
```

この時点で、URL (`https://random-name-30564.herokuapp.com/`) を使用するか、以下のコマンドラインを使用してウェブサイトにアクセスできます。

```
$ heroku open
```

シンプルなウェブサイトが表示されます。メッセージを入力して、Submit (送信) をクリックします。

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2FjzzpIbiK9eUDdNco8bq1%2Fimage.png?alt=media&#x26;token=ed388d78-6d28-421f-8ff2-dcbb83d70492" alt=""><figcaption></figcaption></figure>

作成されたWebhookのチャネルにメッセージが表示されるはずです。

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2FH0rhD1CyScHwqNS55XUx%2Fimage.png?alt=media&#x26;token=edfedc1b-b305-4c59-adc9-6677e57bb4d0" alt=""><figcaption></figcaption></figure>


---

# 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/heroku.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.
