github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/builder/azure/common/vault.go (about)

     1  // Copyright (c) Microsoft Corporation. All rights reserved.
     2  // Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
     3  
     4  // NOTE: vault APIs do not yet exist in the SDK, but once they do this code
     5  // should be removed.
     6  
     7  package common
     8  
     9  import (
    10  	"net/http"
    11  	"strings"
    12  
    13  	"github.com/Azure/go-autorest/autorest"
    14  )
    15  
    16  const (
    17  	AzureVaultApiVersion     = "2015-06-01"
    18  	AzureVaultScope          = "https://vault.azure.net"
    19  	AzureVaultSecretTemplate = "https://{vault-name}.vault.azure.net/secrets/{secret-name}"
    20  )
    21  
    22  type VaultClient struct {
    23  	autorest.Client
    24  }
    25  
    26  type Secret struct {
    27  	ID         *string          `json:"id,omitempty"`
    28  	Value      string           `json:"value"`
    29  	Attributes SecretAttributes `json:"attributes"`
    30  }
    31  
    32  type SecretAttributes struct {
    33  	Enabled bool    `json:"enabled"`
    34  	Created *string `json:"created"`
    35  	Updated *string `json:"updated"`
    36  }
    37  
    38  func (client *VaultClient) GetSecret(vaultName, secretName string) (*Secret, error) {
    39  	p := map[string]interface{}{
    40  		"secret-name": secretName,
    41  	}
    42  	q := map[string]interface{}{
    43  		"api-version": AzureVaultApiVersion,
    44  	}
    45  
    46  	secretURL := strings.Replace(AzureVaultSecretTemplate, "{vault-name}", vaultName, -1)
    47  
    48  	req, err := autorest.Prepare(&http.Request{},
    49  		autorest.AsGet(),
    50  		autorest.WithBaseURL(secretURL),
    51  		autorest.WithPathParameters(p),
    52  		autorest.WithQueryParameters(q))
    53  
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	//resp, err := v.Send(req, http.StatusOK)
    59  	resp, err := autorest.SendWithSender(client, req)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	var secret Secret
    65  
    66  	err = autorest.Respond(
    67  		resp,
    68  		autorest.ByUnmarshallingJSON(&secret))
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return &secret, nil
    74  }