Polish "Expose Elastic's apiKeyCredentials property"

See gh-28400
pull/28436/head
Andy Wilkinson 3 years ago
parent dd475a2445
commit 8bf3780c15

@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -60,6 +61,14 @@ public class ElasticMetricsExportAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ElasticConfig elasticConfig() {
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
entries.put("api-key-credentials", this.properties.getApiKeyCredentials());
entries.put("user-name", this.properties.getUserName());
});
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
entries.put("api-key-credentials", this.properties.getApiKeyCredentials());
entries.put("password", this.properties.getPassword());
});
return new ElasticPropertiesConfigAdapter(this.properties);
}

@ -60,14 +60,12 @@ public class ElasticProperties extends StepRegistryProperties {
private boolean autoCreateIndex = true;
/**
* Login user of the Elastic server. If API key is configured, it will be used for
* authentication instead of username/password.
* Login user of the Elastic server. Mutually exclusive with api-key-credentials.
*/
private String userName;
/**
* Login password of the Elastic server. If API key is configured, it will be used for
* authentication instead of username/password.
* Login password of the Elastic server. Mutually exclusive with api-key-credentials.
*/
private String password;
@ -77,8 +75,7 @@ public class ElasticProperties extends StepRegistryProperties {
private String pipeline;
/**
* Base64-encoded credentials string. If configured, it will be used for
* authentication instead of username/password.
* Base64-encoded credentials string. Mutually exclusive with user-name and password.
*/
private String apiKeyCredentials;

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -22,6 +22,7 @@ import io.micrometer.elastic.ElasticMeterRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -90,6 +91,24 @@ class ElasticMetricsExportAutoConfigurationTests {
});
}
@Test
void apiKeyCredentialsIsMutuallyExclusiveWithUserName() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.elastic.api-key-credentials:secret",
"management.metrics.export.elastic.user-name:alice")
.run((context) -> assertThat(context).hasFailed().getFailure().getRootCause()
.isInstanceOf(MutuallyExclusiveConfigurationPropertiesException.class));
}
@Test
void apiKeyCredentialsIsMutuallyExclusiveWithPassword() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("management.metrics.export.elastic.api-key-credentials:secret",
"management.metrics.export.elastic.password:secret")
.run((context) -> assertThat(context).hasFailed().getFailure().getRootCause()
.isInstanceOf(MutuallyExclusiveConfigurationPropertiesException.class));
}
@Configuration(proxyBeanMethods = false)
static class BaseConfiguration {

Loading…
Cancel
Save