github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/common/vault.go (about) 1 // NOTE: vault APIs do not yet exist in the SDK, but once they do this code 2 // should be removed. 3 4 package common 5 6 import ( 7 "fmt" 8 "net/http" 9 "net/url" 10 11 "github.com/Azure/go-autorest/autorest" 12 "github.com/Azure/go-autorest/autorest/azure" 13 ) 14 15 const ( 16 AzureVaultApiVersion = "2016-10-01" 17 ) 18 19 type VaultClient struct { 20 autorest.Client 21 keyVaultEndpoint url.URL 22 SubscriptionID string 23 baseURI string 24 } 25 26 func NewVaultClient(keyVaultEndpoint url.URL) VaultClient { 27 return VaultClient{ 28 keyVaultEndpoint: keyVaultEndpoint, 29 } 30 } 31 32 func NewVaultClientWithBaseURI(baseURI, subscriptionID string) VaultClient { 33 return VaultClient{ 34 baseURI: baseURI, 35 SubscriptionID: subscriptionID, 36 } 37 } 38 39 type Secret struct { 40 ID *string `json:"id,omitempty"` 41 Value string `json:"value"` 42 } 43 44 func (client *VaultClient) GetSecret(vaultName, secretName string) (*Secret, error) { 45 p := map[string]interface{}{ 46 "secret-name": autorest.Encode("path", secretName), 47 } 48 q := map[string]interface{}{ 49 "api-version": AzureVaultApiVersion, 50 } 51 52 req, err := autorest.Prepare( 53 &http.Request{}, 54 autorest.AsGet(), 55 autorest.WithBaseURL(client.getVaultUrl(vaultName)), 56 autorest.WithPathParameters("/secrets/{secret-name}", p), 57 autorest.WithQueryParameters(q)) 58 59 if err != nil { 60 return nil, err 61 } 62 63 resp, err := autorest.SendWithSender(client, req) 64 if err != nil { 65 return nil, err 66 } 67 68 if resp.StatusCode != 200 { 69 return nil, fmt.Errorf( 70 "Failed to fetch secret from %s/%s, HTTP status code=%d (%s)", 71 vaultName, 72 secretName, 73 resp.StatusCode, 74 http.StatusText(resp.StatusCode)) 75 } 76 77 var secret Secret 78 79 err = autorest.Respond( 80 resp, 81 autorest.ByUnmarshallingJSON(&secret)) 82 if err != nil { 83 return nil, err 84 } 85 86 return &secret, nil 87 } 88 89 // Delete deletes the specified Azure key vault. 90 // 91 // resourceGroupName is the name of the Resource Group to which the vault belongs. vaultName is the name of the vault 92 // to delete 93 func (client *VaultClient) Delete(resourceGroupName string, vaultName string) (result autorest.Response, err error) { 94 req, err := client.DeletePreparer(resourceGroupName, vaultName) 95 if err != nil { 96 err = autorest.NewErrorWithError(err, "keyvault.VaultsClient", "Delete", nil, "Failure preparing request") 97 return 98 } 99 100 resp, err := client.DeleteSender(req) 101 if err != nil { 102 result.Response = resp 103 err = autorest.NewErrorWithError(err, "keyvault.VaultsClient", "Delete", resp, "Failure sending request") 104 return 105 } 106 107 result, err = client.DeleteResponder(resp) 108 if err != nil { 109 err = autorest.NewErrorWithError(err, "keyvault.VaultsClient", "Delete", resp, "Failure responding to request") 110 } 111 112 return 113 } 114 115 // DeletePreparer prepares the Delete request. 116 func (client *VaultClient) DeletePreparer(resourceGroupName string, vaultName string) (*http.Request, error) { 117 pathParameters := map[string]interface{}{ 118 "resourceGroupName": autorest.Encode("path", resourceGroupName), 119 "SubscriptionID": autorest.Encode("path", client.SubscriptionID), 120 "vaultName": autorest.Encode("path", vaultName), 121 } 122 123 queryParameters := map[string]interface{}{ 124 "api-version": AzureVaultApiVersion, 125 } 126 127 preparer := autorest.CreatePreparer( 128 autorest.AsDelete(), 129 autorest.WithBaseURL(client.baseURI), 130 autorest.WithPathParameters("/subscriptions/{SubscriptionID}/resourceGroups/{resourceGroupName}/providers/Microsoft.KeyVault/vaults/{vaultName}", pathParameters), 131 autorest.WithQueryParameters(queryParameters)) 132 return preparer.Prepare(&http.Request{}) 133 } 134 135 // DeleteSender sends the Delete request. The method will close the 136 // http.Response Body if it receives an error. 137 func (client *VaultClient) DeleteSender(req *http.Request) (*http.Response, error) { 138 return autorest.SendWithSender(client, 139 req, 140 azure.DoPollForAsynchronous(client.PollingDelay)) 141 } 142 143 // DeleteResponder handles the response to the Delete request. The method always 144 // closes the http.Response Body. 145 func (client *VaultClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) { 146 err = autorest.Respond( 147 resp, 148 client.ByInspecting(), 149 azure.WithErrorUnlessStatusCode(http.StatusOK), 150 autorest.ByClosing()) 151 result.Response = resp 152 return 153 } 154 155 func (client *VaultClient) getVaultUrl(vaultName string) string { 156 return fmt.Sprintf("%s://%s.%s/", client.keyVaultEndpoint.Scheme, vaultName, client.keyVaultEndpoint.Host) 157 }