public void EnvEndpointMiddleware_PathAndVerbMatching_ReturnsExpected() { var opts = new EnvEndpointOptions(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appSettings); var config = configurationBuilder.Build(); var host = new HostingEnvironment() { EnvironmentName = "EnvironmentName" }; var ep = new EnvEndpoint(opts, config, host); var mgmt = new CloudFoundryManagementOptions() { Path = "/" }; mgmt.EndpointOptions.Add(opts); var middle = new EnvEndpointMiddleware(null, ep, new List <IManagementOptions> { mgmt }); Assert.True(middle.RequestVerbAndPathMatch("GET", "/env")); Assert.False(middle.RequestVerbAndPathMatch("PUT", "/env")); Assert.False(middle.RequestVerbAndPathMatch("GET", "/badpath")); }
public async void HandleEnvRequestAsync_ReturnsExpected() { var opts = new EnvEndpointOptions(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appSettings); var config = configurationBuilder.Build(); var host = new HostingEnvironment() { EnvironmentName = "EnvironmentName" }; var mgmtOptions = TestHelpers.GetManagementOptions(opts); var ep = new EnvEndpoint(opts, config, host); var middle = new EnvEndpointMiddleware(null, ep, mgmtOptions); var context = CreateRequest("GET", "/env"); await middle.HandleEnvRequestAsync(context); context.Response.Body.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(context.Response.Body, Encoding.UTF8); var json = await reader.ReadLineAsync(); var expected = "{\"activeProfiles\":[\"EnvironmentName\"],\"propertySources\":[{\"properties\":{\"management:endpoints:path\":{\"value\":\"/cloudfoundryapplication\"},\"management:endpoints:enabled\":{\"value\":\"true\"},\"Logging:LogLevel:Steeltoe\":{\"value\":\"Information\"},\"Logging:LogLevel:Pivotal\":{\"value\":\"Information\"},\"Logging:LogLevel:Default\":{\"value\":\"Warning\"},\"Logging:IncludeScopes\":{\"value\":\"false\"}},\"name\":\"MemoryConfigurationProvider\"}]}"; Assert.Equal(expected, json); }
public async void EnvInvoke_ReturnsExpected() { // arrange ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(OwinTestHelpers.Appsettings); var config = configurationBuilder.Build(); var ep = new EnvEndpoint(new EnvEndpointOptions(), config, new GenericHostingEnvironment() { EnvironmentName = "EnvironmentName" }); var mgmt = new CloudFoundryManagementOptions() { Path = "/" }; var middle = new EndpointOwinMiddleware <EnvironmentDescriptor>(null, ep, new List <IManagementOptions> { mgmt }); var context = OwinTestHelpers.CreateRequest("GET", "/env"); // act var json = await middle.InvokeAndReadResponse(context); // assert var expected = "{\"activeProfiles\":[\"EnvironmentName\"],\"propertySources\":[{\"properties\":{\"Logging:IncludeScopes\":{\"value\":\"false\"},\"Logging:LogLevel:Default\":{\"value\":\"Warning\"},\"Logging:LogLevel:Pivotal\":{\"value\":\"Information\"},\"Logging:LogLevel:Steeltoe\":{\"value\":\"Information\"},\"management:endpoints:enabled\":{\"value\":\"true\"},\"management:endpoints:path\":{\"value\":\"/cloudfoundryapplication\"}},\"name\":\"MemoryConfigurationProvider\"}]}"; Assert.Equal(expected, json); }
/// <summary> /// Add Environment middleware to OWIN Pipeline /// </summary> /// <param name="builder">OWIN <see cref="IAppBuilder" /></param> /// <param name="config"><see cref="IConfiguration"/> of application for configuring env endpoint and inclusion in response</param> /// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> of the application</param> /// <param name="loggerFactory">For logging within the middleware</param> /// <returns>OWIN <see cref="IAppBuilder" /> with Env Endpoint added</returns> public static IAppBuilder UseEnvActuator(this IAppBuilder builder, IConfiguration config, IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory = null) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } if (hostingEnvironment == null) { throw new ArgumentNullException(nameof(hostingEnvironment)); } IEnvOptions options = new EnvEndpointOptions(config); var mgmtOptions = ManagementOptions.Get(config); foreach (var mgmt in mgmtOptions) { if (!mgmt.EndpointOptions.Contains(options)) { mgmt.EndpointOptions.Add(options); } } var endpoint = new EnvEndpoint(options, config, hostingEnvironment, loggerFactory?.CreateLogger <EnvEndpoint>()); var logger = loggerFactory?.CreateLogger <EndpointOwinMiddleware <EnvironmentDescriptor> >(); return(builder.Use <EndpointOwinMiddleware <EnvironmentDescriptor> >(endpoint, mgmtOptions, new List <HttpMethod> { HttpMethod.Get }, true, logger)); }
public void Sanitized_NonDefault_WhenSet() { var appsettings = new Dictionary <string, string>() { ["management:endpoints:env:keystosanitize:0"] = "credentials", ["password"] = "******" }; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); var config = configurationBuilder.Build(); var opts = new EnvEndpointOptions(config); var ep = new EnvEndpoint(opts, config, new TestHosting()); var result = ep.DoInvoke(config); Assert.NotNull(result); var desc = result.PropertySources[0]; Assert.Equal("MemoryConfigurationProvider", desc.Name); var props = desc.Properties; Assert.NotNull(props); Assert.Contains("password", props.Keys); Assert.NotNull(props["password"]); Assert.Equal("mysecret", props["password"].Value); }
public void GetPropertySourceDescriptor_ReturnsExpected() { var opts = new EnvEndpointOptions(); 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:heapdump:sensitive"] = "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 EnvEndpoint(opts, config, new TestHosting()); var provider = config.Providers.Single(); var desc = ep.GetPropertySourceDescriptor(provider, config); Assert.Equal("MemoryConfigurationProvider", desc.Name); var props = desc.Properties; Assert.NotNull(props); Assert.Equal(7, props.Count); Assert.Contains("management:endpoints:enabled", props.Keys); var prop = props["management:endpoints:enabled"]; Assert.NotNull(prop); Assert.Equal("false", prop.Value); Assert.Null(prop.Origin); }
public void GetPropertySourceName_ReturnsExpected() { var opts = new EnvEndpointOptions(); var builder = new ConfigurationBuilder(); builder.AddEnvironmentVariables(); var config = builder.Build(); var env = HostingHelpers.GetHostingEnvironment(); var ep = new EnvEndpoint(opts, config, env); var provider = config.Providers.Single(); var name = ep.GetPropertySourceName(provider); Assert.Equal(provider.GetType().Name, name); builder = new ConfigurationBuilder(); builder.AddJsonFile("foobar", true); config = builder.Build(); ep = new EnvEndpoint(opts, config, env); provider = config.Providers.Single(); name = ep.GetPropertySourceName(provider); Assert.Equal("JsonConfigurationProvider: [foobar]", name); }
public static void UseEnvActuator(IConfiguration configuration, IHostingEnvironment hostingEnvironment = null, ILoggerFactory loggerFactory = null) { var options = new EnvOptions(configuration); hostingEnvironment = hostingEnvironment ?? new DefaultHostingEnvironment("development"); var ep = new EnvEndpoint(options, configuration, hostingEnvironment, CreateLogger <EnvEndpoint>(loggerFactory)); var handler = new EnvHandler(ep, SecurityService, CreateLogger <EnvHandler>(loggerFactory)); ConfiguredHandlers.Add(handler); }
public void GetPropertySourceDescriptor_ReturnsExpected() { var opts = new EnvEndpointOptions(); 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:heapdump:sensitive"] = "true", ["management:endpoints:cloudfoundry:validatecertificates"] = "true", ["management:endpoints:cloudfoundry:enabled"] = "true", ["common"] = "appsettings", ["CharSize"] = "should not duplicate" }; var otherAppsettings = new Dictionary <string, string>() { ["common"] = "otherAppsettings", ["charSize"] = "should not duplicate" }; var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); configurationBuilder.AddInMemoryCollection(otherAppsettings); var config = configurationBuilder.Build(); var env = HostingHelpers.GetHostingEnvironment(); var ep = new EnvEndpoint(opts, config, env); var appsettingsProvider = config.Providers.ElementAt(0); var appsettingsDesc = ep.GetPropertySourceDescriptor(appsettingsProvider); var otherAppsettingsProvider = config.Providers.ElementAt(1); var otherAppsettingsDesc = ep.GetPropertySourceDescriptor(otherAppsettingsProvider); Assert.Equal("MemoryConfigurationProvider", appsettingsDesc.Name); var props = appsettingsDesc.Properties; Assert.NotNull(props); Assert.Equal(9, props.Count); Assert.Contains("management:endpoints:enabled", props.Keys); var prop = props["management:endpoints:enabled"]; Assert.NotNull(prop); Assert.Equal("false", prop.Value); Assert.Null(prop.Origin); var otherProps = otherAppsettingsDesc.Properties; var appsettingsCommonProp = props["common"]; var otherAppsettingCommonProp = otherProps["common"]; Assert.Equal("appsettings", appsettingsCommonProp.Value); Assert.Equal("otherAppsettings", otherAppsettingCommonProp.Value); }
public void EnvEndpointMiddleware_PathAndVerbMatching_ReturnsExpected() { var opts = new EnvOptions(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); var config = configurationBuilder.Build(); var host = new GenericHostingEnvironment() { EnvironmentName = "EnvironmentName" }; var ep = new EnvEndpoint(opts, config, host); var middle = new EndpointOwinMiddleware <EnvironmentDescriptor>(null, ep); Assert.True(middle.RequestVerbAndPathMatch("GET", "/env")); Assert.False(middle.RequestVerbAndPathMatch("PUT", "/env")); Assert.False(middle.RequestVerbAndPathMatch("GET", "/badpath")); }
public void GetPropertySources_ReturnsExpected_WithPlaceholders() { // arrange var appsettings = new Dictionary <string, string>() { ["management:endpoints:path"] = "/cloudfoundryapplication", ["appsManagerBase"] = "${management:endpoints:path}" }; var config = new ConfigurationBuilder().AddInMemoryCollection(appsettings).AddPlaceholderResolver().Build(); var endpoint = new EnvEndpoint(new EnvEndpointOptions(), config, HostingHelpers.GetHostingEnvironment()); // act var result = endpoint.GetPropertySources(config); var testProp = config["appsManagerBase"]; // assert Assert.NotNull(result); Assert.Equal(2, result.Count); Assert.NotNull(testProp); Assert.Equal("/cloudfoundryapplication", testProp); }
public void DoInvoke_ReturnsExpected() { var opts = new EnvEndpointOptions(); 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 EnvEndpoint(opts, config, new TestHosting()); var result = ep.DoInvoke(config); Assert.NotNull(result); Assert.Single(result.ActiveProfiles); Assert.Equal("EnvironmentName", result.ActiveProfiles[0]); Assert.Single(result.PropertySources); var desc = result.PropertySources[0]; Assert.Equal("MemoryConfigurationProvider", desc.Name); var props = desc.Properties; Assert.NotNull(props); Assert.Equal(6, props.Count); Assert.Contains("management:endpoints:loggers:enabled", props.Keys); var prop = props["management:endpoints:loggers:enabled"]; Assert.NotNull(prop); Assert.Equal("false", prop.Value); Assert.Null(prop.Origin); }
public void Sanitized_ReturnsExpected() { var opts = new EnvEndpointOptions(); var appsettings = new Dictionary <string, string>() { ["password"] = "******", ["secret"] = "mysecret", ["key"] = "mysecret", ["token"] = "mysecret", ["my_credentials"] = "mysecret", ["credentials_of"] = "mysecret", ["my_credentialsof"] = "mysecret", ["vcap_services"] = "mysecret" }; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddInMemoryCollection(appsettings); var config = configurationBuilder.Build(); var ep = new EnvEndpoint(opts, config, new TestHosting()); var result = ep.DoInvoke(config); Assert.NotNull(result); var desc = result.PropertySources[0]; Assert.Equal("MemoryConfigurationProvider", desc.Name); var props = desc.Properties; Assert.NotNull(props); foreach (var key in appsettings.Keys) { Assert.Contains(key, props.Keys); Assert.NotNull(props[key]); Assert.Equal("******", props[key].Value); Assert.Null(props[key].Origin); } }
/// <summary> /// Add Environment middleware to OWIN Pipeline /// </summary> /// <param name="builder">OWIN <see cref="IAppBuilder" /></param> /// <param name="config"><see cref="IConfiguration"/> of application for configuring env endpoint and inclusion in response</param> /// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> of the application</param> /// <param name="loggerFactory">For logging within the middleware</param> /// <returns>OWIN <see cref="IAppBuilder" /> with Env Endpoint added</returns> public static IAppBuilder UseEnvActuator(this IAppBuilder builder, IConfiguration config, IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory = null) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } if (hostingEnvironment == null) { throw new ArgumentNullException(nameof(hostingEnvironment)); } var endpoint = new EnvEndpoint(new EnvOptions(config), config, hostingEnvironment, loggerFactory?.CreateLogger <EnvEndpoint>()); var logger = loggerFactory?.CreateLogger <EndpointOwinMiddleware <EnvironmentDescriptor> >(); return(builder.Use <EndpointOwinMiddleware <EnvironmentDescriptor> >(endpoint, new List <HttpMethod> { HttpMethod.Get }, true, logger)); }