Skip to content

Commit 4903ce1

Browse files
committed
Merge pull request #28400 from jonatan-ivanov
* gh-28400: Polish "Expose Elastic's apiKeyCredentials property" Expose Elastic's apiKeyCredentials property Closes gh-28400
2 parents 2ffcda3 + 8bf3780 commit 4903ce1

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-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: 16 additions & 3 deletions
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.
@@ -60,12 +60,12 @@ public class ElasticProperties extends StepRegistryProperties {
6060
private boolean autoCreateIndex = true;
6161

6262
/**
63-
* Login user of the Elastic server.
63+
* Login user of the Elastic server. Mutually exclusive with api-key-credentials.
6464
*/
6565
private String userName;
6666

6767
/**
68-
* Login password of the Elastic server.
68+
* Login password of the Elastic server. Mutually exclusive with api-key-credentials.
6969
*/
7070
private String password;
7171

@@ -74,6 +74,11 @@ public class ElasticProperties extends StepRegistryProperties {
7474
*/
7575
private String pipeline;
7676

77+
/**
78+
* Base64-encoded credentials string. Mutually exclusive with user-name and password.
79+
*/
80+
private String apiKeyCredentials;
81+
7782
public String getHost() {
7883
return this.host;
7984
}
@@ -146,4 +151,12 @@ public void setPipeline(String pipeline) {
146151
this.pipeline = pipeline;
147152
}
148153

154+
public String getApiKeyCredentials() {
155+
return this.apiKeyCredentials;
156+
}
157+
158+
public void setApiKeyCredentials(String apiKeyCredentials) {
159+
this.apiKeyCredentials = apiKeyCredentials;
160+
}
161+
149162
}

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

Lines changed: 6 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.
@@ -82,4 +82,9 @@ public String pipeline() {
8282
return get(ElasticProperties::getPipeline, ElasticConfig.super::pipeline);
8383
}
8484

85+
@Override
86+
public String apiKeyCredentials() {
87+
return get(ElasticProperties::getApiKeyCredentials, ElasticConfig.super::apiKeyCredentials);
88+
}
89+
8590
}

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

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

Lines changed: 8 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.
@@ -90,4 +90,11 @@ void whenPropertiesPipelineIsSetAdapterPipelineReturnsIt() {
9090
assertThat(new ElasticPropertiesConfigAdapter(properties).pipeline()).isEqualTo("testPipeline");
9191
}
9292

93+
@Test
94+
void whenPropertiesApiKeyCredentialsIsSetAdapterPipelineReturnsIt() {
95+
ElasticProperties properties = new ElasticProperties();
96+
properties.setApiKeyCredentials("secret");
97+
assertThat(new ElasticPropertiesConfigAdapter(properties).apiKeyCredentials()).isEqualTo("secret");
98+
}
99+
93100
}

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

Lines changed: 2 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.
@@ -44,6 +44,7 @@ void defaultValuesAreConsistent() {
4444
assertThat(properties.getUserName()).isEqualTo(config.userName());
4545
assertThat(properties.isAutoCreateIndex()).isEqualTo(config.autoCreateIndex());
4646
assertThat(properties.getPipeline()).isEqualTo(config.pipeline());
47+
assertThat(properties.getApiKeyCredentials()).isEqualTo(config.apiKeyCredentials());
4748
}
4849

4950
}

0 commit comments

Comments
 (0)