This repository was archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFunctions.cs
192 lines (169 loc) · 9.24 KB
/
Functions.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Azure.Core;
using Azure.Storage.Blobs;
using Azure.Storage.Sas;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
namespace StorageMSIFunction
{
public static class Functions
{
private static readonly Lazy<IDictionary<string, BlobServiceClient>> _serviceClients = new Lazy<IDictionary<string, BlobServiceClient>>(() => new Dictionary<string, BlobServiceClient>());
private static readonly Lazy<TokenCredential> _msiCredential = new Lazy<TokenCredential>(() =>
{
// https://docs.microsoft.com/en-us/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet
// Using DefaultAzureCredential allows for local dev by setting environment variables for the current user, provided said user
// has the necessary credentials to perform the operations the MSI of the Function app needs in order to do its work. Including
// interactive credentials will allow browser-based login when developing locally.
return new Azure.Identity.DefaultAzureCredential(includeInteractiveCredentials: true);
});
private static readonly Lazy<IAzure> _legacyAzure = new Lazy<IAzure>(() =>
{
// If we find tenant and subscription in environment variables, configure accordingly
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(@"AZURE_TENANT_ID"))
&& !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(@"AZURE_SUBSCRIPTION_ID")))
{
var tokenCred = _msiCredential.Value;
var armToken = tokenCred.GetToken(new TokenRequestContext(scopes: new[] { "https://management.azure.com/.default" }, parentRequestId: null), default).Token;
var armCreds = new Microsoft.Rest.TokenCredentials(armToken);
var graphToken = tokenCred.GetToken(new TokenRequestContext(scopes: new[] { "https://graph.windows.net/.default" }, parentRequestId: null), default).Token;
var graphCreds = new Microsoft.Rest.TokenCredentials(graphToken);
var credentials = new AzureCredentials(armCreds, graphCreds, Environment.GetEnvironmentVariable(@"AZURE_TENANT_ID"), AzureEnvironment.AzureGlobalCloud);
return Microsoft.Azure.Management.Fluent.Azure
.Authenticate(credentials)
.WithSubscription(Environment.GetEnvironmentVariable(@"AZURE_SUBSCRIPTION_ID"));
}
else
{
var credentials = SdkContext.AzureCredentialsFactory
.FromSystemAssignedManagedServiceIdentity(MSIResourceType.AppService, AzureEnvironment.AzureGlobalCloud);
return Microsoft.Azure.Management.Fluent.Azure
.Authenticate(credentials)
.WithDefaultSubscription();
}
});
[FunctionName(nameof(GetSASUrl))]
public static IActionResult GetSASUrl(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
ILogger log)
{
var queryParams = req.GetQueryParameterDictionary();
if (!queryParams.TryGetValue(@"blobUri", out string blobUriString)
|| string.IsNullOrWhiteSpace(blobUriString))
{
return new BadRequestObjectResult($@"Request must contain query parameter 'blobUri' designating the full URI of the Azure blob for which you wish to retrieve a read-only SAS URL");
}
var blobUri = new Uri(blobUriString);
try
{
var blobUriBuilder = new BlobUriBuilder(blobUri);
if (!_serviceClients.Value.TryGetValue(blobUriBuilder.AccountName, out var serviceClient))
{
serviceClient = new BlobServiceClient(new Uri($@"https://{blobUriBuilder.AccountName}.blob.core.windows.net"), _msiCredential.Value);
_serviceClients.Value.Add(blobUriBuilder.AccountName, serviceClient);
}
// Create a SAS token that's valid for secToLive, with a 30-second backoff for clock skew.
BlobSasBuilder sasBuilder = new BlobSasBuilder
{
BlobContainerName = blobUriBuilder.BlobContainerName,
BlobName = blobUriBuilder.BlobName,
Resource = "b", // "b" is for blob
StartsOn = DateTimeOffset.UtcNow.AddSeconds(-30),
ExpiresOn = DateTimeOffset.UtcNow + TimeSpan.FromMinutes(1)
};
// Specify read permissions for the SAS.
sasBuilder.SetPermissions(BlobSasPermissions.Read);
var userDelegation = serviceClient.GetUserDelegationKey(sasBuilder.StartsOn, sasBuilder.ExpiresOn)?.Value;
if (userDelegation == null)
{
log.LogError($@"Unable to get a user delegation key from the Storage service for blob {blobUri}");
return new ObjectResult($@"Unable to get a user delegation key from the Storage service for blob {blobUri}")
{
StatusCode = (int)HttpStatusCode.BadGateway
};
}
var sasToken = sasBuilder.ToSasQueryParameters(userDelegation, blobUriBuilder.AccountName);
blobUriBuilder.Sas = sasToken;
// Construct the full URI, including the SAS token.
return new OkObjectResult(blobUriBuilder.ToUri().ToString());
}
catch (Exception e)
{
log.LogError(e, $@"Failure retrieving SAS URL for '{blobUri}'");
return new ObjectResult(e)
{
StatusCode = (int)HttpStatusCode.BadGateway
};
}
}
[FunctionName(nameof(GetAccountKeys))]
public static IActionResult GetAccountKeys(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
ILogger log)
{
if (!req.GetQueryParameterDictionary().TryGetValue(@"accountName", out var accountName))
{
return new BadRequestObjectResult($@"Request must contain query parameter 'accountName' designating the storage account for which you wish to retrieve the account keys");
}
try
{
var storageAccounts = _legacyAzure.Value.StorageAccounts.List();
var accountKeys = storageAccounts
.FirstOrDefault(sa => sa.Name.Equals(accountName, StringComparison.OrdinalIgnoreCase))?
.GetKeys();
log.LogInformation($@"Successfully retrieved keys for '{accountName}'");
return new OkObjectResult(accountKeys);
}
catch (Exception e)
{
log.LogError(e, $@"Failure retrieving keys for '{accountName}'");
return new ObjectResult(e)
{
StatusCode = (int)HttpStatusCode.BadGateway
};
}
}
[FunctionName(nameof(RegenerateKey))]
public static IActionResult RegenerateKey(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
var queryParams = req.GetQueryParameterDictionary();
if (!queryParams.TryGetValue(@"accountName", out var accountName))
{
return new BadRequestObjectResult($@"Request must contain query parameter 'accountName' designating the storage account for which you wish to regenerate a key");
}
if (!queryParams.TryGetValue(@"keyName", out var keyName))
{
return new BadRequestObjectResult($@"Request must contain query parameter 'keyName' designating the name of the key you wish to regenerate");
}
try
{
var storageAccounts = _legacyAzure.Value.StorageAccounts.List();
var newKey = storageAccounts
.FirstOrDefault(sa => sa.Name.Equals(accountName, StringComparison.OrdinalIgnoreCase))?
.RegenerateKey(keyName)
.First();
log.LogInformation($@"Successfully regenerated key for {accountName}/{newKey.KeyName}. New key value: {newKey.Value}");
return new OkResult();
}
catch (Exception e)
{
log.LogError(e, $@"Failure retrieving keys for '{accountName}'");
return new ObjectResult(e)
{
StatusCode = (int)HttpStatusCode.BadGateway
};
}
}
}
}