Skip to main content

Grafana

Ce guide vous explique comment configurer l'authentification unique (SSO) entre SmartLink et Grafana en utilisant OpenID Connect. Grafana offre un support natif excellent pour OAuth2/OpenID Connect.

Prérequis

  • Grafana version 7.0 ou supérieure
  • Accès administrateur à Grafana
  • Application configurée dans SmartLink avec OpenID Connect
  • HTTPS configuré sur Grafana (recommandé)

1. Créer l'application

  1. Connectez-vous à SmartLink en tant qu'administrateur
  2. Allez dans ApplicationsAjouter
  3. Créez une nouvelle application :
    • Nom : Grafana
    • URL : https://grafana.example.com
    • Description : Plateforme de monitoring et observabilité
    • Icône : Choisissez l'icône Grafana

2. Configurer OpenID Connect

  1. Dans l'onglet Authentification
  2. Sélectionnez OpenID Connect
  3. Notez les informations :
    • Client ID : grafana-xxxxxx
    • Client Secret : secret-xxxxxx
    • Issuer URL : https://votre-smartlink.link.vaultys.org/api/oidc/[appid]
    • App ID : [appid] (identifiant unique de l'application dans SmartLink)

3. URLs de redirection

Ajoutez dans URLs de redirection autorisées :

https://grafana.example.com/login/generic_oauth

4. Scopes et claims

Scopes requis :

  • openid
  • profile
  • email
  • groups (pour le mapping des rôles)

Configuration dans Grafana

1. Configuration via fichier INI

Éditez /etc/grafana/grafana.ini ou /conf/grafana.ini :

#################################### Auth ####################################
[auth]
# Désactiver la création de compte via l'interface
disable_login_form = false
disable_signout_menu = false
# OAuth auto login
oauth_auto_login = true
# Synchronisation automatique des équipes
oauth_allow_insecure_email_lookup = false

#################################### Generic OAuth ##########################
[auth.generic_oauth]
enabled = true
name = SmartLink SSO
allow_sign_up = true
auto_login = false
client_id = grafana-xxxxxx
client_secret = secret-xxxxxx
scopes = openid profile email groups
email_attribute_name = email
email_attribute_path = email
login_attribute_path = email
name_attribute_path = name
groups_attribute_path = groups
role_attribute_path = contains(groups[*], 'grafana-admins') && 'Admin' || contains(groups[*], 'grafana-editors') && 'Editor' || 'Viewer'
role_attribute_strict = false
allow_assign_grafana_admin = true
auth_url = https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/authorize
token_url = https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/token
api_url = https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/userinfo
signout_redirect_url = https://votre-smartlink.link.vaultys.org/logout
use_pkce = true
use_refresh_token = true

# Mapping des équipes (optionnel)
team_ids_attribute_path = groups
teams_url = https://votre-smartlink.link.vaultys.org/api/teams

2. Configuration via variables d'environnement

Pour Docker ou Kubernetes :

version: '3.8'
services:
grafana:
image: grafana/grafana:latest
environment:
# Configuration OAuth
- GF_AUTH_GENERIC_OAUTH_ENABLED=true
- GF_AUTH_GENERIC_OAUTH_NAME=SmartLink SSO
- GF_AUTH_GENERIC_OAUTH_ALLOW_SIGN_UP=true
- GF_AUTH_GENERIC_OAUTH_CLIENT_ID=grafana-xxxxxx
- GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET=secret-xxxxxx
- GF_AUTH_GENERIC_OAUTH_SCOPES=openid profile email groups
- GF_AUTH_GENERIC_OAUTH_EMAIL_ATTRIBUTE_NAME=email
- GF_AUTH_GENERIC_OAUTH_EMAIL_ATTRIBUTE_PATH=email
- GF_AUTH_GENERIC_OAUTH_LOGIN_ATTRIBUTE_PATH=email
- GF_AUTH_GENERIC_OAUTH_NAME_ATTRIBUTE_PATH=name
- GF_AUTH_GENERIC_OAUTH_AUTH_URL=https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/authorize
- GF_AUTH_GENERIC_OAUTH_TOKEN_URL=https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/token
- GF_AUTH_GENERIC_OAUTH_API_URL=https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/userinfo
- GF_AUTH_GENERIC_OAUTH_USE_PKCE=true
# Auto login
- GF_AUTH_OAUTH_AUTO_LOGIN=true
# Désactiver l'authentification anonyme
- GF_AUTH_ANONYMOUS_ENABLED=false
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana

volumes:
grafana-data:

3. Configuration Helm (Kubernetes)

# values.yaml pour le chart Grafana
grafana:
grafana.ini:
auth:
oauth_auto_login: true
auth.generic_oauth:
enabled: true
name: SmartLink SSO
allow_sign_up: true
client_id: grafana-xxxxxx
client_secret: secret-xxxxxx
scopes: openid profile email groups
auth_url: https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/authorize
token_url: https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/token
api_url: https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/userinfo
role_attribute_path: |
contains(groups[*], 'grafana-admins') && 'Admin' ||
contains(groups[*], 'grafana-editors') && 'Editor' ||
'Viewer'

Gestion des rôles et permissions

Mapping automatique des rôles

Grafana supporte trois rôles principaux : Admin, Editor, et Viewer.

Configuration basique

# Rôle par défaut pour les nouveaux utilisateurs
[auth.generic_oauth]
role_attribute_path = contains(groups[*], 'grafana-admins') && 'Admin' || contains(groups[*], 'grafana-editors') && 'Editor' || 'Viewer'

Configuration avancée avec JMESPath

# Mapping complexe basé sur plusieurs attributs
role_attribute_path = |
(contains(groups[*], 'grafana-super-admins') || email == 'admin@example.com') && 'GrafanaAdmin' ||
contains(groups[*], 'grafana-admins') && 'Admin' ||
contains(groups[*], 'grafana-editors') && 'Editor' ||
contains(groups[*], 'grafana-viewers') && 'Viewer' ||
'Viewer'

Synchronisation des organisations

Pour les environnements multi-tenants :

[auth.generic_oauth]
# Activer le support multi-organisation
org_attribute_path = organization
# Mapping des organisations
org_mapping = SmartLink:1:Editor, Marketing:2:Viewer, Engineering:3:Admin

Synchronisation des équipes

[auth.generic_oauth]
# Synchronisation automatique des équipes
team_ids_attribute_path = groups
# Format: groupe:organisation:équipe
teams_mapping = developers:1:Developers, operations:1:Operations

Provisioning automatique

Dashboards et datasources

Créez /etc/grafana/provisioning/datasources/smartlink.yaml :

apiVersion: 1

datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
editable: false

- name: Loki
type: loki
access: proxy
url: http://loki:3100
editable: false

Configuration des dossiers par équipe

# /etc/grafana/provisioning/dashboards/teams.yaml
apiVersion: 1

providers:
- name: 'team-dashboards'
orgId: 1
folder: ''
type: file
disableDeletion: false
updateIntervalSeconds: 10
allowUiUpdates: true
options:
path: /var/lib/grafana/dashboards
foldersFromFilesStructure: true

Test de la configuration

1. Test de connexion

  1. Redémarrez Grafana :

    systemctl restart grafana-server
    # ou
    docker-compose restart grafana
  2. Accédez à https://grafana.example.com

  3. Cliquez sur Sign in with SmartLink SSO

  4. Authentifiez-vous sur SmartLink

  5. Vous devez être connecté à Grafana

2. Vérification des permissions

# Via l'API Grafana
curl -H "Authorization: Bearer YOUR_TOKEN" \
https://grafana.example.com/api/user

# Réponse attendue
{
"id": 1,
"email": "user@example.com",
"name": "John Doe",
"login": "johndoe",
"role": "Admin",
"isGrafanaAdmin": false
}

3. Test des rôles

  1. Connectez-vous avec différents utilisateurs
  2. Vérifiez les permissions :
    • Admin : Peut créer/modifier dashboards et datasources
    • Editor : Peut créer/modifier dashboards
    • Viewer : Lecture seule

Dépannage

Erreur "login.OAuthLogin(missing saved state)"

Problème : Problème de session/cookies

Solution :

  1. Vérifiez la configuration des cookies :
    [security]
    cookie_secure = true
    cookie_samesite = lax
  2. Assurez-vous d'utiliser HTTPS
  3. Vérifiez que les domaines correspondent

Erreur "User not a member of one of the required organizations"

Problème : L'utilisateur n'a pas les bonnes permissions

Solution :

  1. Vérifiez les groupes de l'utilisateur dans SmartLink
  2. Ajustez role_attribute_path
  3. Vérifiez allow_sign_up = true

Les groupes ne sont pas mappés correctement

Problème : Les rôles ne sont pas assignés correctement

Solution :

  1. Activez le debug :
    [log]
    level = debug
    filters = auth.generic_oauth:debug
  2. Vérifiez les logs :
    tail -f /var/log/grafana/grafana.log | grep oauth
  3. Testez le JMESPath sur la réponse UserInfo

Erreur "Failed to get user info"

Problème : Grafana ne peut pas récupérer les informations utilisateur

Solution :

  1. Testez l'endpoint UserInfo :
    curl -H "Authorization: Bearer TOKEN" \
    https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/userinfo
  2. Vérifiez api_url dans la configuration
  3. Vérifiez les scopes demandés

Sécurité

Recommandations

  1. HTTPS obligatoire : Toujours utiliser HTTPS
  2. PKCE activé : use_pkce = true
  3. Cookies sécurisés :
    [security]
    cookie_secure = true
    cookie_httponly = true
  4. Limitation des viewers anonymes :
    [auth.anonymous]
    enabled = false
  5. Audit logs :
    [log]
    mode = file
    level = info
    [log.file]
    log_rotate = true
    max_lines = 1000000
    max_size_shift = 28
    daily_rotate = true
    max_days = 7

Protection contre le brute force

[security]
# Activer le brute force protection
disable_brute_force_login_protection = false
# Secret pour signer les cookies
secret_key = YOUR_SECRET_KEY

Configuration avancée

Multi-tenancy avec organisations

[auth.generic_oauth]
# Créer automatiquement l'organisation si elle n'existe pas
auto_assign_org = true
auto_assign_org_id = 1
# Mapping organisation basé sur un attribut
org_attribute_path = organization
# Permettre le changement d'organisation
allow_org_switch = true

Intégration avec Terraform

resource "grafana_folder" "team_folders" {
for_each = var.teams
title = each.value.name
}

resource "grafana_team" "teams" {
for_each = var.teams
name = each.value.name
members = each.value.members
}

resource "grafana_dashboard_permission" "team_permissions" {
for_each = var.teams
dashboard_id = grafana_folder.team_folders[each.key].id
permissions {
team_id = grafana_team.teams[each.key].id
permission = "Edit"
}
}

Monitoring de l'authentification

# Métriques Prometheus pour l'authentification
[metrics]
enabled = true
interval_seconds = 10

[metrics.graphite]
address = localhost:2003
prefix = prod.grafana.%(instance_name)s.

Ressources