private static SecretManager GetSecretManager(string secretsPath) { var secretManager = new SecretManager(secretsPath); secretManager.GetHostSecrets(); return(secretManager); }
internal static void Initialize(ContainerBuilder builder, WebHostSettings settings) { ScriptHostConfiguration scriptHostConfig = new ScriptHostConfiguration() { RootScriptPath = settings.ScriptPath, RootLogPath = settings.LogPath, FileLoggingEnabled = true }; // If running on Azure Web App, derive the host ID from the site name string hostId = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"); if (!String.IsNullOrEmpty(hostId)) { // Truncate to the max host name length if needed const int MaximumHostIdLength = 32; if (hostId.Length > MaximumHostIdLength) { hostId = hostId.Substring(0, MaximumHostIdLength); } // Trim any trailing - as they can cause problems with queue names hostId = hostId.TrimEnd('-'); scriptHostConfig.HostConfig.HostId = hostId.ToLowerInvariant(); } SecretManager secretManager = new SecretManager(settings.SecretsPath); // Make sure that host secrets get created on startup if they don't exist secretManager.GetHostSecrets(); builder.RegisterInstance <SecretManager>(secretManager); WebScriptHostManager scriptHostManager = new WebScriptHostManager(scriptHostConfig, secretManager); builder.RegisterInstance <WebScriptHostManager>(scriptHostManager); WebHookReceiverManager webHookReceiverManager = new WebHookReceiverManager(secretManager); builder.RegisterInstance <WebHookReceiverManager>(webHookReceiverManager); if (!settings.IsSelfHost) { HostingEnvironment.QueueBackgroundWorkItem((ct) => scriptHostManager.RunAndBlock(ct)); } else { Task.Run(() => scriptHostManager.RunAndBlock()); } }
private async Task <string> GetOrCreateExtensionKey(string extensionName) { var hostSecrets = _secretManager.GetHostSecretsAsync().GetAwaiter().GetResult(); string keyName = GetKeyName(extensionName); string keyValue = null; if (!hostSecrets.SystemKeys.TryGetValue(keyName, out keyValue)) { // if the requested secret doesn't exist, create it on demand keyValue = SecretManager.GenerateSecret(); await _secretManager.AddOrUpdateFunctionSecretAsync(keyName, keyValue, HostKeyScopes.SystemKeys, ScriptSecretsType.Host); } return(keyValue); }
public Fixture() { TestFunctionRoot = Path.Combine(TestHelpers.FunctionsTestDirectory, "Functions"); TestLogsRoot = Path.Combine(TestHelpers.FunctionsTestDirectory, "Logs"); TestSecretsRoot = Path.Combine(TestHelpers.FunctionsTestDirectory, "Secrets"); string testRoot = Path.Combine(TestFunctionRoot, Guid.NewGuid().ToString()); SecretsPath = Path.Combine(TestSecretsRoot, Guid.NewGuid().ToString()); Directory.CreateDirectory(SecretsPath); string logRoot = Path.Combine(TestLogsRoot, Guid.NewGuid().ToString(), @"Functions"); Directory.CreateDirectory(logRoot); FunctionsLogDir = Path.Combine(logRoot, @"Function"); Directory.CreateDirectory(FunctionsLogDir); // Add some secret files (both old and valid) File.Create(Path.Combine(SecretsPath, ScriptConstants.HostMetadataFileName)); File.Create(Path.Combine(SecretsPath, "WebHookTrigger.json")); File.Create(Path.Combine(SecretsPath, "QueueTriggerToBlob.json")); File.Create(Path.Combine(SecretsPath, "Foo.json")); File.Create(Path.Combine(SecretsPath, "Bar.json")); File.Create(Path.Combine(SecretsPath, "Invalid.json")); // Add some old file directories CreateTestFunctionLogs(FunctionsLogDir, "Foo"); CreateTestFunctionLogs(FunctionsLogDir, "Bar"); CreateTestFunctionLogs(FunctionsLogDir, "Baz"); CreateTestFunctionLogs(FunctionsLogDir, "Invalid"); ScriptHostConfiguration config = new ScriptHostConfiguration { RootScriptPath = @"TestScripts\Node", RootLogPath = logRoot, FileLoggingEnabled = true }; SecretManager secretManager = new SecretManager(SecretsPath); WebHostSettings webHostSettings = new WebHostSettings(); HostManager = new WebScriptHostManager(config, secretManager, webHostSettings); Task task = Task.Run(() => { HostManager.RunAndBlock(); }); TestHelpers.Await(() => { return(HostManager.IsRunning); }).GetAwaiter().GetResult(); }
public Fixture() { string testRoot = Path.Combine(Path.GetTempPath(), "FunctionTests"); if (Directory.Exists(testRoot)) { Directory.Delete(testRoot, recursive: true); } SecretsPath = Path.Combine(testRoot, "TestSecrets"); Directory.CreateDirectory(SecretsPath); string logRoot = Path.Combine(testRoot, @"Functions"); Directory.CreateDirectory(logRoot); FunctionsLogDir = Path.Combine(logRoot, @"Function"); Directory.CreateDirectory(FunctionsLogDir); // Add some secret files (both old and valid) File.Create(Path.Combine(SecretsPath, ScriptConstants.HostMetadataFileName)); File.Create(Path.Combine(SecretsPath, "WebHookTrigger.json")); File.Create(Path.Combine(SecretsPath, "QueueTriggerToBlob.json")); File.Create(Path.Combine(SecretsPath, "Foo.json")); File.Create(Path.Combine(SecretsPath, "Bar.json")); File.Create(Path.Combine(SecretsPath, "Invalid.json")); // Add some old file directories CreateTestFunctionLogs(FunctionsLogDir, "Foo"); CreateTestFunctionLogs(FunctionsLogDir, "Bar"); CreateTestFunctionLogs(FunctionsLogDir, "Baz"); CreateTestFunctionLogs(FunctionsLogDir, "Invalid"); ScriptHostConfiguration config = new ScriptHostConfiguration { RootScriptPath = @"TestScripts\Node", RootLogPath = logRoot, FileLoggingEnabled = true }; SecretManager secretManager = new SecretManager(SecretsPath); HostManager = new WebScriptHostManager(config, secretManager); Task task = Task.Run(() => { HostManager.RunAndBlock(); }); TestHelpers.Await(() => { return(HostManager.IsRunning); }).GetAwaiter().GetResult(); }
public async Task EmptyHost_StartsSuccessfully() { string functionTestDir = Path.Combine(_fixture.TestFunctionRoot, Guid.NewGuid().ToString()); Directory.CreateDirectory(functionTestDir); // important for the repro that these directories no not exist string logDir = Path.Combine(_fixture.TestLogsRoot, Guid.NewGuid().ToString()); string secretsDir = Path.Combine(_fixture.TestSecretsRoot, Guid.NewGuid().ToString()); JObject hostConfig = new JObject { { "id", "123456" } }; File.WriteAllText(Path.Combine(functionTestDir, ScriptConstants.HostMetadataFileName), hostConfig.ToString()); ScriptHostConfiguration config = new ScriptHostConfiguration { RootScriptPath = functionTestDir, RootLogPath = logDir, FileLoggingMode = FileLoggingMode.Always }; ISecretManager secretManager = new SecretManager(secretsDir); WebHostSettings webHostSettings = new WebHostSettings(); ScriptHostManager hostManager = new WebScriptHostManager(config, secretManager, webHostSettings); Task runTask = Task.Run(() => hostManager.RunAndBlock()); await TestHelpers.Await(() => hostManager.IsRunning, timeout : 10000); hostManager.Stop(); Assert.False(hostManager.IsRunning); await Task.Delay(FileTraceWriter.LogFlushIntervalMs); string hostLogFilePath = Directory.EnumerateFiles(Path.Combine(logDir, "Host")).Single(); string hostLogs = File.ReadAllText(hostLogFilePath); Assert.Contains("Generating 0 job function(s)", hostLogs); Assert.Contains("No job functions found.", hostLogs); Assert.Contains("Job host started", hostLogs); Assert.Contains("Job host stopped", hostLogs); }
public async Task MultipleHostRestarts() { string functionTestDir = Path.Combine(_fixture.TestFunctionRoot, Guid.NewGuid().ToString()); Directory.CreateDirectory(functionTestDir); string logDir = Path.Combine(_fixture.TestLogsRoot, Guid.NewGuid().ToString()); string secretsDir = Path.Combine(_fixture.TestSecretsRoot, Guid.NewGuid().ToString()); ScriptHostConfiguration config = new ScriptHostConfiguration { RootLogPath = logDir, RootScriptPath = functionTestDir, FileLoggingMode = FileLoggingMode.Always, RestartInterval = TimeSpan.FromMilliseconds(500) }; SecretManager secretManager = new SecretManager(secretsDir); WebHostSettings webHostSettings = new WebHostSettings(); var factoryMock = new Mock <IScriptHostFactory>(); int count = 0; factoryMock.Setup(p => p.Create(config)).Callback(() => { count++; }).Throws(new Exception("Kaboom!")); ScriptHostManager hostManager = new WebScriptHostManager(config, secretManager, webHostSettings, factoryMock.Object); Task runTask = Task.Run(() => hostManager.RunAndBlock()); await TestHelpers.Await(() => { return(count > 3); }); hostManager.Stop(); Assert.False(hostManager.IsRunning); // regression test: previously on multiple restarts we were recomposing // the writer on each restart, resulting in a nested chain of writers // increasing on each restart Assert.Equal(typeof(SystemTraceWriter), config.TraceWriter.GetType()); }
private static void EnsureInitialized(WebHostSettings settings) { // standby mode can only change from true to false // When standby mode changes, we reset all instances var standbyMode = WebScriptHostManager.InStandbyMode; if (!standbyMode) { if (_activeHostManager == null) { if (_standbyHostManager != null) { // reintialize app settings if we were in standby ReinitializeAppSettings(); } _activeScriptHostConfig = GetScriptHostConfiguration(settings.ScriptPath, settings.LogPath); _activeSecretManager = GetSecretManager(settings.SecretsPath); _activeReceiverManager = new WebHookReceiverManager(_activeSecretManager); _activeHostManager = new WebScriptHostManager(_activeScriptHostConfig, _activeSecretManager, settings); _standbySecretManager?.Dispose(); _standbyHostManager?.Dispose(); _standbyReceiverManager?.Dispose(); _standbyScriptHostConfig = null; _standbySecretManager = null; _standbyHostManager = null; _standbyReceiverManager = null; } } else { if (_standbyHostManager == null) { _standbyScriptHostConfig = GetScriptHostConfiguration(settings.ScriptPath, settings.LogPath); _standbySecretManager = GetSecretManager(settings.SecretsPath); _standbyReceiverManager = new WebHookReceiverManager(_standbySecretManager); _standbyHostManager = new WebScriptHostManager(_standbyScriptHostConfig, _standbySecretManager, settings); } } }
public static void Reset() { _standbySecretManager?.Dispose(); _standbyHostManager?.Dispose(); _standbyReceiverManager?.Dispose(); _standbyScriptHostConfig = null; _standbySecretManager = null; _standbyHostManager = null; _standbyReceiverManager = null; _activeSecretManager?.Dispose(); _activeHostManager?.Dispose(); _activeReceiverManager?.Dispose(); _activeScriptHostConfig = null; _activeSecretManager = null; _activeHostManager = null; _activeReceiverManager = null; }
public WebScriptHostManager(ScriptHostConfiguration config, SecretManager secretManager, WebHostSettings webHostSettings, IScriptHostFactory scriptHostFactory = null) : base(config, scriptHostFactory) { _metricsLogger = new WebHostMetricsLogger(); _exceptionHandler = new WebScriptHostExceptionHandler(this); _secretManager = secretManager; _webHostSettings = webHostSettings; var systemEventGenerator = config.HostConfig.GetService <IEventGenerator>() ?? new EventGenerator(); var systemTraceWriter = new SystemTraceWriter(systemEventGenerator, TraceLevel.Verbose); if (config.TraceWriter != null) { config.TraceWriter = new CompositeTraceWriter(new TraceWriter[] { config.TraceWriter, systemTraceWriter }); } else { config.TraceWriter = systemTraceWriter; } }
public WebScriptHostManager(ScriptHostConfiguration config, SecretManager secretManager) : base(config) { _metricsLogger = new WebHostMetricsLogger(); _secretManager = secretManager; }
public Fixture() { EventGenerator = new TestSystemEventGenerator(); TestFunctionRoot = Path.Combine(TestHelpers.FunctionsTestDirectory, "Functions"); TestLogsRoot = Path.Combine(TestHelpers.FunctionsTestDirectory, "Logs"); TestSecretsRoot = Path.Combine(TestHelpers.FunctionsTestDirectory, "Secrets"); string testRoot = Path.Combine(TestFunctionRoot, Guid.NewGuid().ToString()); SecretsPath = Path.Combine(TestSecretsRoot, Guid.NewGuid().ToString()); Directory.CreateDirectory(SecretsPath); string logRoot = Path.Combine(TestLogsRoot, Guid.NewGuid().ToString(), @"Functions"); Directory.CreateDirectory(logRoot); FunctionsLogDir = Path.Combine(logRoot, @"Function"); Directory.CreateDirectory(FunctionsLogDir); // Add some secret files (both old and valid) File.WriteAllText(Path.Combine(SecretsPath, ScriptConstants.HostMetadataFileName), string.Empty); File.WriteAllText(Path.Combine(SecretsPath, "WebHookTrigger.json"), string.Empty); File.WriteAllText(Path.Combine(SecretsPath, "QueueTriggerToBlob.json"), string.Empty); File.WriteAllText(Path.Combine(SecretsPath, "Foo.json"), string.Empty); File.WriteAllText(Path.Combine(SecretsPath, "Bar.json"), string.Empty); File.WriteAllText(Path.Combine(SecretsPath, "Invalid.json"), string.Empty); // Add some old file directories CreateTestFunctionLogs(FunctionsLogDir, "Foo"); CreateTestFunctionLogs(FunctionsLogDir, "Bar"); CreateTestFunctionLogs(FunctionsLogDir, "Baz"); CreateTestFunctionLogs(FunctionsLogDir, "Invalid"); ScriptHostConfiguration config = new ScriptHostConfiguration { RootScriptPath = @"TestScripts\Node", RootLogPath = logRoot, FileLoggingMode = FileLoggingMode.Always }; ISecretManager secretManager = new SecretManager(SecretsPath); WebHostSettings webHostSettings = new WebHostSettings(); var hostConfig = config.HostConfig; var testEventGenerator = new TestSystemEventGenerator(); hostConfig.AddService <IEventGenerator>(EventGenerator); var mockHostManager = new WebScriptHostManager(config, secretManager, webHostSettings); HostManager = mockHostManager; Task task = Task.Run(() => { HostManager.RunAndBlock(); }); TestHelpers.Await(() => { return(HostManager.IsRunning); }).GetAwaiter().GetResult(); // verify startup system trace logs string[] expectedPatterns = new string[] { "Info Reading host configuration file", "Info Host lock lease acquired by instance ID '(.+)'", "Info Function 'Excluded' is marked as excluded", @"Info Generating ([0-9]+) job function\(s\)", @"Info Starting Host \(HostId=function-tests-node, Version=(.+), ProcessId=[0-9]+, Debug=False\)", "Info WebJobs.Indexing Found the following functions:", "Info The next 5 occurrences of the schedule will be:", "Info WebJobs.Host Job host started", "Error The following 1 functions are in error:" }; foreach (string pattern in expectedPatterns) { Assert.True(EventGenerator.Events.Any(p => Regex.IsMatch(p, pattern)), $"Expected trace event {pattern} not found."); } }
public WebScriptHostManager(ScriptHostConfiguration config, SecretManager secretManager, WebHostSettings webHostSettings) : base(config) { _metricsLogger = new WebHostMetricsLogger(); _secretManager = secretManager; _webHostSettings = webHostSettings; }
public WebScriptHostManager(ScriptHostConfiguration config, SecretManager secretManager, WebHostSettings webHostSettings) : this(config, secretManager, webHostSettings, new ScriptHostFactory()) { }