private 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)
                {
                    _activeScriptHostConfig = CreateScriptHostConfiguration(settings);

                    _activeHostManager     = new WebScriptHostManager(_activeScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings);
                    _activeReceiverManager = new WebHookReceiverManager(_activeHostManager.SecretManager);

                    _standbyHostManager?.Dispose();
                    _standbyReceiverManager?.Dispose();

                    _standbyScriptHostConfig = null;
                    _standbyHostManager      = null;
                    _standbyReceiverManager  = null;
                    _settingsManager.Reset();
                }
            }
            else
            {
                if (_standbyHostManager == null)
                {
                    _standbyScriptHostConfig = CreateScriptHostConfiguration(settings);

                    _standbyHostManager     = new WebScriptHostManager(_standbyScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings);
                    _standbyReceiverManager = new WebHookReceiverManager(_standbyHostManager.SecretManager);
                }
            }
        }
        public void ApplyHeaderValuesToQuery_ValuesInHeadersAndQuery_ReturnsExpectedValue()
        {
            // query string value takes precedence
            var request = new HttpRequestMessage(HttpMethod.Post, "http://test.com/api/test?code=foo&clientid=bar");

            request.Headers.Add(AuthorizationLevelAttribute.FunctionsKeyHeaderName, TestKey);
            WebHookReceiverManager.ApplyHeaderValuesToQuery(request);
            Assert.Equal($"http://test.com/api/test?code=foo&clientid=bar", request.RequestUri.ToString());

            // case insensitive query param lookups
            request = new HttpRequestMessage(HttpMethod.Post, "http://test.com/api/test?CODE=foo&clientid=bar");
            request.Headers.Add(AuthorizationLevelAttribute.FunctionsKeyHeaderName, TestKey);
            WebHookReceiverManager.ApplyHeaderValuesToQuery(request);
            Assert.Equal($"http://test.com/api/test?CODE=foo&clientid=bar", request.RequestUri.ToString());

            // code via query param, id via header
            request = new HttpRequestMessage(HttpMethod.Post, $"http://test.com/api/test?code={TestKey}");
            request.Headers.Add(WebHookReceiverManager.FunctionsClientIdHeaderName, TestId);
            WebHookReceiverManager.ApplyHeaderValuesToQuery(request);
            Assert.Equal($"http://test.com/api/test?code={TestKey}", request.RequestUri.ToString());

            // id via query param, code via header
            request = new HttpRequestMessage(HttpMethod.Post, $"http://test.com/api/test?clientid={TestId}");
            request.Headers.Add(AuthorizationLevelAttribute.FunctionsKeyHeaderName, TestKey);
            WebHookReceiverManager.ApplyHeaderValuesToQuery(request);
            Assert.Equal($"http://test.com/api/test?clientid={TestId}&code={TestKey}", request.RequestUri.ToString());
        }
        public void ApplyHeaderValuesToQuery_CodeInHeaders_ReturnsExpectedValue()
        {
            var request = new HttpRequestMessage(HttpMethod.Post, $"http://test.com/api/test?clientid={TestId}");

            request.Headers.Add(AuthorizationLevelAttribute.FunctionsKeyHeaderName, TestKey);
            WebHookReceiverManager.ApplyHeaderValuesToQuery(request);
            Assert.Equal($"http://test.com/api/test?clientid={TestId}&code={TestKey}", request.RequestUri.ToString());
        }
 public WebHookDispatcher(WebHooksConfiguration webHooksConfig, JobHost host, JobHostConfiguration config, TraceWriter trace)
 {
     _functions = new ConcurrentDictionary<string, ITriggeredFunctionExecutor>();
     _trace = trace;
     _port = webHooksConfig.Port;
     _types = config.TypeLocator.GetTypes().ToArray();
     _host = host;
     _webHookReceiverManager = new WebHookReceiverManager(_trace);
 }
        public void ApplyHeaderValuesToQuery_NoHeadersPresent_ReturnsExpectedValue()
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://test.com/api/test");

            WebHookReceiverManager.ApplyHeaderValuesToQuery(request);
            Assert.Equal($"http://test.com/api/test", request.RequestUri.ToString());

            request = new HttpRequestMessage(HttpMethod.Post, $"http://test.com/api/test?code={TestKey}&clientid={TestId}");
            WebHookReceiverManager.ApplyHeaderValuesToQuery(request);
            Assert.Equal($"http://test.com/api/test?code={TestKey}&clientid={TestId}", request.RequestUri.ToString());
        }
        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());
            }
        }
        internal static void Initialize(ContainerBuilder builder)
        {
            string logFilePath;
            string scriptRootPath;
            string secretsPath;
            string home    = Environment.GetEnvironmentVariable("HOME");
            bool   isLocal = string.IsNullOrEmpty(home);

            if (isLocal)
            {
                // we're running locally
                scriptRootPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, @"..\..\sample");
                logFilePath    = Path.Combine(Path.GetTempPath(), @"Functions");
                secretsPath    = HttpContext.Current.Server.MapPath("~/App_Data/Secrets");
            }
            else
            {
                // we're running in Azure
                scriptRootPath = Path.Combine(home, @"site\wwwroot");
                logFilePath    = Path.Combine(home, @"LogFiles\Application\Functions");
                secretsPath    = Path.Combine(home, @"data\Functions\secrets");
            }

            ScriptHostConfiguration scriptHostConfig = new ScriptHostConfiguration()
            {
                RootScriptPath     = scriptRootPath,
                RootLogPath        = logFilePath,
                FileLoggingEnabled = true
            };
            WebScriptHostManager scriptHostManager = new WebScriptHostManager(scriptHostConfig);

            builder.RegisterInstance <WebScriptHostManager>(scriptHostManager);

            SecretManager secretManager = new SecretManager(secretsPath);

            builder.RegisterInstance <SecretManager>(secretManager);

            WebHookReceiverManager webHookRecieverManager = new WebHookReceiverManager(secretManager);

            builder.RegisterInstance <WebHookReceiverManager>(webHookRecieverManager);

            Task.Run(() => scriptHostManager.RunAndBlock(CancellationToken.None));
        }
        private 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(_settingsManager, settings.SecretsPath);
                    _activeReceiverManager  = new WebHookReceiverManager(_activeSecretManager);
                    _activeHostManager      = new WebScriptHostManager(_activeScriptHostConfig, _activeSecretManager, _settingsManager, settings);

                    (_standbySecretManager as IDisposable)?.Dispose();
                    _standbyHostManager?.Dispose();
                    _standbyReceiverManager?.Dispose();

                    _standbyScriptHostConfig = null;
                    _standbySecretManager    = null;
                    _standbyHostManager      = null;
                    _standbyReceiverManager  = null;
                    _settingsManager.Reset();
                }
            }
            else
            {
                if (_standbyHostManager == null)
                {
                    _standbyScriptHostConfig = GetScriptHostConfiguration(settings.ScriptPath, settings.LogPath);
                    _standbySecretManager    = GetSecretManager(_settingsManager, settings.SecretsPath);
                    _standbyReceiverManager  = new WebHookReceiverManager(_standbySecretManager);
                    _standbyHostManager      = new WebScriptHostManager(_standbyScriptHostConfig, _standbySecretManager, _settingsManager, settings);
                }
            }
        }
Пример #9
0
        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;
        }
        internal void EnsureInitialized(WebHostSettings settings)
        {
            if (!WebScriptHostManager.InStandbyMode)
            {
                // standby mode can only change from true to false
                // When standby mode changes, we reset all instances
                if (_activeHostManager == null)
                {
                    _activeScriptHostConfig = CreateScriptHostConfiguration(settings);
                    _activeHostManager      = new WebScriptHostManager(_activeScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings);
                    _activeReceiverManager  = new WebHookReceiverManager(_activeHostManager.SecretManager);
                    InitializeFileSystem();

                    if (_standbyHostManager != null)
                    {
                        // we're undergoing the one and only one
                        // standby mode specialization
                        _activeScriptHostConfig.TraceWriter.Info(Resources.HostSpecializationTrace);
                    }

                    _standbyHostManager?.Dispose();
                    _standbyReceiverManager?.Dispose();
                    _standbyScriptHostConfig = null;
                    _standbyHostManager      = null;
                    _standbyReceiverManager  = null;
                    _settingsManager.Reset();
                }
            }
            else
            {
                if (_standbyHostManager == null)
                {
                    _standbyScriptHostConfig = CreateScriptHostConfiguration(settings, true);
                    _standbyHostManager      = new WebScriptHostManager(_standbyScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings);
                    _standbyReceiverManager  = new WebHookReceiverManager(_standbyHostManager.SecretManager);

                    InitializeFileSystem();
                    StandbyManager.Initialize(_standbyScriptHostConfig);
                }
            }
        }
 public FunctionsController(WebScriptHostManager scriptHostManager, WebHookReceiverManager webHookReceiverManager)
 {
     _scriptHostManager      = scriptHostManager;
     _webHookReceiverManager = webHookReceiverManager;
 }
        public void Dispose()
        {
            if (_httpHost != null)
            {
                ((IDisposable)_httpHost).Dispose();
                _httpHost = null;
            }

            if (_webHookReceiverManager != null)
            {
                ((IDisposable)_webHookReceiverManager).Dispose();
                _webHookReceiverManager = null;
            }
        }