-
Notifications
You must be signed in to change notification settings - Fork 15
migrate from adal to azidentity #83
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,23 +4,31 @@ import ( | |
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault" | ||
| "github.com/Azure/go-autorest/autorest/to" | ||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud" | ||
| "github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets" | ||
| "github.com/libopenstorage/secrets" | ||
| "github.com/portworx/sched-ops/task" | ||
| ) | ||
|
|
||
| const ( | ||
| Name = secrets.TypeAzure | ||
| AzureCloud = "AzurePublicCloud" | ||
| Name = secrets.TypeAzure | ||
| AzureCloud = "AzurePublicCloud" | ||
| AzureGovernment = "AzureUSGovernment" | ||
| AzureChina = "AzureChinaCloud" | ||
| // AzureTenantID for Azure Active Directory | ||
| AzureTenantID = "AZURE_TENANT_ID" | ||
| // AzureClientID of service principal account | ||
| AzureClientID = "AZURE_CLIENT_ID" | ||
| // AzureClientSecret of service principal account | ||
| AzureClientSecret = "AZURE_CLIENT_SECRET" | ||
| // AzureClientCertPath is path of the client certificate | ||
| AzureClientCertPath = "AZURE_CLIENT_CERT_PATH" | ||
| // AzureClientCertPassword is the password of the private key | ||
| AzureClientCertPassword = "AZURE_CIENT_CERT_PASSWORD" | ||
| // AzureEnviornment to connect | ||
| AzureEnviornment = "AZURE_ENVIRONMENT" | ||
| // AzureVaultURI of azure key vault | ||
|
|
@@ -37,6 +45,7 @@ var ( | |
| ErrAzureTenantIDNotSet = errors.New("AZURE_TENANT_ID not set.") | ||
| ErrAzureClientIDNotSet = errors.New("AZURE_CLIENT_ID not set.") | ||
| ErrAzureSecretIDNotSet = errors.New("AZURE_SECRET_ID not set.") | ||
| ErrAzureAuthMedhodNotSet = errors.New("AZURE_SECRET_ID or AZURE_CLIENT_CERT_PATH not set") | ||
| ErrAzureVaultURLNotSet = errors.New("AZURE_VAULT_URL not set.") | ||
| ErrAzureEnvironmentNotset = errors.New("AZURE_ENVIRONMENT not set.") | ||
| ErrAzureConfigMissing = errors.New("AzureConfig is not provided") | ||
|
|
@@ -45,7 +54,7 @@ var ( | |
| ) | ||
|
|
||
| type azureSecrets struct { | ||
| kv keyvault.BaseClient | ||
| kv azsecrets.Client | ||
| baseURL string | ||
| } | ||
|
|
||
|
|
@@ -62,26 +71,34 @@ func New( | |
| return nil, ErrAzureClientIDNotSet | ||
| } | ||
| secretID := getAzureKVParams(secretConfig, AzureClientSecret) | ||
| if secretID == "" { | ||
| return nil, ErrAzureSecretIDNotSet | ||
| } | ||
| envName := getAzureKVParams(secretConfig, AzureEnviornment) | ||
| if envName == "" { | ||
| // we set back to default AzurePublicCloud | ||
| envName = AzureCloud | ||
| } | ||
| clientCertPath := getAzureKVParams(secretConfig, AzureClientCertPath) | ||
| clientCertPassword := getAzureKVParams(secretConfig, AzureClientCertPassword) | ||
|
|
||
| vaultURL := getAzureKVParams(secretConfig, AzureVaultURL) | ||
| if vaultURL == "" { | ||
| return nil, ErrAzureVaultURLNotSet | ||
| } | ||
|
|
||
| client, err := getAzureVaultClient(clientID, secretID, tenantID, envName) | ||
| if err != nil { | ||
| return nil, err | ||
| clientOpts := getAzureClientOptions(secretConfig) | ||
|
|
||
| var client *azsecrets.Client | ||
| var err error | ||
| if secretID != "" { | ||
| client, err = getAzureVaultClient(clientID, secretID, tenantID, vaultURL, clientOpts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } else if clientCertPath != "" { | ||
| client, err = getAzureVaultClientWithCert(clientID, tenantID, vaultURL, clientCertPath, clientCertPassword, clientOpts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } else { | ||
| return nil, ErrAzureAuthMedhodNotSet | ||
| } | ||
|
|
||
| return &azureSecrets{ | ||
| kv: client, | ||
| kv: *client, | ||
| baseURL: vaultURL, | ||
| }, nil | ||
| } | ||
|
|
@@ -98,8 +115,14 @@ func (az *azureSecrets) GetSecret( | |
| } | ||
|
|
||
| t := func() (interface{}, bool, error) { | ||
| secretResp, err := az.kv.GetSecret(ctx, az.baseURL, secretID, "") | ||
| // passing empty version to always get the latest version of the secret. | ||
| secretResp, err := az.kv.GetSecret(ctx, secretID, "", nil) | ||
| if err != nil { | ||
| // don't retry if the secret key was not found | ||
| responseError, ok := err.(*azcore.ResponseError) | ||
| if ok && responseError.StatusCode == 404 { | ||
| return nil, false, secrets.ErrSecretNotFound | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you return this error instead. -
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adityadani Sorry for the delay. I'll update the PR this week. |
||
| } | ||
| return nil, true, err | ||
| } | ||
| return secretResp, false, nil | ||
|
|
@@ -109,7 +132,7 @@ func (az *azureSecrets) GetSecret( | |
| return nil, secrets.NoVersion, err | ||
| } | ||
|
|
||
| secretResp, ok := resp.(keyvault.SecretBundle) | ||
| secretResp, ok := resp.(azsecrets.GetSecretResponse) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Azure now returns secret version. secretResp.ID.Version()
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No other KMS in the library is returning the version currently. So I think for now, we can keep it as it is and revisit this later.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AWS Secrets Manager returns the version.The only reason all providers have not been updated to return the version is lack of bandwidth to check and test if each one of them If we are modifying this provider and you are able to verify its correctness we can modify it |
||
| if !ok || secretResp.Value == nil { | ||
| return nil, secrets.NoVersion, ErrInvalidSecretResp | ||
| } | ||
|
|
@@ -133,7 +156,7 @@ func (az *azureSecrets) PutSecret( | |
| ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) | ||
| defer cancel() | ||
|
|
||
| var secretResp keyvault.SecretBundle | ||
| var secretResp azsecrets.SetSecretResponse | ||
| if secretName == "" { | ||
| return secrets.NoVersion, secrets.ErrEmptySecretId | ||
| } | ||
|
|
@@ -146,10 +169,10 @@ func (az *azureSecrets) PutSecret( | |
| return secrets.NoVersion, err | ||
| } | ||
|
|
||
| valueStr := string(value) | ||
| t := func() (interface{}, bool, error) { | ||
| secretResp, err = az.kv.SetSecret(ctx, az.baseURL, secretName, keyvault.SecretSetParameters{ | ||
| Value: to.StringPtr(string(value)), | ||
| }) | ||
| params := azsecrets.SetSecretParameters{Value: &valueStr} | ||
| secretResp, err = az.kv.SetSecret(ctx, secretName, params, nil) | ||
| if err != nil { | ||
| return nil, true, err | ||
| } | ||
|
|
@@ -169,7 +192,7 @@ func (az *azureSecrets) DeleteSecret( | |
| if secretName == "" { | ||
| return secrets.ErrEmptySecretId | ||
| } | ||
| _, err := az.kv.DeleteSecret(ctx, az.baseURL, secretName) | ||
| _, err := az.kv.DeleteSecret(ctx, secretName, nil) | ||
|
|
||
| return err | ||
| } | ||
|
|
@@ -213,3 +236,17 @@ func init() { | |
| panic(err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func getAzureClientOptions(secretConfig map[string]interface{}) azcore.ClientOptions { | ||
| opts := azcore.ClientOptions{} | ||
| cloudEnv := getAzureKVParams(secretConfig, AzureEnviornment) | ||
| if strings.EqualFold(cloudEnv, AzureGovernment) { | ||
| opts.Cloud = cloud.AzureGovernment | ||
| } else if strings.EqualFold(cloudEnv, AzureChina) { | ||
| opts.Cloud = cloud.AzureChina | ||
| } else if cloudEnv == AzureCloud || cloudEnv == "" { | ||
| opts.Cloud = cloud.AzurePublic | ||
| } | ||
| return opts | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.