-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathAzureVault.fs
421 lines (369 loc) · 19.4 KB
/
AzureVault.fs
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/// F# helpers to the Azure Vault API
/// Copyright (c) Microsoft Corporation.
module Microsoft.FSharpLu.Azure.Vault
open System.Linq
open Microsoft.FSharpLu.Azure
open Microsoft.FSharpLu.Async
open Microsoft.FSharpLu.Logging
open Microsoft.FSharpLu.ErrorHandling
open Microsoft.FSharpLu
open Microsoft.Azure.Management.KeyVault
open Microsoft.Azure.Management.KeyVault.Models
open Microsoft.FSharpLu.Azure.Request.ErrorHandling
/// Settings needed to access a vault secret
type VaultSecretAccessContext =
{
resourceGroupName : string
vaultName : string
secretName : string
/// Logging tags to be logged for every call to TraceTags.info/warning/error from the Vault helper functions
tags : (string * string) list
}
/// Represents errors that may be thrown in case a key vault entry is not found.
type KeyVaultEntryNotFoundError =
| VaultClientError of Microsoft.Azure.KeyVault.Models.Error
| CloudError of Microsoft.Rest.Azure.CloudError
/// Match a key vault exception with the specified error code
let SomeKeyVaultException errorCode (e:System.Exception) =
match e with
| :? Microsoft.Azure.KeyVault.Models.KeyVaultErrorException as e when
e.Body.Error.Code = errorCode -> Some (VaultClientError e.Body.Error)
| :? Microsoft.Rest.Azure.CloudException as e when
e.Body.Code = errorCode -> Some (CloudError e.Body)
| _ -> None
let SomeKeyVaultEntryNotFoundException e = SomeKeyVaultException "SecretNotFound" e
let SomeKeyVaultForbiddenException e = SomeKeyVaultException "Forbidden" e
/// Gets an Azure Vault URL given the vault name.
let getVaultUrl vaultName =
sprintf "https://%s.vault.azure.net/" vaultName
/// Gets the key vault ID given the subscription id, resource group name, and vault name
let getVaultId subscriptionId resourceGroupName vaultName =
sprintf "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.KeyVault/vaults/%s"
subscriptionId resourceGroupName vaultName
/// Parse a KeyVault secret URL of the form "https://{vaultName}.vault.azure.net[:443]/secrets/{secretName}/{secretVersion}"
/// and return the keyvault vault base URL, secret name and secret version.
let parseKeyvaultSecretUrl secretUrl =
let secretUri = System.Uri(secretUrl)
let trimTrailingSlash (s:System.String) = s.TrimEnd('/')
let keyvaultUrl =
sprintf "%s://%s:%d/" secretUri.Scheme secretUri.Authority secretUri.Port
match secretUri.Segments with
| [|"/"; "secrets/"; name; version|] ->
keyvaultUrl, trimTrailingSlash name, trimTrailingSlash version
| [|"/"; "secrets/"; name; |] ->
keyvaultUrl, trimTrailingSlash name, ""
| _ ->
TraceTags.failwith "Could not parse Azure KeyVault secret name and version from specified URL" [ "secretUrl", secretUrl ]
/// Return the URL to the specified secret
let getSecretUrl (azure:Auth.Subscription) vaultName (secretName:string) =
async {
use c = Context.createVaultContext azure.Authentication
let vaultUrl = getVaultUrl vaultName
let! secret = c.GetSecretWithHttpMessagesAsync(vaultUrl, secretName, "").AsAsync
return secret.Body.Id
}
/// Gets a secret from the specified vault if it exists.
let tryGetSecret (azure:Auth.Subscription) (secretContext:VaultSecretAccessContext) =
async {
use c = Context.createVaultContext azure.Authentication
let vaultUrl = getVaultUrl secretContext.vaultName
try
let! secret = c.GetSecretWithHttpMessagesAsync(vaultUrl, secretContext.secretName, "").AsAsync
return Some secret
with
| IsAggregateOf SomeKeyVaultEntryNotFoundException e ->
return None
}
/// Set a secret value in the specified vault. Create the secret if it does not already exist.
let setSecret (azure:Auth.Subscription) (secretContext:VaultSecretAccessContext) (secretValue:string) =
async {
use c = Context.createVaultContext azure.Authentication
let vaultUrl = getVaultUrl secretContext.vaultName
let! secret = c.SetSecretWithHttpMessagesAsync(vaultUrl, secretContext.secretName, secretValue).AsAsync
TraceTags.info "Secret value set"
(secretContext.tags @
[ "resourceGroup", secretContext.resourceGroupName
"vaultName", secretContext.vaultName
"secretName", secretContext.secretName
"secretId", secret.Body.Id])
return secret.Body.Id
}
/// Inserts a new secret into the specified vault. Throws if the secret already exists.
let insertSecret (azure:Auth.Subscription) (secretContext:VaultSecretAccessContext) (secretValue:string) =
async {
let! existingSecret = tryGetSecret azure secretContext
if existingSecret.IsSome then
TraceTags.failwith "Secret with this name already exists in specified keyvault"
(secretContext.tags @
[ "resourceGroup", secretContext.resourceGroupName
"vaultName", secretContext.vaultName
"secretName", secretContext.secretName ])
use c = Context.createVaultContext azure.Authentication
let vaultUrl = getVaultUrl secretContext.vaultName
let! secret = c.SetSecretWithHttpMessagesAsync(vaultUrl, secretContext.secretName, secretValue).AsAsync
TraceTags.info "Added new secret to KeyVault"
(secretContext.tags @
[ "resourceGroup", secretContext.resourceGroupName
"vaultName", secretContext.vaultName
"secretName", secretContext.secretName
"secretId", secret.Body.Id])
return secret.Body.Id
}
/// Deletes the specified secret from the vault.
let deleteSecret (azure:Auth.Subscription) (secretContext:VaultSecretAccessContext) =
async {
use c = Context.createVaultContext azure.Authentication
let vaultUrl = getVaultUrl secretContext.vaultName
let! secret = c.DeleteSecretWithHttpMessagesAsync(vaultUrl, secretContext.secretName).AsAsync
return secret
}
/// Update KeyVault access policies for the specified application and object ID and specified keyvault
let setAccessPolicy
(authenticationToken:string)
subscriptionId
(resourceGroup:string)
(vault:Vault)
(applicationId:string option, objectId:string)
(permissionsToKeys:(string[]) option)
(permissionsToSecrets:(string[]) option)
(permissionsToCertificates:(string[]) option)
(permissionsToStorage:(string[]) option)
=
async {
if permissionsToKeys.IsNone
&& permissionsToSecrets.IsNone
&& permissionsToCertificates.IsNone
&& permissionsToStorage.IsNone then
raise <| System.ArgumentException("No permissions specified")
use c = new Microsoft.Azure.Management.KeyVault.KeyVaultManagementClient(
Microsoft.Rest.TokenCredentials(authenticationToken),
SubscriptionId = subscriptionId)
if isNull vault then
raise <| System.NullReferenceException("Vault parameter is null: vault")
let emptyPermissions (s:(string []) option) =
s.IsNone || Seq.isEmpty s.Value
let shouldDeleteEntry =
emptyPermissions permissionsToKeys
&& emptyPermissions permissionsToSecrets
&& emptyPermissions permissionsToCertificates
&& emptyPermissions permissionsToStorage
let appId = applicationId|> Option.map System.Guid.Parse |> Option.toNullable
let matchObjectAppId (entry:AccessPolicyEntry) =
entry.ApplicationId = appId
&& System.String.Equals(entry.ObjectId, objectId, System.StringComparison.OrdinalIgnoreCase)
let existingEntry = vault.Properties.AccessPolicies |> Seq.tryFind matchObjectAppId
let entryAlreadyExists = existingEntry.IsSome
let comparePermissions existingPermissions newPermissions =
match newPermissions with
| None -> 0 // permissions not updated
| Some p ->
Seq.compareWith
(fun x y -> System.StringComparer.InvariantCultureIgnoreCase.Compare(x,y))
(Set.ofSeq existingPermissions)
(Set.ofSeq p)
let shouldUpdate =
match existingEntry with
| None ->
// Add new entry
not shouldDeleteEntry
| Some existingEntry ->
// Delete existing entry
shouldDeleteEntry
// Update existing entry
|| comparePermissions existingEntry.Permissions.Keys permissionsToKeys <> 0
|| comparePermissions existingEntry.Permissions.Secrets permissionsToSecrets <> 0
|| comparePermissions existingEntry.Permissions.Certificates permissionsToCertificates <> 0
|| comparePermissions existingEntry.Permissions.Storage permissionsToStorage <> 0
if shouldUpdate then
let mergeWithExistingEntry (existingPolicy:AccessPolicyEntry) =
let mergePermissions existingPolicy (existingPermissions:System.Collections.Generic.IList<string>) =
function
| Some p -> p
| None ->
if not <| isNull existingPolicy && not <| isNull existingPermissions then
existingPermissions.ToArray()
else
null
AccessPolicyEntry(
TenantId = vault.Properties.TenantId,
ObjectId = objectId,
ApplicationId = appId,
Permissions =
Permissions
(
Keys = mergePermissions existingPolicy existingPolicy.Permissions.Keys permissionsToKeys,
Secrets = mergePermissions existingPolicy existingPolicy.Permissions.Secrets permissionsToSecrets,
Certificates = mergePermissions existingPolicy existingPolicy.Permissions.Certificates permissionsToCertificates,
Storage = mergePermissions existingPolicy existingPolicy.Permissions.Storage permissionsToStorage
)
)
let updatedAccessPolicies : AccessPolicyEntry[] =
if shouldDeleteEntry then
vault.Properties.AccessPolicies
|> Seq.filter (matchObjectAppId >> not)
|> Seq.toArray
else
if entryAlreadyExists then
vault.Properties.AccessPolicies
|> Seq.map
(fun ap ->
if matchObjectAppId ap then
mergeWithExistingEntry ap
else
ap
)
|> Seq.toArray
else
vault.Properties.AccessPolicies
|> Seq.append [
AccessPolicyEntry(
TenantId = vault.Properties.TenantId,
ObjectId = objectId,
ApplicationId = appId,
Permissions = Permissions
(
Keys = (permissionsToKeys |> Option.defaultValue null),
Secrets = (permissionsToSecrets |> Option.defaultValue null),
Certificates = (permissionsToCertificates |> Option.defaultValue null),
Storage = (permissionsToStorage |> Option.defaultValue null)
)
)]
|> Seq.toArray
let! updatedVault =
c.Vaults.UpdateAsync(
resourceGroup,
vault.Name,
VaultPatchParameters(
Properties = VaultPatchProperties(AccessPolicies = updatedAccessPolicies),
Tags = vault.Tags
)).AsAsync
TraceTags.info "Vault access policy updated" [ "vaultId", updatedVault.Id ]
else
TraceTags.info "Vault access already granted, no need to update access policy" [ "vaultId", vault.Id ]
}
/// Try updating KeyVault access policies for the specified application and object ID and specified keyvault
/// Returns Result.OK () if succeeded or Result.Error (isTransientError, errorMessage, errorTags) if a failure occurs.
let trySetAccessPolicy
(authenticationToken:string)
subscriptionId
resourceGroupName
vaultName
(applicationId:string option, objectId:string)
(permissionsToKeys:(string[]) option)
(permissionsToSecrets:(string[]) option)
(permissionsToCertificates:(string[]) option)
(permissionsToStorage:(string[]) option)
=
async {
if permissionsToKeys.IsNone
&& permissionsToSecrets.IsNone
&& permissionsToCertificates.IsNone
&& permissionsToStorage.IsNone then
raise <| System.ArgumentException("No permissions specified")
use c = new Microsoft.Azure.Management.KeyVault.KeyVaultManagementClient(
Microsoft.Rest.TokenCredentials(authenticationToken),
SubscriptionId = subscriptionId)
let! vault =
async {
try
let! v = c.Vaults.GetAsync(resourceGroupName, vaultName).AsAsync
return Result.Ok v
with
| IsAggregateOf SomeNotFoundException _
| IsAggregateOf SomeResourceNotFoundException _ ->
return Result.Error (false, sprintf "Keyvault does not exist %s" vaultName, [])
| IsAggregateOf SomeResourceGroupNotFoundException _ ->
return Result.Error (false, sprintf "Resource group does not exist %s" resourceGroupName, [])
}
match vault with
| Result.Error (transient, errorMessage, tags) as e ->
return Result.Error (transient, errorMessage, tags)
| Result.Ok vault ->
return!
async {
try
do! setAccessPolicy authenticationToken
subscriptionId
resourceGroupName
vault
(applicationId, objectId)
permissionsToKeys
permissionsToSecrets
permissionsToCertificates
permissionsToStorage
return Result.Ok ()
with
| IsAggregateOf (SomeCloudExceptionStatusCode System.Net.HttpStatusCode.Conflict) e ->
return Result.Error (true, "Could not set access policy",
[ "keyvaultResourceGroupName", resourceGroupName
"vaultUri", vault.Id
"response", e.Content
"reasonPhrase", e.ReasonPhrase ])
| IsAggregateOf SomeConflictErrorException e ->
return Result.Error (true, "Could not set access policy",
[ "keyvaultResourceGroupName", resourceGroupName
"vaultUri", vault.Id
"responseMessage", e.Message
"additionalInfo", e.AdditionalInfo.ToString()
"details", e.Details.ToString()
])
}
}
/// Deletes the specified secret from the vault if it exists.
let deleteSecretIfExists (azure:Auth.Subscription) (secretContext:VaultSecretAccessContext) =
async {
try
let! secret = deleteSecret azure secretContext
return Some secret
with
| IsAggregateOf SomeKeyVaultEntryNotFoundException _ ->
TraceTags.info "Secret does not exist, nothing to delete"
(secretContext.tags @
[ "resourceGroup", secretContext.resourceGroupName
"vaultName", secretContext.vaultName
"secretName", secretContext.secretName ])
return None
| IsAggregateOf SomeNameResolutionFailure e ->
TraceTags.info "Keyvault dot not exist, nothing to delete"
(secretContext.tags @
[ "resourceGroup", secretContext.resourceGroupName
"vaultName", secretContext.vaultName
"secretName", secretContext.secretName
"exception", e.ToString() ])
return None
| IsAggregateOf SomeKeyVaultForbiddenException e ->
TraceTags.error "Keyvault could not be deleted due to missing access permissions"
(secretContext.tags @
[ "resourceGroup", secretContext.resourceGroupName
"vaultName", secretContext.vaultName
"secretName", secretContext.secretName
"exception", e.ToString()
"authentication", sprintf "%A" azure.Authentication])
return None
}
/// Copy a secret from one vault to another
/// If overwrite is true then replace existing secret with new value, otherwise
/// leave existing value intact.
let copySecret azure sourceVaultSecret targetVaultSecret overwrite =
async {
let! targetExistingSecret =
if overwrite then
async.Return None
else
tryGetSecret azure targetVaultSecret
match targetExistingSecret with
| Some existingSecretId when (not overwrite) ->
return existingSecretId.Body.Id
| _ ->
let! secret = tryGetSecret azure sourceVaultSecret
let sourceSecretContent =
match secret with
| Some s -> s.Body.Value
| None ->
TraceTags.failwith "Could not located secret with given name in specified KeyVault"
(sourceVaultSecret.tags @
[ "resourceGroup", sourceVaultSecret.resourceGroupName
"vaultName", sourceVaultSecret.vaultName
"secretName", sourceVaultSecret.secretName ])
return! setSecret azure targetVaultSecret sourceSecretContent
}