Skip to content

Commit

Permalink
Introduce Wavefront API Token Type
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Sep 28, 2023
1 parent 973581c commit aba75b9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.micrometer.wavefront;

import com.wavefront.sdk.common.clients.service.token.TokenService;
import com.wavefront.sdk.common.clients.service.token.TokenService.Type;
import io.micrometer.common.lang.Nullable;
import io.micrometer.core.instrument.config.validate.InvalidReason;
import io.micrometer.core.instrument.config.validate.Validated;
Expand Down Expand Up @@ -111,6 +113,13 @@ default String source() {
});
}

default TokenService.Type apiTokenType() {
return getEnum(this, TokenService.Type.class, "apiTokenType")
.invalidateWhen(tokenType -> tokenType == Type.NO_TOKEN && WavefrontMeterRegistry.isDirectToApi(this),
"must be set whenever publishing directly to the Wavefront API", InvalidReason.MISSING)
.orElse(WavefrontMeterRegistry.isDirectToApi(this) ? Type.WAVEFRONT_API_TOKEN : Type.NO_TOKEN);
}

/**
* Required when publishing directly to the Wavefront API host, otherwise does
* nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ static String getWavefrontReportingUri(WavefrontConfig wavefrontConfig) {
* @since 1.5.0
*/
public static WavefrontClient.Builder getDefaultSenderBuilder(WavefrontConfig config) {
return new WavefrontClient.Builder(getWavefrontReportingUri(config), config.apiToken())
return new WavefrontClient.Builder(getWavefrontReportingUri(config), config.apiTokenType(), config.apiToken())
.batchSize(config.batchSize())
.flushInterval((int) config.step().toMillis(), TimeUnit.MILLISECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package io.micrometer.wavefront;

import com.wavefront.sdk.common.clients.service.token.TokenService.Type;
import io.micrometer.core.Issue;
import io.micrometer.core.instrument.config.validate.ValidationException;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

class WavefrontConfigTest {
Expand All @@ -44,4 +47,50 @@ void defaultUriImplementationThrowsForInvalidUri() throws IOException {
.hasMessageContaining("it must be a valid URI");
}

@Test
void noExceptionAndNoTokenTypeIfTokenMissingButProxyUsed() {
Map<String, String> wavefrontProps = Map.of("wavefront.uri", "proxy://example.org:2878");
WavefrontConfig config = wavefrontProps::get;
assertThatCode(config::apiToken).doesNotThrowAnyException();
assertThat(config.apiTokenType()).isSameAs(Type.NO_TOKEN);
}

@Test
void noExceptionAndWavefrontTokenTypeIfTokenPresentAndDirectAccessUsed() {
Map<String, String> wavefrontProps = Map.of("wavefront.uri", "https://example.org:2878", "wavefront.apiToken",
"s3cr3t");
WavefrontConfig config = wavefrontProps::get;
assertThatCode(config::apiToken).doesNotThrowAnyException();
assertThat(config.apiTokenType()).isSameAs(Type.WAVEFRONT_API_TOKEN);
}

@Test
void apiTokenFailsIfNoTokenPresentAndDirectAccessUsed() {
Map<String, String> wavefrontProps = Map.of("wavefront.uri", "https://example.org:2878");
WavefrontConfig config = wavefrontProps::get;
assertThatCode(config::apiToken).isExactlyInstanceOf(ValidationException.class)
.hasNoCause()
.hasMessage(
"wavefront.apiToken was 'null' but it must be set whenever publishing directly to the Wavefront API");
}

@Test
void apiTokenTypeFailsIfNoTokenTypeAndDirectAccessUsed() {
Map<String, String> wavefrontProps = Map.of("wavefront.uri", "https://example.org:2878",
"wavefront.apiTokenType", Type.NO_TOKEN.name());
WavefrontConfig config = wavefrontProps::get;
assertThatCode(config::apiTokenType).isExactlyInstanceOf(ValidationException.class)
.hasNoCause()
.hasMessage(
"wavefront.apiTokenType was 'No-Op/Proxy' but it must be set whenever publishing directly to the Wavefront API");
}

@Test
void apiTokenTypeShouldBeUsedIfDirectAccessUsed() {
Map<String, String> wavefrontProps = Map.of("wavefront.uri", "https://example.org:2878",
"wavefront.apiTokenType", Type.CSP_API_TOKEN.name());
WavefrontConfig config = wavefrontProps::get;
assertThat(config.apiTokenType()).isSameAs(Type.CSP_API_TOKEN);
}

}

0 comments on commit aba75b9

Please sign in to comment.