Skip to content

Commit a09891a

Browse files
authored
Only add / apply bundles configuration if file not present and file system is not readonly (#5580)
1 parent 7acc0a0 commit a09891a

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/WebJobs.Script/Config/HostJsonFileConfigurationSource.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ internal JObject LoadHostConfig(string configFilePath)
195195
_logger.HostConfigNotFound();
196196

197197
hostConfigObject = GetDefaultHostConfigObject();
198+
199+
// Add bundle configuration if no file exists and file system is not read only
200+
hostConfigObject = TryAddBundleConfiguration(hostConfigObject);
198201
TryWriteHostJson(configFilePath, hostConfigObject);
199202
}
200203

@@ -204,7 +207,7 @@ internal JObject LoadHostConfig(string configFilePath)
204207

205208
private JObject GetDefaultHostConfigObject()
206209
{
207-
var hostJsonJObj = JObject.Parse("{'version': '2.0', 'extensionBundle': { 'id': 'Microsoft.Azure.Functions.ExtensionBundle', 'version': '[1.*, 2.0.0)'}}");
210+
var hostJsonJObj = JObject.Parse("{'version': '2.0'}");
208211
if (string.Equals(_configurationSource.Environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName), "powershell", StringComparison.InvariantCultureIgnoreCase)
209212
&& !_configurationSource.Environment.IsFileSystemReadOnly())
210213
{
@@ -233,6 +236,17 @@ private void TryWriteHostJson(string filePath, JObject content)
233236
}
234237
}
235238

239+
private JObject TryAddBundleConfiguration(JObject content)
240+
{
241+
if (!_configurationSource.Environment.IsFileSystemReadOnly())
242+
{
243+
string bundleConfiguration = "{ 'id': 'Microsoft.Azure.Functions.ExtensionBundle', 'version': '[1.*, 2.0.0)'}";
244+
content.Add("extensionBundle", JToken.Parse(bundleConfiguration));
245+
_logger.AddingExtensionBundleConfiguration(bundleConfiguration);
246+
}
247+
return content;
248+
}
249+
236250
internal static string SanitizeHostJson(JObject hostJsonObject)
237251
{
238252
JObject sanitizedObject = new JObject();

src/WebJobs.Script/Diagnostics/Extensions/HostLoggerExtension.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public static class HostLoggerExtension
5959
new EventId(207, nameof(DebugerManagerUnableToUpdateSentinelFile)),
6060
"Unable to update the debug sentinel file.");
6161

62+
private static readonly Action<ILogger, string, string, Exception> _addExtensionBundleConfiguration =
63+
LoggerMessage.Define<string, string>(
64+
LogLevel.Information,
65+
new EventId(208, nameof(AddingExtensionBundleConfiguration)),
66+
"Adding bundle configuration to default host configuration: {newLine}{bundleConfiguration}");
67+
6268
public static void HostConfigApplied(this ILogger logger)
6369
{
6470
_hostConfigApplied(logger, null);
@@ -87,6 +93,12 @@ public static void HostConfigNotFound(this ILogger logger)
8793
_hostConfigNotFound(logger, fileName, null);
8894
}
8995

96+
public static void AddingExtensionBundleConfiguration(this ILogger logger, string bundleConfiguration)
97+
{
98+
string newLine = NewLine;
99+
_addExtensionBundleConfiguration(logger, newLine, bundleConfiguration, null);
100+
}
101+
90102
public static void HostConfigCreationFailed(this ILogger logger)
91103
{
92104
string fileName = ScriptConstants.HostMetadataFileName;

test/WebJobs.Script.Tests/Configuration/HostJsonFileConfigurationSourceTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration
1818
{
1919
public class HostJsonFileConfigurationSourceTests
2020
{
21-
private readonly string _defaultHostJson = "{\r\n \"version\": \"2.0\",\r\n \"extensionBundle\": {\r\n \"id\": \"Microsoft.Azure.Functions.ExtensionBundle\",\r\n \"version\": \"[1.*, 2.0.0)\"\r\n }\r\n}";
21+
private readonly string _hostJsonWithBundles = "{\r\n \"version\": \"2.0\",\r\n \"extensionBundle\": {\r\n \"id\": \"Microsoft.Azure.Functions.ExtensionBundle\",\r\n \"version\": \"[1.*, 2.0.0)\"\r\n }\r\n}";
22+
private readonly string _defaultHostJson = "{\r\n \"version\": \"2.0\"\r\n}";
2223
private readonly ScriptApplicationHostOptions _options;
2324
private readonly string _hostJsonFile;
2425
private readonly TestLoggerProvider _loggerProvider = new TestLoggerProvider();
@@ -55,7 +56,7 @@ public void MissingHostJson_CreatesDefaultFile()
5556

5657
AreExpectedMetricsGenerated(testMetricsLogger);
5758

58-
Assert.Equal(_defaultHostJson, File.ReadAllText(_hostJsonFile));
59+
Assert.Equal(_hostJsonWithBundles, File.ReadAllText(_hostJsonFile));
5960

6061
var log = _loggerProvider.GetAllLogMessages().Single(l => l.FormattedMessage == "No host configuration file found. Creating a default host.json file.");
6162
Assert.Equal(LogLevel.Information, log.Level);
@@ -108,7 +109,10 @@ public void ReadOnlyFileSystem_SkipsDefaultHostJsonCreation()
108109
TestMetricsLogger testMetricsLogger = new TestMetricsLogger();
109110
IConfiguration config = BuildHostJsonConfiguration(testMetricsLogger, environment);
110111
AreExpectedMetricsGenerated(testMetricsLogger);
112+
var configList = config.AsEnumerable().ToList();
111113
Assert.Equal(config["AzureFunctionsJobHost:version"], "2.0");
114+
Assert.Equal(configList.Count, 2);
115+
Assert.True(configList.TrueForAll((k) => !k.Key.Contains("extensionBundle")));
112116

113117
var log = _loggerProvider.GetAllLogMessages().Single(l => l.FormattedMessage == "No host configuration file found. Creating a default host.json file.");
114118
Assert.Equal(LogLevel.Information, log.Level);

0 commit comments

Comments
 (0)