-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature][Add] Added Azure support to injector (#2)
* Added Azure support to injector Signed-off-by: iamabhishek-dubey <[email protected]> * Updated README and Makefile with latest information Signed-off-by: iamabhishek-dubey <[email protected]> * Changed version information Signed-off-by: iamabhishek-dubey <[email protected]>
- Loading branch information
1 parent
2341250
commit 8f9ba73
Showing
8 changed files
with
215 additions
and
5 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,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"k8s-secret-injector/pkg/azure" | ||
) | ||
|
||
var ( | ||
azureVaultName string | ||
) | ||
|
||
// azureCmd represente the Azure commands | ||
var azureCmd = &cobra.Command{ | ||
Use: "azure", | ||
Short: "Fetch secrets from Azure Key Vault", | ||
Long: `Fetch secrets from Azure Key Vault`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
cfg := &azure.AzureConfig{ | ||
AzureVaultName: azureVaultName, | ||
} | ||
|
||
secretData := azure.RetrieveSecretFromAzure(*cfg) | ||
processSecrets(secretData, args) | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(azureCmd) | ||
|
||
viper.SetDefault("azure_vault_name", "test-secret") | ||
viper.AutomaticEnv() | ||
|
||
azureCmd.Flags().StringVar(&azureVaultName, "azure-vault-name", viper.GetString("azure_vault_name"), "Name of the azure vault (default: test-secret)") | ||
} |
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,67 @@ | ||
package azure | ||
|
||
import ( | ||
"context" | ||
"github.com/Azure/azure-sdk-for-go/profiles/latest/keyvault/keyvault" | ||
kvauth "github.com/Azure/azure-sdk-for-go/services/keyvault/auth" | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
"path" | ||
"strings" | ||
) | ||
|
||
type AzureConfig struct { | ||
AzureVaultName string | ||
} | ||
|
||
// getAzureVaultsClient will return the azure client interface | ||
func getAzureVaultsClient() keyvault.BaseClient { | ||
vaultsClient := keyvault.New() | ||
authorizer, err := kvauth.NewAuthorizerFromEnvironment() | ||
if err != nil { | ||
log.Errorf("Failed to initialize azure auth %v", err) | ||
} | ||
vaultsClient.Authorizer = authorizer | ||
return vaultsClient | ||
} | ||
|
||
// getSecret will get the value of secret | ||
func getSecret(secname string, cfg AzureConfig) string { | ||
basicClient := getAzureVaultsClient() | ||
secretResp, err := basicClient.GetSecret(context.Background(), "https://"+cfg.AzureVaultName+".vault.azure.net", secname, "") | ||
if err != nil { | ||
log.Errorf("unable to get list of secrets: %v", err) | ||
os.Exit(1) | ||
} | ||
return *secretResp.Value | ||
} | ||
|
||
// getVault returns an existing vault | ||
func getVault(ctx context.Context, cfg AzureConfig) keyvault.SecretListResultPage { | ||
vaultsClient := getAzureVaultsClient() | ||
secretsList, err := vaultsClient.GetSecrets(ctx, "https://"+cfg.AzureVaultName+".vault.azure.net", nil) | ||
if err != nil { | ||
log.Errorf("unable to get list of secrets: %v", err) | ||
os.Exit(1) | ||
} | ||
return secretsList | ||
} | ||
|
||
// RetrieveSecretFromAzure will retrieve the secret from Azure | ||
func RetrieveSecretFromAzure(cfg AzureConfig) map[string]interface{} { | ||
secretData := make(map[string]interface{}) | ||
secretList := getVault(context.Background(), cfg) | ||
for ; secretList.NotDone(); secretList.NextWithContext(context.Background()) { | ||
secWithoutType := make([]string, 1) | ||
for _, secret := range secretList.Values() { | ||
secWithoutType = append(secWithoutType, path.Base(*secret.ID)) | ||
} | ||
for _, wov := range secWithoutType { | ||
if wov != "" { | ||
tempValue := strings.ReplaceAll(wov, "-", "_") | ||
secretData[strings.ToUpper(tempValue)] = getSecret(wov, cfg) | ||
} | ||
} | ||
} | ||
return secretData | ||
} |
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