Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental: OAuth Token Retriever #702

Merged
merged 11 commits into from
Jun 26, 2023
Merged

Experimental: OAuth Token Retriever #702

merged 11 commits into from
Jun 26, 2023

Conversation

andresmgot
Copy link
Contributor

What this PR does / why we need it:

Will use this as a library for grafana/grafana-plugin-examples#138

Which issue(s) this PR fixes:

Ref grafana/grafana#66245

Special notes for your reviewer:

@andresmgot andresmgot requested a review from a team as a code owner June 12, 2023 14:52
@andresmgot andresmgot requested review from wbrowne and marefr and removed request for a team June 12, 2023 14:52
@marefr
Copy link
Member

marefr commented Jun 15, 2023

Feels like this might be provided by a 3rd party, something like github.com/go-jose/go-jose maybe - we're already using that within Grafana. Or something already available in the SDK perhaps, like https://github.com/grafana/grafana-plugin-sdk-go/blob/main/experimental/authclient/oauth2.go.

@andresmgot
Copy link
Contributor Author

Feels like this might be provided by a 3rd party, something like github.com/go-jose/go-jose maybe - we're already using that within Grafana.

Some Internet search revealed nothing regarding a library that already does this. I checked go-jose but that is related to JSON Web Signatures which I believe is different from what we need here. I tried and signatures generated with that package doesn't work (maybe I am missing something, this goes beyond my cryptographic knowledge). @mgyongyosi do you have any input?

Or something already available in the SDK perhaps, like https://github.com/grafana/grafana-plugin-sdk-go/blob/main/experimental/authclient/oauth2.go.

That does not cover signatures.

@marefr
Copy link
Member

marefr commented Jun 15, 2023

Some Internet search revealed nothing regarding a library that already does this. I checked go-jose but that is related to JSON Web Signatures which I believe is different from what we need here. I tried and signatures generated with that package doesn't work (maybe I am missing something, this goes beyond my cryptographic knowledge). @mgyongyosi do you have any input?

https://github.com/go-jose/go-jose/blob/6531eff483988d182103292d4348e075a6090c9e/jose-util/generator/utils.go#L71 for loading private key. Found some others as well.

That does not cover signatures.

I just noticed usage of a private key below and we're basically looking for creating an OAuth client, right?

PrivateKey: options.PrivateKey,

That seems to call this to parse the private key internally:
https://github.com/golang/oauth2/blob/2323c81c8dba82e8650ed3a24a1a5667e293af38/internal/oauth2.go#L20

Regarding signatures, what about https://pkg.go.dev/gopkg.in/go-jose/go-jose.v2?utm_source=godoc#NewSigner ? Some example usages in Grafana https://github.com/search?q=repo%3Agrafana%2Fgrafana%20NewSigner&type=code

Copy link
Member

@marefr marefr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. My common impression though is that OAuth is standardized and therefore should already be libraries available to do all this. but I might be wrong/misunderstand. This is not a blocker for now since this is in experimental why I approve, but would be good to figure out 3rd party libraries etc.

@@ -29,4 +29,4 @@ Dimensions: 2 Fields by 15 Rows


====== TEST DATA RESPONSE (arrow base64) ======
FRAME=QVJST1cxAAD/////yAEAABAAAAAAAAoADgAMAAsABAAKAAAAFAAAAAAAAAEEAAoADAAAAAgABAAKAAAACAAAALgAAAADAAAATAAAACgAAAAEAAAAwP7//wgAAAAMAAAAAAAAAAAAAAAFAAAAcmVmSWQAAADg/v//CAAAAAwAAAAAAAAAAAAAAAQAAABuYW1lAAAAAAD///8IAAAAUAAAAEQAAAB7InR5cGUiOiJkaXJlY3RvcnktbGlzdGluZyIsInR5cGVWZXJzaW9uIjpbMCwwXSwicGF0aFNlcGFyYXRvciI6Ii8ifQAAAAAEAAAAbWV0YQAAAAACAAAAeAAAAAQAAACi////FAAAADwAAAA8AAAAAAAABTgAAAABAAAABAAAAJD///8IAAAAEAAAAAYAAABzdHJpbmcAAAYAAAB0c3R5cGUAAAAAAACI////CgAAAG1lZGlhLXR5cGUAAAAAEgAYABQAAAATAAwAAAAIAAQAEgAAABQAAABEAAAASAAAAAAAAAVEAAAAAQAAAAwAAAAIAAwACAAEAAgAAAAIAAAAEAAAAAYAAABzdHJpbmcAAAYAAAB0c3R5cGUAAAAAAAAEAAQABAAAAAQAAABuYW1lAAAAAP/////YAAAAFAAAAAAAAAAMABYAFAATAAwABAAMAAAAgAEAAAA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J0eXBlVmVyc2lvbiI6WzAsMF0sInBhdGhTZXBhcmF0b3IiOiIvIn0AAAAABAAAAG1ldGEAAAAAAgAAAHgAAAAEAAAAov///xQAAAA8AAAAPAAAAAAAAAU4AAAAAQAAAAQAAACQ////CAAAABAAAAAGAAAAc3RyaW5nAAAGAAAAdHN0eXBlAAAAAAAAiP///woAAABtZWRpYS10eXBlAAAAABIAGAAUAAAAEwAMAAAACAAEABIAAAAUAAAARAAAAEgAAAAAAAAFRAAAAAEAAAAMAAAACAAMAAgABAAIAAAACAAAABAAAAAGAAAAc3RyaW5nAAAGAAAAdHN0eXBlAAAAAAAABAAEAAQAAAAEAAAAbmFtZQAAAAD4AQAAQVJST1cx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm why did this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everytime there is a new package, this test fails so the snapshot here needs to be updated 🤷

@mgyongyosi
Copy link
Contributor

mgyongyosi commented Jun 15, 2023

Feels like this might be provided by a 3rd party, something like github.com/go-jose/go-jose maybe - we're already using that within Grafana.

Some Internet search revealed nothing regarding a library that already does this. I checked go-jose but that is related to JSON Web Signatures which I believe is different from what we need here. I tried and signatures generated with that package doesn't work (maybe I am missing something, this goes beyond my cryptographic knowledge). @mgyongyosi do you have any input?

Or something already available in the SDK perhaps, like https://github.com/grafana/grafana-plugin-sdk-go/blob/main/experimental/authclient/oauth2.go.

That does not cover signatures.

@andresmgot JSON Web Signature is what we need. The go-jose library can be used to sign a JWT (example: https://github.com/grafana/grafana/blob/21f8dd9599693597524aa55b5f14d76926bc70f5/pkg/services/authn/clients/ext_jwt_test.go#L539)

Apologies for the confusion I caused, I had a custom implementation before I realized that go-jose should contain everything we need.

@CLAassistant
Copy link

CLAassistant commented Jun 21, 2023

CLA assistant check
All committers have signed the CLA.

@andresmgot andresmgot changed the title Experimental: Sign package Experimental: OAuth Token Retriever Jun 21, 2023
@andresmgot
Copy link
Contributor Author

Thanks to the changes from @mgyongyosi I was able to higly simplify the sign code and simply use go-jose so what I did next was to extract more (generic?) logic from the plugin code. Now the sign function is not exposed, only a function to retrieve a token so the interface looks something like:

		tr, err := oauthtokenretriever.New(
			app.httpClient,
			app.grafanaAppURL,
			os.Getenv("GF_PLUGIN_APP_CLIENT_ID"),
			os.Getenv("GF_PLUGIN_APP_CLIENT_SECRET"),
			os.Getenv("GF_PLUGIN_APP_PRIVATE_KEY"),
		)
... 
	token, err := tr.GetExternalServiceToken(req.FormValue("userID"))
... 
	proxyReq.Header.Set("Authorization", token)

WDYT?

@andresmgot andresmgot requested a review from marefr June 21, 2023 15:50
Copy link
Member

@marefr marefr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this is in experimental I approve this. However, added some nits and suggestions.

In general, would be helpful to see how grafana/grafana-plugin-examples#138 would implement this before merge. You can do that in examples repo with go mod edit -replace=github.com/grafana/grafana-plugin-sdk-go=github.com/grafana/grafana-plugin-sdk-go@experimentalSign. If I understand it correctly it's up to each consumer (plugin) to retrieve client id, secret and private key from environment variables - we probably want to hide this from plugin authors within the SDK eventually?

In regards to oauth2 package nits/suggestions. As noted earlier this can happen at a later stage, but if we cannot use those out of the box, would then suggest implement the oauth2.TokenSource interface to make it compatible with the oauth2 package. The oauth2 client would possible be a nice abstraction to hide the retrieval of client id, secret and private key for plugin authors within the SDK.

}

// retrieveSelfToken returns a JWT bearer token for the service account created with the app.
func (t *tokenRetriever) retrieveSelfToken() (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Can't let go of this since exactly this seems to be provided by the oauth2 package: https://cs.opensource.google/go/x/oauth2/+/refs/tags/v0.9.0:clientcredentials/clientcredentials.go

Doesn't need to be fixed now, but seems like what we might want in the future since it provides standard things plus automatic reuse of token until expiration and automatically refreshes when expired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plus automatic reuse of token until expiration and automatically refreshes when expired

For the record, I checked this with @mgyongyosi and for the moment, the token returned does not contain a refresh token so it cannot be automatically refreshed.

}

// retrieveJWTBearerToken returns a JWT bearer token for the given user ID.
func (t *tokenRetriever) retrieveJWTBearerToken(userID string) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Can't let go of this since exactly this seems to be provided by the oauth2 package: https://cs.opensource.google/go/x/oauth2/+/refs/tags/v0.9.0:jwt/jwt.go

Doesn't need to be fixed now, but seems like what we might want in the future since it provides standard things plus automatic reuse of token until expiration and automatically refreshes when expired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case, I was not able to use the jwt package since I couldn't find a way to set the necessary client_id and client_secret (the endpoint returned an error because that info was missing).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another reason: that library only supports RS256 keys, in our case the default is ES256:

https://github.com/golang/oauth2/blob/master/internal/oauth2.go#L20

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this ❤️

experimental/oauthtokenretriever/sign.go Show resolved Hide resolved
experimental/oauthtokenretriever/token.go Outdated Show resolved Hide resolved
@andresmgot
Copy link
Contributor Author

In general, would be helpful to see how grafana/grafana-plugin-examples#138 would implement this before merge. You can do that in examples repo with go mod edit -replace=github.com/grafana/grafana-plugin-sdk-go=github.com/grafana/grafana-plugin-sdk-go@experimentalSign. If I understand it correctly it's up to each consumer (plugin) to retrieve client id, secret and private key from environment variables - we probably want to hide this from plugin authors within the SDK eventually?

yes, I am keeping that up to date with the changes in the rest of repos. See how it's reading env vars:

https://github.com/grafana/grafana-plugin-examples/pull/138/files#diff-41427faf689ff7375a1ea69d019375d414befb4b1e175b3806c39f807a791f39R64

It makes sense to also hide the envvar management to the SDK 🤔

In regards to oauth2 package nits/suggestions. As noted earlier this can happen at a later stage, but if we cannot use those out of the box, would then suggest implement the oauth2.TokenSource interface to make it compatible with the oauth2 package. The oauth2 client would possible be a nice abstraction to hide the retrieval of client id, secret and private key for plugin authors within the SDK.

Let me also check if that works 👍

experimental/oauthtokenretriever/token.go Outdated Show resolved Hide resolved
experimental/oauthtokenretriever/token.go Outdated Show resolved Hide resolved
@andresmgot andresmgot merged commit 6be9232 into main Jun 26, 2023
2 checks passed
@andresmgot andresmgot deleted the experimentalSign branch June 26, 2023 11:23
wbrowne added a commit that referenced this pull request Jun 29, 2023
* add test manage func

* flesh out

* fix linter

* adjust test to use experimental package

* regenerate

* delete dir

* Experimental: OAuth Token Retriever (#702)

Co-authored-by: Mihaly Gyongyosi <mgyongyosi@users.noreply.github.com>

* fix golden file

* update package name

* fix test

* fix golden file

* undo commit

* fix comment

* shutdown for plugin

---------

Co-authored-by: Andres Martinez Gotor <andres.martinez@grafana.com>
Co-authored-by: Mihaly Gyongyosi <mgyongyosi@users.noreply.github.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.

None yet

4 participants