Example #1
0
func TestGetToken(t *testing.T) {
	// Obtain credentials from the environment.
	ao, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		t.Fatalf("Unable to acquire credentials: %v", err)
	}

	// Trim out unused fields. Skip if we don't have a UserID.
	ao.Username, ao.TenantID, ao.TenantName = "", "", ""
	if ao.UserID == "" {
		t.Logf("Skipping identity v3 tests because no OS_USERID is present.")
		return
	}

	// Create an unauthenticated client.
	provider, err := openstack.NewClient(ao.IdentityEndpoint)
	if err != nil {
		t.Fatalf("Unable to instantiate client: %v", err)
	}

	// Create a service client.
	service := openstack.NewIdentityV3(provider)

	// Use the service to create a token.
	token, err := tokens3.Create(service, ao, nil).Extract()
	if err != nil {
		t.Fatalf("Unable to get token: %v", err)
	}

	t.Logf("Acquired token: %s", token.ID)
}
Example #2
0
func createAuthenticatedClient(t *testing.T) *gophercloud.ServiceClient {
	// Obtain credentials from the environment.
	ao, err := openstack.AuthOptionsFromEnv()
	th.AssertNoErr(t, err)

	// Trim out unused fields.
	ao.Username, ao.TenantID, ao.TenantName = "", "", ""

	if ao.UserID == "" {
		t.Logf("Skipping identity v3 tests because no OS_USERID is present.")
		return nil
	}

	// Create a client and manually authenticate against v3.
	providerClient, err := openstack.NewClient(ao.IdentityEndpoint)
	if err != nil {
		t.Fatalf("Unable to instantiate client: %v", err)
	}

	err = openstack.AuthenticateV3(providerClient, ao)
	if err != nil {
		t.Fatalf("Unable to authenticate against identity v3: %v", err)
	}

	// Create a service client.
	return openstack.NewIdentityV3(providerClient)
}
Example #3
0
// NewKeystoneClient initilizes a keystone client with the provided configuration.
func NewKeystoneClient(config Config) (KeystoneClient, error) {
	opts := config.AuthOptions
	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}
	client := openstack.NewIdentityV3(provider)
	// build a closure for ksClient reauthentication
	client.ReauthFunc = func() error {
		return openstack.AuthenticateV3(client.ProviderClient, opts)
	}
	return &KeystoneClientImpl{client: client, opts: opts, token: nil, monascaURL: nil}, nil
}
func TestGetTokenWhenValid(t *testing.T) {
	// setup
	opts := testConfig.AuthOptions
	provider, err := openstack.AuthenticatedClient(opts)
	assert.NoError(t, err)
	client := openstack.NewIdentityV3(provider)
	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: validToken, monascaURL: nil}

	// do
	token, err := ksClient.GetToken()

	// assert
	assert.NoError(t, err)
	assert.Equal(t, token, testToken)
}
Example #5
0
//NewKeystoneV3Client is an constructor for KeystoneV3Client
func NewKeystoneV3Client(authURL, userName, password, domainName, tenantName string) (KeystoneClient, error) {
	opts := gophercloud.AuthOptions{
		IdentityEndpoint: authURL,
		Username:         userName,
		Password:         password,
		DomainName:       domainName,
		TenantName:       tenantName,
		AllowReauth:      true,
	}

	client, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}
	client.HTTPClient = newHTTPClient()
	return &keystoneV3Client{client: openstack.NewIdentityV3(client)}, nil
}
func TestKeystoneClientReauthenticate(t *testing.T) {
	// setup
	opts := testConfig.AuthOptions
	provider, err := openstack.AuthenticatedClient(opts)
	assert.NoError(t, err)
	client := openstack.NewIdentityV3(provider)
	client.TokenID = "someinvalidtoken"
	client.ReauthFunc = func() error { return openstack.AuthenticateV3(client.ProviderClient, opts) }
	ksClient := &KeystoneClientImpl{client: client, opts: opts, token: validToken, monascaURL: nil}

	// do
	token, err := ksClient.GetToken()

	// assert
	assert.NoError(t, err)
	assert.Equal(t, token, testToken)
}
func trustv3auth(client *gophercloud.ProviderClient, endpoint string, options AuthOptionsExt) error {
	//In case of Trust TokenId would be Provided so we have to populate the value in service client
	//to not throw password error,also if it is not provided it will be empty which maintains
	//the current implementation.
	client.TokenID = options.AuthOptions.TokenID
	// Override the generated service endpoint with the one returned by the version endpoint.
	v3Client := openstack.NewIdentityV3(client)
	if endpoint != "" {
		v3Client.Endpoint = endpoint
	}

	// copy the auth options to a local variable that we can change. `options`
	// needs to stay as-is for reauth purposes
	v3Options := options

	var scope *token3.Scope

	result := token3.Create(v3Client, v3Options, scope)

	token, err := result.ExtractToken()
	if err != nil {
		return err
	}

	catalog, err := result.ExtractServiceCatalog()
	if err != nil {
		return err
	}

	client.TokenID = token.ID

	if options.AuthOptions.AllowReauth {
		client.ReauthFunc = func() error {
			client.TokenID = ""
			return trustv3auth(client, endpoint, options)
		}
	}
	client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) {
		return openstack.V3EndpointURL(catalog, opts)
	}

	return nil
}
Example #8
0
//GetOpenstackClient makes openstack client
func GetOpenstackClient(authURL, userName, password, domainName, tenantName, version string) (*gophercloud.ServiceClient, error) {
	opts := gophercloud.AuthOptions{
		IdentityEndpoint: authURL,
		Username:         userName,
		Password:         password,
		DomainName:       domainName,
		TenantName:       tenantName,
		AllowReauth:      true,
	}

	client, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		return nil, err
	}
	client.HTTPClient = cloud.NewHTTPClient()
	if version == "v2.0" {
		return openstack.NewIdentityV2(client), nil
	} else if version == "v3" {
		return openstack.NewIdentityV3(client), nil
	} else {
		return nil, fmt.Errorf("Unsupported keystone version: %s", version)
	}
}