Heroku

Keepers Secrets Managerを使用して、Herokuアプリケーションの機密性の高いシークレットを保存。

機能

  • シークレットのクレデンシャルをKeeperボルトに安全に保存し、Herokuで公開せずに使用できます

  • Keeper ボルトからHerokuアプリにファイルをコピーします

  • Herokuの他のSecrets Manager SDK機能 (記録の作成やTOTPコードなど) を利用できます

Keeper Secrets Manager機能の完全なリストについては、概要をご参照ください。

概説

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

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

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

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

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

Slackの設定

最初の手順は、Slackのインスタンスからwebhookトークンを取得することです。Slackのウェブサイトにログインし、Your Appsに移動します。

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

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

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

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

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

Keeperボルトの設定

Keeperボルトで、共有フォルダにSlackのWebhook URLを格納するログイン記録を作成します。共有フォルダは、アプリケーションを使用して追加できます。詳細については、クイックスタートガイドをご参照ください。

Heroku

この例では、HerokuアカウントとPython 3が必要です。次の手順では、説明に従って、Heroku CLIをインストールします。

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

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

生成されたアプリケーションの名前は、この例とは異なります。

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

$ 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です。

Base64設定の作成について詳しくは、設定のドキュメントをご参照ください。

$ 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が含まれています。

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

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

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に渡します。

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

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

$ 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 (送信) をクリックします。

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

最終更新