Skip to content

Commit 0616eeb

Browse files
authored
Support Swift V1 reauth (#47)
Swift V1 tokens expire after some time and gophercloud does not support refreshing them. Fortunately the gophercloud.PrividerClient does allow to refine a ReauthFunc. So this is what is done here.
1 parent d8ec5f8 commit 0616eeb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

openstack.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ func NewOpenstackOSBackendV1Auth(container string, prefix string, caCert string)
186186
Transport: httpTransport,
187187
}
188188

189+
// gophercloud does not support reauth for Swift V1 clients, so we handle this here.
190+
// This is more or less a carbon copy of what gophercloud/openstack/client.go does vor v2.
191+
//
192+
// here we're creating a throw-away client (tac). it's a copy of the user's provider client, but
193+
// with the token and reauth func zeroed out. This should retry authentication only once.
194+
tac := *provider
195+
tac.SetThrowaway(true)
196+
tac.ReauthFunc = nil
197+
tac.SetTokenAndAuthResult(nil)
198+
tao := authOpts
199+
provider.ReauthFunc = func() error {
200+
auth, err := swauth.Auth(&tac, tao).Extract()
201+
if err != nil {
202+
return err
203+
}
204+
// safely copy the token from tac to this ProviderClient
205+
provider.SetToken(auth.Token)
206+
return nil
207+
}
208+
189209
client, err := swauth.NewObjectStorageV1(provider, authOpts)
190210
if err != nil {
191211
panic(fmt.Sprintf("Openstack (object storage): %s", err))

0 commit comments

Comments
 (0)