Skip to content

Commit

Permalink
Support certificate-based authentication for Azure
Browse files Browse the repository at this point in the history
Support certificate-based authentication for Azure

Fixes #6735

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
  • Loading branch information
ywk253100 committed Mar 21, 2024
1 parent 6ec1701 commit 4c95edd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/7549-ywk253100
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support certificate-based authentication for Azure
21 changes: 13 additions & 8 deletions pkg/util/azure/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type configCredentialOptions struct {
AdditionallyAllowedTenants []string
}

// newConfigCredential works same as the azidentity.EnvironmentCredential but reads the credentials from a map
// newConfigCredential works similar as the azidentity.EnvironmentCredential but reads the credentials from a map
// rather than environment variables. This is required for Velero to run B/R concurrently
// https://github.com/Azure/azure-sdk-for-go/blob/sdk/azidentity/v1.3.0/sdk/azidentity/environment_credential.go#L80
func newConfigCredential(creds map[string]string, options configCredentialOptions) (azcore.TokenCredential, error) {
Expand All @@ -102,19 +102,24 @@ func newConfigCredential(creds map[string]string, options configCredentialOption
})
}

// certificate
if certPath := creds[CredentialKeyClientCertificatePath]; certPath != "" {
certData, err := os.ReadFile(certPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read certificate file %s", certPath)
// raw certificate or certificate file
if rawCerts, certsPath := []byte(creds[CredentialKeyClientCertificate]), creds[CredentialKeyClientCertificatePath]; len(rawCerts) > 0 || len(certsPath) > 0 {
var err error
// raw certificate isn't specified while certificate path is specified
if len(rawCerts) == 0 {
rawCerts, err = os.ReadFile(certsPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read certificate file %s", certsPath)
}

Check warning on line 113 in pkg/util/azure/credential.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/azure/credential.go#L112-L113

Added lines #L112 - L113 were not covered by tests
}

var password []byte
if v := creds[CredentialKeyClientCertificatePassword]; v != "" {
password = []byte(v)
}
certs, key, err := azidentity.ParseCertificates(certData, password)
certs, key, err := azidentity.ParseCertificates(rawCerts, password)
if err != nil {
return nil, errors.Wrapf(err, "failed to load certificate from %s", certPath)
return nil, errors.Wrap(err, "failed to parse certificate")

Check warning on line 122 in pkg/util/azure/credential.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/azure/credential.go#L122

Added line #L122 was not covered by tests
}
o := &azidentity.ClientCertificateCredentialOptions{
AdditionallyAllowedTenants: options.AdditionallyAllowedTenants,
Expand Down
1 change: 1 addition & 0 deletions pkg/util/azure/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
CredentialKeyTenantID = "AZURE_TENANT_ID" // #nosec
CredentialKeyClientID = "AZURE_CLIENT_ID" // #nosec
CredentialKeyClientSecret = "AZURE_CLIENT_SECRET" // #nosec
CredentialKeyClientCertificate = "AZURE_CLIENT_CERTIFICATE" // #nosec
CredentialKeyClientCertificatePath = "AZURE_CLIENT_CERTIFICATE_PATH" // #nosec
CredentialKeyClientCertificatePassword = "AZURE_CLIENT_CERTIFICATE_PASSWORD" // #nosec
CredentialKeySendCertChain = "AZURE_CLIENT_SEND_CERTIFICATE_CHAIN" // #nosec
Expand Down

0 comments on commit 4c95edd

Please sign in to comment.