Beispiel #1
0
        public IOcelotBuilder AddCacheManager(Action <ConfigurationBuilderCachePart> settings)
        {
            var cacheManagerOutputCache  = CacheFactory.Build <CachedResponse>("OcelotOutputCache", settings);
            var ocelotOutputCacheManager = new OcelotCacheManagerCache <CachedResponse>(cacheManagerOutputCache);

            _services.RemoveAll(typeof(ICacheManager <CachedResponse>));
            _services.RemoveAll(typeof(IOcelotCache <CachedResponse>));
            _services.AddSingleton <ICacheManager <CachedResponse> >(cacheManagerOutputCache);
            _services.AddSingleton <IOcelotCache <CachedResponse> >(ocelotOutputCacheManager);

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

            _services.RemoveAll(typeof(ICacheManager <IInternalConfiguration>));
            _services.RemoveAll(typeof(IOcelotCache <IInternalConfiguration>));
            _services.AddSingleton <ICacheManager <IInternalConfiguration> >(ocelotConfigCacheManagerOutputCache);
            _services.AddSingleton <IOcelotCache <IInternalConfiguration> >(ocelotConfigCacheManager);

            var fileConfigCacheManagerOutputCache = CacheFactory.Build <FileConfiguration>("FileConfigurationCache", settings);
            var fileConfigCacheManager            = new OcelotCacheManagerCache <FileConfiguration>(fileConfigCacheManagerOutputCache);

            _services.RemoveAll(typeof(ICacheManager <FileConfiguration>));
            _services.RemoveAll(typeof(IOcelotCache <FileConfiguration>));
            _services.AddSingleton <ICacheManager <FileConfiguration> >(fileConfigCacheManagerOutputCache);
            _services.AddSingleton <IOcelotCache <FileConfiguration> >(fileConfigCacheManager);
            return(this);
        }
        public static IServiceCollection AddOcelotOutputCaching(this IServiceCollection services, Action <ConfigurationBuilderCachePart> settings)
        {
            var cacheManagerOutputCache = CacheFactory.Build <HttpResponseMessage>("OcelotOutputCache", settings);
            var ocelotCacheManager      = new OcelotCacheManagerCache <HttpResponseMessage>(cacheManagerOutputCache);

            services.AddSingleton <ICacheManager <HttpResponseMessage> >(cacheManagerOutputCache);
            services.AddSingleton <IOcelotCache <HttpResponseMessage> >(ocelotCacheManager);

            return(services);
        }
        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, 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);
        }
Beispiel #5
0
 public CacheManagerCacheTests()
 {
     _mockCacheManager         = new Mock <ICacheManager <string> >();
     _ocelotOcelotCacheManager = new OcelotCacheManagerCache <string>(_mockCacheManager.Object);
 }