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

Specifying option.WithScopes is ignored when using option.WithTokenSource #1644

Closed
salrashid123 opened this issue Aug 4, 2022 · 7 comments · Fixed by #1670
Closed

Specifying option.WithScopes is ignored when using option.WithTokenSource #1644

salrashid123 opened this issue Aug 4, 2022 · 7 comments · Fixed by #1670
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@salrashid123
Copy link

If you use both

at the same time as shown below, the scopes settings from the tokensource is used.

The following works for cloudidentity api because the "books" api scope is simply ignored

    configci, err := google.JWTConfigFromJSON(serviceAccountJSON, cloudidentity.CloudPlatformScope, cloudidentity.CloudIdentityGroupsReadonlyScope)

    tsci := configci.TokenSource(ctx)

    cisvc, err := cloudidentity.NewService(ctx, option.WithTokenSource(tsci), option.WithScopes("https://www.googleapis.com/auth/books"))

but if you invert it, you'll see a scopes error since the actual token from the config is used

    configci, err := google.JWTConfigFromJSON(serviceAccountJSON, "https://www.googleapis.com/auth/books")
    tsci := configci.TokenSource(ctx)

    cisvc, err := cloudidentity.NewService(ctx, option.WithTokenSource(tsci), option.WithScopes(cloudidentity.CloudPlatformScope, cloudidentity.CloudIdentityGroupsReadonlyScope))

consider throwing an error if both are set


$ go version
go version go1.17.1 linux/amd64
module main

go 1.17

require (
	golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c
	google.golang.org/api v0.91.0
)
@salrashid123 salrashid123 added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Aug 4, 2022
@codyoss
Copy link
Member

codyoss commented Aug 4, 2022

The end behavior is intentional here, but I agree this could lead to hard to track down bugs. I think we should have a fast fail here and disallow passing both of these options at the same time as whatever is specified in the TS will always win.

@eisandbar
Copy link
Contributor

It can be something analogous to this

if len(ds.Scopes) > 0 && len(ds.Audiences) > 0 {
return errors.New("WithScopes is incompatible with WithAudience")
}

if len(ds.Scopes) > 0 && ds.TokenSource != nil {
    return errors.New("WithScopes is incompatible with WithTokenSource")
}

eisandbar added a commit to eisandbar/google-api-go-client that referenced this issue Aug 16, 2022
If both ds.TokenSource and ds.Scopes are set, then an error will be returned as Scopes will be ignored

Fixes googleapis#1644
@codyoss
Copy link
Member

codyoss commented Aug 17, 2022

I commented this on the linked PR but wanted to make sure the same comment was captured in this issue as well

Although I think is a good change to make, it can't be made just yet. We will first need to audit our 
libraries to ensure we are not passing scopes via the WithScopes client option. I know where are in
clients in this repo and may still have places where this is done in our sister repo as well. Adding this
check would cause people to never be able to pass the TokenSource option right now in these
instances.

I also due worry thinking more about this that there may be libraries built on libraries where these
options are passed today. Once we have a better logging solution in place it may be better to log a
warning in cases like this. I will need to think about this a little more.

@salrashid123
Copy link
Author

fwiw, i'd be happy if its just documented in the links above (no need at the moment to aggressively throw err)

@codyoss
Copy link
Member

codyoss commented Aug 17, 2022

Yeah, I think that may be the best option for now. @eisandbar If you are willing to make that change I think that is a way we can move forward for now.

@eisandbar
Copy link
Contributor

Should I add it to both WithScopes and WithTokenSource, or only WithScopes?

@codyoss
Copy link
Member

codyoss commented Aug 17, 2022

I think just withscopes would be fine -- noting that it has no effect when used with tokensource opt.

eisandbar added a commit to eisandbar/google-api-go-client that referenced this issue Aug 17, 2022
Clarify that when both WithScopes and WithTokenSource are used, the
scope settings will be taken from the token source and the WithScopes
option will be ignored.

Fixes googleapis#1644
eisandbar added a commit to eisandbar/google-api-go-client that referenced this issue Aug 17, 2022
Clarify that when both WithScopes and WithTokenSource are used, the
scope settings will be taken from the token source and the WithScopes
option will be ignored.

Fixes googleapis#1644
gcf-merge-on-green bot pushed a commit that referenced this issue Aug 17, 2022
Clarify that when both WithScopes and WithTokenSource are used, the
scope settings will be taken from the token source and the WithScopes
option will be ignored.

Fixes #1644
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
3 participants