/// <summary> /// Register the Info 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> /// <param name="contributors">Contributors to application information</param> public static void RegisterInfoActuator(this ContainerBuilder container, IConfiguration config, params IInfoContributor[] contributors) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } foreach (var c in contributors) { container.RegisterInstance(c).As <IInfoContributor>().SingleInstance(); } container.Register(c => { var options = new InfoEndpointOptions(config); var mgmtOptions = c.Resolve <IEnumerable <IManagementOptions> >(); foreach (var mgmt in mgmtOptions) { mgmt.EndpointOptions.Add(options); } return(options); }).As <IInfoOptions>().IfNotRegistered(typeof(IInfoOptions)).SingleInstance(); container.RegisterType <InfoEndpoint>().IfNotRegistered(typeof(InfoEndpoint)).As <IEndpoint <Dictionary <string, object> > >().SingleInstance(); container.RegisterType <EndpointOwinMiddleware <Dictionary <string, object> > >().IfNotRegistered(typeof(EndpointOwinMiddleware <Dictionary <string, object> >)).SingleInstance(); }
public void Invoke_HonorsEndpointEnabled_ReturnsExpectedLinks() { var infoOpts = new InfoEndpointOptions { Enabled = false }; var cloudOpts = new HypermediaEndpointOptions(); var mgmtOptions = new ActuatorManagementOptions(); mgmtOptions.EndpointOptions.AddRange(new List <IEndpointOptions>() { infoOpts, cloudOpts }); var ep = new ActuatorEndpoint(cloudOpts, new List <IManagementOptions> { mgmtOptions }); var info = ep.Invoke("http://localhost:5000/foobar"); Assert.NotNull(info); Assert.NotNull(info._links); Assert.True(info._links.ContainsKey("self")); Assert.Equal("http://localhost:5000/foobar", info._links["self"].href); Assert.False(info._links.ContainsKey("info")); Assert.Single(info._links); }
public void Constructor_BindsConfigurationCorrectly() { var appsettings = new Dictionary <string, string>() { ["management:endpoints:enabled"] = "false", ["management:endpoints:path"] = "/cloudfoundryapplication", ["management:endpoints:info:enabled"] = "true", ["management:endpoints:info:path"] = "infopath", ["management:endpoints:cloudfoundry:validatecertificates"] = "false", ["management:endpoints:cloudfoundry:enabled"] = "true" }; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); var config = configurationBuilder.Build(); var opts = new InfoEndpointOptions(config); var actOpts = new HypermediaEndpointOptions(config); Assert.Equal("info", opts.Id); Assert.Equal("infopath", opts.Path); }
/// <summary> /// Add Info actuator endpoint to OWIN Pipeline /// </summary> /// <param name="builder">OWIN <see cref="IAppBuilder" /></param> /// <param name="config"><see cref="IConfiguration"/> of application for configuring info endpoint</param> /// <param name="contributors">IInfo Contributors to collect into from</param> /// <param name="loggerFactory">For logging within the middleware</param> /// <returns>OWIN <see cref="IAppBuilder" /> with Info Endpoint added</returns> public static IAppBuilder UseInfoActuator(this IAppBuilder builder, IConfiguration config, IList <IInfoContributor> contributors, ILoggerFactory loggerFactory = null) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } if (contributors == null) { throw new ArgumentNullException(nameof(contributors)); } var mgmtOptions = ManagementOptions.Get(config); var options = new InfoEndpointOptions(config); foreach (var mgmt in mgmtOptions) { mgmt.EndpointOptions.Add(options); } var endpoint = new InfoEndpoint(options, contributors, loggerFactory?.CreateLogger <InfoEndpoint>()); var logger = loggerFactory?.CreateLogger <EndpointOwinMiddleware <Dictionary <string, object> > >(); return(builder.Use <EndpointOwinMiddleware <Dictionary <string, object> > >(endpoint, mgmtOptions, new List <HttpMethod> { HttpMethod.Get }, true, logger)); }
public void Constructor_InitializesWithDefaults() { var opts = new InfoEndpointOptions(); Assert.Null(opts.Enabled); Assert.Equal("info", opts.Id); }
public void Invoke_HandlesExceptions() { var opts = new InfoEndpointOptions(); var contributors = new List <IInfoContributor>() { new TestContrib(), new TestContrib(true), new TestContrib() }; var ep = new InfoEndpoint(opts, contributors, GetLogger <InfoEndpoint>()); var info = ep.Invoke(); foreach (var contrib in contributors) { var tc = (TestContrib)contrib; if (tc.Throws) { Assert.False(tc.Called); } else { Assert.True(tc.Called); } } }
public void RoutesByPathAndVerb() { var options = new InfoEndpointOptions(); Assert.True(options.ExactMatch); Assert.Equal("/actuator/info", options.GetContextPath(new ActuatorManagementOptions())); Assert.Equal("/cloudfoundryapplication/info", options.GetContextPath(new CloudFoundryManagementOptions())); Assert.Null(options.AllowedVerbs); }
public void IsExposedCorrectly() { var mgmtOptions = new ActuatorManagementOptions(); mgmtOptions.Exposure.Exclude = new string[] { "*" }.ToList(); var options = new InfoEndpointOptions(); Assert.False(options.IsExposed(mgmtOptions)); }
public void Invoke_NoContributors_ReturnsExpectedInfo() { var opts = new InfoEndpointOptions(); var contributors = new List <IInfoContributor>(); var ep = new InfoEndpoint(opts, contributors, GetLogger <InfoEndpoint>()); var info = ep.Invoke(); Assert.NotNull(info); Assert.Empty(info); }
public void InfoEndpointMiddleware_PathAndVerbMatching_ReturnsExpected() { var opts = new InfoEndpointOptions(); var mopts = TestHelpers.GetManagementOptions(opts); var contribs = new List <IInfoContributor>() { new GitInfoContributor() }; var ep = new InfoEndpoint(opts, contribs); var middle = new InfoEndpointMiddleware(null, ep, mopts); Assert.True(middle.RequestVerbAndPathMatch("GET", "/cloudfoundryapplication/info")); Assert.False(middle.RequestVerbAndPathMatch("PUT", "/cloudfoundryapplication/info")); Assert.False(middle.RequestVerbAndPathMatch("GET", "/cloudfoundryapplication/badpath")); }
public void Invoke_CallsAllContributors() { var opts = new InfoEndpointOptions(); var contributors = new List <IInfoContributor>() { new TestContrib(), new TestContrib(), new TestContrib() }; var ep = new InfoEndpoint(opts, contributors, GetLogger <InfoEndpoint>()); var info = ep.Invoke(); foreach (var contrib in contributors) { var tc = (TestContrib)contrib; Assert.True(tc.Called); } }
public void Invoke_CloudFoundryDisable_ReturnsExpectedLinks() { var infoOpts = new InfoEndpointOptions { Enabled = true }; var cloudOpts = new CloudFoundryEndpointOptions { Enabled = false }; var mgmtOptions = TestHelper.GetManagementOptions(infoOpts, cloudOpts); var ep = new CloudFoundryEndpoint(cloudOpts, mgmtOptions); var info = ep.Invoke("http://localhost:5000/foobar"); Assert.NotNull(info); Assert.NotNull(info._links); Assert.Empty(info._links); }
public async void InfoInvoke_ReturnsExpected() { // arrange var opts = new InfoEndpointOptions(); var ep = new TestInfoEndpoint(opts, new List <IInfoContributor>() { new GitInfoContributor() }); var mgmtOpts = TestHelpers.GetManagementOptions(opts); var middle = new EndpointOwinMiddleware <Dictionary <string, object> >(null, ep, mgmtOpts); var context = OwinTestHelpers.CreateRequest("GET", "/cloudfoundryapplication/info"); // act var json = await middle.InvokeAndReadResponse(context); // assert Assert.Equal("{}", json); }
public void Invoke_HonorsEndpointEnabled_ReturnsExpectedLinks() { var infoOpts = new InfoEndpointOptions { Enabled = false }; var cloudOpts = new CloudFoundryEndpointOptions(); var mgmtOptions = TestHelper.GetManagementOptions(infoOpts, cloudOpts); var ep = new CloudFoundryEndpoint(cloudOpts, mgmtOptions); var info = ep.Invoke("http://localhost:5000/foobar"); Assert.NotNull(info); Assert.NotNull(info._links); Assert.True(info._links.ContainsKey("self")); Assert.Equal("http://localhost:5000/foobar", info._links["self"].href); Assert.False(info._links.ContainsKey("info")); Assert.Single(info._links); }
public void Constructor_BindsConfigurationCorrectly() { var appsettings = new Dictionary <string, string>() { ["management:endpoints:enabled"] = "false", ["management:endpoints:path"] = "/management", ["management:endpoints:info:enabled"] = "false", ["management:endpoints:info:id"] = "infomanagement" }; var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); var config = configurationBuilder.Build(); var opts = new InfoEndpointOptions(config); Assert.False(opts.Enabled); Assert.Equal("infomanagement", opts.Id); }
public async void HandleInfoRequestAsync_ReturnsExpected() { var opts = new InfoEndpointOptions(); var mopts = TestHelpers.GetManagementOptions(opts); var contribs = new List <IInfoContributor>() { new GitInfoContributor() }; var ep = new TestInfoEndpoint(opts, contribs); var middle = new InfoEndpointMiddleware(null, ep, mopts); var context = CreateRequest("GET", "/loggers"); await middle.HandleInfoRequestAsync(context); context.Response.Body.Seek(0, SeekOrigin.Begin); StreamReader rdr = new StreamReader(context.Response.Body); string json = await rdr.ReadToEndAsync(); Assert.Equal("{}", json); }
/// <summary> /// Adds the services used by the Info 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 AddInfoActuatorServices(this IServiceCollection services, IConfiguration configuration) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var options = new InfoEndpointOptions(configuration); services.TryAddSingleton <IInfoOptions>(options); services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IEndpointOptions), options)); services.TryAddSingleton <InfoEndpoint>(); return(services); }
public static void UseInfoActuator(IConfiguration configuration, IEnumerable <IInfoContributor> contributors = null, ILoggerFactory loggerFactory = null) { var options = new InfoEndpointOptions(configuration); _mgmtOptions.RegisterEndpointOptions(configuration, options); if (ConfiguredHandlers.OfType <InfoHandler>().Any()) { return; } contributors = contributors ?? new List <IInfoContributor>() { new GitInfoContributor(), new AppSettingsInfoContributor(configuration) }; var ep = new InfoEndpoint(options, contributors, CreateLogger <InfoEndpoint>(loggerFactory)); var handler = new InfoHandler(ep, SecurityServices, _mgmtOptions, CreateLogger <InfoHandler>(loggerFactory)); ConfiguredHandlers.Add(handler); }
public void Invoke_ReturnsExpectedLinks() { var infoOpts = new InfoEndpointOptions(); var cloudOpts = new CloudFoundryEndpointOptions(); var mgmtOptions = new CloudFoundryManagementOptions(); mgmtOptions.EndpointOptions.Add(infoOpts); mgmtOptions.EndpointOptions.Add(cloudOpts); var ep = new CloudFoundryEndpoint(cloudOpts, mgmtOptions, null); var info = ep.Invoke("http://localhost:5000/foobar"); Assert.NotNull(info); Assert.NotNull(info._links); Assert.True(info._links.ContainsKey("self")); Assert.Equal("http://localhost:5000/foobar", info._links["self"].Href); Assert.True(info._links.ContainsKey("info")); Assert.Equal("http://localhost:5000/foobar/info", info._links["info"].Href); Assert.Equal(2, info._links.Count); }
public void Invoke_CloudFoundryDisable_ReturnsExpectedLinks() { var infoOpts = new InfoEndpointOptions { Enabled = true }; var cloudOpts = new HypermediaEndpointOptions { Enabled = false }; var mgmtOptions = new ActuatorManagementOptions(); mgmtOptions.EndpointOptions.AddRange(new List <IEndpointOptions>() { infoOpts, cloudOpts }); var ep = new ActuatorEndpoint(cloudOpts, mgmtOptions); var info = ep.Invoke("http://localhost:5000/foobar"); Assert.NotNull(info); Assert.NotNull(info._links); Assert.Empty(info._links); }
public void Invoke_ReturnsExpectedLinks() { var mgmtOptions = new ActuatorManagementOptions(); var infoOpts = new InfoEndpointOptions(); var cloudOpts = new HypermediaEndpointOptions(); mgmtOptions.EndpointOptions.AddRange(new List <IEndpointOptions>() { infoOpts, cloudOpts }); var ep = new ActuatorEndpoint(cloudOpts, mgmtOptions); var info = ep.Invoke("http://localhost:5000/foobar"); Assert.NotNull(info); Assert.NotNull(info._links); Assert.True(info._links.ContainsKey("self")); Assert.Equal("http://localhost:5000/foobar", info._links["self"].Href); Assert.True(info._links.ContainsKey("info")); Assert.Equal("http://localhost:5000/foobar/info", info._links["info"].Href); Assert.Equal(2, info._links.Count); }