From b7b43cb6a34ebe41a1608e98f6e1a8425ad84659 Mon Sep 17 00:00:00 2001 From: Chris Bono Date: Thu, 21 Nov 2024 21:31:44 -0600 Subject: [PATCH] Add property guard on Actuator health adapter This commit adds a ConditionalOnProperty on the Actuator health adapter autoconfiguration to allow users to disable only that part of the health system. Also, unrelated, the generated configprops with their default values. See #56 Signed-off-by: Chris Bono --- .../modules/ROOT/partials/_configprops.adoc | 24 ++++++++--------- .../GrpcServerHealthAutoConfiguration.java | 2 ++ ...rpcServerHealthAutoConfigurationTests.java | 26 +++++++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc b/spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc index 59a5b0b..1b1eb8d 100644 --- a/spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc +++ b/spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc @@ -12,32 +12,32 @@ |spring.grpc.client.default-channel.max-inbound-message-size | | |spring.grpc.client.default-channel.max-inbound-metadata-size | | |spring.grpc.client.default-channel.negotiation-type | | The negotiation type for the channel. Default is {@link NegotiationType#PLAINTEXT}. -|spring.grpc.client.default-channel.secure | | Flag to say that strict SSL checks are not enabled (so the remote certificate could be anonymous). +|spring.grpc.client.default-channel.secure | `+++true+++` | Flag to say that strict SSL checks are not enabled (so the remote certificate could be anonymous). |spring.grpc.client.default-channel.ssl.bundle | | SSL bundle name. |spring.grpc.client.default-channel.ssl.enabled | | Whether to enable SSL support. Enabled automatically if "bundle" is provided unless specified otherwise. |spring.grpc.client.default-channel.user-agent | | |spring.grpc.server.address | | The address to bind to. could be a host:port combination or a pseudo URL like static://host:port. Can not be set if host or port are set independently. |spring.grpc.server.exception-handling.enabled | `+++true+++` | Whether to enable user-defined global exception handling on the gRPC server. -|spring.grpc.server.health.actuator.enabled | | Whether to adapt Actuator health checks into gRPC health checks. +|spring.grpc.server.health.actuator.enabled | `+++true+++` | Whether to adapt Actuator health checks into gRPC health checks. |spring.grpc.server.health.actuator.endpoints | | List of Actuator health checks to adapt into gRPC health checks. -|spring.grpc.server.health.enabled | | Whether to auto-configure Health feature on the gRPC server. -|spring.grpc.server.host | | Server address to bind to. The default is any IP address ('*'). +|spring.grpc.server.health.enabled | `+++true+++` | Whether to auto-configure Health feature on the gRPC server. +|spring.grpc.server.host | `+++*+++` | Server address to bind to. The default is any IP address ('*'). |spring.grpc.server.keep-alive.max-age | | Maximum time a connection may exist before being gracefully terminated (default infinite). |spring.grpc.server.keep-alive.max-age-grace | | Maximum time for graceful connection termination (default infinite). |spring.grpc.server.keep-alive.max-idle | | Maximum time a connection can remain idle before being gracefully terminated (default infinite). -|spring.grpc.server.keep-alive.permit-time | | Maximum keep-alive time clients are permitted to configure (default 5m). -|spring.grpc.server.keep-alive.permit-without-calls | | Whether clients are permitted to send keep alive pings when there are no outstanding RPCs on the connection (default false). -|spring.grpc.server.keep-alive.time | | Duration without read activity before sending a keep alive ping (default 2h). -|spring.grpc.server.keep-alive.timeout | | Maximum time to wait for read activity after sending a keep alive ping. If sender does not receive an acknowledgment within this time, it will close the connection (default 20s). -|spring.grpc.server.max-inbound-message-size | | Maximum message size allowed to be received by the server (default 4MiB). -|spring.grpc.server.max-inbound-metadata-size | | Maximum metadata size allowed to be received by the server (default 8KiB). +|spring.grpc.server.keep-alive.permit-time | `+++5m+++` | Maximum keep-alive time clients are permitted to configure (default 5m). +|spring.grpc.server.keep-alive.permit-without-calls | `+++false+++` | Whether clients are permitted to send keep alive pings when there are no outstanding RPCs on the connection (default false). +|spring.grpc.server.keep-alive.time | `+++2h+++` | Duration without read activity before sending a keep alive ping (default 2h). +|spring.grpc.server.keep-alive.timeout | `+++20s+++` | Maximum time to wait for read activity after sending a keep alive ping. If sender does not receive an acknowledgment within this time, it will close the connection (default 20s). +|spring.grpc.server.max-inbound-message-size | `+++4194304B+++` | Maximum message size allowed to be received by the server (default 4MiB). +|spring.grpc.server.max-inbound-metadata-size | `+++8192B+++` | Maximum metadata size allowed to be received by the server (default 8KiB). |spring.grpc.server.observations.enabled | `+++true+++` | Whether to enable Observations on the server. |spring.grpc.server.port | `+++9090+++` | Server port to listen on. When the value is 0, a random available port is selected. The default is 9090. |spring.grpc.server.reflection.enabled | `+++true+++` | Whether to enable Reflection on the gRPC server. -|spring.grpc.server.shutdown-grace-period | | Maximum time to wait for the server to gracefully shutdown. When the value is negative, the server waits forever. When the value is 0, the server will force shutdown immediately. The default is 30 seconds. +|spring.grpc.server.shutdown-grace-period | `+++30s+++` | Maximum time to wait for the server to gracefully shutdown. When the value is negative, the server waits forever. When the value is 0, the server will force shutdown immediately. The default is 30 seconds. |spring.grpc.server.ssl.bundle | | SSL bundle name. |spring.grpc.server.ssl.client-auth | | Client authentication mode. |spring.grpc.server.ssl.enabled | | Whether to enable SSL support. Enabled automatically if "bundle" is provided unless specified otherwise. -|spring.grpc.server.ssl.secure | | Flag to indicate that client authentication is secure (i.e. certificates are checked). Do not set this to false in production. +|spring.grpc.server.ssl.secure | `+++true+++` | Flag to indicate that client authentication is secure (i.e. certificates are checked). Do not set this to false in production. |=== \ No newline at end of file diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfiguration.java index 8fbd596..99dd5f5 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfiguration.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfiguration.java @@ -61,6 +61,8 @@ public class GrpcServerHealthAutoConfiguration { @AutoConfigureAfter(name = "org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration") @ConditionalOnClass(HealthEndpoint.class) @ConditionalOnBean(HealthEndpoint.class) + @ConditionalOnProperty(name = "spring.grpc.server.health.actuator.enabled", havingValue = "true", + matchIfMissing = true) @EnableConfigurationProperties(GrpcServerProperties.class) static class ActuatorHealthAdapterConfiguration { diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfigurationTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfigurationTests.java index 8481e84..e575251 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfigurationTests.java +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/health/GrpcServerHealthAutoConfigurationTests.java @@ -165,6 +165,32 @@ class GrpcServerHealthAutoConfigurationTests { .doesNotHaveBean(GrpcServerHealthAutoConfiguration.ActuatorHealthAdapterConfiguration.class)); } + @Test + void whenActuatorPropertyNotSetAdapterIsAutoConfigured() { + GrpcServerHealthAutoConfigurationTests.this.contextRunner() + .withBean("healthEndpoint", HealthEndpoint.class, Mockito::mock) + .run((context) -> assertThat(context) + .hasSingleBean(GrpcServerHealthAutoConfiguration.ActuatorHealthAdapterConfiguration.class)); + } + + @Test + void whenActuatorPropertyIsTrueAdapterIsAutoConfigured() { + GrpcServerHealthAutoConfigurationTests.this.contextRunner() + .withBean("healthEndpoint", HealthEndpoint.class, Mockito::mock) + .withPropertyValues("spring.grpc.server.health.actuator.enabled=true") + .run((context) -> assertThat(context) + .hasSingleBean(GrpcServerHealthAutoConfiguration.ActuatorHealthAdapterConfiguration.class)); + } + + @Test + void whenActuatorPropertyIsFalseAdapterIsNotAutoConfigured() { + GrpcServerHealthAutoConfigurationTests.this.contextRunner() + .withBean("healthEndpoint", HealthEndpoint.class, Mockito::mock) + .withPropertyValues("spring.grpc.server.health.actuator.enabled=false") + .run((context) -> assertThat(context) + .doesNotHaveBean(GrpcServerHealthAutoConfiguration.ActuatorHealthAdapterConfiguration.class)); + } + @Test void adapterAutoConfiguredAsExpected() { GrpcServerHealthAutoConfigurationTests.this.contextRunner()