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

Add Azure credentials type in configuration #1555

Merged
merged 3 commits into from
Nov 15, 2023
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
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type AzureConfig struct {
}

type AzureClientOpt struct {
ClientCredentialsType string `config:"type"`
ClientID string `config:"client_id"`
TenantID string `config:"tenant_id"`
ClientSecret string `config:"client_secret"`
Expand All @@ -99,6 +100,13 @@ type AzureClientOpt struct {
ClientCertificatePassword string `config:"client_certificate_password"`
}

const (
AzureClientCredentialsTypeManagedIdentity = "managed_identity"
AzureClientCredentialsTypeSecret = "service_principal_with_client_secret"
AzureClientCredentialsTypeCertificate = "service_principal_with_client_certificate"
AzureClientCredentialsTypeUsernamePassword = "service_principal_with_client_username_and_password"
)

const (
SingleAccount = "single-account"
OrganizationAccount = "organization-account"
Expand Down
17 changes: 11 additions & 6 deletions resources/providers/azurelib/auth/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ type ConfigProvider struct {
}

func (p *ConfigProvider) GetAzureClientConfig(cfg config.AzureConfig) (*AzureFactoryConfig, error) {
switch {
case cfg.Credentials.ClientSecret != "":
switch cfg.Credentials.ClientCredentialsType {
case config.AzureClientCredentialsTypeSecret:
return p.getSecretCredentialsConfig(cfg)
case cfg.Credentials.ClientCertificatePath != "":
case config.AzureClientCredentialsTypeCertificate:
return p.getCertificateCredentialsConfig(cfg)
case cfg.Credentials.ClientUsername != "" || cfg.Credentials.ClientPassword != "":
case config.AzureClientCredentialsTypeUsernamePassword:
if cfg.Credentials.ClientUsername == "" || cfg.Credentials.ClientPassword == "" {
return nil, ErrIncompleteUsernamePassword
}
return p.getUsernamePasswordCredentialsConfig(cfg)
case "", config.AzureClientCredentialsTypeManagedIdentity:
return p.getDefaultCredentialsConfig()
}

return p.getDefaultCredentialsConfig()
return nil, ErrWrongCredentialsType
}

func (p *ConfigProvider) getDefaultCredentialsConfig() (*AzureFactoryConfig, error) {
Expand Down Expand Up @@ -115,4 +117,7 @@ func (p *ConfigProvider) getUsernamePasswordCredentialsConfig(cfg config.AzureCo
}, nil
}

var ErrIncompleteUsernamePassword = errors.New("incomplete username and password credentials")
var (
ErrWrongCredentialsType = errors.New("wrong credentials type")
ErrIncompleteUsernamePassword = errors.New("incomplete username and password credentials")
)
46 changes: 31 additions & 15 deletions resources/providers/azurelib/auth/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,25 @@ func TestConfigProvider_GetAzureClientConfig(t *testing.T) {
Credentials: &azidentity.DefaultAzureCredential{},
},
},
{
name: "Should return a error on unknown client credentials type",
config: config.AzureConfig{
Credentials: config.AzureClientOpt{
ClientCredentialsType: "unknown",
},
},
authProviderInitFn: func(m *MockAzureAuthProviderAPI) {},
want: nil,
wantErr: true,
},
{
name: "Should return a ClientSecretCredential",
config: config.AzureConfig{
Credentials: config.AzureClientOpt{
TenantID: "tenant_a",
ClientID: "client_id",
ClientSecret: "secret",
ClientCredentialsType: config.AzureClientCredentialsTypeSecret,
TenantID: "tenant_a",
ClientID: "client_id",
ClientSecret: "secret",
},
},
authProviderInitFn: func(m *MockAzureAuthProviderAPI) {
Expand All @@ -68,10 +80,11 @@ func TestConfigProvider_GetAzureClientConfig(t *testing.T) {
name: "Should return a UsernamePasswordCredential",
config: config.AzureConfig{
Credentials: config.AzureClientOpt{
TenantID: "tenant_a",
ClientID: "client_id",
ClientUsername: "username",
ClientPassword: "password",
ClientCredentialsType: config.AzureClientCredentialsTypeUsernamePassword,
TenantID: "tenant_a",
ClientID: "client_id",
ClientUsername: "username",
ClientPassword: "password",
},
},
authProviderInitFn: func(m *MockAzureAuthProviderAPI) {
Expand All @@ -89,10 +102,11 @@ func TestConfigProvider_GetAzureClientConfig(t *testing.T) {
name: "Should return error on incomplete Username Password Credential (missing password)",
config: config.AzureConfig{
Credentials: config.AzureClientOpt{
TenantID: "tenant_a",
ClientID: "client_id",
ClientUsername: "username",
ClientPassword: "",
ClientCredentialsType: config.AzureClientCredentialsTypeUsernamePassword,
TenantID: "tenant_a",
ClientID: "client_id",
ClientUsername: "username",
ClientPassword: "",
},
},
authProviderInitFn: func(m *MockAzureAuthProviderAPI) {},
Expand All @@ -103,10 +117,11 @@ func TestConfigProvider_GetAzureClientConfig(t *testing.T) {
name: "Should return error on incomplete Username Password Credential (missing username)",
config: config.AzureConfig{
Credentials: config.AzureClientOpt{
TenantID: "tenant_a",
ClientID: "client_id",
ClientUsername: "",
ClientPassword: "password",
ClientCredentialsType: config.AzureClientCredentialsTypeUsernamePassword,
TenantID: "tenant_a",
ClientID: "client_id",
ClientUsername: "",
ClientPassword: "password",
},
},
authProviderInitFn: func(m *MockAzureAuthProviderAPI) {},
Expand All @@ -117,6 +132,7 @@ func TestConfigProvider_GetAzureClientConfig(t *testing.T) {
name: "Should return a ClientCertificateCredential",
config: config.AzureConfig{
Credentials: config.AzureClientOpt{
ClientCredentialsType: config.AzureClientCredentialsTypeCertificate,
orestisfl marked this conversation as resolved.
Show resolved Hide resolved
TenantID: "tenant_a",
ClientID: "client_id",
ClientCertificatePath: "/path/cert",
Expand Down
Loading