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

identity: use AuthOptionsBuilder for v2 auth #2655

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cardoe
Copy link
Contributor

@cardoe cardoe commented Jun 22, 2023

Rather than creating a tokens2.AuthOptions inside the authentication flow for identity v2, allow any structure that implements the tokens2.AuthBuilderOptions interface to be used. The interface is modified to add a CanReauth function like the v3 version has but provides a default implementation. The gophercloud.AuthOptions already implements this interface so there is no change to its API. Lastly add an AuthenticateV2Ext function which will allow an out of tree identity v2 auth mechanism to be implemented. fixes #2651. ref #1330.

@cardoe
Copy link
Contributor Author

cardoe commented Jun 22, 2023

I tried my best not to make an API change here. The unfortunate result is that tokens2.AuthOptionsBuilder does have a function added that mirrors what tokens3.AuthOptionsBuilder has. So anyone that implemented their own would need to add that method but it'd literally be a 1 liner.

I do think that my approach to handling the setting of AllowReauth when creating the ReauthFunc is something that the v3 side of the code could use and then out of tree auth plugins could be used for v3 as well. But I'm happy to do that in a separate PR.

@coveralls
Copy link

coveralls commented Jun 22, 2023

Coverage Status

coverage: 78.277% (-0.04%) from 78.32%
when pulling 894f777 on cardoe:fix-auth-v2
into 280727e on gophercloud:master.

cardoe added a commit to os-pc/cloud-provider-rackspace that referenced this pull request Jun 22, 2023
Taking advantage of gophercloud/gophercloud#2655, simplify how we can
implement Rackspace API key authentication. This allows us to implement
just the Rackspace specific bits out of tree but utilize the rest of
gophercloud for all other authentication mechanisms.
cardoe added a commit to os-pc/cloud-provider-rackspace that referenced this pull request Jun 22, 2023
Taking advantage of gophercloud/gophercloud#2655, simplify how we can
implement Rackspace API key authentication. This allows us to implement
just the Rackspace specific bits out of tree but utilize the rest of
gophercloud for all other authentication mechanisms.
@mandre mandre added the semver:major Breaking change label Jun 30, 2023
@cardoe
Copy link
Contributor Author

cardoe commented Jul 14, 2023

Just wondering if you've had any thoughts on this approach @pierreprinetti ?

@EmilienM
Copy link
Contributor

This LGTM but I'll like to see Pierre's review on this one.

@cardoe
Copy link
Contributor Author

cardoe commented Aug 10, 2023

I noticed @mandre marked this as a major semver bump. If that's the case and I'll have to wait for 2.0 for this then would it make more sense to not add a method and instead change the existing one?

@cardoe
Copy link
Contributor Author

cardoe commented Aug 10, 2023

Hoping you've got some feedback here @pierreprinetti

Copy link
Contributor

@pierreprinetti pierreprinetti left a comment

Choose a reason for hiding this comment

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

This change looks beautifully done. I have just a couple questions inline.

We will have to test the hell out of this PR when it's ready to merge.

openstack/identity/v2/tokens/requests.go Outdated Show resolved Hide resolved
openstack/identity/v2/tokens/requests.go Outdated Show resolved Hide resolved
openstack/client.go Outdated Show resolved Hide resolved
@github-actions github-actions bot added semver:major Breaking change and removed semver:major Breaking change labels Apr 13, 2024
@github-actions github-actions bot added semver:major Breaking change and removed semver:major Breaking change labels Apr 14, 2024
Rather than creating a tokens2.AuthOptions inside the authentication
flow for identity v2, allow any structure that implements the
tokens2.AuthBuilderOptions interface to be used. The interface is
modified to add a CanReauth function like the v3 version has but
provides a default implementation. The gophercloud.AuthOptions already
implements this interface so there is no change to its API. This will
allow an out of tree identity v2 auth mechanism to be implemented,
just like the v3. fixes gophercloud#2651. ref gophercloud#1330.
@cardoe
Copy link
Contributor Author

cardoe commented Apr 14, 2024

I've opened #3030 as well which performs the same changes in a non semver:major way.

@pierreprinetti
Copy link
Contributor

pierreprinetti commented May 15, 2024

Thank you for this, and sorry for the time it's taken to get a review.

I am not convinced that we need a type assertion, and I would only use that as a last resort. For example: how do you prevent an infinite reauth loop with custom types?

One alternative could be embedding and overloading the CanReauth method. Can you please see if this makes sense?

diff --git a/openstack/client.go b/openstack/client.go
index dfe5e53e..07cc6d40 100644
--- a/openstack/client.go
+++ b/openstack/client.go
@@ -113,6 +113,12 @@ func AuthenticateV2(ctx context.Context, client *gophercloud.ProviderClient, opt
 	return v2auth(ctx, client, "", options, eo)
 }
 
+type v2TokenNoReauth struct {
+	tokens2.AuthOptionsBuilder
+}
+
+func (v2TokenNoReauth) CanReauth() bool { return false }
+
 func v2auth(ctx context.Context, client *gophercloud.ProviderClient, endpoint string, options tokens2.AuthOptionsBuilder, eo gophercloud.EndpointOpts) error {
 	v2Client, err := NewIdentityV2(client, eo)
 	if err != nil {
@@ -143,21 +149,8 @@ func v2auth(ctx context.Context, client *gophercloud.ProviderClient, endpoint st
 		tac.SetThrowaway(true)
 		tac.ReauthFunc = nil
 		tac.SetTokenAndAuthResult(nil)
-		var tao tokens2.AuthOptionsBuilder
-		switch ot := options.(type) {
-		case *gophercloud.AuthOptions:
-			o := *ot
-			o.AllowReauth = false
-			tao = &o
-		case *tokens2.AuthOptions:
-			o := *ot
-			o.AllowReauth = false
-			tao = &o
-		default:
-			tao = options
-		}
 		client.ReauthFunc = func(ctx context.Context) error {
-			err := v2auth(ctx, &tac, endpoint, tao, eo)
+			err := v2auth(ctx, &tac, endpoint, &v2TokenNoReauth{options}, eo)
 			if err != nil {
 				return err
 			}
diff --git a/openstack/client_test.go b/openstack/client_test.go
new file mode 100644
index 00000000..68a9fdb0
--- /dev/null
+++ b/openstack/client_test.go
@@ -0,0 +1,5 @@
+package openstack
+
+import tokens2 "github.com/gophercloud/gophercloud/v2/openstack/identity/v2/tokens"
+
+var _ tokens2.AuthOptionsBuilder = &v2TokenNoReauth{}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver:major Breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use Identity v2 AuthOptionsBuilder
5 participants