2018-09-06 05:11:56 -05:00
+++
2018-12-16 17:26:23 -06:00
title = "GitLab OAuth2 Authentication"
2018-09-06 05:11:56 -05:00
description = "Grafana OAuthentication Guide "
keywords = ["grafana", "configuration", "documentation", "oauth"]
2020-10-01 16:37:26 -05:00
weight = 900
2018-09-06 05:11:56 -05:00
+++
# GitLab OAuth2 Authentication
2020-02-20 03:19:39 -06:00
To enable GitLab OAuth2 you must register the application in GitLab. GitLab will generate a client ID and secret key for you to use.
2018-09-06 05:11:56 -05:00
## Create GitLab OAuth keys
You need to [create a GitLab OAuth application ](https://docs.gitlab.com/ce/integration/oauth_provider.html ).
2021-08-06 08:52:36 -05:00
Choose a descriptive _Name_ , and use the following _Redirect URI_ :
2018-09-06 05:11:56 -05:00
```
https://grafana.example.com/login/gitlab
```
where `https://grafana.example.com` is the URL you use to connect to Grafana.
Adjust it as needed if you don't use HTTPS or if you use a different port; for
instance, if you access Grafana at `http://203.0.113.31:3000` , you should use
```
http://203.0.113.31:3000/login/gitlab
```
2021-09-13 11:44:37 -05:00
Finally, select _read_api_as the_Scope_and submit the form. Note that if you're
2018-09-06 05:11:56 -05:00
not going to use GitLab groups for authorization (i.e. not setting
2021-09-13 11:44:37 -05:00
`allowed_groups` , see below), you can select_read_user_ instead of _read_api_as
the_Scope_, thus giving a more restricted access to your GitLab API.
2018-09-06 05:11:56 -05:00
2021-08-06 08:52:36 -05:00
You'll get an _Application Id_ and a _Secret_ in return; we'll call them
2018-09-06 05:11:56 -05:00
`GITLAB_APPLICATION_ID` and `GITLAB_SECRET` respectively for the rest of this
section.
## Enable GitLab in Grafana
Add the following to your Grafana configuration file to enable GitLab
authentication:
2018-09-06 06:15:36 -05:00
```bash
2018-09-06 05:11:56 -05:00
[auth.gitlab]
2019-01-18 07:06:15 -06:00
enabled = true
2018-09-06 05:11:56 -05:00
allow_sign_up = false
client_id = GITLAB_APPLICATION_ID
client_secret = GITLAB_SECRET
2020-10-02 08:42:57 -05:00
scopes = read_api
2018-09-06 05:11:56 -05:00
auth_url = https://gitlab.com/oauth/authorize
token_url = https://gitlab.com/oauth/token
api_url = https://gitlab.com/api/v4
allowed_groups =
```
2020-11-09 14:26:49 -06:00
You may have to set the `root_url` option of `[server]` for the callback URL to be
2018-10-11 14:22:02 -05:00
correct. For example in case you are serving Grafana behind a proxy.
2018-09-06 05:11:56 -05:00
Restart the Grafana backend for your changes to take effect.
If you use your own instance of GitLab instead of `gitlab.com` , adjust
`auth_url` , `token_url` and `api_url` accordingly by replacing the `gitlab.com`
hostname with your own.
With `allow_sign_up` set to `false` , only existing users will be able to login
2021-08-06 08:52:36 -05:00
using their GitLab account, but with `allow_sign_up` set to `true` , _any_ user
2018-09-06 05:11:56 -05:00
who can authenticate on GitLab will be able to login on your Grafana instance;
if you use the public `gitlab.com` , it means anyone in the world would be able
to login on your Grafana instance.
2020-02-20 03:19:39 -06:00
You can limit access to only members of a given group or list of
2018-09-06 05:11:56 -05:00
groups by setting the `allowed_groups` option.
### allowed_groups
To limit access to authenticated users that are members of one or more [GitLab
groups](https://docs.gitlab.com/ce/user/group/index.html), set `allowed_groups`
to a comma- or space-separated list of groups. For instance, if you want to
only give access to members of the `example` group, set
```ini
allowed_groups = example
```
If you want to also give access to members of the subgroup `bar` , which is in
the group `foo` , set
```ini
allowed_groups = example, foo/bar
```
Note that in GitLab, the group or subgroup name doesn't always match its
display name, especially if the display name contains spaces or special
characters. Make sure you always use the group or subgroup name as it appears
in the URL of the group or subgroup.
2021-09-13 11:44:37 -05:00
Here's a complete example with `allow_sign_up` enabled, with access limited to
the `example` and `foo/bar` groups. The example also promotes all GitLab Admins to Grafana Admins:
2018-09-06 05:11:56 -05:00
```ini
[auth.gitlab]
2018-10-28 09:25:42 -05:00
enabled = true
2018-09-06 05:11:56 -05:00
allow_sign_up = true
client_id = GITLAB_APPLICATION_ID
client_secret = GITLAB_SECRET
2021-09-13 11:44:37 -05:00
scopes = read_api
2018-09-06 05:11:56 -05:00
auth_url = https://gitlab.com/oauth/authorize
token_url = https://gitlab.com/oauth/token
api_url = https://gitlab.com/api/v4
allowed_groups = example, foo/bar
2021-09-13 11:44:37 -05:00
role_attribute_path = is_admin & & 'Admin' || 'Viewer'
2018-09-06 05:11:56 -05:00
```
2021-09-13 11:44:37 -05:00
### Map roles
You can use GitLab OAuth to map roles. During mapping, Grafana checks for the presence of a role using the [JMESPath ](http://jmespath.org/examples.html ) specified via the `role_attribute_path` configuration option.
2022-02-15 13:20:45 -06:00
For the path lookup, Grafana uses JSON obtained from querying GitLab's API [`/api/v4/user` ](https://docs.gitlab.com/ee/api/users.html#list-current-user-for-normal-users ) endpoint. The result of evaluating the `role_attribute_path` JMESPath expression must be a valid Grafana role, for example, `Viewer` , `Editor` or `Admin` . For more information about roles and permissions in Grafana, refer to [About users and permissions ]({{< relref "../administration/manage-users-and-permissions/about-users-and-permissions.md" >}} ).
2021-09-13 11:44:37 -05:00
An example Query could look like the following:
```bash
role_attribute_path = is_admin & & 'Admin' || 'Viewer'
```
This allows every GitLab Admin to be an Admin in Grafana.
2019-08-07 09:05:35 -05:00
### Team Sync (Enterprise only)
> Only available in Grafana Enterprise v6.4+
With Team Sync you can map your GitLab groups to teams in Grafana so that your users will automatically be added to
2020-11-09 14:26:49 -06:00
the correct teams.
2019-08-07 09:05:35 -05:00
Your GitLab groups can be referenced in the same way as `allowed_groups` , like `example` or `foo/bar` .
2020-01-24 09:33:59 -06:00
[Learn more about Team Sync ]({{< relref "team-sync.md" >}} )