Skip to content

Commit

Permalink
constructors consistently copy options
Browse files Browse the repository at this point in the history
  • Loading branch information
chlowell committed Oct 21, 2021
1 parent ae53f3f commit 62e772d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 35 deletions.
11 changes: 6 additions & 5 deletions sdk/azidentity/authorization_code_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ func NewAuthorizationCodeCredential(tenantID string, clientID string, authCode s
if !validTenantID(tenantID) {
return nil, &CredentialUnavailableError{credentialType: "Authorization Code Credential", message: tenantIDValidationErr}
}
if options == nil {
options = &AuthorizationCodeCredentialOptions{}
cp := AuthorizationCodeCredentialOptions{}
if options != nil {
cp = *options
}
authorityHost, err := setAuthorityHost(options.AuthorityHost)
authorityHost, err := setAuthorityHost(cp.AuthorityHost)
if err != nil {
return nil, err
}
c, err := newAADIdentityClient(authorityHost, &options.ClientOptions)
c, err := newAADIdentityClient(authorityHost, &cp.ClientOptions)
if err != nil {
return nil, err
}
return &AuthorizationCodeCredential{tenantID: tenantID, clientID: clientID, authCode: authCode, clientSecret: options.ClientSecret, redirectURI: redirectURL, client: c}, nil
return &AuthorizationCodeCredential{tenantID: tenantID, clientID: clientID, authCode: authCode, clientSecret: cp.ClientSecret, redirectURI: redirectURL, client: c}, nil
}

// GetToken obtains a token from Azure Active Directory, using the specified authorization code to authenticate.
Expand Down
15 changes: 8 additions & 7 deletions sdk/azidentity/client_certificate_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,28 @@ func NewClientCertificateCredential(tenantID string, clientID string, certData [
if !validTenantID(tenantID) {
return nil, &CredentialUnavailableError{credentialType: "Client Certificate Credential", message: tenantIDValidationErr}
}
if options == nil {
options = &ClientCertificateCredentialOptions{}
cp := ClientCertificateCredentialOptions{}
if options != nil {
cp = *options
}
cert, err := loadPEMCert(certData, options.Password, options.SendCertificateChain)
cert, err := loadPEMCert(certData, cp.Password, cp.SendCertificateChain)
if err != nil {
cert, err = loadPKCS12Cert(certData, options.Password, options.SendCertificateChain)
cert, err = loadPKCS12Cert(certData, cp.Password, cp.SendCertificateChain)
}
if err != nil {
credErr := &CredentialUnavailableError{credentialType: "Client Certificate Credential", message: err.Error()}
logCredentialError(credErr.credentialType, credErr)
return nil, credErr
}
authorityHost, err := setAuthorityHost(options.AuthorityHost)
authorityHost, err := setAuthorityHost(cp.AuthorityHost)
if err != nil {
return nil, err
}
c, err := newAADIdentityClient(authorityHost, &options.ClientOptions)
c, err := newAADIdentityClient(authorityHost, &cp.ClientOptions)
if err != nil {
return nil, err
}
return &ClientCertificateCredential{tenantID: tenantID, clientID: clientID, cert: cert, sendCertificateChain: options.SendCertificateChain, client: c}, nil
return &ClientCertificateCredential{tenantID: tenantID, clientID: clientID, cert: cert, sendCertificateChain: cp.SendCertificateChain, client: c}, nil
}

// contains decoded cert contents we care about
Expand Down
9 changes: 5 additions & 4 deletions sdk/azidentity/client_secret_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ func NewClientSecretCredential(tenantID string, clientID string, clientSecret st
if !validTenantID(tenantID) {
return nil, &CredentialUnavailableError{credentialType: "Client Secret Credential", message: tenantIDValidationErr}
}
if options == nil {
options = &ClientSecretCredentialOptions{}
cp := ClientSecretCredentialOptions{}
if options != nil {
cp = *options
}
authorityHost, err := setAuthorityHost(options.AuthorityHost)
authorityHost, err := setAuthorityHost(cp.AuthorityHost)
if err != nil {
return nil, err
}
c, err := newAADIdentityClient(authorityHost, &options.ClientOptions)
c, err := newAADIdentityClient(authorityHost, &cp.ClientOptions)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions sdk/azidentity/default_azure_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ func NewDefaultAzureCredential(options *DefaultAzureCredentialOptions) (*Chained
var creds []azcore.TokenCredential
errMsg := ""

if options == nil {
options = &DefaultAzureCredentialOptions{}
cp := DefaultAzureCredentialOptions{}
if options != nil {
cp = *options
}

envCred, err := NewEnvironmentCredential(&EnvironmentCredentialOptions{AuthorityHost: options.AuthorityHost,
ClientOptions: options.ClientOptions,
envCred, err := NewEnvironmentCredential(&EnvironmentCredentialOptions{AuthorityHost: cp.AuthorityHost,
ClientOptions: cp.ClientOptions,
})
if err == nil {
creds = append(creds, envCred)
} else {
errMsg += err.Error()
}

msiCred, err := NewManagedIdentityCredential(&ManagedIdentityCredentialOptions{ClientOptions: options.ClientOptions})
msiCred, err := NewManagedIdentityCredential(&ManagedIdentityCredentialOptions{ClientOptions: cp.ClientOptions})
if err == nil {
creds = append(creds, msiCred)
} else {
Expand Down
11 changes: 6 additions & 5 deletions sdk/azidentity/environment_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ type EnvironmentCredential struct {
// If the expected environment variables are not found at this time, then a CredentialUnavailableError will be returned.
// options: The options used to configure the management of the requests sent to Azure Active Directory.
func NewEnvironmentCredential(options *EnvironmentCredentialOptions) (*EnvironmentCredential, error) {
if options == nil {
options = &EnvironmentCredentialOptions{}
cp := EnvironmentCredentialOptions{}
if options != nil {
cp = *options
}
tenantID := os.Getenv("AZURE_TENANT_ID")
if tenantID == "" {
Expand All @@ -57,7 +58,7 @@ func NewEnvironmentCredential(options *EnvironmentCredentialOptions) (*Environme
}
if clientSecret := os.Getenv("AZURE_CLIENT_SECRET"); clientSecret != "" {
log.Write(EventCredential, "Azure Identity => NewEnvironmentCredential() invoking ClientSecretCredential")
cred, err := NewClientSecretCredential(tenantID, clientID, clientSecret, &ClientSecretCredentialOptions{AuthorityHost: options.AuthorityHost, ClientOptions: options.ClientOptions})
cred, err := NewClientSecretCredential(tenantID, clientID, clientSecret, &ClientSecretCredentialOptions{AuthorityHost: cp.AuthorityHost, ClientOptions: cp.ClientOptions})
if err != nil {
return nil, err
}
Expand All @@ -69,7 +70,7 @@ func NewEnvironmentCredential(options *EnvironmentCredentialOptions) (*Environme
if err != nil {
return nil, &CredentialUnavailableError{credentialType: "Environment Credential", message: "Failed to read certificate file: " + err.Error()}
}
cred, err := NewClientCertificateCredential(tenantID, clientID, certData, &ClientCertificateCredentialOptions{AuthorityHost: options.AuthorityHost, ClientOptions: options.ClientOptions})
cred, err := NewClientCertificateCredential(tenantID, clientID, certData, &ClientCertificateCredentialOptions{AuthorityHost: cp.AuthorityHost, ClientOptions: cp.ClientOptions})
if err != nil {
return nil, err
}
Expand All @@ -78,7 +79,7 @@ func NewEnvironmentCredential(options *EnvironmentCredentialOptions) (*Environme
if username := os.Getenv("AZURE_USERNAME"); username != "" {
if password := os.Getenv("AZURE_PASSWORD"); password != "" {
log.Write(EventCredential, "Azure Identity => NewEnvironmentCredential() invoking UsernamePasswordCredential")
cred, err := NewUsernamePasswordCredential(tenantID, clientID, username, password, &UsernamePasswordCredentialOptions{AuthorityHost: options.AuthorityHost, ClientOptions: options.ClientOptions})
cred, err := NewUsernamePasswordCredential(tenantID, clientID, username, password, &UsernamePasswordCredentialOptions{AuthorityHost: cp.AuthorityHost, ClientOptions: cp.ClientOptions})
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions sdk/azidentity/managed_identity_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ type ManagedIdentityCredential struct {
// options: ManagedIdentityCredentialOptions that configure the pipeline for requests sent to Azure Active Directory.
func NewManagedIdentityCredential(options *ManagedIdentityCredentialOptions) (*ManagedIdentityCredential, error) {
// Create a new Managed Identity Client with default options
if options == nil {
options = &ManagedIdentityCredentialOptions{}
cp := ManagedIdentityCredentialOptions{}
if options != nil {
cp = *options
}
client := newManagedIdentityClient(options)
client := newManagedIdentityClient(&cp)
msiType, err := client.getMSIType()
// If there is an error that means that the code is not running in a Managed Identity environment
if err != nil {
Expand All @@ -89,7 +90,7 @@ func NewManagedIdentityCredential(options *ManagedIdentityCredentialOptions) (*M
// Assign the msiType discovered onto the client
client.msiType = msiType
// check if no clientID is specified then check if it exists in an environment variable
id := options.ID
id := cp.ID
if id == nil {
cID := os.Getenv("AZURE_CLIENT_ID")
if cID != "" {
Expand Down
11 changes: 6 additions & 5 deletions sdk/azidentity/username_password_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// UsernamePasswordCredentialOptions can be used to provide additional information to configure the UsernamePasswordCredential.
// Use these options to modify the default pipeline behavior through the TokenCredentialOptions.
// Use these options to modify the default pipeline behavior through the TokenCredentialcp.
// All zero-value fields will be initialized with their default values.
type UsernamePasswordCredentialOptions struct {
azcore.ClientOptions
Expand Down Expand Up @@ -43,14 +43,15 @@ func NewUsernamePasswordCredential(tenantID string, clientID string, username st
if !validTenantID(tenantID) {
return nil, &CredentialUnavailableError{credentialType: "Username Password Credential", message: tenantIDValidationErr}
}
if options == nil {
options = &UsernamePasswordCredentialOptions{}
cp := UsernamePasswordCredentialOptions{}
if options != nil {
cp = *options
}
authorityHost, err := setAuthorityHost(options.AuthorityHost)
authorityHost, err := setAuthorityHost(cp.AuthorityHost)
if err != nil {
return nil, err
}
c, err := newAADIdentityClient(authorityHost, &options.ClientOptions)
c, err := newAADIdentityClient(authorityHost, &cp.ClientOptions)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 62e772d

Please sign in to comment.