Grafana 串接 GitHub 登入

如果使用 Grafana 平常登入要在使用一組帳密實在是有點麻煩,這時候就可以串接 GitHub OAuth2 Authentication。

串接方法

新增 GitHub OAuth Apps

它串接的方法很簡單,只需要到 GitHub 的後台找到 Developer settings 的 OAuth Apps,新增一個 New OAuth App 然後輸入一下資訊就可以了。

設定 Grafana 的設定檔

修改 Grafana 的設定檔(/etc/grafana/grafana.ini),找到 [auth.github] 部分,將相關設定填入如下:

#################################### GitHub Auth ##########################
[auth.github]
enabled = true
allow_sign_up = true
auto_login = false
client_id = 0000007a199e9c6f0000
client_secret = 0000b231e614ec837eb24855772954e100000000
scopes = user:email,read:org
auth_url = https://github.com/login/oauth/authorize
token_url = https://github.com/login/oauth/access_token
api_url = https://api.github.com/user
;allowed_domains =
;team_ids =
allowed_organizations = clarencetw
;role_attribute_path =
;role_attribute_strict = false
;allow_assign_grafana_admin = false

問題排解

如果出現以下問題

t=2023-01-01T00:00:00+0000 lvl=eror msg="failed to login " logger=oauth error=redirect_uri_mismatch errorDesc="The redirect_uri MUST match the registered callback URL for this application."

解決方法是修改 root_url 參數,將其改成目前伺服器的位置。如以上範例修改 Grafana 的設定檔(/etc/grafana/grafana.ini)

[server]
root_url = https://grafana.example.com/