-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
helpers.go
212 lines (182 loc) · 5.56 KB
/
helpers.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package client
import (
"context"
"fmt"
"net/url"
"strings"
"sync"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
resourcesClient "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/client"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
var (
keyVaultsCache = map[string]keyVaultDetails{}
keysmith = &sync.RWMutex{}
lock = map[string]*sync.RWMutex{}
)
type keyVaultDetails struct {
keyVaultId string
dataPlaneBaseUri string
resourceGroup string
}
func (c *Client) AddToCache(keyVaultId commonids.KeyVaultId, dataPlaneUri string) {
cacheKey := c.cacheKeyForKeyVault(keyVaultId.VaultName)
keysmith.Lock()
keyVaultsCache[cacheKey] = keyVaultDetails{
keyVaultId: keyVaultId.ID(),
dataPlaneBaseUri: dataPlaneUri,
resourceGroup: keyVaultId.ResourceGroupName,
}
keysmith.Unlock()
}
func (c *Client) BaseUriForKeyVault(ctx context.Context, keyVaultId commonids.KeyVaultId) (*string, error) {
cacheKey := c.cacheKeyForKeyVault(keyVaultId.VaultName)
keysmith.Lock()
if lock[cacheKey] == nil {
lock[cacheKey] = &sync.RWMutex{}
}
keysmith.Unlock()
lock[cacheKey].Lock()
defer lock[cacheKey].Unlock()
if v, ok := keyVaultsCache[cacheKey]; ok {
return &v.dataPlaneBaseUri, nil
}
resp, err := c.VaultsClient.Get(ctx, keyVaultId)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return nil, fmt.Errorf("%s was not found", keyVaultId)
}
return nil, fmt.Errorf("retrieving %s: %+v", keyVaultId, err)
}
vaultUri := ""
if model := resp.Model; model != nil {
if model.Properties.VaultUri != nil {
vaultUri = *model.Properties.VaultUri
}
}
if vaultUri == "" {
return nil, fmt.Errorf("retrieving %s: `properties.VaultUri` was nil", keyVaultId)
}
c.AddToCache(keyVaultId, vaultUri)
return &vaultUri, nil
}
func (c *Client) Exists(ctx context.Context, keyVaultId commonids.KeyVaultId) (bool, error) {
cacheKey := c.cacheKeyForKeyVault(keyVaultId.VaultName)
keysmith.Lock()
if lock[cacheKey] == nil {
lock[cacheKey] = &sync.RWMutex{}
}
keysmith.Unlock()
lock[cacheKey].Lock()
defer lock[cacheKey].Unlock()
if _, ok := keyVaultsCache[cacheKey]; ok {
return true, nil
}
resp, err := c.VaultsClient.Get(ctx, keyVaultId)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return false, nil
}
return false, fmt.Errorf("retrieving %s: %+v", keyVaultId, err)
}
vaultUri := ""
if model := resp.Model; model != nil {
if model.Properties.VaultUri != nil {
vaultUri = *model.Properties.VaultUri
}
}
if vaultUri == "" {
return false, fmt.Errorf("retrieving %s: `properties.VaultUri` was nil", keyVaultId)
}
c.AddToCache(keyVaultId, vaultUri)
return true, nil
}
func (c *Client) KeyVaultIDFromBaseUrl(ctx context.Context, resourcesClient *resourcesClient.Client, keyVaultBaseUrl string) (*string, error) {
keyVaultName, err := c.parseNameFromBaseUrl(keyVaultBaseUrl)
if err != nil {
return nil, err
}
cacheKey := c.cacheKeyForKeyVault(*keyVaultName)
keysmith.Lock()
if lock[cacheKey] == nil {
lock[cacheKey] = &sync.RWMutex{}
}
keysmith.Unlock()
lock[cacheKey].Lock()
defer lock[cacheKey].Unlock()
if v, ok := keyVaultsCache[cacheKey]; ok {
return &v.keyVaultId, nil
}
filter := fmt.Sprintf("resourceType eq 'Microsoft.KeyVault/vaults' and name eq '%s'", *keyVaultName)
result, err := resourcesClient.ResourcesClient.List(ctx, filter, "", utils.Int32(5))
if err != nil {
return nil, fmt.Errorf("listing resources matching %q: %+v", filter, err)
}
for result.NotDone() {
for _, v := range result.Values() {
if v.ID == nil {
continue
}
id, err := commonids.ParseKeyVaultID(*v.ID)
if err != nil {
return nil, fmt.Errorf("parsing %q: %+v", *v.ID, err)
}
if !strings.EqualFold(id.VaultName, *keyVaultName) {
continue
}
resp, err := c.VaultsClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}
vaultUri := ""
if model := resp.Model; model != nil {
if model.Properties.VaultUri != nil {
vaultUri = *model.Properties.VaultUri
}
}
if vaultUri == "" {
return nil, fmt.Errorf("retrieving %s: `properties.VaultUri` was nil", id)
}
c.AddToCache(*id, vaultUri)
return utils.String(id.ID()), nil
}
if err := result.NextWithContext(ctx); err != nil {
return nil, fmt.Errorf("iterating over results: %+v", err)
}
}
// we haven't found it, but Data Sources and Resources need to handle this error separately
return nil, nil
}
func (c *Client) Purge(keyVaultId commonids.KeyVaultId) {
cacheKey := c.cacheKeyForKeyVault(keyVaultId.VaultName)
keysmith.Lock()
if lock[cacheKey] == nil {
lock[cacheKey] = &sync.RWMutex{}
}
keysmith.Unlock()
lock[cacheKey].Lock()
delete(keyVaultsCache, cacheKey)
lock[cacheKey].Unlock()
}
func (c *Client) cacheKeyForKeyVault(name string) string {
return strings.ToLower(name)
}
func (c *Client) parseNameFromBaseUrl(input string) (*string, error) {
uri, err := url.Parse(input)
if err != nil {
return nil, err
}
// https://the-keyvault.vault.azure.net
// https://the-keyvault.vault.microsoftazure.de
// https://the-keyvault.vault.usgovcloudapi.net
// https://the-keyvault.vault.cloudapi.microsoft
// https://the-keyvault.vault.azure.cn
segments := strings.Split(uri.Host, ".")
if len(segments) < 3 || segments[1] != "vault" {
return nil, fmt.Errorf("expected a URI in the format `the-keyvault-name.vault.**` but got %q", uri.Host)
}
return &segments[0], nil
}