Auth Proxy Authentication

You can configure Grafana to let a http reverse proxy handling authentication. Popular web servers have a very extensive list of pluggable authentication modules, and any of them can be used with the AuthProxy feature. Below we detail the configuration options for auth proxy.

  1. [auth.proxy]
  2. # Defaults to false, but set to true to enable this feature
  3. enabled = true
  4. # HTTP Header name that will contain the username or email
  5. header_name = X-WEBAUTH-USER
  6. # HTTP Header property, defaults to `username` but can also be `email`
  7. header_property = username
  8. # Set to `true` to enable auto sign up of users who do not exist in Grafana DB. Defaults to `true`.
  9. auto_sign_up = true
  10. # If combined with Grafana LDAP integration define sync interval
  11. ldap_sync_ttl = 60
  12. # Limit where auth proxy requests come from by configuring a list of IP addresses.
  13. # This can be used to prevent users spoofing the X-WEBAUTH-USER header.
  14. # Example `whitelist = 192.168.1.1, 192.168.1.0/24, 2001::23, 2001::0/120`
  15. whitelist =
  16. # Optionally define more headers to sync other user attributes
  17. # Example `headers = Name:X-WEBAUTH-NAME Email:X-WEBAUTH-EMAIL Groups:X-WEBAUTH-GROUPS`
  18. headers =

Interacting with Grafana’s AuthProxy via curl

  1. curl -H "X-WEBAUTH-USER: admin" http://localhost:3000/api/users
  2. [
  3. {
  4. "id":1,
  5. "name":"",
  6. "login":"admin",
  7. "email":"admin@localhost",
  8. "isAdmin":true
  9. }
  10. ]

We can then send a second request to the /api/user method which will return the details of the logged in user. We will use this request to show how Grafana automatically adds the new user we specify to the system. Here we create a new user called “anthony”.

  1. curl -H "X-WEBAUTH-USER: anthony" http://localhost:3000/api/user
  2. {
  3. "email":"anthony",
  4. "name":"",
  5. "login":"anthony",
  6. "theme":"",
  7. "orgId":1,
  8. "isGrafanaAdmin":false
  9. }

Making Apache’s auth work together with Grafana’s AuthProxy

I’ll demonstrate how to use Apache for authenticating users. In this example we use BasicAuth with Apache’s text file based authentication handler, i.e. htpasswd files. However, any available Apache authentication capabilities could be used.

Apache BasicAuth

In this example we use Apache as a reverse proxy in front of Grafana. Apache handles the Authentication of users before forwarding requests to the Grafana backend service.

Apache configuration

  1. <VirtualHost *:80>
  2. ServerAdmin webmaster@authproxy
  3. ServerName authproxy
  4. ErrorLog "logs/authproxy-error_log"
  5. CustomLog "logs/authproxy-access_log" common
  6. <Proxy *>
  7. AuthType Basic
  8. AuthName GrafanaAuthProxy
  9. AuthBasicProvider file
  10. AuthUserFile /etc/apache2/grafana_htpasswd
  11. Require valid-user
  12. RewriteEngine On
  13. RewriteRule .* - [E=PROXY_USER:%{LA-U:REMOTE_USER},NS]
  14. RequestHeader set X-WEBAUTH-USER "%{PROXY_USER}e"
  15. </Proxy>
  16. RequestHeader unset Authorization
  17. ProxyRequests Off
  18. ProxyPass / http://localhost:3000/
  19. ProxyPassReverse / http://localhost:3000/
  20. </VirtualHost>
  • The first 4 lines of the virtualhost configuration are standard, so we won’t go into detail on what they do.

  • We use a configuration block for applying our authentication rules to every proxied request. These rules include requiring basic authentication where user:password credentials are stored in the /etc/apache2/grafana_htpasswd file. This file can be created with the htpasswd command.

    • The next part of the configuration is the tricky part. We use Apache’s rewrite engine to create our X-WEBAUTH-USER header, populated with the authenticated user.

      • RewriteRule .* - [E=PROXY_USER:%{LA-U:REMOTE_USER}, NS]: This line is a little bit of magic. What it does, is for every request use the rewriteEngines look-ahead (LA-U) feature to determine what the REMOTE_USER variable would be set to after processing the request. Then assign the result to the variable PROXY_USER. This is necessary as the REMOTE_USER variable is not available to the RequestHeader function.

      • RequestHeader set X-WEBAUTH-USER “%{PROXY_USER}e”: With the authenticated username now stored in the PROXY_USER variable, we create a new HTTP request header that will be sent to our backend Grafana containing the username.

  • The RequestHeader unset Authorization removes the Authorization header from the HTTP request before it is forwarded to Grafana. This ensures that Grafana does not try to authenticate the user using these credentials (BasicAuth is a supported authentication handler in Grafana).

  • The last 3 lines are then just standard reverse proxy configuration to direct all authenticated requests to our Grafana server running on port 3000.

Full walk through using Docker.

For this example, we use the official Grafana docker image available at Docker Hub

  • Create a file grafana.ini with the following contents
  1. [users]
  2. allow_sign_up = false
  3. auto_assign_org = true
  4. auto_assign_org_role = Editor
  5. [auth.proxy]
  6. enabled = true
  7. header_name = X-WEBAUTH-USER
  8. header_property = username
  9. auto_sign_up = true

Launch the Grafana container, using our custom grafana.ini to replace /etc/grafana/grafana.ini. We don’t expose any ports for this container as it will only be connected to by our Apache container.

  1. docker run -i -v $(pwd)/grafana.ini:/etc/grafana/grafana.ini --name grafana grafana/grafana

Apache Container

For this example we use the official Apache docker image available at Docker Hub

  • Create a file httpd.conf with the following contents
  1. ServerRoot "/usr/local/apache2"
  2. Listen 80
  3. LoadModule authn_file_module modules/mod_authn_file.so
  4. LoadModule authn_core_module modules/mod_authn_core.so
  5. LoadModule authz_host_module modules/mod_authz_host.so
  6. LoadModule authz_user_module modules/mod_authz_user.so
  7. LoadModule authz_core_module modules/mod_authz_core.so
  8. LoadModule auth_basic_module modules/mod_auth_basic.so
  9. LoadModule log_config_module modules/mod_log_config.so
  10. LoadModule env_module modules/mod_env.so
  11. LoadModule headers_module modules/mod_headers.so
  12. LoadModule unixd_module modules/mod_unixd.so
  13. LoadModule rewrite_module modules/mod_rewrite.so
  14. LoadModule proxy_module modules/mod_proxy.so
  15. LoadModule proxy_http_module modules/mod_proxy_http.so
  16. <IfModule unixd_module>
  17. User daemon
  18. Group daemon
  19. </IfModule>
  20. ServerAdmin you@example.com
  21. <Directory />
  22. AllowOverride none
  23. Require all denied
  24. </Directory>
  25. DocumentRoot "/usr/local/apache2/htdocs"
  26. ErrorLog /proc/self/fd/2
  27. LogLevel error
  28. <IfModule log_config_module>
  29. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  30. LogFormat "%h %l %u %t \"%r\" %>s %b" common
  31. <IfModule logio_module>
  32. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
  33. </IfModule>
  34. CustomLog /proc/self/fd/1 common
  35. </IfModule>
  36. <Proxy *>
  37. AuthType Basic
  38. AuthName GrafanaAuthProxy
  39. AuthBasicProvider file
  40. AuthUserFile /tmp/htpasswd
  41. Require valid-user
  42. RewriteEngine On
  43. RewriteRule .* - [E=PROXY_USER:%{LA-U:REMOTE_USER},NS]
  44. RequestHeader set X-WEBAUTH-USER "%{PROXY_USER}e"
  45. </Proxy>
  46. RequestHeader unset Authorization
  47. ProxyRequests Off
  48. ProxyPass / http://grafana:3000/
  49. ProxyPassReverse / http://grafana:3000/
  • Create a htpasswd file. We create a new user anthony with the password password
  1. htpasswd -bc htpasswd anthony password
  • Launch the httpd container using our custom httpd.conf and our htpasswd file. The container will listen on port 80, and we create a link to the grafana container so that this container can resolve the hostname grafana to the grafana container’s ip address.
  1. docker run -i -p 80:80 --link grafana:grafana -v $(pwd)/httpd.conf:/usr/local/apache2/conf/httpd.conf -v $(pwd)/htpasswd:/tmp/htpasswd httpd:2.4

Use grafana.

With our Grafana and Apache containers running, you can now connect to http://localhost/ and log in using the username/password we created in the htpasswd file.