Exemple #1
0
        /// <summary>
        /// Parse the auth scheme from config to concrete type
        /// </summary>
        /// <param name="route"></param>
        /// <param name="configurationSection"></param>
        /// <param name="path"></param>
        public static void ParseAuthScheme(WebhookConfig route, IConfiguration configurationSection, string path)
        {
            if (route.AuthenticationConfig.Type == AuthenticationType.Basic)
            {
                var basicAuthenticationConfig = new BasicAuthenticationConfig
                {
                    Username = configurationSection[path + ":username"],
                    Password = configurationSection[path + ":password"]
                };
                route.AuthenticationConfig = basicAuthenticationConfig;
            }

            if (route.AuthenticationConfig.Type == AuthenticationType.OIDC)
            {
                route.AuthenticationConfig = ParseOidcAuthenticationConfig(configurationSection.GetSection(path));
            }

            if (route.AuthenticationConfig.Type != AuthenticationType.Custom)
            {
                return;
            }

            route.AuthenticationConfig      = ParseOidcAuthenticationConfig(configurationSection.GetSection(path));
            route.AuthenticationConfig.Type = AuthenticationType.Custom;
        }
Exemple #2
0
        public void ConfigNotEmpty()
        {
            var kvUri = "https://dgtest.vault.azure.net/";

            var config = new ConfigurationBuilder().AddAzureKeyVault(
                kvUri,
                new KeyVaultClient(
                    new KeyVaultClient.AuthenticationCallback(new AzureServiceTokenProvider()
                                                              .KeyVaultTokenCallback)),
                new DefaultKeyVaultSecretManager()).Build();

            //autowire up configs in keyvault to webhooks
            var section = config.GetSection("event");
            var values  = section.GetChildren().ToList();

            var eventHandlerList = new List <EventHandlerConfig>();
            var webhookList      = new List <WebhookConfig>(values.Count);

            foreach (var configurationSection in values)
            {
                //temp work around until config comes in through the API
                var eventHandlerConfig = configurationSection.Get <EventHandlerConfig>();
                eventHandlerList.Add(eventHandlerConfig);

                if (eventHandlerConfig.WebHookConfig != null)
                {
                    if (eventHandlerConfig.WebHookConfig.AuthenticationConfig.Type == AuthenticationType.Basic)
                    {
                        var basicAuthenticationConfig = new BasicAuthenticationConfig
                        {
                            Username = configurationSection["webhookconfig:authenticationconfig:username"],
                            Password = configurationSection["webhookconfig:authenticationconfig:password"]
                        };
                        eventHandlerConfig.WebHookConfig.AuthenticationConfig = basicAuthenticationConfig;
                    }

                    if (eventHandlerConfig.WebHookConfig.AuthenticationConfig.Type == AuthenticationType.OIDC)
                    {
                        eventHandlerConfig.WebHookConfig.AuthenticationConfig =
                            ParseOidcAuthenticationConfig(
                                configurationSection.GetSection("webhookconfig:authenticationconfig"));
                    }

                    if (eventHandlerConfig.WebHookConfig.AuthenticationConfig.Type == AuthenticationType.Custom)
                    {
                        eventHandlerConfig.WebHookConfig.AuthenticationConfig =
                            ParseOidcAuthenticationConfig(
                                configurationSection.GetSection("webhookconfig:authenticationconfig"));
                        eventHandlerConfig.WebHookConfig.AuthenticationConfig.Type = AuthenticationType.Custom;
                    }

                    webhookList.Add(eventHandlerConfig.WebHookConfig);
                }

                if (eventHandlerConfig.CallBackEnabled)
                {
                    if (eventHandlerConfig.CallbackConfig.AuthenticationConfig.Type == AuthenticationType.Basic)
                    {
                        var basicAuthenticationConfig = new BasicAuthenticationConfig
                        {
                            Username = configurationSection["webhookconfig:authenticationconfig:username"],
                            Password = configurationSection["webhookconfig:authenticationconfig:password"]
                        };
                        eventHandlerConfig.CallbackConfig.AuthenticationConfig = basicAuthenticationConfig;
                    }

                    if (eventHandlerConfig.CallbackConfig.AuthenticationConfig.Type == AuthenticationType.OIDC)
                    {
                        eventHandlerConfig.CallbackConfig.AuthenticationConfig =
                            ParseOidcAuthenticationConfig(
                                configurationSection.GetSection("callbackconfig:authenticationconfig"));
                    }

                    if (eventHandlerConfig.CallbackConfig.AuthenticationConfig.Type == AuthenticationType.Custom)
                    {
                        eventHandlerConfig.CallbackConfig.AuthenticationConfig =
                            ParseOidcAuthenticationConfig(
                                configurationSection.GetSection("callbackconfig:authenticationconfig"));
                        eventHandlerConfig.CallbackConfig.AuthenticationConfig.Type = AuthenticationType.Custom;
                    }

                    webhookList.Add(eventHandlerConfig.CallbackConfig);
                }
            }

            Assert.NotEmpty(eventHandlerList);
            Assert.NotEmpty(webhookList);
        }
Exemple #3
0
        public BasicAuthenticationHandler(AuthenticationConfig authenticationConfig)
        {
            var basicAuthenticationConfig = authenticationConfig as BasicAuthenticationConfig;

            BasicAuthenticationConfig = basicAuthenticationConfig ?? throw new ArgumentException($"configuration for basic authentication is not of type {typeof(BasicAuthenticationConfig)}", nameof(authenticationConfig));
        }
Exemple #4
0
        /// <summary>
        ///     This is the entry point of the service host process.
        /// </summary>
        private static async Task Main()
        {
            try
            {
                var kvUri = Environment.GetEnvironmentVariable(ConfigurationSettings.KeyVaultUriEnvVariable);

                var config = new ConfigurationBuilder().AddAzureKeyVault(
                    kvUri,
                    new KeyVaultClient(
                        new KeyVaultClient.AuthenticationCallback(new AzureServiceTokenProvider()
                                                                  .KeyVaultTokenCallback)),
                    new DefaultKeyVaultSecretManager()).Build();

                //autowire up configs in keyvault to webhooks
                var section = config.GetSection("event");
                var values  = section.GetChildren().ToList();

                var eventHandlerList = new List <EventHandlerConfig>();
                var webhookList      = new List <WebhookConfig>(values.Count);
                foreach (var configurationSection in values)
                {
                    //temp work around until config comes in through the API
                    var eventHandlerConfig = configurationSection.Get <EventHandlerConfig>();
                    eventHandlerList.Add(eventHandlerConfig);

                    if (eventHandlerConfig.WebHookConfig != null)
                    {
                        if (eventHandlerConfig.WebHookConfig.AuthenticationConfig.Type == AuthenticationType.Basic)
                        {
                            var basicAuthenticationConfig = new BasicAuthenticationConfig
                            {
                                Username = configurationSection["webhookconfig:authenticationconfig:username"],
                                Password = configurationSection["webhookconfig:authenticationconfig:password"]
                            };
                            eventHandlerConfig.WebHookConfig.AuthenticationConfig = basicAuthenticationConfig;
                        }

                        if (eventHandlerConfig.WebHookConfig.AuthenticationConfig.Type == AuthenticationType.OIDC)
                        {
                            eventHandlerConfig.WebHookConfig.AuthenticationConfig = ParseOidcAuthenticationConfig(configurationSection.GetSection("webhookconfig:authenticationconfig"));
                        }

                        if (eventHandlerConfig.WebHookConfig.AuthenticationConfig.Type == AuthenticationType.Custom)
                        {
                            eventHandlerConfig.WebHookConfig.AuthenticationConfig      = ParseOidcAuthenticationConfig(configurationSection.GetSection("webhookconfig:authenticationconfig"));
                            eventHandlerConfig.WebHookConfig.AuthenticationConfig.Type = AuthenticationType.Custom;
                        }

                        webhookList.Add(eventHandlerConfig.WebHookConfig);
                    }

                    if (eventHandlerConfig.CallBackEnabled)
                    {
                        if (eventHandlerConfig.CallbackConfig.AuthenticationConfig.Type == AuthenticationType.Basic)
                        {
                            var basicAuthenticationConfig = new BasicAuthenticationConfig
                            {
                                Username = configurationSection["webhookconfig:authenticationconfig:username"],
                                Password = configurationSection["webhookconfig:authenticationconfig:password"]
                            };
                            eventHandlerConfig.CallbackConfig.AuthenticationConfig = basicAuthenticationConfig;
                        }

                        if (eventHandlerConfig.CallbackConfig.AuthenticationConfig.Type == AuthenticationType.OIDC)
                        {
                            eventHandlerConfig.CallbackConfig.AuthenticationConfig = ParseOidcAuthenticationConfig(configurationSection.GetSection("callbackconfig:authenticationconfig"));
                        }

                        if (eventHandlerConfig.CallbackConfig.AuthenticationConfig.Type == AuthenticationType.Custom)
                        {
                            eventHandlerConfig.CallbackConfig.AuthenticationConfig      = ParseOidcAuthenticationConfig(configurationSection.GetSection("callbackconfig:authenticationconfig"));
                            eventHandlerConfig.CallbackConfig.AuthenticationConfig.Type = AuthenticationType.Custom;
                        }

                        webhookList.Add(eventHandlerConfig.CallbackConfig);
                    }
                }

                var settings = new ConfigurationSettings();
                config.Bind(settings);

                var bb = new BigBrother(settings.InstrumentationKey, settings.InstrumentationKey);
                bb.UseEventSourceSink().ForExceptions();

                var builder = new ContainerBuilder();
                builder.RegisterInstance(bb)
                .As <IBigBrother>()
                .SingleInstance();

                builder.RegisterInstance(settings)
                .SingleInstance();

                builder.RegisterType <EventHandlerFactory>().As <IEventHandlerFactory>().SingleInstance();
                builder.RegisterType <AuthenticationHandlerFactory>().As <IAuthHandlerFactory>().SingleInstance();

                //Register each webhook authenticationConfig separately for injection
                foreach (var setting in eventHandlerList)
                {
                    builder.RegisterInstance(setting).Named <EventHandlerConfig>(setting.Name);
                }

                foreach (var webhookConfig in webhookList)
                {
                    builder.RegisterInstance(webhookConfig).Named <WebhookConfig>(webhookConfig.Name);
                    builder.RegisterInstance(new HttpClient()).Named <HttpClient>(webhookConfig.Name).SingleInstance();
                }

                builder.RegisterServiceFabricSupport();
                builder.RegisterActor <EventHandlerActor>();

                using (builder.Build())
                {
                    await Task.Delay(Timeout.Infinite);
                }
            }
            catch (Exception e)
            {
                BigBrother.Write(e);
                throw;
            }
        }