docs: add guide for vmgateway configuration with OpenID and Grafana
7.8 KiB
How to configure vmgateway for multi-tenant access using Grafana and OpenID Connect
Using Grafana with vmgateway is a great way to provide multi-tenant access to your metrics. vmgateway provides a way to authenticate users using JWT tokens issued by an external identity provider. Those tokens can include information about the user and the tenant they belong to, which can be used to restrict access to metrics to only those that belong to the tenant.
Prerequisites
- Identity service that can issue JWT tokens
- Grafana
- VictoriaMetrics single-node or cluster version
- vmgateway
Configure identity service
The identity service must be able to issue JWT tokens with the following vm_access
claim:
{
"vm_access": {
"tenant_id": {
"account_id": 0,
"project_id": 0
}
}
}
See details about all supported options in the vmgateway documentation.
Configuration example for Keycloak
Keycloak is an open source identity service that can be used to issue JWT tokens.
- Log in with admin credentials to your Keycloak instance
- Go to
Clients
->Create
. UseOpenID Connect
asClient Type
. Specifygrafana
asClient ID
. ClickNext
. - Enable
Client authentication
. EnableAuthorization
. ClickNext
. - Add Grafana URL as
Valid Redirect URIs
. For example,http://localhost:3000/
. ClickSave
. - Go to
Clients
->grafana
->Credentials
. Copy the value ofClient secret
. It will be used later in Grafana configuration. - Go to
Clients
->grafana
->Client scopes
. Click atgrafana-dedicated
->Add mapper
. Configure the mapper as followsMapper Type
asUser Attribute
.Name
asvm_access
.Token Claim Name
asvm_access
.User Attribute
asvm_access
.Claim JSON Type
asJSON
. EnableAdd to ID token
andAdd to access token
. ClickSave
.
- Go to
Users
-> select user to configure claims ->Attributes
. Specifyvm_access
asKey
. Specify{"tenant_id" : {"account_id": 0, "project_id": 0 }}
asValue
. ClickSave
.
Configure grafana
To forward JWT tokens Grafana must be configured to use OpenID Connect authentication as follows:
[auth.generic_oauth]
enabled = true
allow_sign_up = true
name = keycloak
client_id = {CLIENT_ID_FROM_IDENTITY_PROVIDER}
client_secret = {SECRET_FROM_IDENTITY_PROVIDER}
scopes = openid profile email
auth_url = http://localhost:3001/realms/{KEYCLOACK_REALM}/protocol/openid-connect/auth
token_url = http://localhost:3001/realms/{KEYCLOACK_REALM}/protocol/openid-connect/token
api_url = http://localhost:3001/realms/{KEYCLOACK_REALM}/protocol/openid-connect/userinfo
After restarting Grafana with the new config you should be able to log in using your identity provider.
Start vmgateway
Multi-tenant access for VictoriaMetrics cluster
Now starting vmgateway with enabled authentication is as simple as adding the -enable.auth=true
flag.
In order to enable multi-tenant access, you must also specify the -clusterMode=true
flag.
./bin/vmgateway -eula \
-enable.auth=true \
-clusterMode=true \
-write.url=http://localhost:8480 \
-read.url=http://localhost:8481
With this configuration vmgateway will use the vm_access
claim from the JWT token to restrict access to metrics.
For example, if the JWT token contains the following vm_access
claim:
{
"vm_access": {
"tenant_id": {
"account_id": 0,
"project_id": 0
}
}
}
Note: in case
project_id
is not specified, default value0
is used.
Then vmgateway will proxy request to an endpoint with the following path:
http://localhost:8480/select/0:0/
This allows to restrict access to specific tenants without having to create separate datasources in Grafana, or manually managing access at another proxy level.
Multi-tenant access for single-node VictoriaMetrics
In order to use multi-tenant access with single-node VictoriaMetrics, you can use token claims such as extra_labels
or extra_filters
filled dynamically by using Identity Provider's user information.
vmgateway uses those claims and enhanced Prometheus querying API
to provide additional filtering capabilities.
For example, the following claims can be used to restrict user access to specific metrics:
{
"vm_access": {
"extra_labels": {
"team": "dev"
},
"extra_filters": ["{env=~\"aws|gcp\",cluster!=\"production\"}"]
}
}
This will add the following query args to the proxied request:
extra_labels=team=dev
extra_filters={env=~"aws|gcp",cluster!="production"}
With this configuration VictoriaMetrics will add the following filters to every query: {team="dev", env=~"aws|gcp", cluster!="production"}
.
So when user will try to query vm_http_requests_total
query will be transformed to vm_http_requests_total{team="dev", env=~"aws|gcp", cluster!="production"}
.
Token signature verification
It is also possible to enable JWT token signature verification at
vmgateway.
To do this by using OpenID Connect discovery endpoint you need to specify the -auth.oidcDiscoveryEndpoints
flag. For example:
./bin/vmgateway -eula \
-enable.auth=true \
-clusterMode=true \
-write.url=http://localhost:8480 \
-read.url=http://localhost:8481
-auth.oidcDiscoveryEndpoints=http://localhost:3001/realms/master/.well-known/openid-configuration
Now vmgateway will print the following message on startup:
2023-03-13T14:45:31.552Z info VictoriaMetrics/app/vmgateway/main.go:154 using 2 keys for JWT token signature verification
That means that vmgateway has successfully fetched the public keys from the OpenID Connect discovery endpoint.
It is also possible to provide the public keys directly via the -auth.publicKeys
flag. See the vmgateway documentation for details.
Use Grafana to query metrics
Create a new Prometheus datasource in Grafana with the following URL http://<vmgateway>:8431
.
URL should point to the vmgateway instance.
You can also use VictoriaMetrics Grafana datasource plugin. See installation instructions here.
Enable Forward OAuth identity
flag.
Now you can use Grafana to query metrics from the specified tenant.
Users with vm_access
claim will be able to query metrics from the specified tenant.