Beispiel #1
0
        public IOcelotAdministrationBuilder AddAdministration(string path, string secret)
        {
            var administrationPath = new AdministrationPath(path);

            //add identity server for admin area
            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration(secret);

            if (identityServerConfiguration != null)
            {
                AddIdentityServer(identityServerConfiguration, administrationPath);
            }

            _services.AddSingleton <IAdministrationPath>(administrationPath);
            return(new OcelotAdministrationBuilder(_services, _configurationRoot));
        }
Beispiel #2
0
        public IOcelotAdministrationBuilder AddAdministration(string path, string secret)
        {
            var administrationPath = new AdministrationPath(path);

            //add identity server for admin area
            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration(secret);

            if (identityServerConfiguration != null)
            {
                AddIdentityServer(identityServerConfiguration, administrationPath);
            }

            var descriptor = new ServiceDescriptor(typeof(IAdministrationPath), administrationPath);

            _services.Replace(descriptor);
            return(new OcelotAdministrationBuilder(_services, _configurationRoot));
        }
        public static IServiceCollection AddOcelot(this IServiceCollection services, IConfigurationRoot configurationRoot, Action <ConfigurationBuilderCachePart> settings)
        {
            var cacheManagerOutputCache  = CacheFactory.Build <HttpResponseMessage>("OcelotOutputCache", settings);
            var ocelotOutputCacheManager = new OcelotCacheManagerCache <HttpResponseMessage>(cacheManagerOutputCache);

            services.TryAddSingleton <ICacheManager <HttpResponseMessage> >(cacheManagerOutputCache);
            services.TryAddSingleton <IOcelotCache <HttpResponseMessage> >(ocelotOutputCacheManager);

            var ocelotConfigCacheManagerOutputCache = CacheFactory.Build <IOcelotConfiguration>("OcelotConfigurationCache", settings);
            var ocelotConfigCacheManager            = new OcelotCacheManagerCache <IOcelotConfiguration>(ocelotConfigCacheManagerOutputCache);

            services.TryAddSingleton <ICacheManager <IOcelotConfiguration> >(ocelotConfigCacheManagerOutputCache);
            services.TryAddSingleton <IOcelotCache <IOcelotConfiguration> >(ocelotConfigCacheManager);

            services.Configure <FileConfiguration>(configurationRoot);
            services.TryAddSingleton <IOcelotConfigurationCreator, FileOcelotConfigurationCreator>();
            services.TryAddSingleton <IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>();
            services.TryAddSingleton <IConfigurationValidator, FileConfigurationValidator>();
            services.TryAddSingleton <IClaimsToThingCreator, ClaimsToThingCreator>();
            services.TryAddSingleton <IAuthenticationOptionsCreator, AuthenticationOptionsCreator>();
            services.TryAddSingleton <IUpstreamTemplatePatternCreator, UpstreamTemplatePatternCreator>();
            services.TryAddSingleton <IRequestIdKeyCreator, RequestIdKeyCreator>();
            services.TryAddSingleton <IServiceProviderConfigurationCreator, ServiceProviderConfigurationCreator>();
            services.TryAddSingleton <IQoSOptionsCreator, QoSOptionsCreator>();
            services.TryAddSingleton <IReRouteOptionsCreator, ReRouteOptionsCreator>();
            services.TryAddSingleton <IRateLimitOptionsCreator, RateLimitOptionsCreator>();
            services.TryAddSingleton <IBaseUrlFinder, BaseUrlFinder>();

            var assembly = typeof(FileConfigurationController).GetTypeInfo().Assembly;

            services.AddMvcCore()
            .AddApplicationPart(assembly)
            .AddControllersAsServices()
            .AddAuthorization()
            .AddJsonFormatters();

            services.AddLogging();
            services.TryAddSingleton <IRegionCreator, RegionCreator>();
            services.TryAddSingleton <IFileConfigurationRepository, FileConfigurationRepository>();
            services.TryAddSingleton <IFileConfigurationSetter, FileConfigurationSetter>();
            services.TryAddSingleton <IFileConfigurationProvider, FileConfigurationProvider>();
            services.TryAddSingleton <IQosProviderHouse, QosProviderHouse>();
            services.TryAddSingleton <IQoSProviderFactory, QoSProviderFactory>();
            services.TryAddSingleton <IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>();
            services.TryAddSingleton <ILoadBalancerFactory, LoadBalancerFactory>();
            services.TryAddSingleton <ILoadBalancerHouse, LoadBalancerHouse>();
            services.TryAddSingleton <IOcelotLoggerFactory, AspDotNetLoggerFactory>();
            services.TryAddSingleton <IUrlBuilder, UrlBuilder>();
            services.TryAddSingleton <IRemoveOutputHeaders, RemoveOutputHeaders>();
            services.TryAddSingleton <IOcelotConfigurationProvider, OcelotConfigurationProvider>();
            services.TryAddSingleton <IClaimToThingConfigurationParser, ClaimToThingConfigurationParser>();
            services.TryAddSingleton <IClaimsAuthoriser, ClaimsAuthoriser>();
            services.TryAddSingleton <IScopesAuthoriser, ScopesAuthoriser>();
            services.TryAddSingleton <IAddClaimsToRequest, AddClaimsToRequest>();
            services.TryAddSingleton <IAddHeadersToRequest, AddHeadersToRequest>();
            services.TryAddSingleton <IAddQueriesToRequest, AddQueriesToRequest>();
            services.TryAddSingleton <IClaimsParser, ClaimsParser>();
            services.TryAddSingleton <IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
            services.TryAddSingleton <IUrlPathPlaceholderNameAndValueFinder, UrlPathPlaceholderNameAndValueFinder>();
            services.TryAddSingleton <IDownstreamPathPlaceholderReplacer, DownstreamTemplatePathPlaceholderReplacer>();
            services.TryAddSingleton <IDownstreamRouteFinder, DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
            services.TryAddSingleton <IHttpRequester, HttpClientHttpRequester>();
            services.TryAddSingleton <IHttpResponder, HttpContextResponder>();
            services.TryAddSingleton <IRequestCreator, HttpRequestCreator>();
            services.TryAddSingleton <IErrorsToHttpStatusCodeMapper, ErrorsToHttpStatusCodeMapper>();
            services.TryAddSingleton <IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();
            services.TryAddSingleton <IHttpClientCache, MemoryHttpClientCache>();
            services.TryAddSingleton <IRequestMapper, RequestMapper>();
            services.TryAddSingleton <IHttpHandlerOptionsCreator, HttpHandlerOptionsCreator>();

            // see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
            // could maybe use a scoped data repository
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.TryAddScoped <IRequestScopedDataRepository, HttpDataRepository>();
            services.AddMemoryCache();

            //Used to log the the start and ending of middleware
            services.TryAddSingleton <OcelotDiagnosticListener>();
            services.AddMiddlewareAnalysis();
            services.AddWebEncoders();

            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();

            if (identityServerConfiguration != null)
            {
                services.AddIdentityServer(identityServerConfiguration, configurationRoot);
            }

            return(services);
        }
        public static IServiceCollection AddOcelot(this IServiceCollection services, IConfigurationRoot configurationRoot)
        {
            services.Configure <FileConfiguration>(configurationRoot);
            services.AddSingleton <IOcelotConfigurationCreator, FileOcelotConfigurationCreator>();
            services.AddSingleton <IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>();
            services.AddSingleton <IConfigurationValidator, FileConfigurationValidator>();
            services.AddSingleton <IBaseUrlFinder, BaseUrlFinder>();
            services.AddSingleton <IClaimsToThingCreator, ClaimsToThingCreator>();
            services.AddSingleton <IAuthenticationOptionsCreator, AuthenticationOptionsCreator>();
            services.AddSingleton <IUpstreamTemplatePatternCreator, UpstreamTemplatePatternCreator>();
            services.AddSingleton <IRequestIdKeyCreator, RequestIdKeyCreator>();
            services.AddSingleton <IServiceProviderConfigurationCreator, ServiceProviderConfigurationCreator>();
            services.AddSingleton <IQoSOptionsCreator, QoSOptionsCreator>();
            services.AddSingleton <IReRouteOptionsCreator, ReRouteOptionsCreator>();
            services.AddSingleton <IRateLimitOptionsCreator, RateLimitOptionsCreator>();

            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();

            if (identityServerConfiguration != null)
            {
                services.AddSingleton <IIdentityServerConfiguration>(identityServerConfiguration);
                services.AddSingleton <IHashMatcher, HashMatcher>();
                services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddInMemoryApiResources(new List <ApiResource>
                {
                    new ApiResource
                    {
                        Name        = identityServerConfiguration.ApiName,
                        Description = identityServerConfiguration.Description,
                        Enabled     = identityServerConfiguration.Enabled,
                        DisplayName = identityServerConfiguration.ApiName,
                        Scopes      = identityServerConfiguration.AllowedScopes.Select(x => new Scope(x)).ToList(),
                        ApiSecrets  = new List <Secret>
                        {
                            new Secret
                            {
                                Value = identityServerConfiguration.ApiSecret.Sha256()
                            }
                        }
                    }
                })
                .AddInMemoryClients(new List <Client>
                {
                    new Client
                    {
                        ClientId          = identityServerConfiguration.ApiName,
                        AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                        ClientSecrets     = new List <Secret> {
                            new Secret(identityServerConfiguration.ApiSecret.Sha256())
                        },
                        AllowedScopes       = identityServerConfiguration.AllowedScopes,
                        AccessTokenType     = identityServerConfiguration.AccessTokenType,
                        Enabled             = identityServerConfiguration.Enabled,
                        RequireClientSecret = identityServerConfiguration.RequireClientSecret
                    }
                }).AddResourceOwnerValidator <OcelotResourceOwnerPasswordValidator>();
            }

            services.AddMvcCore()
            .AddAuthorization()
            .AddJsonFormatters();
            services.AddLogging();
            services.AddSingleton <IFileConfigurationRepository, FileConfigurationRepository>();
            services.AddSingleton <IFileConfigurationSetter, FileConfigurationSetter>();
            services.AddSingleton <IFileConfigurationProvider, FileConfigurationProvider>();
            services.AddSingleton <IQosProviderHouse, QosProviderHouse>();
            services.AddSingleton <IQoSProviderFactory, QoSProviderFactory>();
            services.AddSingleton <IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>();
            services.AddSingleton <ILoadBalancerFactory, LoadBalancerFactory>();
            services.AddSingleton <ILoadBalancerHouse, LoadBalancerHouse>();
            services.AddSingleton <IOcelotLoggerFactory, AspDotNetLoggerFactory>();
            services.AddSingleton <IUrlBuilder, UrlBuilder>();
            services.AddSingleton <IRemoveOutputHeaders, RemoveOutputHeaders>();
            services.AddSingleton <IOcelotConfigurationProvider, OcelotConfigurationProvider>();
            services.AddSingleton <IClaimToThingConfigurationParser, ClaimToThingConfigurationParser>();
            services.AddSingleton <IAuthoriser, ClaimsAuthoriser>();
            services.AddSingleton <IAddClaimsToRequest, AddClaimsToRequest>();
            services.AddSingleton <IAddHeadersToRequest, AddHeadersToRequest>();
            services.AddSingleton <IAddQueriesToRequest, AddQueriesToRequest>();
            services.AddSingleton <IClaimsParser, ClaimsParser>();
            services.AddSingleton <IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
            services.AddSingleton <IUrlPathPlaceholderNameAndValueFinder, UrlPathPlaceholderNameAndValueFinder>();
            services.AddSingleton <IDownstreamPathPlaceholderReplacer, DownstreamTemplatePathPlaceholderReplacer>();
            services.AddSingleton <IDownstreamRouteFinder, DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
            services.AddSingleton <IHttpRequester, HttpClientHttpRequester>();
            services.AddSingleton <IHttpResponder, HttpContextResponder>();
            services.AddSingleton <IRequestCreator, HttpRequestCreator>();
            services.AddSingleton <IErrorsToHttpStatusCodeMapper, ErrorsToHttpStatusCodeMapper>();
            services.AddSingleton <IAuthenticationHandlerFactory, AuthenticationHandlerFactory>();
            services.AddSingleton <IAuthenticationHandlerCreator, AuthenticationHandlerCreator>();
            services.AddSingleton <IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();

            // see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
            // could maybe use a scoped data repository
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped <IRequestScopedDataRepository, HttpDataRepository>();
            services.AddMemoryCache();
            return(services);
        }
Beispiel #5
0
        public void happy_path_only_exists_for_test_coverage_even_uncle_bob_probably_wouldnt_test_this()
        {
            var result = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();

            result.ApiName.ShouldBe("admin");
        }
        public static IServiceCollection AddOcelot(this IServiceCollection services, IConfigurationRoot configurationRoot, Action <ConfigurationBuilderCachePart> settings)
        {
            var cacheManagerOutputCache  = CacheFactory.Build <HttpResponseMessage>("OcelotOutputCache", settings);
            var ocelotOutputCacheManager = new OcelotCacheManagerCache <HttpResponseMessage>(cacheManagerOutputCache);

            services.TryAddSingleton <ICacheManager <HttpResponseMessage> >(cacheManagerOutputCache);
            services.TryAddSingleton <IOcelotCache <HttpResponseMessage> >(ocelotOutputCacheManager);

            var ocelotConfigCacheManagerOutputCache = CacheFactory.Build <IOcelotConfiguration>("OcelotConfigurationCache", settings);
            var ocelotConfigCacheManager            = new OcelotCacheManagerCache <IOcelotConfiguration>(ocelotConfigCacheManagerOutputCache);

            services.TryAddSingleton <ICacheManager <IOcelotConfiguration> >(ocelotConfigCacheManagerOutputCache);
            services.TryAddSingleton <IOcelotCache <IOcelotConfiguration> >(ocelotConfigCacheManager);

            services.Configure <FileConfiguration>(configurationRoot);
            services.TryAddSingleton <IOcelotConfigurationCreator, FileOcelotConfigurationCreator>();
            services.TryAddSingleton <IAuthenticationProviderConfigCreator, AuthenticationProviderConfigCreator>();
            services.TryAddSingleton <IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>();
            services.TryAddSingleton <IConfigurationValidator, FileConfigurationValidator>();
            services.TryAddSingleton <IBaseUrlFinder, BaseUrlFinder>();
            services.TryAddSingleton <IClaimsToThingCreator, ClaimsToThingCreator>();
            services.TryAddSingleton <IAuthenticationOptionsCreator, AuthenticationOptionsCreator>();
            services.TryAddSingleton <IUpstreamTemplatePatternCreator, UpstreamTemplatePatternCreator>();
            services.TryAddSingleton <IRequestIdKeyCreator, RequestIdKeyCreator>();
            services.TryAddSingleton <IServiceProviderConfigurationCreator, ServiceProviderConfigurationCreator>();
            services.TryAddSingleton <IQoSOptionsCreator, QoSOptionsCreator>();
            services.TryAddSingleton <IReRouteOptionsCreator, ReRouteOptionsCreator>();
            services.TryAddSingleton <IRateLimitOptionsCreator, RateLimitOptionsCreator>();
            services.TryAddSingleton <IHttpHandlerOptionsCreator, HttpHandlerOptionsCreator>();

            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();

            if (identityServerConfiguration != null)
            {
                services.TryAddSingleton <IIdentityServerConfiguration>(identityServerConfiguration);
                services.TryAddSingleton <IHashMatcher, HashMatcher>();
                var identityServerBuilder = services
                                            .AddIdentityServer(options => {
                    options.IssuerUri = "Ocelot";
                })
                                            .AddInMemoryApiResources(new List <ApiResource>
                {
                    new ApiResource
                    {
                        Name        = identityServerConfiguration.ApiName,
                        Description = identityServerConfiguration.Description,
                        Enabled     = identityServerConfiguration.Enabled,
                        DisplayName = identityServerConfiguration.ApiName,
                        Scopes      = identityServerConfiguration.AllowedScopes.Select(x => new Scope(x)).ToList(),
                        ApiSecrets  = new List <Secret>
                        {
                            new Secret
                            {
                                Value = identityServerConfiguration.ApiSecret.Sha256()
                            }
                        }
                    }
                })
                                            .AddInMemoryClients(new List <Client>
                {
                    new Client
                    {
                        ClientId          = identityServerConfiguration.ApiName,
                        AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                        ClientSecrets     = new List <Secret> {
                            new Secret(identityServerConfiguration.ApiSecret.Sha256())
                        },
                        AllowedScopes       = identityServerConfiguration.AllowedScopes,
                        AccessTokenType     = identityServerConfiguration.AccessTokenType,
                        Enabled             = identityServerConfiguration.Enabled,
                        RequireClientSecret = identityServerConfiguration.RequireClientSecret
                    }
                }).AddResourceOwnerValidator <OcelotResourceOwnerPasswordValidator>();

                if (string.IsNullOrEmpty(identityServerConfiguration.CredentialsSigningCertificateLocation) || string.IsNullOrEmpty(identityServerConfiguration.CredentialsSigningCertificatePassword))
                {
                    identityServerBuilder.AddTemporarySigningCredential();
                }
                else
                {
                    var cert = new X509Certificate2(identityServerConfiguration.CredentialsSigningCertificateLocation, identityServerConfiguration.CredentialsSigningCertificatePassword);
                    identityServerBuilder.AddSigningCredential(cert);
                }
            }

            var assembly = typeof(FileConfigurationController).GetTypeInfo().Assembly;

            services.AddMvcCore()
            .AddApplicationPart(assembly)
            .AddControllersAsServices()
            .AddAuthorization()
            .AddJsonFormatters();

            services.AddLogging();
            services.TryAddSingleton <IRegionCreator, RegionCreator>();
            services.TryAddSingleton <IFileConfigurationRepository, FileConfigurationRepository>();
            services.TryAddSingleton <IFileConfigurationSetter, FileConfigurationSetter>();
            services.TryAddSingleton <IFileConfigurationProvider, FileConfigurationProvider>();
            services.TryAddSingleton <IQosProviderHouse, QosProviderHouse>();
            services.TryAddSingleton <IQoSProviderFactory, QoSProviderFactory>();
            services.TryAddSingleton <IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>();
            services.TryAddSingleton <ILoadBalancerFactory, LoadBalancerFactory>();
            services.TryAddSingleton <ILoadBalancerHouse, LoadBalancerHouse>();
            services.TryAddSingleton <IOcelotLoggerFactory, AspDotNetLoggerFactory>();
            services.TryAddSingleton <IUrlBuilder, UrlBuilder>();
            services.TryAddSingleton <IRemoveOutputHeaders, RemoveOutputHeaders>();
            services.TryAddSingleton <IOcelotConfigurationProvider, OcelotConfigurationProvider>();
            services.TryAddSingleton <IClaimToThingConfigurationParser, ClaimToThingConfigurationParser>();
            services.TryAddSingleton <IClaimsAuthoriser, ClaimsAuthoriser>();
            services.TryAddSingleton <IScopesAuthoriser, ScopesAuthoriser>();
            services.TryAddSingleton <IAddClaimsToRequest, AddClaimsToRequest>();
            services.TryAddSingleton <IAddHeadersToRequest, AddHeadersToRequest>();
            services.TryAddSingleton <IAddQueriesToRequest, AddQueriesToRequest>();
            services.TryAddSingleton <IClaimsParser, ClaimsParser>();
            services.TryAddSingleton <IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
            services.TryAddSingleton <IUrlPathPlaceholderNameAndValueFinder, UrlPathPlaceholderNameAndValueFinder>();
            services.TryAddSingleton <IDownstreamPathPlaceholderReplacer, DownstreamTemplatePathPlaceholderReplacer>();
            services.TryAddSingleton <IDownstreamRouteFinder, DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
            services.TryAddSingleton <IHttpRequester, HttpClientHttpRequester>();
            services.TryAddSingleton <IHttpResponder, HttpContextResponder>();
            services.TryAddSingleton <IRequestCreator, HttpRequestCreator>();
            services.TryAddSingleton <IErrorsToHttpStatusCodeMapper, ErrorsToHttpStatusCodeMapper>();
            services.TryAddSingleton <IAuthenticationHandlerFactory, AuthenticationHandlerFactory>();
            services.TryAddSingleton <IAuthenticationHandlerCreator, AuthenticationHandlerCreator>();
            services.TryAddSingleton <IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();
            services.TryAddSingleton <IHttpClientCache, MemoryHttpClientCache>();
            services.TryAddSingleton <IRequestMapper, RequestMapper>();

            // see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
            // could maybe use a scoped data repository
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.TryAddScoped <IRequestScopedDataRepository, HttpDataRepository>();
            services.AddMemoryCache();

            //Used to log the the start and ending of middleware
            services.TryAddSingleton <OcelotDiagnosticListener>();
            services.AddMiddlewareAnalysis();

            return(services);
        }