Skip to content

Commit 8ab6409

Browse files
committed
Add configuration for auth
1 parent 1802577 commit 8ab6409

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

crypto/cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func FlagSet() *pflag.FlagSet {
4747
flags.String("crypto.azurekv.url", defs.AzureKeyVault.URL, "The URL of the Azure Key Vault.")
4848
flags.Duration("crypto.azurekv.timeout", defs.AzureKeyVault.Timeout, "Timeout of client calls to Azure Key Vault, in Golang time.Duration string format (e.g. 10s).")
4949
flags.Bool("crypto.azurekv.hsm", defs.AzureKeyVault.UseHSM, fmt.Sprintf("Whether to store the key in a hardware security module (HSM). If true, the Azure Key Vault must be configured for HSM usage. Default: %t", defs.AzureKeyVault.UseHSM))
50+
flags.String("crypto.azurekv.credential.type", defs.AzureKeyVault.Credential.Type, fmt.Sprintf("Credential type to use when authenticating to the Azure Key Vault. Options: %s, %s (see https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/azidentity/README.md for an explanation of the options).", azure.DefaultChainCredentialType, azure.ManagedIdentityCredentialType))
5051
flags.String("crypto.external.address", defs.External.Address, "Address of the external storage service.")
5152
flags.Duration("crypto.external.timeout", defs.External.Timeout, "Time-out when invoking the external storage backend, in Golang time.Duration string format (e.g. 1s).")
5253

crypto/storage/azure/interface.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,24 @@ type Config struct {
3434
// UseHSM specifies whether to store the key in a hardware security module (HSM).
3535
// If true, the Azure Key Vault must be configured for HSM usage.
3636
UseHSM bool `koanf:"hsm"`
37+
// Credential specifies the credential to use for authentication to the Azure Key Vault.
38+
Credential CredentialConfig `koanf:"credential"`
39+
}
40+
41+
// CredentialConfig contains the config options to configure the credential to use for authentication to the Azure Key Vault.
42+
type CredentialConfig struct {
43+
// Type specifies the type of credential to use for authentication to the Azure Key Vault.
44+
Type string `koanf:"type"`
3745
}
3846

3947
// DefaultConfig returns the default configuration for the Azure Key Vault storage backend.
4048
func DefaultConfig() Config {
4149
return Config{
4250
Timeout: 10 * time.Second,
4351
UseHSM: false,
52+
Credential: CredentialConfig{
53+
Type: DefaultChainCredentialType,
54+
},
4455
}
4556
}
4657

crypto/storage/azure/keyvault.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ import (
4040
"time"
4141
)
4242

43+
const (
44+
DefaultChainCredentialType string = "default"
45+
ManagedIdentityCredentialType string = "managed_identity"
46+
)
47+
4348
// New creates a new Azure Key Vault storage backend.
4449
// If useHSM is true, the key type will be azkeys.KeyTypeECHSM, otherwise azkeys.KeyTypeEC.
45-
func New(keyVaultUrl string, timeout time.Duration, useHSM bool) (spi.Storage, error) {
50+
func New(keyVaultUrl string, timeout time.Duration, useHSM bool, credentialType string) (spi.Storage, error) {
4651
if keyVaultUrl == "" {
4752
return nil, errors.New("missing Azure Key Vault URL")
4853
}
49-
credential, err := azidentity.NewManagedIdentityCredential(nil)
54+
credential, err := createCredential(credentialType)
5055
if err != nil {
5156
return nil, err
5257
}
@@ -57,6 +62,17 @@ func New(keyVaultUrl string, timeout time.Duration, useHSM bool) (spi.Storage, e
5762
return &keyvault{client: client, timeOut: timeout, useHSM: useHSM}, nil
5863
}
5964

65+
func createCredential(credentialType string) (azcore.TokenCredential, error) {
66+
switch credentialType {
67+
case DefaultChainCredentialType:
68+
return azidentity.NewDefaultAzureCredential(nil)
69+
case ManagedIdentityCredentialType:
70+
return azidentity.NewManagedIdentityCredential(nil)
71+
default:
72+
return nil, fmt.Errorf("unsupported Azure Key Vault credential type: %s", credentialType)
73+
}
74+
}
75+
6076
// StorageType is the name of this storage type, used in health check reports and configuration.
6177
const StorageType = "azure-keyvault"
6278

@@ -71,7 +87,6 @@ func (a keyvault) Name() string {
7187
}
7288

7389
func (a keyvault) CheckHealth() map[string]core.Health {
74-
7590
return nil
7691
}
7792

crypto/storage/azure/keyvault_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func TestIntegrationTest(t *testing.T) {
297297
os.Setenv("AZURE_CLIENT_ID", "")
298298
os.Setenv("AZURE_CLIENT_SECRET", "")
299299

300-
store, err := New("https://geheim-keyvault.vault.azure.net/", 10*time.Second, false)
300+
store, err := New("https://geheim-keyvault.vault.azure.net/", 10*time.Second, false, DefaultChainCredentialType)
301301
assert.NoError(t, err)
302302

303303
var kid = "did:web:example.com#" + uuid.NewString()

0 commit comments

Comments
 (0)