public void DoInvoke_ReturnsExpected() { var opts = new RefreshEndpointOptions(); var appsettings = new Dictionary <string, string>() { ["management:endpoints:enabled"] = "false", ["management:endpoints:path"] = "/cloudfoundryapplication", ["management:endpoints:loggers:enabled"] = "false", ["management:endpoints:heapdump:enabled"] = "true", ["management:endpoints:cloudfoundry:validatecertificates"] = "true", ["management:endpoints:cloudfoundry:enabled"] = "true" }; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); var config = configurationBuilder.Build(); var ep = new RefreshEndpoint(opts, config); var result = ep.DoInvoke(config); Assert.NotNull(result); Assert.Contains("management:endpoints:loggers:enabled", result); Assert.Contains("management:endpoints:cloudfoundry:enabled", result); }
public async void HandleRefreshRequestAsync_ReturnsExpected() { var opts = new RefreshEndpointOptions(); var mopts = new ActuatorManagementOptions(); mopts.EndpointOptions.Add(opts); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(AppSettings); var config = configurationBuilder.Build(); var ep = new RefreshEndpoint(opts, config); var middle = new RefreshEndpointMiddleware(null, ep, mopts); var context = CreateRequest("GET", "/refresh"); await middle.HandleRefreshRequestAsync(context); context.Response.Body.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(context.Response.Body, Encoding.UTF8); var json = await reader.ReadLineAsync(); var expected = "[\"management\",\"management:endpoints\",\"management:endpoints:enabled\",\"Logging\",\"Logging:LogLevel\",\"Logging:LogLevel:Steeltoe\",\"Logging:LogLevel:Pivotal\",\"Logging:LogLevel:Default\",\"Logging:IncludeScopes\"]"; Assert.Equal(expected, json); }
/// <summary> /// Register the Refresh endpoint, OWIN middleware and options /// </summary> /// <param name="container">Autofac DI <see cref="ContainerBuilder"/></param> /// <param name="config">Your application's <see cref="IConfiguration"/></param> public static void RegisterRefreshActuator(this ContainerBuilder container, IConfiguration config) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } container.Register(c => { var options = new RefreshEndpointOptions(config); var mgmtOptions = c.Resolve <IEnumerable <IManagementOptions> >(); foreach (var mgmt in mgmtOptions) { mgmt.EndpointOptions.Add(options); } return(options); }).As <IRefreshOptions>().IfNotRegistered(typeof(IRefreshOptions)); container.RegisterType <RefreshEndpoint>().As <IEndpoint <IList <string> > >().SingleInstance(); container.RegisterType <EndpointOwinMiddleware <IList <string> > >().SingleInstance(); }
/// <summary> /// Add (Config) Refresh actuator endpoint to OWIN Pipeline /// </summary> /// <param name="builder">OWIN <see cref="IAppBuilder" /></param> /// <param name="config"><see cref="IConfiguration"/> of application for configuring refresh endpoint</param> /// <param name="loggerFactory">For logging within the middleware</param> /// <returns>OWIN <see cref="IAppBuilder" /> with Refresh Endpoint added</returns> public static IAppBuilder UseRefreshActuator(this IAppBuilder builder, IConfiguration config, ILoggerFactory loggerFactory = null) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } IRefreshOptions options = new RefreshEndpointOptions(config); var mgmtOptions = ManagementOptions.Get(config); foreach (var mgmt in mgmtOptions) { mgmt.EndpointOptions.Add(options); } var endpoint = new RefreshEndpoint(options, config, loggerFactory?.CreateLogger <RefreshEndpoint>()); var logger = loggerFactory?.CreateLogger <EndpointOwinMiddleware <IList <string> > >(); return(builder.Use <EndpointOwinMiddleware <IList <string> > >(endpoint, mgmtOptions, new List <HttpMethod> { HttpMethod.Get }, true, logger)); }
public void Constructor_InitializesWithDefaults() { var opts = new RefreshEndpointOptions(); Assert.Null(opts.Enabled); Assert.Equal("refresh", opts.Id); Assert.Equal(Permissions.RESTRICTED, opts.RequiredPermissions); }
public void RoutesByPathAndVerb() { var options = new RefreshEndpointOptions(); Assert.True(options.ExactMatch); Assert.Equal("/actuator/refresh", options.GetContextPath(new ActuatorManagementOptions())); Assert.Equal("/cloudfoundryapplication/refresh", options.GetContextPath(new CloudFoundryManagementOptions())); Assert.Null(options.AllowedVerbs); }
public static void UseRefreshActuator(IConfiguration configuration, ILoggerFactory loggerFactory = null) { var options = new RefreshEndpointOptions(configuration); _mgmtOptions.RegisterEndpointOptions(configuration, options); var ep = new RefreshEndpoint(options, configuration, CreateLogger <RefreshEndpoint>(loggerFactory)); var handler = new RefreshHandler(ep, SecurityServices, _mgmtOptions, CreateLogger <RefreshHandler>(loggerFactory)); ConfiguredHandlers.Add(handler); }
public void Constructor_ThrowsIfNulls() { IRefreshOptions options = null; IConfigurationRoot configuration = null; Assert.Throws <ArgumentNullException>(() => new RefreshEndpoint(options, configuration)); options = new RefreshEndpointOptions(); Assert.Throws <ArgumentNullException>(() => new RefreshEndpoint(options, configuration)); }
public void RefreshEndpointMiddleware_PathAndVerbMatching_ReturnsExpected() { var opts = new RefreshEndpointOptions(); var mopts = TestHelpers.GetManagementOptions(opts); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(AppSettings); var config = configurationBuilder.Build(); var ep = new RefreshEndpoint(opts, config); var middle = new RefreshEndpointMiddleware(null, ep, mopts); Assert.True(middle.RequestVerbAndPathMatch("GET", "/cloudfoundryapplication/refresh")); Assert.False(middle.RequestVerbAndPathMatch("PUT", "/cloudfoundryapplication/refresh")); Assert.False(middle.RequestVerbAndPathMatch("GET", "/cloudfoundryapplication/badpath")); }
public async void RefreshInvoke_ReturnsExpected() { // arrange ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(OwinTestHelpers.Appsettings); var opts = new RefreshEndpointOptions(); var mopts = TestHelpers.GetManagementOptions(opts); var middle = new EndpointOwinMiddleware <IList <string> >(null, new RefreshEndpoint(opts, configurationBuilder.Build()), mopts); var context = OwinTestHelpers.CreateRequest("GET", "/cloudfoundryapplication/refresh"); // act var json = await middle.InvokeAndReadResponse(context); // assert var expected = "[\"management\",\"management:endpoints\",\"management:endpoints:path\",\"management:endpoints:enabled\",\"Logging\",\"Logging:LogLevel\",\"Logging:LogLevel:Steeltoe\",\"Logging:LogLevel:Pivotal\",\"Logging:LogLevel:Default\",\"Logging:IncludeScopes\"]"; Assert.Equal(expected, json); }
/// <summary> /// Adds the services used by the Refresh actuator /// </summary> /// <param name="services">Reference to the service collection</param> /// <param name="configuration">Reference to the configuration system</param> /// <returns>A reference to the service collection</returns> public static IServiceCollection AddRefreshActuatorServices(this IServiceCollection services, IConfiguration configuration) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var options = new RefreshEndpointOptions(configuration); services.TryAddSingleton <IRefreshOptions>(options); services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IEndpointOptions), options)); services.TryAddSingleton <RefreshEndpoint>(); return(services); }