OAuth Integration in Grafana
Introduction
OAuth is an open standard authentication protocol that allows secure authorization without sharing password credentials. For Grafana administrators, integrating OAuth provides several advantages:
- Simplified login experience for users who can authenticate with existing accounts
- Enhanced security through delegated authentication
- Centralized user management when connecting to identity providers
- Seamless Single Sign-On (SSO) across multiple applications
This guide will walk you through understanding OAuth concepts, configuring various OAuth providers in Grafana, and implementing best practices for a secure authentication system.
Understanding OAuth Basics
Before diving into configuration, let's understand the core OAuth concepts as they apply to Grafana.
What is OAuth?
OAuth (Open Authorization) is a token-based authentication protocol that allows third-party services to access resources on behalf of users without exposing their credentials.
The OAuth Flow in Grafana
Key OAuth Terminology
- Client ID: Unique identifier for your Grafana instance
- Client Secret: Secret key known only to Grafana and the OAuth provider
- Authorization Endpoint: URL where users are redirected for authentication
- Token Endpoint: URL where Grafana exchanges the authorization code for tokens
- Scopes: Permissions requested from the OAuth provider
Configuring OAuth in Grafana
Grafana supports various OAuth providers. Let's look at how to configure some of the most common ones.
General Configuration Steps
- Register Grafana as an application with your OAuth provider
- Obtain Client ID and Client Secret
- Configure Grafana's configuration file or environment variables
- Set up role mapping and team synchronization
Modifying Grafana Configuration
OAuth settings are configured in Grafana's main configuration file (grafana.ini
) or using environment variables.
[auth.generic_oauth]
enabled = true
name = OAuth Provider
client_id = YOUR_CLIENT_ID
client_secret = YOUR_CLIENT_SECRET
auth_url = https://your-oauth-provider.com/oauth/authorize
token_url = https://your-oauth-provider.com/oauth/token
api_url = https://your-oauth-provider.com/api/user
scopes = user:email profile
allow_sign_up = true
Popular OAuth Provider Integrations
Let's explore how to set up Grafana with some commonly used OAuth providers.
Google OAuth Integration
[auth.google]
enabled = true
client_id = YOUR_GOOGLE_CLIENT_ID
client_secret = YOUR_GOOGLE_CLIENT_SECRET
scopes = https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
auth_url = https://accounts.google.com/o/oauth2/auth
token_url = https://accounts.google.com/o/oauth2/token
api_url = https://www.googleapis.com/oauth2/v1/userinfo
allowed_domains = yourdomain.com
allow_sign_up = true
For Google OAuth setup:
- Go to Google Cloud Console
- Create a new project
- Enable the Google+ API
- Configure the OAuth consent screen
- Create OAuth credentials (Web application type)
- Add your Grafana URL as an authorized redirect URI:
https://your-grafana-url/login/google
GitHub OAuth Integration
[auth.github]
enabled = true
allow_sign_up = true
client_id = YOUR_GITHUB_CLIENT_ID
client_secret = YOUR_GITHUB_CLIENT_SECRET
scopes = user:email,read:org
auth_url = https://github.com/login/oauth/authorize
token_url = https://github.com/login/oauth/access_token
api_url = https://api.github.com/user
team_ids =
allowed_organizations = your-github-org
For GitHub OAuth setup:
- Go to GitHub Developer Settings
- Create a new OAuth App
- Set the Authorization callback URL to
https://your-grafana-url/login/github
- Register the application to get Client ID and Client Secret
Okta OAuth Integration
[auth.generic_oauth]
name = Okta
enabled = true
allow_sign_up = true
client_id = YOUR_OKTA_CLIENT_ID
client_secret = YOUR_OKTA_CLIENT_SECRET
scopes = openid profile email
auth_url = https://your-domain.okta.com/oauth2/v1/authorize
token_url = https://your-domain.okta.com/oauth2/v1/token
api_url = https://your-domain.okta.com/oauth2/v1/userinfo
For Okta OAuth setup:
- Log in to your Okta admin console
- Go to Applications > Add Application
- Select Web platform
- Set Login redirect URIs to
https://your-grafana-url/login/generic_oauth
- Grant the necessary scopes (openid, profile, email)
Role Mapping and Team Synchronization
One powerful feature of OAuth integration is the ability to map OAuth roles to Grafana roles and synchronize teams.
Role Mapping Configuration
[auth.generic_oauth]
# Other OAuth settings...
role_attribute_path = contains(groups[*], 'admin') && 'Admin' || contains(groups[*], 'editor') && 'Editor' || 'Viewer'
This example uses JMESPath syntax to map roles based on group membership.
Team Synchronization
Grafana can automatically synchronize team memberships based on OAuth groups:
[auth.generic_oauth]
# Other OAuth settings...
team_ids_attribute_path = groups
Troubleshooting OAuth Integration
When implementing OAuth, you might encounter these common issues:
Common Problems and Solutions
-
Redirect URI Mismatch
- Ensure the redirect URI registered with the OAuth provider exactly matches your Grafana URI
- Check for trailing slashes or HTTP vs HTTPS mismatches
-
Scope Issues
- Verify that you've requested all required scopes
- Some providers need specific scopes for email or profile information
-
User Not Created
- Check that
allow_sign_up
is set totrue
- Verify that the user's email domain is in
allowed_domains
if configured
- Check that
-
Debug Logging
Enable debug logging to get more information:
[log]
level = debug
filters = auth.generic_oauth:debug
OAuth Security Best Practices
When implementing OAuth in Grafana, follow these security practices:
- Use HTTPS for all communication
- Rotate client secrets periodically
- Limit redirect URIs to only trusted domains
- Implement proper role mapping to avoid privilege escalation
- Audit authentication logs regularly
- Use state parameters to prevent CSRF attacks
Implementing a Complete OAuth Solution
Let's walk through a complete example of setting up Grafana with Azure AD OAuth.
Step 1: Register Grafana in Azure AD
- Log in to the Azure portal
- Navigate to Azure Active Directory > App registrations
- Create a new registration for Grafana
- Set the redirect URI:
https://your-grafana-url/login/azuread
- Note the Application (client) ID and create a client secret
Step 2: Configure Grafana
[auth.azuread]
name = Azure AD
enabled = true
allow_sign_up = true
client_id = YOUR_AZURE_CLIENT_ID
client_secret = YOUR_AZURE_CLIENT_SECRET
scopes = openid email profile
auth_url = https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/authorize
token_url = https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token
api_url = https://graph.microsoft.com/v1.0/me
allowed_domains = yourdomain.com
allowed_groups = GRAPH_GROUP_ID_1 GRAPH_GROUP_ID_2
role_attribute_path = contains(groups[*], 'ADMIN_GROUP_ID') && 'Admin' || contains(groups[*], 'EDITOR_GROUP_ID') && 'Editor' || 'Viewer'
Step 3: Test and Verify
- Restart Grafana to apply the changes
- Visit your Grafana login page
- Click on "Sign in with Azure AD"
- Complete the authentication flow
- Verify that the user is created with the correct permissions
Summary
OAuth integration in Grafana enhances your authentication system by leveraging existing identity providers, reducing password fatigue, and centralizing user management. Key takeaways include:
- OAuth provides a secure, token-based authentication mechanism
- Grafana supports multiple OAuth providers including Google, GitHub, Okta, and Azure AD
- Role mapping and team synchronization enable automated access control
- Proper configuration requires attention to redirect URIs, scopes, and security best practices
By implementing OAuth, you can create a more secure and user-friendly authentication experience while simplifying administration tasks.
Additional Resources
- Official Grafana OAuth Documentation
- JMESPath Documentation for role mapping expressions
- OAuth 2.0 Specification
Practice Exercises
- Configure Google OAuth for your Grafana instance
- Set up role mapping to assign Admin roles to members of a specific group
- Implement team synchronization based on OAuth groups
- Enable debug logging and analyze the authentication flow
- Audit your OAuth configuration for security best practices
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)