Skip to content

Commit 8bf3780

Browse files
committed
Polish "Expose Elastic's apiKeyCredentials property"
See gh-28400
1 parent dd475a2 commit 8bf3780

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticMetricsExportAutoConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3333
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3434
import org.springframework.boot.context.properties.EnableConfigurationProperties;
35+
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
3536
import org.springframework.context.annotation.Bean;
3637
import org.springframework.context.annotation.Configuration;
3738

@@ -60,6 +61,14 @@ public ElasticMetricsExportAutoConfiguration(ElasticProperties properties) {
6061
@Bean
6162
@ConditionalOnMissingBean
6263
public ElasticConfig elasticConfig() {
64+
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
65+
entries.put("api-key-credentials", this.properties.getApiKeyCredentials());
66+
entries.put("user-name", this.properties.getUserName());
67+
});
68+
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
69+
entries.put("api-key-credentials", this.properties.getApiKeyCredentials());
70+
entries.put("password", this.properties.getPassword());
71+
});
6372
return new ElasticPropertiesConfigAdapter(this.properties);
6473
}
6574

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticProperties.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,12 @@ public class ElasticProperties extends StepRegistryProperties {
6060
private boolean autoCreateIndex = true;
6161

6262
/**
63-
* Login user of the Elastic server. If API key is configured, it will be used for
64-
* authentication instead of username/password.
63+
* Login user of the Elastic server. Mutually exclusive with api-key-credentials.
6564
*/
6665
private String userName;
6766

6867
/**
69-
* Login password of the Elastic server. If API key is configured, it will be used for
70-
* authentication instead of username/password.
68+
* Login password of the Elastic server. Mutually exclusive with api-key-credentials.
7169
*/
7270
private String password;
7371

@@ -77,8 +75,7 @@ public class ElasticProperties extends StepRegistryProperties {
7775
private String pipeline;
7876

7977
/**
80-
* Base64-encoded credentials string. If configured, it will be used for
81-
* authentication instead of username/password.
78+
* Base64-encoded credentials string. Mutually exclusive with user-name and password.
8279
*/
8380
private String apiKeyCredentials;
8481

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticMetricsExportAutoConfigurationTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
2526
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2627
import org.springframework.context.annotation.Bean;
2728
import org.springframework.context.annotation.Configuration;
@@ -90,6 +91,24 @@ void stopsMeterRegistryWhenContextIsClosed() {
9091
});
9192
}
9293

94+
@Test
95+
void apiKeyCredentialsIsMutuallyExclusiveWithUserName() {
96+
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
97+
.withPropertyValues("management.metrics.export.elastic.api-key-credentials:secret",
98+
"management.metrics.export.elastic.user-name:alice")
99+
.run((context) -> assertThat(context).hasFailed().getFailure().getRootCause()
100+
.isInstanceOf(MutuallyExclusiveConfigurationPropertiesException.class));
101+
}
102+
103+
@Test
104+
void apiKeyCredentialsIsMutuallyExclusiveWithPassword() {
105+
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
106+
.withPropertyValues("management.metrics.export.elastic.api-key-credentials:secret",
107+
"management.metrics.export.elastic.password:secret")
108+
.run((context) -> assertThat(context).hasFailed().getFailure().getRootCause()
109+
.isInstanceOf(MutuallyExclusiveConfigurationPropertiesException.class));
110+
}
111+
93112
@Configuration(proxyBeanMethods = false)
94113
static class BaseConfiguration {
95114

0 commit comments

Comments
 (0)