Skip to content

Commit

Permalink
feat: add default az cred
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahanmmi committed Apr 11, 2024
1 parent b0d74d8 commit 6f2f000
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/vault/azure_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"
"go.uber.org/zap"
Expand All @@ -25,8 +26,15 @@ type AzureVaultSourceConfig struct {
AesKey []byte
}

func newAzureCredential(config AzureVaultConfig) (azcore.TokenCredential, error) {
if config.ClientId == "" || config.ClientSecret == "" {
return azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{TenantID: config.TenantId})
}
return azidentity.NewClientSecretCredential(config.TenantId, config.ClientId, config.ClientSecret, nil)
}

func NewAzureVaultClient(ctx context.Context, logger *zap.Logger, config AzureVaultConfig, secretId string) (*AzureVaultSourceConfig, error) {
cred, err := azidentity.NewClientSecretCredential(config.TenantId, config.ClientId, config.ClientSecret, nil)
cred, err := newAzureCredential(config)
if err != nil {
logger.Error("failed to create Azure Key Vault credential", zap.Error(err))
return nil, err
Expand Down Expand Up @@ -137,7 +145,7 @@ type AzureVaultSecretHandler struct {
}

func NewAzureVaultSecretHandler(logger *zap.Logger, config AzureVaultConfig) (*AzureVaultSecretHandler, error) {
cred, err := azidentity.NewClientSecretCredential(config.TenantId, config.ClientId, config.ClientSecret, nil)
cred, err := newAzureCredential(config)
if err != nil {
logger.Error("failed to create Azure Key Vault credential", zap.Error(err))
return nil, err
Expand Down

0 comments on commit 6f2f000

Please sign in to comment.