Carpe Diem

備忘録

HashiCorp VaultのAppRoleを使ってトークン取得

概要

HashiCorp Vaultではトークンを取得するための様々な認証方法がありますが、その中でアプリケーションに向いたAppRoleという認証方法があります。

f:id:quoll00:20180803184031p:plain

ref: AppRole Pull Authentication | Vault - HashiCorp Learn

この図ではRole IDSecret IDというID & Password的な認証方法ですが、他にも特定のIP Rangeのアクセスの時のみトークンを発行するといった設定もでき、アプリケーションのPrivate IPの範囲を指定すれば外部からはトークンは発行できず、アプリケーションのみ発行できるということが実現できます。

AppRoleの設定

有効化

まずは有効化

$ vault auth enable approle

Roleの用意

予め用意したポリシーkv-secretトークンにアタッチされるようなロールを以下のように用意します。

$ vault write auth/approle/role/my-role \
  secret_id_bound_cidrs=10.1.0.0/16 \
  token_bound_cidrs=10.1.0.0/16 \
  bind_secret_id=false \
  token_ttl=15m \
  token_max_ttl=15m \
  policies=kv-secret

ポイントは

  • bind_secret_id=falsesecret_idは使わない
  • secret_id_bound_cidrsでAppRoleを使えるサーバのIP範囲を指定
  • token_bound_cidrsで生成したトークンを使えるサーバのIP範囲を指定

です。
設定できたら確認します。

$ vault read auth/approle/role/my-role
WARNING! The following warnings were returned from Vault:

  * The "bound_cidr_list" parameter is deprecated and will be removed in favor
  of "secret_id_bound_cidrs".

Key                      Value
---                      -----
bind_secret_id           false
bound_cidr_list          [10.1.0.0/16]
local_secret_ids         false
period                   0s
policies                 [kv-secret]
secret_id_bound_cidrs    [10.1.0.0/16]
secret_id_num_uses       0
secret_id_ttl            0s
token_bound_cidrs        [10.1.0.0/16]
token_max_ttl            15m
token_num_uses           0
token_ttl                15m

RoleIDの取得

トークン取得時にはどのRoleを使うか指定する必要があるのでIDを取得します。

$ vault read auth/approle/role/my-role/role-id
Key        Value
---        -----
role_id    5b38a95d-76cb-d6c1-bb9a-845cc21eaf6f

このrole_idRoleを上書きしても変わりません

動作確認

トークンの発行(内部から)

$ vault write auth/approle/login role_id=5b38a95d-76cb-d6c1-bb9a-845cc21eaf6f
Key                     Value
---                     -----
token                   307713ae-6cb8-a470-ee33-b1f776d4cf79
token_accessor          4e17d27d-0e26-e2dc-f1fe-3dee873fd848
token_duration          15m
token_renewable         true
token_policies          ["default" "kv-secret"]
identity_policies       []
policies                ["default" "kv-secret"]
token_meta_role_name    my-role

トークンが取得できました。

vaultクライアントがない場合は以下のようにcurlでも取得できます。

$ curl --request POST \
   --data '{"role_id": "5b38a95d-76cb-d6c1-bb9a-845cc21eaf6f"}' \
   https://YOUR_VAULT_ADDR:8200/v1/auth/approle/login

トークンの発行(外から)

悪い人がRoleIDを知ったので内部ネットワーク外からアクセスしようとすると

$ vault write auth/approle/login role_id=5b38a95d-76cb-d6c1-bb9a-845cc21eaf6f
Error writing data to auth/approle/login: Error making API request.

URL: PUT https://YOUR_VAULT_ADDR:8200/v1/auth/approle/login
Code: 400. Errors:

* source address "13.112.18.124" unauthorized by CIDR restrictions on the role: <nil>

ちゃんと弾かれました。

ログイン(外から)

生成時にCIDRチェックをクリアして発行されたトークンを悪い人が盗んだとします。
悪い人がCIDR外からログインしてみると

$ vault login ee14c947-629a-fecf-2507-2d87d79b76e2
Error authenticating: error looking up token: Error making API request.

URL: GET https://YOUR_VAULT_ADDR:8200/v1/auth/token/lookup-self
Code: 403. Errors:

* permission denied

ちゃんと弾かれました。

まとめ

AppRoleを使うことでアプリケーションが柔軟にVaultのトークンを発行できるようになることが分かりました。

ソース