-
Notifications
You must be signed in to change notification settings - Fork 447
/
FunctionsSyncManager.cs
807 lines (707 loc) · 36.5 KB
/
FunctionsSyncManager.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Storage;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Models;
using Microsoft.Azure.WebJobs.Script.WebHost.Extensions;
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
using Microsoft.Azure.WebJobs.Script.WebHost.Security;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.WebJobs.Script.WebHost.Management
{
public class FunctionsSyncManager : IFunctionsSyncManager, IDisposable
{
private const string HubName = "HubName";
private const string TaskHubName = "taskHubName";
private const string Connection = "connection";
private const string DurableTaskV1StorageConnectionName = "azureStorageConnectionStringName";
private const string DurableTaskV2StorageOptions = "storageProvider";
private const string DurableTaskV2StorageConnectionName = "connectionStringName";
private const string DurableTaskV2MaxConcurrentActivityFunctions = "maxConcurrentActivityFunctions";
private const string DurableTaskV2MaxConcurrentOrchestratorFunctions = "maxConcurrentOrchestratorFunctions";
private const string DurableTask = "durableTask";
// 45 alphanumeric characters gives us a buffer in our table/queue/blob container names.
private const int MaxTaskHubNameSize = 45;
private const int MinTaskHubNameSize = 3;
private const string TaskHubPadding = "Hub";
//Managed Kubernetes build service variables
private const string ManagedKubernetesBuildServicePort = "8181";
private const string ManagedKubernetesBuildServiceName = "k8se-build-service";
private const string ManagedKubernetesBuildServiceNamespace = "k8se-system";
private readonly Regex versionRegex = new Regex(@"Version=(?<majorversion>\d)\.\d\.\d");
private readonly IOptionsMonitor<ScriptApplicationHostOptions> _applicationHostOptions;
private readonly ILogger _logger;
private readonly HttpClient _httpClient;
private readonly ISecretManagerProvider _secretManagerProvider;
private readonly IHostIdProvider _hostIdProvider;
private readonly IScriptWebHostEnvironment _webHostEnvironment;
private readonly IEnvironment _environment;
private readonly HostNameProvider _hostNameProvider;
private readonly IFunctionMetadataManager _functionMetadataManager;
private readonly SemaphoreSlim _syncSemaphore = new SemaphoreSlim(1, 1);
private readonly IAzureBlobStorageProvider _azureBlobStorageProvider;
private readonly IOptions<FunctionsHostingConfigOptions> _hostingConfigOptions;
private BlobClient _hashBlobClient;
public FunctionsSyncManager(IHostIdProvider hostIdProvider, IOptionsMonitor<ScriptApplicationHostOptions> applicationHostOptions, ILogger<FunctionsSyncManager> logger, IHttpClientFactory httpClientFactory, ISecretManagerProvider secretManagerProvider, IScriptWebHostEnvironment webHostEnvironment, IEnvironment environment, HostNameProvider hostNameProvider, IFunctionMetadataManager functionMetadataManager, IAzureBlobStorageProvider azureBlobStorageProvider, IOptions<FunctionsHostingConfigOptions> functionsHostingConfigOptions)
{
_applicationHostOptions = applicationHostOptions;
_logger = logger;
_httpClient = httpClientFactory.CreateClient();
_secretManagerProvider = secretManagerProvider;
_hostIdProvider = hostIdProvider;
_webHostEnvironment = webHostEnvironment;
_environment = environment;
_hostNameProvider = hostNameProvider;
_functionMetadataManager = functionMetadataManager;
_azureBlobStorageProvider = azureBlobStorageProvider;
_hostingConfigOptions = functionsHostingConfigOptions;
}
internal bool ArmCacheEnabled
{
get
{
return _environment.GetEnvironmentVariableOrDefault(EnvironmentSettingNames.AzureWebsiteArmCacheEnabled, "1") == "1";
}
}
public async Task<SyncTriggersResult> TrySyncTriggersAsync(bool isBackgroundSync = false)
{
var result = new SyncTriggersResult
{
Success = true
};
if (!IsSyncTriggersEnvironment(_webHostEnvironment, _environment))
{
result.Success = false;
result.Error = "Invalid environment for SyncTriggers operation.";
_logger.LogWarning(result.Error);
return result;
}
try
{
await _syncSemaphore.WaitAsync();
PrepareSyncTriggers();
var hashBlobClient = await GetHashBlobAsync();
if (isBackgroundSync && hashBlobClient == null && !_environment.IsKubernetesManagedHosting())
{
// short circuit before doing any work in background sync
// cases where we need to check/update hash but don't have
// storage access in non-Kubernetes environments.
return result;
}
var payload = await GetSyncTriggersPayload();
if (isBackgroundSync && payload.Count == 0)
{
// We don't do background sync for empty triggers.
// We've seen error cases where a site temporarily gets into a situation
// where it's site content is empty. Doing the empty sync can cause the app
// to go idle when it shouldn't.
_logger.LogDebug("No functions found. Skipping Sync operation.");
return result;
}
bool shouldSyncTriggers = true;
string newHash = null;
if (isBackgroundSync && !_environment.IsKubernetesManagedHosting())
{
newHash = await CheckHashAsync(hashBlobClient, payload.Content);
shouldSyncTriggers = newHash != null;
}
if (shouldSyncTriggers)
{
var (success, error) = await SetTriggersAsync(payload.Content);
if (success && newHash != null)
{
await UpdateHashAsync(hashBlobClient, newHash);
}
result.Success = success;
result.Error = error;
}
}
catch (Exception ex)
{
// best effort - log error and continue
result.Success = false;
result.Error = "SyncTriggers operation failed.";
_logger.LogError(ex, result.Error);
}
finally
{
_syncSemaphore.Release();
}
return result;
}
/// <summary>
/// SyncTriggers is performed whenever deployments or other changes are made to the application.
/// There are some operations we want to perform whenever this happens.
/// </summary>
private void PrepareSyncTriggers()
{
// We clear cache to ensure that secrets are reloaded. This is important because secrets are part
// of the StartupContext payload (see StartupContextProvider) and that payload comes from the
// SyncTriggers operation. So there's a chicken and egg situation here. Consider the following scenario:
// - app is using blob storage for keys
// - a SyncTriggers operation has happened previously and the StartupContext has key info
// - app instances initialize keys from StartupContext (keys aren't loaded from storage)
// - user updates the app to use a new storage account
// - a SyncTriggers operation is performed
// - the app initializes from StartupContext, and **previous old key info is loaded**
// - the SyncTriggers operation uses this old key info, so trigger cache is never updated with new key info
// - Portal/ARM APIs will continue to show old key info.
// By clearing cache, we ensure that this host instance reloads keys when they're requested, and the SyncTriggers
// operation will contain current keys.
if (_secretManagerProvider.SecretsEnabled)
{
_secretManagerProvider.Current.ClearCache();
}
}
internal static bool IsSyncTriggersEnvironment(IScriptWebHostEnvironment webHostEnvironment, IEnvironment environment)
{
if (environment.IsCoreTools())
{
// don't sync triggers when running locally or not running in a cloud
// hosted environment
return false;
}
if (environment.GetEnvironmentVariable(EnvironmentSettingNames.WebSiteAuthEncryptionKey) == null)
{
// We don't have the encryption key required for SetTriggers,
// so sync calls would fail auth anyways.
// This might happen in when running locally for example.
return false;
}
if (webHostEnvironment.InStandbyMode)
{
// don’t sync triggers when in standby mode
return false;
}
// Windows (Dedicated/Consumption)
// Linux Consumption
if ((environment.IsWindowsAzureManagedHosting() || environment.IsAnyLinuxConsumption()) &&
!environment.IsContainerReady())
{
// container ready flag not set yet – site not fully specialized/initialized
return false;
}
return true;
}
internal async Task<string> CheckHashAsync(BlobClient hashBlobClient, string content)
{
try
{
// compute the current hash value and compare it with
// the last stored value
string currentHash = null;
using (var sha256 = SHA256.Create())
{
byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(content));
currentHash = hash
.Aggregate(new StringBuilder(), (a, b) => a.Append(b.ToString("x2")))
.ToString();
}
// get the last hash value if present
string lastHash = null;
if (await hashBlobClient.ExistsAsync())
{
var downloadResponse = await hashBlobClient.DownloadAsync();
using (StreamReader reader = new StreamReader(downloadResponse.Value.Content))
{
lastHash = reader.ReadToEnd();
}
_logger.LogDebug($"SyncTriggers hash (Last='{lastHash}', Current='{currentHash}')");
}
if (string.Compare(currentHash, lastHash) != 0)
{
// hash will need to be updated - return the
// new hash value
return currentHash;
}
}
catch (Exception ex)
{
// best effort
_logger.LogError(ex, "Error checking SyncTriggers hash");
}
// if the last and current hash values are the same,
// or if any error occurs, return null
return null;
}
internal async Task UpdateHashAsync(BlobClient hashBlobClient, string hash)
{
try
{
// hash value has changed or was not yet stored
// update the last hash value in storage
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(hash)))
{
await hashBlobClient.UploadAsync(stream, overwrite: true);
}
_logger.LogDebug($"SyncTriggers hash updated to '{hash}'");
}
catch (Exception ex)
{
// best effort
_logger.LogError(ex, "Error updating SyncTriggers hash");
}
}
internal async Task<BlobClient> GetHashBlobAsync()
{
if (_hashBlobClient == null)
{
if (_azureBlobStorageProvider.TryCreateBlobServiceClientFromConnection(ConnectionStringNames.Storage, out BlobServiceClient blobClient))
{
string hostId = await _hostIdProvider.GetHostIdAsync(CancellationToken.None);
var blobContainerClient = blobClient.GetBlobContainerClient(ScriptConstants.AzureWebJobsHostsContainerName);
string hashBlobPath = $"synctriggers/{hostId}/last";
_hashBlobClient = blobContainerClient.GetBlobClient(hashBlobPath);
}
}
return _hashBlobClient;
}
public async Task<SyncTriggersPayload> GetSyncTriggersPayload()
{
var hostOptions = _applicationHostOptions.CurrentValue.ToHostOptions();
var functionsMetadata = _functionMetadataManager.GetFunctionMetadata().Where(m => !m.IsProxy());
// trigger information used by the ScaleController
var triggers = await GetFunctionTriggers(functionsMetadata, hostOptions);
var triggersArray = new JArray(triggers);
int count = triggersArray.Count;
// Form the base minimal result
string hostId = await _hostIdProvider.GetHostIdAsync(CancellationToken.None);
JObject result = GetMinimalPayload(hostId, triggersArray);
if (!ArmCacheEnabled)
{
// extended format is disabled - just return minimal results
return new SyncTriggersPayload
{
Content = JsonConvert.SerializeObject(result),
Count = count
};
}
// Add all listable functions details to the payload
JObject functions = new JObject();
var listableFunctions = _functionMetadataManager.GetFunctionMetadata().Where(m => !m.IsCodeless());
var functionDetails = await WebFunctionsManager.GetFunctionMetadataResponse(listableFunctions, hostOptions, _hostNameProvider);
result.Add("functions", new JArray(functionDetails.Select(p => JObject.FromObject(p))));
// TEMP: refactor this code to properly add extensions in all scenario(#7394)
// Add the host.json extensions to the payload
if (_environment.IsKubernetesManagedHosting())
{
JObject extensionsPayload = await GetHostJsonExtensionsAsync(_applicationHostOptions, _logger);
if (extensionsPayload != null)
{
result.Add("extensions", extensionsPayload);
}
}
if (_secretManagerProvider.SecretsEnabled)
{
// Add functions secrets to the payload
// Only secret types we own/control can we cache directly
// Encryption is handled by Antares before storage
var secretsStorageType = _environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsSecretStorageType);
if (string.IsNullOrEmpty(secretsStorageType) ||
string.Equals(secretsStorageType, "files", StringComparison.OrdinalIgnoreCase) ||
string.Equals(secretsStorageType, "blob", StringComparison.OrdinalIgnoreCase))
{
var functionAppSecrets = new FunctionAppSecrets();
// add host secrets
var hostSecretsInfo = await _secretManagerProvider.Current.GetHostSecretsAsync();
functionAppSecrets.Host = new FunctionAppSecrets.HostSecrets
{
Master = hostSecretsInfo.MasterKey,
Function = hostSecretsInfo.FunctionKeys,
System = hostSecretsInfo.SystemKeys
};
// add function secrets
var httpFunctions = functionsMetadata.Where(p => !p.IsProxy() && p.InputBindings.Any(q => q.IsTrigger && string.Equals(q.Type, "httptrigger", StringComparison.OrdinalIgnoreCase))).Select(p => p.Name).ToArray();
functionAppSecrets.Function = new FunctionAppSecrets.FunctionSecrets[httpFunctions.Length];
for (int i = 0; i < httpFunctions.Length; i++)
{
var currFunctionName = httpFunctions[i];
var currSecrets = await _secretManagerProvider.Current.GetFunctionSecretsAsync(currFunctionName);
functionAppSecrets.Function[i] = new FunctionAppSecrets.FunctionSecrets
{
Name = currFunctionName,
Secrets = currSecrets
};
}
result.Add("secrets", JObject.FromObject(functionAppSecrets));
}
else
{
// TODO: handle other external key storage types
// like KeyVault when the feature comes online
}
}
string json = JsonConvert.SerializeObject(result);
if (json.Length > ScriptConstants.MaxTriggersStringLength && !_environment.IsKubernetesManagedHosting())
{
// The settriggers call to the FE enforces a max request size limit.
// If we're over limit, revert to the minimal triggers format.
_logger.LogWarning($"SyncTriggers payload of length '{json.Length}' exceeds max length of '{ScriptConstants.MaxTriggersStringLength}'. Reverting to minimal format.");
var minimalResult = GetMinimalPayload(hostId, triggersArray);
json = JsonConvert.SerializeObject(minimalResult);
}
return new SyncTriggersPayload
{
Content = json,
Count = count
};
}
private JObject GetMinimalPayload(string hostId, JArray triggersArray)
{
// When the HostId is sent, ScaleController will use it directly rather than compute it itself.
return new JObject
{
{ "triggers", triggersArray },
{ "hostId", hostId }
};
}
internal static async Task<JObject> GetHostJsonExtensionsAsync(IOptionsMonitor<ScriptApplicationHostOptions> applicationHostOptions, ILogger logger)
{
var hostOptions = applicationHostOptions.CurrentValue.ToHostOptions();
string hostJsonPath = Path.Combine(hostOptions.RootScriptPath, ScriptConstants.HostMetadataFileName);
if (FileUtility.FileExists(hostJsonPath))
{
try
{
var hostJson = JObject.Parse(await FileUtility.ReadAsync(hostJsonPath));
if (hostJson.TryGetValue("extensions", out JToken token))
{
return (JObject)token;
}
else
{
return null;
}
}
catch (JsonException ex)
{
logger.LogWarning($"Unable to parse host configuration file '{hostJsonPath}'. : {ex}");
return null;
}
}
return null;
}
internal async Task<IEnumerable<JObject>> GetFunctionTriggers(IEnumerable<FunctionMetadata> functionsMetadata, ScriptJobHostOptions hostOptions)
{
var triggers = (await functionsMetadata
.Where(f => !f.IsProxy())
.Select(f => f.ToFunctionTrigger(hostOptions, _environment.IsFlexConsumptionSku()))
.WhenAll())
.Where(t => t != null);
// TODO: We should remove extension-specific logic from the Host. See: https://github.com/Azure/azure-functions-host/issues/5390
if (triggers.Any(IsDurableTrigger))
{
DurableConfig durableTaskConfig = await ReadDurableTaskConfig();
// If any host level durable config values, we need to apply them to all durable triggers
if (durableTaskConfig.HasValues())
{
triggers = triggers.Select(t => UpdateDurableFunctionConfig(t, durableTaskConfig));
}
}
if (FileUtility.FileExists(Path.Combine(hostOptions.RootScriptPath, ScriptConstants.ProxyMetadataFileName)))
{
// This is because we still need to scale function apps that are proxies only
triggers = triggers.Append(JObject.FromObject(new { type = "routingTrigger" }));
}
return triggers;
}
private static bool IsDurableTrigger(JObject trigger)
{
return trigger["type"]?.ToString().Equals("orchestrationTrigger", StringComparison.OrdinalIgnoreCase) == true
|| trigger["type"]?.ToString().Equals("entityTrigger", StringComparison.OrdinalIgnoreCase) == true
|| trigger["type"]?.ToString().Equals("activityTrigger", StringComparison.OrdinalIgnoreCase) == true;
}
private static JObject UpdateDurableFunctionConfig(JObject trigger, DurableConfig durableTaskConfig)
{
if (IsDurableTrigger(trigger))
{
if (durableTaskConfig.HubName != null)
{
trigger[TaskHubName] = durableTaskConfig.HubName;
}
if (durableTaskConfig.Connection != null)
{
trigger[Connection] = durableTaskConfig.Connection;
}
if (durableTaskConfig.StorageProvider != null)
{
trigger[DurableTaskV2StorageOptions] = durableTaskConfig.StorageProvider;
}
if (durableTaskConfig.MaxConcurrentOrchestratorFunctions != 0)
{
trigger[DurableTaskV2MaxConcurrentOrchestratorFunctions] = durableTaskConfig.MaxConcurrentOrchestratorFunctions;
}
if (durableTaskConfig.MaxConcurrentActivityFunctions != 0)
{
trigger[DurableTaskV2MaxConcurrentActivityFunctions] = durableTaskConfig.MaxConcurrentActivityFunctions;
}
}
return trigger;
}
private async Task<DurableConfig> ReadDurableTaskConfig()
{
JObject hostJson = null;
JObject durableHostConfig = null;
var hostOptions = _applicationHostOptions.CurrentValue.ToHostOptions();
string hostJsonPath = Path.Combine(hostOptions.RootScriptPath, ScriptConstants.HostMetadataFileName);
if (FileUtility.FileExists(hostJsonPath))
{
hostJson = JObject.Parse(await FileUtility.ReadAsync(hostJsonPath));
// get the DurableTask extension config section
if (hostJson != null &&
hostJson.TryGetValue("extensions", StringComparison.OrdinalIgnoreCase, out JToken extensionsValue))
{
// we will allow case insensitivity given it is likely user hand edited
// see https://github.com/Azure/azure-functions-durable-extension/issues/111
var extensions = extensionsValue as JObject;
if (extensions != null &&
extensions.TryGetValue(DurableTask, StringComparison.OrdinalIgnoreCase, out JToken durableTaskValue))
{
durableHostConfig = durableTaskValue as JObject;
}
}
}
var durableMajorVersion = await GetDurableMajorVersionAsync(hostJson, hostOptions);
if (durableMajorVersion == null || durableMajorVersion.Equals("1"))
{
return GetDurableV1Config(durableHostConfig);
}
else
{
// v2 or greater
return GetDurableV2Config(durableHostConfig);
}
}
// This is a stopgap approach to get the Durable extension version. It duplicates some logic in ExtensionManager.cs.
private async Task<string> GetDurableMajorVersionAsync(JObject hostJson, ScriptJobHostOptions hostOptions)
{
string metadataFilePath;
bool isUsingBundles = hostJson != null && hostJson.TryGetValue("extensionBundle", StringComparison.OrdinalIgnoreCase, out _);
// This feature flag controls whether to opt out of the SyncTrigger metadata fix for OOProc DF apps from https://github.com/Azure/azure-functions-host/pull/9331
if (FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagEnableLegacyDurableVersionCheck))
{
// using legacy behavior, which concludes that out of process DF apps (including .NET isolated) are using DF Extension V1.x
// as a result, the SyncTriggers payload for these apps will be missing some metadata like "taskHubName"
if (isUsingBundles)
{
return "1";
}
string binPath = binPath = Path.Combine(hostOptions.RootScriptPath, "bin");
metadataFilePath = Path.Combine(binPath, ScriptConstants.ExtensionsMetadataFileName);
if (!FileUtility.FileExists(metadataFilePath))
{
return null;
}
}
else
{
if (isUsingBundles)
{
// From Functions runtime V4 onwards, only bundles >= V2.x is supported, which implies the app should be using DF V2 or greater.
return "2";
}
// If the app is not using bundles, we look for extensions.json
if (!Utility.TryResolveExtensionsMetadataPath(hostOptions.RootScriptPath, out string metadataDirectoryPath, out _))
{
return null;
}
metadataFilePath = Path.Combine(metadataDirectoryPath, ScriptConstants.ExtensionsMetadataFileName);
}
var extensionMetadata = JObject.Parse(await FileUtility.ReadAsync(metadataFilePath));
var extensionItems = extensionMetadata["extensions"]?.ToObject<List<ExtensionReference>>();
var durableExtension = extensionItems?.FirstOrDefault(ext => string.Equals(ext.Name, "DurableTask", StringComparison.OrdinalIgnoreCase));
if (durableExtension == null)
{
return null;
}
var versionMatch = versionRegex.Match(durableExtension.TypeName);
if (!versionMatch.Success)
{
return null;
}
// Grab the captured group.
return versionMatch.Groups["majorversion"].Captures[0].Value;
}
private DurableConfig GetDurableV1Config(JObject durableHostConfig)
{
var config = new DurableConfig();
if (durableHostConfig != null)
{
if (durableHostConfig.TryGetValue(HubName, StringComparison.OrdinalIgnoreCase, out JToken nameValue) && nameValue != null)
{
config.HubName = nameValue.ToString();
}
if (durableHostConfig.TryGetValue(DurableTaskV1StorageConnectionName, StringComparison.OrdinalIgnoreCase, out nameValue) && nameValue != null)
{
config.Connection = nameValue.ToString();
}
}
return config;
}
private DurableConfig GetDurableV2Config(JObject durableHostConfig)
{
var config = new DurableConfig();
if (durableHostConfig != null)
{
if (durableHostConfig.TryGetValue(HubName, StringComparison.OrdinalIgnoreCase, out JToken nameValue) && nameValue != null)
{
config.HubName = nameValue.ToString();
}
if (durableHostConfig.TryGetValue(DurableTaskV2StorageOptions, StringComparison.OrdinalIgnoreCase, out JToken storageOptions) && (storageOptions as JObject) != null)
{
if (((JObject)storageOptions).TryGetValue(DurableTaskV2StorageConnectionName, StringComparison.OrdinalIgnoreCase, out nameValue) && nameValue != null)
{
config.Connection = nameValue.ToString();
}
config.StorageProvider = storageOptions;
}
if (durableHostConfig.TryGetValue(DurableTaskV2MaxConcurrentOrchestratorFunctions, StringComparison.OrdinalIgnoreCase, out JToken maxConcurrentOrchestratorFunctions) && maxConcurrentOrchestratorFunctions != null)
{
config.MaxConcurrentOrchestratorFunctions = int.Parse(maxConcurrentOrchestratorFunctions.ToString());
}
if (durableHostConfig.TryGetValue(DurableTaskV2MaxConcurrentActivityFunctions, StringComparison.OrdinalIgnoreCase, out JToken maxConcurrentActivityFunctions) && maxConcurrentActivityFunctions != null)
{
config.MaxConcurrentActivityFunctions = int.Parse(maxConcurrentActivityFunctions.ToString());
}
}
if (config.HubName == null)
{
config.HubName = GetDefaultDurableV2HubName();
}
return config;
}
// This logic will eventually be moved to ScaleController once it has access to version information.
private string GetDefaultDurableV2HubName()
{
// See https://github.com/Azure/azure-functions-durable-extension/blob/eb186eadb73a21d0efdc33cd7603fde5d802cab9/src/WebJobs.Extensions.DurableTask/Options/DurableTaskOptions.cs#L42
string hubName = _environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteName);
// See https://github.com/Azure/azure-functions-durable-extension/blob/eb186eadb73a21d0efdc33cd7603fde5d802cab9/src/WebJobs.Extensions.DurableTask/Options/AzureStorageOptions.cs#L145
hubName = new string(hubName.ToCharArray()
.Where(char.IsLetterOrDigit)
.Take(MaxTaskHubNameSize)
.ToArray());
if (hubName.Length < MinTaskHubNameSize)
{
hubName += TaskHubPadding;
}
return hubName;
}
internal HttpRequestMessage BuildSetTriggersRequest()
{
var url = default(string);
if (_environment.IsAnyKubernetesEnvironment())
{
var hostName = _environment.GetEnvironmentVariable("FUNCTIONS_API_SERVER") ??
_environment.GetEnvironmentVariable("BUILD_SERVICE_HOSTNAME");
if (string.IsNullOrEmpty(hostName))
{
hostName = $"http://{ManagedKubernetesBuildServiceName}.{ManagedKubernetesBuildServiceNamespace}.svc.cluster.local:{ManagedKubernetesBuildServicePort}";
}
url = $"{hostName}/api/operations/settriggers";
}
else
{
var protocol = "https";
if (_environment.GetEnvironmentVariable(EnvironmentSettingNames.SkipSslValidation) == "1")
{
// On private stamps with no ssl certificate use http instead.
protocol = "http";
}
var hostname = _hostNameProvider.Value;
url = $"{protocol}://{hostname}/operations/settriggers";
}
return new HttpRequestMessage(HttpMethod.Post, url);
}
// This function will call POST https://{app}.azurewebsites.net/operation/settriggers with the content
// of triggers. It'll verify app ownership using a SWT token valid for 5 minutes. It should be plenty.
private async Task<(bool Success, string ErrorMessage)> SetTriggersAsync(string content)
{
string sanitizedContentString = content;
if (ArmCacheEnabled)
{
// sanitize the content before logging
var sanitizedContent = JToken.Parse(content);
if (sanitizedContent.Type == JTokenType.Object)
{
((JObject)sanitizedContent).Remove("secrets");
sanitizedContentString = sanitizedContent.ToString();
}
}
using (var request = BuildSetTriggersRequest())
{
var requestId = Guid.NewGuid().ToString();
request.Headers.Add(ScriptConstants.AntaresLogIdHeaderName, requestId);
request.Headers.Add("User-Agent", ScriptConstants.FunctionsUserAgent);
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
if (_hostingConfigOptions.Value.SwtIssuerEnabled)
{
string swtToken = SimpleWebTokenHelper.CreateToken(DateTime.UtcNow.AddMinutes(5));
request.Headers.Add(ScriptConstants.SiteRestrictedTokenHeaderName, swtToken);
}
string jwtToken = JwtTokenHelper.CreateToken(DateTime.UtcNow.AddMinutes(5));
request.Headers.Add(ScriptConstants.SiteTokenHeaderName, jwtToken);
if (_environment.IsManagedAppEnvironment())
{
request.Headers.Add("K8SE-APP-NAME", _environment.GetEnvironmentVariable("CONTAINER_APP_NAME"));
request.Headers.Add("K8SE-APP-NAMESPACE", _environment.GetEnvironmentVariable("CONTAINER_APP_NAMESPACE"));
request.Headers.Add("K8SE-APP-REVISION", _environment.GetEnvironmentVariable("CONTAINER_APP_REVISION"));
}
else if (_environment.IsKubernetesManagedHosting())
{
request.Headers.Add(ScriptConstants.KubernetesManagedAppName, _environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteName));
request.Headers.Add(ScriptConstants.KubernetesManagedAppNamespace, _environment.GetEnvironmentVariable(EnvironmentSettingNames.PodNamespace));
}
_logger.LogDebug($"Making SyncTriggers request (RequestId={requestId}, Uri={request.RequestUri.ToString()}, Content={sanitizedContentString}).");
var response = await _httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
_logger.LogDebug("SyncTriggers call succeeded.");
return (true, null);
}
else
{
string message = $"SyncTriggers call failed (StatusCode={response.StatusCode}).";
_logger.LogDebug(message);
return (false, message);
}
}
}
public void Dispose()
{
_syncSemaphore.Dispose();
}
public class SyncTriggersPayload
{
public string Content { get; set; }
public int Count { get; set; }
}
private class DurableConfig
{
public string HubName { get; set; }
public string Connection { get; set; }
public JToken StorageProvider { get; set; }
public int MaxConcurrentActivityFunctions { get; set; }
public int MaxConcurrentOrchestratorFunctions { get; set; }
public bool HasValues()
{
return this.HubName != null || this.Connection != null || this.StorageProvider != null || this.MaxConcurrentOrchestratorFunctions != 0 || this.MaxConcurrentOrchestratorFunctions != 0;
}
}
}
}