-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure.go
57 lines (47 loc) · 1.47 KB
/
azure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"context"
"encoding/json"
"path"
"github.com/Azure/azure-sdk-for-go/profiles/latest/keyvault/keyvault"
"github.com/Azure/go-autorest/autorest/azure/auth"
)
const azureKeyVaultResourceURL = "https://vault.azure.net"
func getAzureSecrets(azureVault string) (string, error) {
var azureSecrets string = ""
azureBasicClient := keyvault.New()
azureAuthorizer, err := auth.NewAuthorizerFromEnvironmentWithResource(azureKeyVaultResourceURL)
if err == nil {
azureBasicClient.Authorizer = azureAuthorizer
}
vaultSecretsList, err := azureBasicClient.GetSecrets(context.Background(), azureVault, nil)
if err != nil {
return azureSecrets, err
}
// retrive all secrets from keyVault
secretsList := make(map[string]string)
for {
// retrive values from keyVault secrets
for _, secret := range vaultSecretsList.Values() {
secretValue, err := azureBasicClient.GetSecret(context.Background(), azureVault, path.Base(*secret.ID), "")
if err == nil {
secretsList[path.Base(*secret.ID)] = *secretValue.Value
}
}
// retrive next page of secrets from keyVault
if err := vaultSecretsList.NextWithContext(context.Background()); err != nil {
return azureSecrets, err
}
// check that all secrets was already retrieved
if len(vaultSecretsList.Values()) == 0 {
break
}
}
// convert secrets map to json
JSON, err := json.Marshal(secretsList)
if err != nil {
return azureSecrets, err
}
azureSecrets = string(JSON)
return azureSecrets, err
}