2020-12-29 19:38:38 +00:00
---
title: Grafana
---
2022-06-15 19:31:34 +00:00
<span class="badge badge--primary">Support level: authentik</span>
2020-12-29 19:38:38 +00:00
## What is Grafana
2023-07-31 10:16:58 +00:00
> Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources, Grafana Enterprise version with additional capabilities is also available. It is expandable through a plug-in system.
>
> -- https://en.wikipedia.org/wiki/Grafana
2020-12-29 19:38:38 +00:00
## Preparation
The following placeholders will be used:
2022-05-09 19:22:41 +00:00
- `grafana.company` is the FQDN of the Grafana install.
- `authentik.company` is the FQDN of the authentik install.
2020-12-29 19:38:38 +00:00
2024-01-10 04:00:24 +00:00
Create an OAuth2/OpenID provider with the following parameters:
2020-12-29 19:38:38 +00:00
2022-05-09 19:22:41 +00:00
- Client Type: `Confidential`
- Scopes: OpenID, Email and Profile
- Signing Key: Select any available key
- Redirect URIs: `https://grafana.company/login/generic_oauth`
2020-12-29 19:38:38 +00:00
2024-01-10 04:00:24 +00:00
Note the Client ID and Client Secret values.
2023-12-30 15:20:29 +00:00
2024-01-10 04:00:24 +00:00
Create an application, using the provider you've created above. Note the slug of the application you've created.
2020-12-29 19:38:38 +00:00
2023-10-19 12:26:40 +00:00
## Terraform provider
```hcl
data "authentik_flow" "default-provider-authorization-implicit-consent" {
slug = "default-provider-authorization-implicit-consent"
}
data "authentik_scope_mapping" "scope-email" {
name = "authentik default OAuth Mapping: OpenID 'email'"
}
data "authentik_scope_mapping" "scope-profile" {
name = "authentik default OAuth Mapping: OpenID 'profile'"
}
data "authentik_scope_mapping" "scope-openid" {
name = "authentik default OAuth Mapping: OpenID 'openid'"
}
resource "authentik_provider_oauth2" "grafana" {
name = "Grafana"
# Required. You can use the output of:
# $ openssl rand -hex 16
client_id = "my_client_id"
# Optional: will be generated if not provided
# client_secret = "my_client_secret"
authorization_flow = data.authentik_flow.default-provider-authorization-implicit-consent.id
redirect_uris = ["https://grafana.company/login/generic_oauth"]
property_mappings = [
data.authentik_scope_mapping.scope-email.id,
data.authentik_scope_mapping.scope-profile.id,
data.authentik_scope_mapping.scope-openid.id,
]
}
resource "authentik_application" "grafana" {
name = "Grafana"
slug = "grafana"
protocol_provider = authentik_provider_oauth2.grafana.id
}
resource "authentik_group" "grafana_admins" {
name = "Grafana Admins"
}
resource "authentik_group" "grafana_editors" {
name = "Grafana Editors"
}
resource "authentik_group" "grafana_viewers" {
name = "Grafana Viewers"
}
```
2023-12-14 19:38:34 +00:00
## Grafana configuration
2020-12-29 19:38:38 +00:00
2022-05-09 19:22:41 +00:00
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
2021-03-02 21:10:54 +00:00
<Tabs
defaultValue="docker"
values={[
{label: 'Docker', value: 'docker'},
{label: 'Standalone', value: 'standalone'},
2023-10-19 12:26:40 +00:00
{label: 'Helm', value: 'helm'},
2021-03-02 21:10:54 +00:00
]}>
<TabItem value="docker">
2023-10-19 12:26:40 +00:00
If your Grafana instance is running in Docker, set the following environment variables:
2020-12-29 19:38:38 +00:00
```yaml
environment:
GF_AUTH_GENERIC_OAUTH_ENABLED: "true"
GF_AUTH_GENERIC_OAUTH_NAME: "authentik"
GF_AUTH_GENERIC_OAUTH_CLIENT_ID: "<Client ID from above>"
GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET: "<Client Secret from above>"
GF_AUTH_GENERIC_OAUTH_SCOPES: "openid profile email"
GF_AUTH_GENERIC_OAUTH_AUTH_URL: "https://authentik.company/application/o/authorize/"
GF_AUTH_GENERIC_OAUTH_TOKEN_URL: "https://authentik.company/application/o/token/"
GF_AUTH_GENERIC_OAUTH_API_URL: "https://authentik.company/application/o/userinfo/"
GF_AUTH_SIGNOUT_REDIRECT_URL: "https://authentik.company/application/o/<Slug of the application from above>/end-session/"
2021-04-19 17:07:09 +00:00
# Optionally enable auto-login (bypasses Grafana login screen)
2020-12-29 19:38:38 +00:00
GF_AUTH_OAUTH_AUTO_LOGIN: "true"
2021-04-19 17:07:09 +00:00
# Optionally map user groups to Grafana roles
2024-01-10 04:00:24 +00:00
GF_AUTH_GENERIC_OAUTH_ROLE_ATTRIBUTE_PATH: "contains(groups, 'Grafana Admins') && 'Admin' || contains(groups, 'Grafana Editors') && 'Editor' || 'Viewer'"
2020-12-29 19:38:38 +00:00
```
2022-05-09 19:22:41 +00:00
2021-03-02 21:10:54 +00:00
</TabItem>
<TabItem value="standalone">
2020-12-29 19:38:38 +00:00
If you are using a config-file instead, you have to set these options:
```ini
[auth]
signout_redirect_url = https://authentik.company/application/o/<Slug of the application from above>/end-session/
# Optionally enable auto-login
oauth_auto_login = true
[auth.generic_oauth]
name = authentik
enabled = true
client_id = <Client ID from above>
client_secret = <Client Secret from above>
scopes = openid email profile
auth_url = https://authentik.company/application/o/authorize/
token_url = https://authentik.company/application/o/token/
api_url = https://authentik.company/application/o/userinfo/
2021-04-19 17:07:09 +00:00
# Optionally map user groups to Grafana roles
2024-01-10 04:00:24 +00:00
role_attribute_path = contains(groups, 'Grafana Admins') && 'Admin' || contains(groups, 'Grafana Editors') && 'Editor' || 'Viewer'
2020-12-29 19:38:38 +00:00
```
2022-05-09 19:22:41 +00:00
2023-10-19 12:26:40 +00:00
</TabItem>
<TabItem value="helm">
If you are using a Helm `values.yaml` file instead, you have to set these options:
```yaml
grafana.ini:
auth:
signout_redirect_url: "https://authentik.company/application/o/<Slug of the application from above>/end-session/"
oauth_auto_login: true
auth.generic_oauth:
name: authentik
enabled: true
client_id: "<Client ID from above>"
client_secret: "<Client Secret from above>"
scopes: "openid profile email"
auth_url: "https://authentik.company/application/o/authorize/"
token_url: "https://authentik.company/application/o/token/"
api_url: "https://authentik.company/application/o/userinfo/"
# Optionally map user groups to Grafana roles
2024-01-10 04:00:24 +00:00
role_attribute_path: contains(groups, 'Grafana Admins') && 'Admin' || contains(groups, 'Grafana Editors') && 'Editor' || 'Viewer'
2023-10-19 12:26:40 +00:00
```
:::note
For security reasons you shouldn't inline the client_secret in the values, but use a secret instead. For more information, see https://github.com/grafana/helm-charts/blob/main/charts/grafana/README.md#how-to-securely-reference-secrets-in-grafanaini
:::
2021-03-02 21:10:54 +00:00
</TabItem>
</Tabs>
2021-04-19 17:07:09 +00:00
### Role Mappings
In the configuration above you can see an example of a role mapping. Upon login, this configuration looks at the groups of which the current user is a member. If any of the specified group names are found, the user will be granted the resulting role in Grafana.
2021-12-22 21:09:49 +00:00
In the example shown above, one of the specified group names is "Grafana Admins". If the user is a member of this group, they will be granted the "Admin" role in Grafana.
2021-04-19 17:07:09 +00:00
If the user is not a member of the "Grafana Admins" group, it moves on to see if the user is a member of the "Grafana Editors" group. If they are, they are granted the "Editor" role. Finally, if the user is not found to be a member of either of these groups, it fails back to granting the "Viewer" role.
For more information on group/role mappings, see [Grafana's docs](https://grafana.com/docs/grafana/latest/auth/generic-oauth/#role-mapping).
2021-08-14 20:05:23 +00:00
### Grafana Configuration Considerations
2023-03-14 12:44:08 +00:00
Make sure in your configuration that `root_url` is set correctly, otherwise your redirect url might get processed incorrectly. For example, if your grafana instance is running on the default configuration and is accessible behind a reverse proxy at `https://grafana.company`, your redirect url will end up looking like this, `https://grafana.company/`.
2021-08-14 20:05:23 +00:00
If you get `user does not belong to org` error when trying to log into grafana for the first time via OAuth, check if you have an organization with the ID of `1`, if not, then you have to add the following to your grafana config:
```ini
[users]
auto_assign_org = true
auto_assign_org_id = <id-of-your-default-organization>
2021-12-22 21:09:49 +00:00
```