LoginSignup
4
1

More than 3 years have passed since last update.

App IdでGrafanaの認証とロールの設定をする

Last updated at Posted at 2020-08-04

目的

過去記事「App IDでGrafanaの認証をする」では、GrafanaのGeneric OAuthとIBM Cloud App IDを利用することで認証をしてみました。今回はそのアップデートとして、GrafanaのRoleをApp IDのカスタム属性を使って実現してみます。

手順

App IDのカスタム属性の設定

すでにユーザーは追加済とします。詳細は前回の記事を参照ください。

ユーザー・プロファイルからユーザーを選択し、カスタム属性を追加します。次のように、

{
  "grafanaRole": "Admin"
}

としてみます。キーは任意です。値はGrafanaのロールに基づき、次のいずれかです。

  • Admin
  • Editor
  • Viewer

image.png

カスタム属性の更新禁止設定

カスタム属性はデフォルトではユーザー権限で更新ができてしまいます。例えば、やり方さえ知っていればEditorロールのユーザーが自分のロールをAdminに自力で更新できてしまいます。それを避けるため、カスタム属性はユーザーは更新できないようにします。

下記画面で、アプリからのカスタム属性の変更無効にします。

image.png

カスタム属性の確認

curlで確認できます。

$ _token=$(curl -s https://jp-tok.appid.cloud.ibm.com/oauth/v4/${tenant_id}/token -d "client_id=${client_id}" -d "client_secret=${client_secret}" -d "username=${username}" -d "password=${password}" -d "grant_type=password" https://jp-tok.appid.cloud.ibm.com/oauth/v4/${tenant_id}/token | jq -r .access_token | tail -1)
$ curl -s https://jp-tok.appid.cloud.ibm.com/api/v1/attributes -H "Authorization: Bearer ${_token}"
{"grafanaRole":"Admin"}

grafana.iniの設定

次のように設定します。ポイントはapi_urlrole_attribute_pathです。api_urlはGrafanaがカスタム属性を取得するためのエンドポイントです。role_attribute_pathはattributeエンドポイントで得られたJSONのうち、Grafanaのロールを示すキーです。従って、カスタム属性のキーと一致させます。

grafana.ini
[auth.generic_oauth]
enabled = true
name = App ID # 任意
client_id = ${client_id}
client_secret = ${client_secret}
auth_url = https://jp-tok.appid.cloud.ibm.com/oauth/v4/${tenant_id}/authorization
token_url = https://jp-tok.appid.cloud.ibm.com/oauth/v4/${tenant_id}/token
api_url = https://jp-tok.appid.cloud.ibm.com/api/v1/attributes
role_attribute_path = grafanaRole # カスタム属性のキー

動作確認

Grafanaを(再)起動し、OAuthでログインします。ユーザーのPreferenceを確認します。次のようにRoleがAdminになっていればOKです。なお、もし設定に失敗している場合、つまりロールの指定に失敗した場合はViewerになります。

image.png

以上

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1