From fbec06a134122d3d0615e5f8baa02455061b47cb Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 5 Oct 2023 10:44:19 +0200 Subject: [PATCH] Support new CSP auth method for Wavefront Closes gh-37165 --- .../WavefrontPropertiesConfigAdapter.java | 16 ++++++++ .../wavefront/WavefrontProperties.java | 39 +++++++++++++++++++ ...WavefrontPropertiesConfigAdapterTests.java | 20 ++++++++++ 3 files changed, 75 insertions(+) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontPropertiesConfigAdapter.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontPropertiesConfigAdapter.java index 9ffa626b2b..1f64342b16 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontPropertiesConfigAdapter.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontPropertiesConfigAdapter.java @@ -16,11 +16,13 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront; +import com.wavefront.sdk.common.clients.service.token.TokenService.Type; import io.micrometer.wavefront.WavefrontConfig; import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PushRegistryPropertiesConfigAdapter; import org.springframework.boot.actuate.autoconfigure.wavefront.WavefrontProperties; import org.springframework.boot.actuate.autoconfigure.wavefront.WavefrontProperties.Metrics.Export; +import org.springframework.boot.actuate.autoconfigure.wavefront.WavefrontProperties.TokenType; /** * Adapter to convert {@link WavefrontProperties} to a {@link WavefrontConfig}. @@ -84,4 +86,18 @@ public class WavefrontPropertiesConfigAdapter return get(Export::isReportDayDistribution, WavefrontConfig.super::reportDayDistribution); } + @Override + public Type apiTokenType() { + TokenType apiTokenType = this.properties.getApiTokenType(); + if (apiTokenType == null) { + return WavefrontConfig.super.apiTokenType(); + } + return switch (apiTokenType) { + case NO_TOKEN -> Type.NO_TOKEN; + case WAVEFRONT_API_TOKEN -> Type.WAVEFRONT_API_TOKEN; + case CSP_API_TOKEN -> Type.CSP_API_TOKEN; + case CSP_CLIENT_CREDENTIALS -> Type.CSP_CLIENT_CREDENTIALS; + }; + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontProperties.java index 19c66c0071..77fbd189a6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/wavefront/WavefrontProperties.java @@ -57,6 +57,11 @@ public class WavefrontProperties { */ private String apiToken; + /** + * Type of the API token. + */ + private TokenType apiTokenType; + /** * Application configuration. */ @@ -167,6 +172,14 @@ public class WavefrontProperties { this.traceDerivedCustomTagKeys = traceDerivedCustomTagKeys; } + public TokenType getApiTokenType() { + return this.apiTokenType; + } + + public void setApiTokenType(TokenType apiTokenType) { + this.apiTokenType = apiTokenType; + } + public static class Application { /** @@ -385,4 +398,30 @@ public class WavefrontProperties { } + /** + * Wavefront token type. + * + * @since 3.2.0 + */ + public enum TokenType { + + /** + * No token. + */ + NO_TOKEN, + /** + * Wavefront API token. + */ + WAVEFRONT_API_TOKEN, + /** + * CSP API token. + */ + CSP_API_TOKEN, + /** + * CSP client credentials. + */ + CSP_CLIENT_CREDENTIALS + + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontPropertiesConfigAdapterTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontPropertiesConfigAdapterTests.java index 54234f8787..6fd14b676d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontPropertiesConfigAdapterTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/wavefront/WavefrontPropertiesConfigAdapterTests.java @@ -18,11 +18,15 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.wavefront; import java.net.URI; +import com.wavefront.sdk.common.clients.service.token.TokenService.Type; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PushRegistryPropertiesConfigAdapterTests; import org.springframework.boot.actuate.autoconfigure.wavefront.WavefrontProperties; import org.springframework.boot.actuate.autoconfigure.wavefront.WavefrontProperties.Metrics.Export; +import org.springframework.boot.actuate.autoconfigure.wavefront.WavefrontProperties.TokenType; import static org.assertj.core.api.Assertions.assertThat; @@ -107,4 +111,20 @@ class WavefrontPropertiesConfigAdapterTests extends assertThat(createConfigAdapter(properties).reportDayDistribution()).isTrue(); } + @ParameterizedTest + @CsvSource(textBlock = """ + null, WAVEFRONT_API_TOKEN + NO_TOKEN, NO_TOKEN + WAVEFRONT_API_TOKEN, WAVEFRONT_API_TOKEN + CSP_API_TOKEN, CSP_API_TOKEN + CSP_CLIENT_CREDENTIALS, CSP_CLIENT_CREDENTIALS + """) + void whenTokenTypeIsSetAdapterReturnsIt(String property, String wavefront) { + TokenType propertyToken = property.equals("null") ? null : TokenType.valueOf(property); + Type wavefrontToken = Type.valueOf(wavefront); + WavefrontProperties properties = new WavefrontProperties(); + properties.setApiTokenType(propertyToken); + assertThat(new WavefrontPropertiesConfigAdapter(properties).apiTokenType()).isEqualTo(wavefrontToken); + } + }