From 8d933fcb033283943f6e53723aff950508219a63 Mon Sep 17 00:00:00 2001 From: Braunson <5280764+braunsonm@users.noreply.github.com> Date: Tue, 21 Mar 2023 19:58:01 -0400 Subject: [PATCH] Support Customizing Observation Conventions Closes gh-12534 --- .../ObservationAuthenticationManager.java | 14 +++++++++++++- .../ObservationReactiveAuthenticationManager.java | 15 ++++++++++++++- .../ObservationAuthorizationManager.java | 15 ++++++++++++++- .../ObservationReactiveAuthorizationManager.java | 15 ++++++++++++++- .../ObservationAuthenticationManagerTests.java | 5 +++++ ...rvationReactiveAuthenticationManagerTests.java | 6 ++++++ .../ObservationAuthorizationManagerTests.java | 5 +++++ ...ervationReactiveAuthorizationManagerTests.java | 5 +++++ 8 files changed, 76 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/springframework/security/authentication/ObservationAuthenticationManager.java b/core/src/main/java/org/springframework/security/authentication/ObservationAuthenticationManager.java index 377761be2b..4994ae3db9 100644 --- a/core/src/main/java/org/springframework/security/authentication/ObservationAuthenticationManager.java +++ b/core/src/main/java/org/springframework/security/authentication/ObservationAuthenticationManager.java @@ -17,6 +17,7 @@ package org.springframework.security.authentication; import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationConvention; import io.micrometer.observation.ObservationRegistry; import org.springframework.security.core.Authentication; @@ -35,7 +36,7 @@ public final class ObservationAuthenticationManager implements AuthenticationMan private final AuthenticationManager delegate; - private final AuthenticationObservationConvention convention = new AuthenticationObservationConvention(); + private ObservationConvention convention = new AuthenticationObservationConvention(); public ObservationAuthenticationManager(ObservationRegistry registry, AuthenticationManager delegate) { Assert.notNull(registry, "observationRegistry cannot be null"); @@ -56,4 +57,15 @@ public final class ObservationAuthenticationManager implements AuthenticationMan }); } + /** + * Use the provided convention for reporting observation data + * + * @param convention The provided convention + * + * @since 6.1 + */ + public void setConvention(ObservationConvention convention) { + Assert.notNull(convention, "The observation convention cannot be null"); + this.convention = convention; + } } diff --git a/core/src/main/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManager.java b/core/src/main/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManager.java index dc54ff4dd0..bec6635f72 100644 --- a/core/src/main/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManager.java +++ b/core/src/main/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManager.java @@ -17,8 +17,10 @@ package org.springframework.security.authentication; import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationConvention; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; +import org.springframework.util.Assert; import reactor.core.publisher.Mono; import org.springframework.security.core.Authentication; @@ -36,7 +38,7 @@ public class ObservationReactiveAuthenticationManager implements ReactiveAuthent private final ReactiveAuthenticationManager delegate; - private final AuthenticationObservationConvention convention = new AuthenticationObservationConvention(); + private ObservationConvention convention = new AuthenticationObservationConvention(); public ObservationReactiveAuthenticationManager(ObservationRegistry registry, ReactiveAuthenticationManager delegate) { @@ -62,4 +64,15 @@ public class ObservationReactiveAuthenticationManager implements ReactiveAuthent }); } + /** + * Use the provided convention for reporting observation data + * + * @param convention The provided convention + * + * @since 6.1 + */ + public void setConvention(ObservationConvention convention) { + Assert.notNull(convention, "The observation convention cannot be null"); + this.convention = convention; + } } diff --git a/core/src/main/java/org/springframework/security/authorization/ObservationAuthorizationManager.java b/core/src/main/java/org/springframework/security/authorization/ObservationAuthorizationManager.java index 343762e0ac..92ae44135c 100644 --- a/core/src/main/java/org/springframework/security/authorization/ObservationAuthorizationManager.java +++ b/core/src/main/java/org/springframework/security/authorization/ObservationAuthorizationManager.java @@ -19,10 +19,12 @@ package org.springframework.security.authorization; import java.util.function.Supplier; import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationConvention; import io.micrometer.observation.ObservationRegistry; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.core.Authentication; +import org.springframework.util.Assert; /** * An {@link AuthorizationManager} that observes the authorization @@ -36,7 +38,7 @@ public final class ObservationAuthorizationManager implements AuthorizationMa private final AuthorizationManager delegate; - private final AuthorizationObservationConvention convention = new AuthorizationObservationConvention(); + private ObservationConvention> convention = new AuthorizationObservationConvention(); public ObservationAuthorizationManager(ObservationRegistry registry, AuthorizationManager delegate) { this.registry = registry; @@ -68,4 +70,15 @@ public final class ObservationAuthorizationManager implements AuthorizationMa } } + /** + * Use the provided convention for reporting observation data + * + * @param convention The provided convention + * + * @since 6.1 + */ + public void setConvention(ObservationConvention> convention) { + Assert.notNull(convention, "The observation convention cannot be null"); + this.convention = convention; + } } diff --git a/core/src/main/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManager.java b/core/src/main/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManager.java index e40c3f10f2..ae7b650412 100644 --- a/core/src/main/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManager.java +++ b/core/src/main/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManager.java @@ -17,8 +17,10 @@ package org.springframework.security.authorization; import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationConvention; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; +import org.springframework.util.Assert; import reactor.core.publisher.Mono; import org.springframework.security.access.AccessDeniedException; @@ -36,7 +38,7 @@ public final class ObservationReactiveAuthorizationManager implements Reactiv private final ReactiveAuthorizationManager delegate; - private final AuthorizationObservationConvention convention = new AuthorizationObservationConvention(); + private ObservationConvention> convention = new AuthorizationObservationConvention(); public ObservationReactiveAuthorizationManager(ObservationRegistry registry, ReactiveAuthorizationManager delegate) { @@ -67,4 +69,15 @@ public final class ObservationReactiveAuthorizationManager implements Reactiv }); } + /** + * Use the provided convention for reporting observation data + * + * @param convention The provided convention + * + * @since 6.1 + */ + public void setConvention(ObservationConvention> convention) { + Assert.notNull(convention, "The observation convention cannot be null"); + this.convention = convention; + } } diff --git a/core/src/test/java/org/springframework/security/authentication/ObservationAuthenticationManagerTests.java b/core/src/test/java/org/springframework/security/authentication/ObservationAuthenticationManagerTests.java index 4583d61746..46e5fe4b83 100644 --- a/core/src/test/java/org/springframework/security/authentication/ObservationAuthenticationManagerTests.java +++ b/core/src/test/java/org/springframework/security/authentication/ObservationAuthenticationManagerTests.java @@ -93,4 +93,9 @@ public class ObservationAuthenticationManagerTests { assertThat(context.getAuthenticationResult()).isNull(); } + @Test + void conventionSetterThrowsWhenNull() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.tested.setConvention(null)); + } } diff --git a/core/src/test/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManagerTests.java b/core/src/test/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManagerTests.java index e9da7a7024..718ba010de 100644 --- a/core/src/test/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManagerTests.java +++ b/core/src/test/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManagerTests.java @@ -96,4 +96,10 @@ public class ObservationReactiveAuthenticationManagerTests { assertThat(context.getAuthenticationResult()).isNull(); } + @Test + void conventionSetterThrowsWhenNull() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.tested.setConvention(null)); + } + } diff --git a/core/src/test/java/org/springframework/security/authorization/ObservationAuthorizationManagerTests.java b/core/src/test/java/org/springframework/security/authorization/ObservationAuthorizationManagerTests.java index 16204bf50e..1f2b68e1d0 100644 --- a/core/src/test/java/org/springframework/security/authorization/ObservationAuthorizationManagerTests.java +++ b/core/src/test/java/org/springframework/security/authorization/ObservationAuthorizationManagerTests.java @@ -118,4 +118,9 @@ public class ObservationAuthorizationManagerTests { assertThat(context.getDecision()).isEqualTo(this.grant); } + @Test + void conventionSetterThrowsWhenNull() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.tested.setConvention(null)); + } } diff --git a/core/src/test/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManagerTests.java b/core/src/test/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManagerTests.java index 044c1c24b1..3148134650 100644 --- a/core/src/test/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManagerTests.java +++ b/core/src/test/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManagerTests.java @@ -117,4 +117,9 @@ public class ObservationReactiveAuthorizationManagerTests { assertThat(context.getDecision()).isEqualTo(this.grant); } + @Test + void conventionSetterThrowsWhenNull() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> this.tested.setConvention(null)); + } }