Skip to content

Add OAuth2 client credentials grant support - #305

Open
murthy-chelankuri wants to merge 3 commits into
grafana:mainfrom
murthy-chelankuri:oauth2-client-credentials
Open

Add OAuth2 client credentials grant support#305
murthy-chelankuri wants to merge 3 commits into
grafana:mainfrom
murthy-chelankuri:oauth2-client-credentials

Conversation

@murthy-chelankuri

Copy link
Copy Markdown

Summary

  • Recreates the feature proposed in support for prometheus oauth2.0 client credential grafana#119576, which the maintainer closed after the Prometheus datasource was extracted into this repo, asking for it to be resubmitted here instead.
  • Adds a new "OAuth2 Client Credentials" auth method to the datasource config editor (Client ID, Client Secret, Token URL, Scopes), alongside the existing Basic Auth / Forward OAuth Identity / Cross-site credentials options.
  • Adds a backend middleware (pkg/promlib/middleware/oauth2_client_credentials.go) that exchanges those credentials for a bearer token via golang.org/x/oauth2/clientcredentials and attaches it to outgoing Prometheus requests, with automatic caching/refresh.

Test plan

  • go build/go vet/go test ./... pass in both the root module and the pkg/promlib module, including new middleware and settings-validation tests
  • npm run typecheck and npm run lint clean
  • npm run test:ci — full suite (1080 tests) passes, including new config-editor tests
  • mage -v backend build and npm run build frontend build both succeed
  • /validate-plugin packaging check — no new errors/warnings introduced
  • Manual end-to-end verification against a real Grafana + Prometheus (this repo's docker compose dev stack) and a mock OAuth2 client-credentials token endpoint:
    • Correct credentials → datasource health check succeeds, token request observed with grant_type=client_credentials
    • Wrong client secret → health check fails closed with oauth2: "invalid_client", proving the request is genuinely gated on a valid token

grafana/grafana#119576 proposed this for core Grafana before the Prometheus
datasource was extracted into this repo; recreating it here per the
maintainer's request on that PR.

Adds a new "OAuth2 Client Credentials" auth method to the datasource config
editor (client ID/secret, token URL, scopes), and a backend middleware that
exchanges those for a bearer token via golang.org/x/oauth2/clientcredentials
and attaches it to outgoing Prometheus requests, refreshing as needed.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@murthy-chelankuri
murthy-chelankuri requested a review from a team as a code owner August 5, 2026 16:09
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Signed commits report

3 of 3 commits between main and oauth2-client-credentials could not be fully verified:

Commit Author Reason Message
81831f65 Murthy Chelankuri unsigned Add OAuth2 client credentials grant support
d7bb7a66 Murthy Chelankuri unsigned fix(oauth2): refresh client-credentials token on 401, not just local expiry
a64a2979 Murthy Chelankuri unsigned fix(auth): move "No Authentication" to the end of the auth method dropdown

This repository requires all commits to be signed. See GitHub docs on commit signature verification.

@cla-assistant

cla-assistant Bot commented Aug 5, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cla-assistant

cla-assistant Bot commented Aug 5, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@cla-assistant

cla-assistant Bot commented Aug 5, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

murthy-chelankuri and others added 2 commits August 9, 2026 21:53
…expiry

The oauth2.Transport used for the client-credentials grant only refreshed
its cached token once its local expiry timer passed. If the auth server
invalidated a token earlier (revocation, secret rotation, clock skew) or a
token response omitted expires_in, requests kept sending the stale token
and failing indefinitely with no recovery short of a datasource reload.

Replace it with a round tripper that, on a 401, forces a fresh token fetch
and retries the request once with the new token.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…pdown

The Auth component from @grafana/plugin-ui defaults visibleMethods to
[BasicAuth, OAuthForward, NoAuth, ...customMethods], which puts "No
Authentication" ahead of the OAuth2 Client Credentials method instead of
after it. Pass visibleMethods explicitly to keep NoAuth last; any future
auth method needs to be added to this list too.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant