Merge pull request #28400 from jonatan-ivanov

* gh-28400:
  Polish "Expose Elastic's apiKeyCredentials property"
  Expose Elastic's apiKeyCredentials property

Closes gh-28400
This commit is contained in:
Andy Wilkinson
2021-10-21 17:52:00 +01:00
6 changed files with 61 additions and 7 deletions

View File

@@ -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);
}

View File

@@ -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.
@@ -60,12 +60,12 @@ public class ElasticProperties extends StepRegistryProperties {
private boolean autoCreateIndex = true;
/**
* Login user of the Elastic server.
* Login user of the Elastic server. Mutually exclusive with api-key-credentials.
*/
private String userName;
/**
* Login password of the Elastic server.
* Login password of the Elastic server. Mutually exclusive with api-key-credentials.
*/
private String password;
@@ -74,6 +74,11 @@ public class ElasticProperties extends StepRegistryProperties {
*/
private String pipeline;
/**
* Base64-encoded credentials string. Mutually exclusive with user-name and password.
*/
private String apiKeyCredentials;
public String getHost() {
return this.host;
}
@@ -146,4 +151,12 @@ public class ElasticProperties extends StepRegistryProperties {
this.pipeline = pipeline;
}
public String getApiKeyCredentials() {
return this.apiKeyCredentials;
}
public void setApiKeyCredentials(String apiKeyCredentials) {
this.apiKeyCredentials = apiKeyCredentials;
}
}

View File

@@ -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.
@@ -82,4 +82,9 @@ class ElasticPropertiesConfigAdapter extends StepRegistryPropertiesConfigAdapter
return get(ElasticProperties::getPipeline, ElasticConfig.super::pipeline);
}
@Override
public String apiKeyCredentials() {
return get(ElasticProperties::getApiKeyCredentials, ElasticConfig.super::apiKeyCredentials);
}
}

View File

@@ -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 {

View File

@@ -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.
@@ -90,4 +90,11 @@ class ElasticPropertiesConfigAdapterTests {
assertThat(new ElasticPropertiesConfigAdapter(properties).pipeline()).isEqualTo("testPipeline");
}
@Test
void whenPropertiesApiKeyCredentialsIsSetAdapterPipelineReturnsIt() {
ElasticProperties properties = new ElasticProperties();
properties.setApiKeyCredentials("secret");
assertThat(new ElasticPropertiesConfigAdapter(properties).apiKeyCredentials()).isEqualTo("secret");
}
}

View File

@@ -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.
@@ -44,6 +44,7 @@ class ElasticPropertiesTests extends StepRegistryPropertiesTests {
assertThat(properties.getUserName()).isEqualTo(config.userName());
assertThat(properties.isAutoCreateIndex()).isEqualTo(config.autoCreateIndex());
assertThat(properties.getPipeline()).isEqualTo(config.pipeline());
assertThat(properties.getApiKeyCredentials()).isEqualTo(config.apiKeyCredentials());
}
}