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

Auth: introduce ec2 credentials auth support #1900

Merged
merged 3 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions acceptance/openstack/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import (
"time"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/identity/v3/credentials"
"github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/ec2tokens"
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
th "github.com/gophercloud/gophercloud/testhelper"
)

func TestAuthenticatedClient(t *testing.T) {
Expand Down Expand Up @@ -40,6 +46,68 @@ func TestAuthenticatedClient(t *testing.T) {
}
}

func TestEC2AuthMethod(t *testing.T) {
client, err := clients.NewIdentityV3Client()
th.AssertNoErr(t, err)

ao, err := openstack.AuthOptionsFromEnv()
th.AssertNoErr(t, err)

authOptions := tokens.AuthOptions{
Username: ao.Username,
Password: ao.Password,
DomainName: ao.DomainName,
DomainID: ao.DomainID,
// We need a scope to get the token roles list
Scope: tokens.Scope{
ProjectID: ao.TenantID,
ProjectName: ao.TenantName,
DomainID: ao.DomainID,
DomainName: ao.DomainName,
},
}
token, err := tokens.Create(client, &authOptions).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, token)

user, err := tokens.Get(client, token.ID).ExtractUser()
th.AssertNoErr(t, err)
tools.PrintResource(t, user)

project, err := tokens.Get(client, token.ID).ExtractProject()
th.AssertNoErr(t, err)
tools.PrintResource(t, project)

createOpts := credentials.CreateOpts{
ProjectID: project.ID,
Type: "ec2",
UserID: user.ID,
Blob: "{\"access\":\"181920\",\"secret\":\"secretKey\"}",
}

// Create a credential
credential, err := credentials.Create(client, createOpts).Extract()
th.AssertNoErr(t, err)

// Delete a credential
defer credentials.Delete(client, credential.ID)
tools.PrintResource(t, credential)

newClient, err := clients.NewIdentityV3UnauthenticatedClient()
th.AssertNoErr(t, err)

var ec2AuthOptions tokens.AuthOptionsBuilder
ec2AuthOptions = &ec2tokens.AuthOptions{
Access: "181920",
Secret: "secretKey",
}

err = openstack.AuthenticateV3(newClient.ProviderClient, ec2AuthOptions, gophercloud.EndpointOpts{})
th.AssertNoErr(t, err)

tools.PrintResource(t, newClient.TokenID)
}

func TestReauth(t *testing.T) {
ao, err := openstack.AuthOptionsFromEnv()
if err != nil {
Expand Down
13 changes: 12 additions & 1 deletion openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gophercloud/gophercloud"
tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
"github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/ec2tokens"
tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"github.com/gophercloud/gophercloud/openstack/utils"
)
Expand Down Expand Up @@ -224,7 +225,13 @@ func v3auth(client *gophercloud.ProviderClient, endpoint string, opts tokens3.Au
return err
}
} else {
result := tokens3.Create(v3Client, opts)
var result tokens3.CreateResult
switch opts.(type) {
case *ec2tokens.AuthOptions:
result = ec2tokens.Create(v3Client, opts)
default:
result = tokens3.Create(v3Client, opts)
}

err = client.SetTokenAndAuthResult(result)
if err != nil {
Expand Down Expand Up @@ -255,6 +262,10 @@ func v3auth(client *gophercloud.ProviderClient, endpoint string, opts tokens3.Au
o := *ot
o.AllowReauth = false
tao = &o
case *ec2tokens.AuthOptions:
o := *ot
o.AllowReauth = false
tao = &o
default:
tao = opts
}
Expand Down
41 changes: 41 additions & 0 deletions openstack/identity/v3/extensions/ec2tokens/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Package tokens provides information and interaction with the EC2 token API
resource for the OpenStack Identity service.

For more information, see:
https://docs.openstack.org/api-ref/identity/v2-ext/

Example to Create a Token From an EC2 access and secret keys

var authOptions tokens.AuthOptionsBuilder
authOptions = &ec2tokens.AuthOptions{
Access: "a7f1e798b7c2417cba4a02de97dc3cdc",
Secret: "18f4f6761ada4e3795fa5273c30349b9",
}

token, err := ec2tokens.Create(identityClient, authOptions).ExtractToken()
if err != nil {
panic(err)
}

Example to auth a client using EC2 access and secret keys

client, err := openstack.NewClient("http://localhost:5000/v3")
if err != nil {
panic(err)
}

var authOptions tokens.AuthOptionsBuilder
authOptions = &ec2tokens.AuthOptions{
Access: "a7f1e798b7c2417cba4a02de97dc3cdc",
Secret: "18f4f6761ada4e3795fa5273c30349b9",
AllowReauth: true,
}

err = openstack.AuthenticateV3(client, authOptions, gophercloud.EndpointOpts{})
if err != nil {
panic(err)
}

*/
package ec2tokens
Loading