Skip to content

Add authentication to metrics endpoint #39

Open

Description

@jkroepke

Description

Add authentication to the metrics endpoint. It should be only accessible with an specific user role oder admin account.

Expected Behavior

http://127.0.0.1:8080/auth/realms/master/metrics should be return 503 unless a valid session with specific role logged in.

Actual Behavior

http://127.0.0.1:8080/auth/realms/master/metrics is would accessible.

Activity

  1. pb82 commented on Jul 12, 2019

    @pb82
    Contributor

    @jkroepke having a way to restrict access to the metrics endpoint sounds reasonable. Usually this endpoint is scraped by Prometheus which won't have a valid keycloak session. Wondering what's the best way here, i'll try to give it some thought.

  2. jkroepke commented on Jul 12, 2019

    @jkroepke
    Author

    Some inspiration from here https://github.com/AndreyVMarkelov/jira-prometheus-exporter/

    https://github.com/AndreyVMarkelov/jira-prometheus-exporter/blob/master/src/main/java/ru/andreymarkelov/atlas/plugins/promjiraexporter/servlet/PrometheusExporter.java#L35

    This exporter generates a static key. If you want to scrape the endpoint you must attach the key as get parameter. Otherwise you get an unauthorized messages.

    2 possible ways to store the static key inside keycloak.

  3. danifr commented on Jul 10, 2020

    @danifr
    Contributor

    I am interested in this. Having those keycloak metrics open to everyone does not seem like good idea.

    @jkroepke did you managed to configure it? Thanks

  4. danifr commented on Jul 14, 2020

    @danifr
    Contributor

    As a workaround and since I have an HAproxy in front of my Keycloak servers, I added a http-request deny to my HAproxy frontend configuration:

    frontend https-keycloak
      ...
      balance  roundrobin
      http-request  deny if { path -i -m reg /auth/realms/.+\/metrics } !{ src <IP_PROMETHEUS_SERVER> }
      mode  http
    

    This config seems to do the trick.

  5. giner commented on Sep 2, 2020

    @giner

    One way to restrict metrics endpoint is mentioned under Keycloak admin docs https://www.keycloak.org/docs/11.0/server_admin/#admin-endpoints-and-console where you replace path-prefix('/auth/admin') by
    either path-template('/auth/realms/{realm}/metrics')
    or (path-prefix('/auth/admin') or path-template('/auth/realms/{realm}/metrics'))

  6. jelly commented on Sep 9, 2020

    @jelly

    I've fixed this problem with an nginx location rule and basic auth (ofcourse servering on https)

        location ~ /auth/realms/[a-z]+/metrics  {
            auth_basic "Prometheus exporter";
            auth_basic_user_file /etc/nginx/auth/prometheus;
            proxy_set_header    Host               $host;
            proxy_set_header    X-Real-IP          $remote_addr;
            proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto  $scheme;
            proxy_ssl_verify    off;
            proxy_pass https://localhost:8443;
        }
    
        location / {
            proxy_set_header    Host               $host;
            proxy_set_header    X-Real-IP          $remote_addr;
            proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto  $scheme;
            proxy_ssl_verify    off;
            proxy_pass https://localhost:8443;
        }
    

    Prometheus itself supports base64 authentication.

  7. jkroepke commented on Sep 9, 2020

    @jkroepke
    Author

    I still would like to see an in-tree solution without the need to configure an reverse proxy.

  8. Tim-Schwalbe commented on Oct 2, 2020

    @Tim-Schwalbe

    Here is my NGINX Ingress Config to block requests to /auth/realms/master/metrics

       annotations: 
        kubernetes.io/ingress.class: "nginx"
        kubernetes.io/tls-acme: "true"
        nginx.ingress.kubernetes.io/server-snippet: |
          location /auth/realms/master/metrics {
              return 403;
          }
    
    
  9. Cyben commented on Aug 19, 2021

    @Cyben

    I think it is really important to restrict this endpoint from regular users as well.

    Seems like the best and easiest way is configuring it in the keycloak configuration.

    As @giner mentioned:

    One way to restrict metrics endpoint is mentioned under Keycloak admin docs https://www.keycloak.org/docs/11.0/server_admin/#admin-endpoints-and-console where you replace path-prefix('/auth/admin') by
    either path-template('/auth/realms/{realm}/metrics')
    or (path-prefix('/auth/admin') or path-template('/auth/realms/{realm}/metrics'))

    Maybe just add the jboss cli command in the documentation, at least for now.

    There is also a way to implement everything as and "admin rest api" but it requires some work, so I'm not sure if this is the approach you want to go with.

  10. TDog42 commented on Oct 18, 2022

    @TDog42

    It seems that with KeyCloak 17 there is no way to rextrict endpoints anymore within the server configuration (keycloak/keycloak#12394)

    Maybe just add an option for some basic authentication. This way not the whole internet will be able to sniff out the metrics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions