-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: add azure keyvault based secret store
- Loading branch information
Showing
7 changed files
with
206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package fake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package keyvault | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Azure/azure-sdk-for-go/profiles/latest/keyvault/keyvault" | ||
) | ||
|
||
// SecretClient is a testable interface for making operations call for Azure KeyVault. | ||
type SecretClient interface { | ||
GetSecret(ctx context.Context, vaultBaseURL string, secretName string, secretVersion string) (result keyvault.SecretBundle, err error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package keyvault | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/profiles/latest/keyvault/keyvault" | ||
"github.com/Azure/go-autorest/autorest" | ||
"github.com/Azure/go-autorest/autorest/azure" | ||
"github.com/Azure/go-autorest/autorest/azure/auth" | ||
|
||
secretsapi "kusionstack.io/kusion/pkg/apis/secrets" | ||
"kusionstack.io/kusion/pkg/secrets" | ||
) | ||
|
||
const ( | ||
errMissingProviderSpec = "store spec is missing provider" | ||
errMissingAzureProvider = "invalid provider spec. Missing Azure field in store provider spec" | ||
errMissingTenant = "missing tenantID in store provider spec" | ||
errMissingClientIDSecret = "cannot read clientID/clientSecret from environment variables" | ||
) | ||
|
||
// DefaultFactory should implement the secrets.SecretStoreFactory interface | ||
var _ secrets.SecretStoreFactory = &DefaultFactory{} | ||
|
||
// kvSecretStore should implement the secrets.SecretStore interface | ||
var _ secrets.SecretStore = &kvSecretStore{} | ||
|
||
type DefaultFactory struct{} | ||
|
||
// NewSecretStore constructs an Azure KeyVault based secret store with specific secret store spec. | ||
func (p *DefaultFactory) NewSecretStore(spec secretsapi.SecretStoreSpec) (secrets.SecretStore, error) { | ||
providerSpec := spec.Provider | ||
if providerSpec == nil { | ||
return nil, fmt.Errorf(errMissingProviderSpec) | ||
} | ||
if providerSpec.Azure == nil { | ||
return nil, fmt.Errorf(errMissingAzureProvider) | ||
} | ||
|
||
secretClient, err := getSecretClient(providerSpec.Azure) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &kvSecretStore{secretClient}, nil | ||
} | ||
|
||
func getSecretClient(spec *secretsapi.AzureKVProvider) (SecretClient, error) { | ||
authorizer, err := authorizerForServicePrincipal(spec) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := keyvault.New() | ||
client.Authorizer = authorizer | ||
return client, nil | ||
} | ||
|
||
// authorizerForServicePrincipal returns a service principal based authorizer used by clients to access to Azure. | ||
// By-default it uses credentials from the environment; | ||
// See https://docs.microsoft.com/en-us/go/azure/azure-sdk-go-authorization#use-environment-based-authentication. | ||
func authorizerForServicePrincipal(spec *secretsapi.AzureKVProvider) (autorest.Authorizer, error) { | ||
if spec.TenantID == nil { | ||
return nil, fmt.Errorf(errMissingTenant) | ||
} | ||
|
||
clientID := os.Getenv("AZURE_CLIENT_ID") | ||
clientSecret := os.Getenv("AZURE_CLIENT_SECRET") | ||
if clientID == "" || clientSecret == "" { | ||
return nil, fmt.Errorf(errMissingClientIDSecret) | ||
} | ||
|
||
clientCredentialsConfig := auth.NewClientCredentialsConfig(clientID, clientSecret, *spec.TenantID) | ||
clientCredentialsConfig.Resource = kvResourceForProviderConfig(spec.EnvironmentType) | ||
clientCredentialsConfig.AADEndpoint = adEndpointForEnvironmentType(spec.EnvironmentType) | ||
return clientCredentialsConfig.Authorizer() | ||
} | ||
|
||
func adEndpointForEnvironmentType(t secretsapi.AzureEnvironmentType) string { | ||
switch t { | ||
case secretsapi.AzureEnvironmentPublicCloud: | ||
return azure.PublicCloud.ActiveDirectoryEndpoint | ||
case secretsapi.AzureEnvironmentChinaCloud: | ||
return azure.ChinaCloud.ActiveDirectoryEndpoint | ||
case secretsapi.AzureEnvironmentUSGovernmentCloud: | ||
return azure.USGovernmentCloud.ActiveDirectoryEndpoint | ||
case secretsapi.AzureEnvironmentGermanCloud: | ||
return azure.GermanCloud.ActiveDirectoryEndpoint | ||
default: | ||
return azure.PublicCloud.ActiveDirectoryEndpoint | ||
} | ||
} | ||
|
||
func kvResourceForProviderConfig(t secretsapi.AzureEnvironmentType) string { | ||
var res string | ||
switch t { | ||
case secretsapi.AzureEnvironmentPublicCloud: | ||
res = azure.PublicCloud.KeyVaultEndpoint | ||
case secretsapi.AzureEnvironmentChinaCloud: | ||
res = azure.ChinaCloud.KeyVaultEndpoint | ||
case secretsapi.AzureEnvironmentUSGovernmentCloud: | ||
res = azure.USGovernmentCloud.KeyVaultEndpoint | ||
case secretsapi.AzureEnvironmentGermanCloud: | ||
res = azure.GermanCloud.KeyVaultEndpoint | ||
default: | ||
res = azure.PublicCloud.KeyVaultEndpoint | ||
} | ||
return strings.TrimSuffix(res, "/") | ||
} | ||
|
||
type kvSecretStore struct { | ||
baseClient SecretClient | ||
} | ||
|
||
// GetSecret retrieves ref secret value from Azure KeyVault. | ||
func (k *kvSecretStore) GetSecret(ctx context.Context, ref secretsapi.ExternalSecretRef) ([]byte, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package keyvault |