From cf08162148864cb8064c879c2bff78c87ea1960e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 4 Apr 2017 12:31:08 -0500 Subject: [PATCH 1/5] Fix http status code with unknown log level This commit changes the http status code to 400 when an unknown log level is specified. Closes gh-8798 See gh-8813 --- .../actuate/endpoint/mvc/LoggersMvcEndpoint.java | 13 +++++++++---- .../endpoint/mvc/LoggersMvcEndpointTests.java | 9 +++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java index fc8e15aabf..d582330442 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java @@ -22,7 +22,6 @@ import org.springframework.boot.actuate.endpoint.LoggersEndpoint; import org.springframework.boot.actuate.endpoint.LoggersEndpoint.LoggerLevels; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.logging.LogLevel; -import org.springframework.http.HttpEntity; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; @@ -33,6 +32,7 @@ import org.springframework.web.bind.annotation.ResponseBody; * * @author Ben Hale * @author Kazuki Shimizu + * @author Eddú Meléndez * @since 1.5.0 */ @ConfigurationProperties(prefix = "endpoints.loggers") @@ -69,9 +69,14 @@ public class LoggersMvcEndpoint extends EndpointMvcAdapter { return getDisabledResponse(); } String level = configuration.get("configuredLevel"); - LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); - this.delegate.setLogLevel(name, logLevel); - return HttpEntity.EMPTY; + try { + LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); + this.delegate.setLogLevel(name, logLevel); + return ResponseEntity.ok().build(); + } + catch (IllegalArgumentException ex) { + return ResponseEntity.badRequest().build(); + } } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java index bc67e6421c..ac04663ec6 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java @@ -64,6 +64,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * * @author Ben Hale * @author Phillip Webb + * @author Eddú Meléndez */ @RunWith(SpringRunner.class) @SpringBootTest @@ -169,6 +170,14 @@ public class LoggersMvcEndpointTests { verifyZeroInteractions(this.loggingSystem); } + @Test + public void setLoggerWithWrongLogLevel() throws Exception { + this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON) + .content("{\"configuredLevel\":\"other\"}")) + .andExpect(status().is4xxClientError()); + verifyZeroInteractions(this.loggingSystem); + } + @Configuration @Import({ JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, From 82ac8b5ca13be1de2ebafe0b0d76d8c96aed050e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 11 Apr 2017 13:29:53 +0200 Subject: [PATCH 2/5] Polish "Fix http status code with unknown log level" Closes gh-8813 --- .../boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java index d582330442..c774ce1ab9 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java @@ -68,15 +68,17 @@ public class LoggersMvcEndpoint extends EndpointMvcAdapter { // disabled return getDisabledResponse(); } - String level = configuration.get("configuredLevel"); + LogLevel logLevel; try { - LogLevel logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); - this.delegate.setLogLevel(name, logLevel); - return ResponseEntity.ok().build(); + String level = configuration.get("configuredLevel"); + logLevel = level == null ? null : LogLevel.valueOf(level.toUpperCase()); } catch (IllegalArgumentException ex) { return ResponseEntity.badRequest().build(); } + + this.delegate.setLogLevel(name, logLevel); + return ResponseEntity.ok().build(); } } From d3e2e22f8cb1bfcb363f2f395bde726dfd5e70b2 Mon Sep 17 00:00:00 2001 From: Colin Harrington Date: Thu, 16 Mar 2017 16:54:45 -0500 Subject: [PATCH 3/5] Add the ability to disable the trace filter See gh-8650 --- .../TraceWebFilterAutoConfiguration.java | 4 +++- .../TraceWebFilterAutoConfigurationTests.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfiguration.java index 2302b36280..f995cd1052 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -42,6 +43,7 @@ import org.springframework.web.servlet.DispatcherServlet; */ @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, ServletRegistration.class }) @AutoConfigureAfter(TraceRepositoryAutoConfiguration.class) +@ConditionalOnProperty(name = "endpoints.trace.filter.enabled", matchIfMissing = true) @EnableConfigurationProperties(TraceProperties.class) @Configuration public class TraceWebFilterAutoConfiguration { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfigurationTests.java index 91cd9c7bcb..0574dacdee 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfigurationTests.java @@ -24,6 +24,7 @@ import org.springframework.boot.actuate.trace.TraceProperties; import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.WebRequestTraceFilter; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -58,6 +59,19 @@ public class TraceWebFilterAutoConfigurationTests { context.close(); } + @Test + public void skipsFilterIfPropertyDisabled() throws Exception { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(context, + "endpoints.trace.filter.enabled:false"); + context.register(PropertyPlaceholderAutoConfiguration.class, + TraceRepositoryAutoConfiguration.class, + TraceWebFilterAutoConfiguration.class); + context.refresh(); + assertThat(context.getBeansOfType(WebRequestTraceFilter.class).size()).isEqualTo(0); + context.close(); + } + @Configuration static class CustomTraceFilterConfig { From 2ef318c00d2cba24bf7ed53e917c069ca4e37995 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 11 Apr 2017 14:29:54 +0200 Subject: [PATCH 4/5] Polish "Add the ability to disable the trace filter" Closes gh-8650 --- .../TraceWebFilterAutoConfiguration.java | 4 +- ...itional-spring-configuration-metadata.json | 6 ++ .../TraceWebFilterAutoConfigurationTests.java | 56 ++++++++++++------- .../appendix-application-properties.adoc | 1 + 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfiguration.java index f995cd1052..2631cf0c72 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfiguration.java @@ -41,11 +41,11 @@ import org.springframework.web.servlet.DispatcherServlet; * * @author Dave Syer */ +@Configuration @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, ServletRegistration.class }) @AutoConfigureAfter(TraceRepositoryAutoConfiguration.class) -@ConditionalOnProperty(name = "endpoints.trace.filter.enabled", matchIfMissing = true) +@ConditionalOnProperty(prefix = "endpoints.trace.filter", name = "enabled", matchIfMissing = true) @EnableConfigurationProperties(TraceProperties.class) -@Configuration public class TraceWebFilterAutoConfiguration { private final TraceRepository traceRepository; diff --git a/spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json index e32aaacd40..5c0b2639df 100644 --- a/spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -80,6 +80,12 @@ "type": "java.lang.String", "description": "Endpoint URL path." }, + { + "name": "endpoints.trace.filter.enabled", + "type": "java.lang.Boolean", + "description": "Enable the trace servlet filter.", + "defaultValue": true + }, { "name": "info", "type": "java.util.Map", diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfigurationTests.java index 0574dacdee..3e232b42de 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/TraceWebFilterAutoConfigurationTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure; import java.util.Map; +import org.junit.After; import org.junit.Test; import org.springframework.boot.actuate.trace.TraceProperties; @@ -35,41 +36,54 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link TraceWebFilterAutoConfiguration}. * * @author Phillip Webb + * @author Stephane Nicoll */ public class TraceWebFilterAutoConfigurationTests { + private AnnotationConfigApplicationContext context; + + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + @Test public void configureFilter() { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( - PropertyPlaceholderAutoConfiguration.class, - TraceRepositoryAutoConfiguration.class, - TraceWebFilterAutoConfiguration.class); - assertThat(context.getBean(WebRequestTraceFilter.class)).isNotNull(); - context.close(); + load(); + assertThat(this.context.getBean(WebRequestTraceFilter.class)).isNotNull(); } @Test public void overrideTraceFilter() throws Exception { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( - CustomTraceFilterConfig.class, PropertyPlaceholderAutoConfiguration.class, - TraceRepositoryAutoConfiguration.class, - TraceWebFilterAutoConfiguration.class); - WebRequestTraceFilter filter = context.getBean(WebRequestTraceFilter.class); + load(CustomTraceFilterConfig.class); + WebRequestTraceFilter filter = this.context.getBean(WebRequestTraceFilter.class); assertThat(filter).isInstanceOf(TestWebRequestTraceFilter.class); - context.close(); } @Test public void skipsFilterIfPropertyDisabled() throws Exception { - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(context, - "endpoints.trace.filter.enabled:false"); - context.register(PropertyPlaceholderAutoConfiguration.class, - TraceRepositoryAutoConfiguration.class, - TraceWebFilterAutoConfiguration.class); - context.refresh(); - assertThat(context.getBeansOfType(WebRequestTraceFilter.class).size()).isEqualTo(0); - context.close(); + load("endpoints.trace.filter.enabled:false"); + assertThat(this.context.getBeansOfType(WebRequestTraceFilter.class).size()) + .isEqualTo(0); + } + + private void load(String... environment) { + load(null, environment); + } + + private void load(Class config, String... environment) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(ctx, environment); + if (config != null) { + ctx.register(config); + } + ctx.register(PropertyPlaceholderAutoConfiguration.class, + TraceRepositoryAutoConfiguration.class, + TraceWebFilterAutoConfiguration.class); + ctx.refresh(); + this.context = ctx; } @Configuration diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 71b74e4c20..09e7341b05 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -1058,6 +1058,7 @@ content into your application; rather pick only the properties that you need. endpoints.shutdown.path= # Endpoint path. endpoints.shutdown.sensitive= # Mark if the endpoint exposes sensitive information. endpoints.trace.enabled= # Enable the endpoint. + endpoints.trace.filter.enabled=true # Enable the trace servlet filter. endpoints.trace.id= # Endpoint identifier. endpoints.trace.path= # Endpoint path. endpoints.trace.sensitive= # Mark if the endpoint exposes sensitive information. From b148b467515613cca905a091263fd5613b3644ae Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 11 Apr 2017 14:54:16 +0200 Subject: [PATCH 5/5] Polish --- .../actuate/autoconfigure/MetricFilterAutoConfiguration.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java index f3ab0f7827..400f19c039 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,7 +46,7 @@ import org.springframework.web.servlet.HandlerMapping; @ConditionalOnClass({ Servlet.class, ServletRegistration.class, OncePerRequestFilter.class, HandlerMapping.class }) @AutoConfigureAfter(MetricRepositoryAutoConfiguration.class) -@ConditionalOnProperty(name = "endpoints.metrics.filter.enabled", matchIfMissing = true) +@ConditionalOnProperty(prefix = "endpoints.metrics.filter", name="enabled", matchIfMissing = true) @EnableConfigurationProperties({ MetricFilterProperties.class }) public class MetricFilterAutoConfiguration {