Home > Back-end >  how can find list of secrets and certificate list from Azure vault via golang?
how can find list of secrets and certificate list from Azure vault via golang?

Time:10-21

I'm trying to integrate azure vault using Golang. I used below code for getting key lists:

func (rcv Resources) KeyList(cred Credentials, subscriptionID, resourceGp, vaultName string) ([]string, error) {
    var (
        res []string
        err error
    )
    logger.Info("create client for connecting to the azure as vault client for getting key list.")
    cli := keyvault.NewKeysClient(subscriptionID)

    if cli.Authorizer, err = azureAD.Authorizer(cred.TenantID, cred.ClientID, cred.ClientSecret); err != nil {
        logger.Error("can not get authorizer for resources, err : ", err.Error())
        return nil, err
    }
    keyList, err := cli.List(context.Background(), resourceGp, "key-vault-zap-auth-scan2")
    if err != nil {
        logger.Error("can not get resources list , error : ", err.Error())
        return nil, err
    }
    for _, key := range keyList.Values() {

        res = append(res, *key.Name)
    }
    return res, nil
}

As you know, we have different objects to store in azure key-vault such as: keys, secrets and certificates

I'm not able to get secrets list with SDK, because it does not have secret client.

How can I solve this issue ?

CodePudding user response:

For secrets, there's a separate package called azure-keyvault-secrets.

To list secrets, the method you would want to call is ListSecrets. Here's an example for the same taken from here:

cred, err := azidentity.NewDefaultAzureCredential(nil)
client, err := azsecrets.NewClient("https://my-key-vault.vault.azure.net/", cred, nil)

pager := client.ListSecrets(nil)
for pager.NextPage(context.Background()) {
    resp := pager.PageResponse()
    for _, secret := range resp.Secrets {
        fmt.Printf("Secret ID: %s", *secret.ID)
    }
}
  • Related