-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement azure vault provider (#14)
Add support for Azure key vault
- Loading branch information
1 parent
94c20bf
commit fdb9a2b
Showing
12 changed files
with
230 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/eladleev/kubeseal-convert/pkg/kubeseal-convert/domain" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var azureKeyVaultCmd = &cobra.Command{ | ||
Use: "akv", | ||
Aliases: []string{"azurekeyvault", "azure-key-vault", "az", "azure"}, | ||
Short: "Convert Azure Key Vault secrets", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
secretVal := domain.SecretValues{ | ||
Data: AzureKeyVault.GetSecrets(args[0]), | ||
Name: ParseStringFlag(cmd, "name"), | ||
Namespace: ParseStringFlag(cmd, "namespace"), | ||
Labels: ParseLabels(cmd), | ||
Annotations: ParseAnnotations(cmd), | ||
} | ||
KubeSeal.BuildSecretFile(secretVal) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(azureKeyVaultCmd) | ||
} |
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,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/eladleev/kubeseal-convert/mocks" | ||
"gotest.tools/assert" | ||
) | ||
|
||
func TestAzureKeyVaultCmd(t *testing.T) { | ||
rootCmd.AddCommand(azureKeyVaultCmd) | ||
|
||
// mock kubeseal | ||
mockKubeSeal := mocks.NewKubeSeal(t) | ||
mockKubeSeal.On("BuildSecretFile", mock.Anything).Return() | ||
KubeSeal = mockKubeSeal | ||
|
||
// mock azurekeyvault | ||
mockAzureKeyVault := mocks.NewAzureKeyVault(t) | ||
mockAzureKeyVault.On("GetSecrets", mock.Anything).Return(map[string]interface{}{ | ||
"key": "value", | ||
}) | ||
AzureKeyVault = mockAzureKeyVault | ||
|
||
//test az command | ||
output, _ := ExecuteCommand(rootCmd, "az", "dev/secret", "--name", "blabla") | ||
assert.Equal(t, "", output) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
62 changes: 62 additions & 0 deletions
62
pkg/kubeseal-convert/handlers/azurekeyvault/azurekeyvault.go
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,62 @@ | ||
package azurekeyvault | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
identity "github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
secrets "github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets" | ||
"github.com/eladleev/kubeseal-convert/pkg/kubeseal-convert/interfaces" | ||
) | ||
|
||
func createClient(vaultName string) *secrets.Client { | ||
// see https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#readme-defaultazurecredential: this allows getting credentials | ||
// via either environment variables, managed identity, or 'az login' | ||
cred, err := identity.NewDefaultAzureCredential(nil) | ||
if err != nil { | ||
log.Fatalf("Failed to obtain a credential needed to login to the azure vault: %v", err) | ||
} | ||
|
||
vaultURI := fmt.Sprintf("https://%s.vault.azure.net", vaultName) | ||
client, err := secrets.NewClient(vaultURI, cred, nil) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to vault '%s': %v", vaultURI, err) | ||
} | ||
return client | ||
} | ||
|
||
// retrieve secret by name with the client | ||
func getSecrets(client *secrets.Client, vaultName string) map[string]interface{} { | ||
mp := make(map[string]interface{}) | ||
|
||
pager := client.NewListSecretsPager(&secrets.ListSecretsOptions{}) | ||
|
||
for pager.More() { | ||
page, err := pager.NextPage(context.TODO()) | ||
if err != nil { | ||
log.Fatalf("Failed to retrieve secrets from vault '%s': %v", vaultName, err) | ||
} | ||
for _, secret := range page.Value { | ||
value, err := client.GetSecret(context.TODO(), secret.ID.Name(), secret.ID.Version(), &secrets.GetSecretOptions{}) | ||
if err != nil { | ||
log.Fatalf("Failed to retrieve secret '%s' from vault '%s': %v", secret.ID.Name(), vaultName, err) | ||
} | ||
mp[secret.ID.Name()] = *value.Value | ||
} | ||
} | ||
|
||
return mp | ||
} | ||
|
||
type AzureKeyVaultImp struct { | ||
} | ||
|
||
func New() interfaces.AzureKeyVault { | ||
return &AzureKeyVaultImp{} | ||
} | ||
|
||
func (*AzureKeyVaultImp) GetSecrets(vaultName string) map[string]interface{} { | ||
cli := createClient(vaultName) | ||
return getSecrets(cli, vaultName) | ||
} |
11 changes: 11 additions & 0 deletions
11
pkg/kubeseal-convert/handlers/azurekeyvault/azurekeyvault_test.go
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,11 @@ | ||
package azurekeyvault | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_getSecret(t *testing.T) { | ||
|
||
// TODO | ||
|
||
} |
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,5 @@ | ||
package interfaces | ||
|
||
type AzureKeyVault interface { | ||
GetSecrets(vaultName string) map[string]interface{} | ||
} |