Zum Hauptinhalt springen

Grafana

Dieser Leitfaden erklärt, wie man die Einzelanmeldung (SSO) zwischen SmartLink und Grafana mithilfe von OpenID Connect konfiguriert. Grafana bietet eine ausgezeichnete native Unterstützung für OAuth2/OpenID Connect.

Voraussetzungen

  • Grafana Version 7.0 oder höher
  • Administratorzugriff auf Grafana
  • Anwendung in SmartLink mit OpenID Connect konfiguriert
  • HTTPS in Grafana konfiguriert (empfohlen)

1. Anwendung erstellen

  1. Melden Sie sich als Administrator bei SmartLink an
  2. Gehen Sie zu AnwendungenHinzufügen
  3. Erstellen Sie eine neue Anwendung:
    • Name: Grafana
    • URL: https://grafana.example.com
    • Beschreibung: Überwachungs- und Observationsplattform
    • Symbol: Wählen Sie das Grafana-Symbol aus

2. OpenID Connect konfigurieren

  1. Im Tab Authentifizierung
  2. Wählen Sie OpenID Connect
  3. Notieren Sie die Informationen:
    • Client-ID: grafana-xxxxxx
    • Client-Geheimnis: secret-xxxxxx
    • Issuer-URL: https://votre-smartlink.link.vaultys.org/api/oidc/[appid]
    • App-ID: [appid] (eindeutige Kennung der Anwendung in SmartLink)

3. Weiterleitungs-URLs

Fügen Sie hinzu in Erlaubte Weiterleitungs-URLs:

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

4. Bereiche und Ansprüche

Erforderliche Bereiche:

  • openid
  • Profil
  • E-Mail
  • Gruppen (für die Zuordnung von Rollen)

Konfiguration in Grafana

1. Konfiguration über INI-Datei

Bearbeiten Sie /etc/grafana/grafana.ini oder /conf/grafana.ini:

#################################### Auth ####################################
[auth]
# Deaktivieren der Kontoerstellung über die Benutzeroberfläche
disable_login_form = false
disable_signout_menu = false
# OAuth-Auto-Login
oauth_auto_login = true
# Automatische Team-Synchronisierung
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

# Teamzuordnung (optional)
team_ids_attribute_path = groups
teams_url = https://votre-smartlink.link.vaultys.org/api/teams

2. Konfiguration über Umgebungsvariablen

Für Docker oder Kubernetes:

version: '3.8'
services:
grafana:
image: grafana/grafana:latest
environment:
# OAuth-Konfiguration
- 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
# Anonyme Authentifizierung deaktivieren
- GF_AUTH_ANONYMOUS_ENABLED=false
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana

volumes:
grafana-data:

3. Helm-Konfiguration (Kubernetes)

# values.yaml für das Grafana-Chart
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'

Rollen- und Berechtigungsverwaltung

Automatische Rollenzuordnung

Grafana unterstützt drei Hauptrollen: Admin, Editor und Viewer.

Grundkonfiguration

# Standardrolle für neue Benutzer
[auth.generic_oauth]
role_attribute_path = contains(groups[*], 'grafana-admins') && 'Admin' || contains(groups[*], 'grafana-editors') && 'Editor' || 'Viewer'

Erweiterte Konfiguration mit JMESPath

# Komplexe Zuordnung basierend auf mehreren Attributen
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'

Organisationssynchronisierung

Für Multi-Tenant-Umgebungen:

[auth.generic_oauth]
# Multi-Organisation-Unterstützung aktivieren
org_attribute_path = organization
# Organisationen zuordnen
org_mapping = SmartLink:1:Editor, Marketing:2:Viewer, Engineering:3:Admin

Team-Synchronisierung

[auth.generic_oauth]
# Automatische Team-Synchronisierung
team_ids_attribute_path = groups
# Format: Gruppe:Organisation:Team
teams_mapping = developers:1:Developers, operations:1:Operations

Automatische Bereitstellung

Dashboards und Datenquellen

Erstellen Sie /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

Konfiguration von Teamordnern

# /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

Konfigurationstest

1. Verbindungstest

  1. Starten Sie Grafana neu:

    systemctl restart grafana-server
    # oder
    docker-compose restart grafana
  2. Gehen Sie zu https://grafana.example.com

  3. Klicken Sie auf Mit SmartLink SSO anmelden

  4. Melden Sie sich bei SmartLink an

  5. Sie sollten bei Grafana angemeldet sein

2. Berechtigungsüberprüfung

# Über die Grafana-API
curl -H "Authorization: Bearer IHR_TOKEN" \
https://grafana.example.com/api/user

# Erwartete Antwort
{
"id": 1,
"email": "user@example.com",
"name": "John Doe",
"login": "johndoe",
"role": "Admin",
"isGrafanaAdmin": false
}

3. Rollentests

  1. Melden Sie sich mit verschiedenen Benutzern an
  2. Überprüfen Sie die Berechtigungen:
    • Admin: Kann Dashboards und Datenquellen erstellen/bearbeiten
    • Editor: Kann Dashboards erstellen/bearbeiten
    • Viewer: Nur Lesen

Fehlerbehebung

Fehler "login.OAuthLogin(missing saved state)"

Problem: Sitzungs-/Cookie-Problem

Lösung:

  1. Überprüfen Sie die Cookie-Konfiguration:
    [security]
    cookie_secure = true
    cookie_samesite = lax
  2. Stellen Sie sicher, dass HTTPS verwendet wird
  3. Überprüfen Sie, ob die Domains übereinstimmen

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

Problem: Der Benutzer hat nicht die richtigen Berechtigungen

Lösung:

  1. Überprüfen Sie die Gruppen des Benutzers in SmartLink
  2. Passen Sie role_attribute_path an
  3. Überprüfen Sie allow_sign_up = true

Gruppen werden nicht korrekt zugeordnet

Problem: Rollen werden nicht korrekt zugewiesen

Lösung:

  1. Debugging aktivieren:
    [log]
    level = debug
    filters = auth.generic_oauth:debug
  2. Überprüfen Sie die Protokolle:
    tail -f /var/log/grafana/grafana.log | grep oauth
  3. Testen Sie JMESPath auf der UserInfo-Antwort

Fehler "Failed to get user info"

Problem: Grafana kann die Benutzerinformationen nicht abrufen

Lösung:

  1. Testen Sie den UserInfo-Endpunkt:
    curl -H "Authorization: Bearer TOKEN" \
    https://votre-smartlink.link.vaultys.org/api/oidc/[appid]/userinfo
  2. Überprüfen Sie api_url in der Konfiguration
  3. Überprüfen Sie die angeforderten Bereiche

Sicherheit

Empfehlungen

  1. HTTPS erforderlich: Immer HTTPS verwenden
  2. PKCE aktiviert: use_pkce = true
  3. Sichere Cookies:
    [security]
    cookie_secure = true
    cookie_httponly = true
  4. Beschränkung anonymer Viewer:
    [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

Schutz vor Brute Force

[security]
# Aktivieren des Schutzes vor Brute Force
disable_brute_force_login_protection = false
# Geheimer Schlüssel zum Signieren von Cookies
secret_key = IHR_GEHEIMER_SCHLÜSSEL

Erweiterte Konfiguration

Multi-Tenancy mit Organisationen

[auth.generic_oauth]
# Automatische Organisationserstellung, falls nicht vorhanden
auto_assign_org = true
auto_assign_org_id = 1
# Organisation zuordnen basierend auf einem Attribut
org_attribute_path = organization
# Organisationenwechsel zulassen
allow_org_switch = true

Integration mit 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"
}
}

Überwachung der Authentifizierung

# Prometheus-Metriken für die Authentifizierung
[metrics]
enabled = true
interval_seconds = 10

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

Ressourcen