Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
refactor: reduce duplicate function call and simplify aro credential …
Browse files Browse the repository at this point in the history
…method (#37)

* refactor: reduce duplicate function call and simplify aro credential method

Signed-off-by: Dustin Scott <[email protected]>

* refactor: make credential return consistent

Signed-off-by: Dustin Scott <[email protected]>

---------

Signed-off-by: Dustin Scott <[email protected]>
  • Loading branch information
scottd018 authored Oct 26, 2023
1 parent 026d066 commit c1b4cd1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
62 changes: 27 additions & 35 deletions azureopenshift/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package auth

import (
"context"
"errors"
"log"
"strings"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
Expand All @@ -13,8 +11,6 @@ import (
)

const (
credNameAzureCLI = "AroCLICredential"

AzurePublicString = "public"
AzureUSGovernmentString = "usgovernment"

Expand All @@ -31,67 +27,63 @@ type Config struct {
}

type DefaultAroCredential struct {
chain *azidentity.ChainedTokenCredential
chain *azidentity.ChainedTokenCredential
options *azidentity.ClientSecretCredentialOptions
}

func NewDefaultAroCredential(config Config) (*DefaultAroCredential, error) {
var errorMessages []string

options := &azidentity.ClientSecretCredentialOptions{
ClientOptions: GetOptions(config),
// create the credential with the options pointed to the appropriate selected cloud
cred := &DefaultAroCredential{
options: &azidentity.ClientSecretCredentialOptions{
ClientOptions: policy.ClientOptions{
Cloud: getCloud(config),
},
},
}

clientSecretCred, err := azidentity.NewClientSecretCredential(config.TenantId, config.ClientId, config.ClientSecret, options)
clientSecretCred, err := azidentity.NewClientSecretCredential(config.TenantId, config.ClientId, config.ClientSecret, cred.options)
if err != nil {
errorMessages = append(errorMessages, "AroClientSecretCredential: "+err.Error())
return cred, fmt.Errorf("AroClientSecretCredential: %w", err)
}

cliCred, err := azidentity.NewAzureCLICredential(nil)
if err != nil {
errorMessages = append(errorMessages, "AroCLICredential: "+err.Error())
return cred, fmt.Errorf("AroCLICredential: %w", err)
}

creds := []azcore.TokenCredential{clientSecretCred, cliCred}

err = defaultAroCredentialConstructorErrorHandler(len(creds), errorMessages)
chain, err := azidentity.NewChainedTokenCredential([]azcore.TokenCredential{clientSecretCred, cliCred}, nil)
if err != nil {
return nil, err
return cred, err
}

chain, err := azidentity.NewChainedTokenCredential(creds, nil)
if err != nil {
return nil, err
}
return &DefaultAroCredential{chain: chain}, nil
cred.chain = chain

return cred, nil
}

// GetToken requests an access token from Azure Active Directory. This method is called automatically by Azure SDK clients.
func (c *DefaultAroCredential) GetToken(ctx context.Context, opts policy.TokenRequestOptions) (azcore.AccessToken, error) {
return c.chain.GetToken(ctx, opts)
}

func defaultAroCredentialConstructorErrorHandler(numberOfSuccessfulCredentials int, errorMessages []string) (err error) {
errorMessage := strings.Join(errorMessages, "\n\t")

if numberOfSuccessfulCredentials == 0 {
return errors.New(errorMessage)
}

if len(errorMessages) != 0 {
log.Printf("NewDefaultAroCredential failed to initialize some credentials:\n\t%s", errorMessage)
// GetClientOptions returns the options as set on the credential. It is used to pass in consistent options to other providers
// e.g. ARO when creating the individual service requests.
func (c *DefaultAroCredential) GetClientOptions() *policy.ClientOptions {
if c.options == nil {
return nil
}

return nil
return &c.options.ClientOptions
}

func GetOptions(config Config) policy.ClientOptions {
func getCloud(config Config) cloud.Configuration {
switch config.Environment {
// TODO: remove China support for now until ARO supports it.
// case AzureChinaString:
// return cloud.AzureChina
case AzureUSGovernmentString:
return policy.ClientOptions{Cloud: cloud.AzureGovernment}
return cloud.AzureGovernment
default:
return policy.ClientOptions{Cloud: cloud.AzurePublic}
return cloud.AzurePublic
}
}
3 changes: 1 addition & 2 deletions azureopenshift/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func NewClient(stopCtx context.Context, config auth.Config) (*Client, error) {
return nil, err
}

options := &armpolicy.ClientOptions{}
options.ClientOptions = auth.GetOptions(config)
options := &armpolicy.ClientOptions{ClientOptions: *cred.GetClientOptions()}

openshiftClustersClient, err := redhatopenshift.NewOpenShiftClustersClient(config.SubscriptionId, cred, options)
if err != nil {
Expand Down

0 comments on commit c1b4cd1

Please sign in to comment.