From dd475a244575ff3b6b00d9c3afe6959eb0718de9 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Wed, 20 Oct 2021 12:47:36 -0700 Subject: [PATCH] Expose Elastic's apiKeyCredentials property See gh-28400 --- .../export/elastic/ElasticProperties.java | 22 ++++++++++++++++--- .../ElasticPropertiesConfigAdapter.java | 7 +++++- .../ElasticPropertiesConfigAdapterTests.java | 9 +++++++- .../elastic/ElasticPropertiesTests.java | 3 ++- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticProperties.java index be2eea798b..9879583983 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticProperties.java @@ -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,14 @@ public class ElasticProperties extends StepRegistryProperties { private boolean autoCreateIndex = true; /** - * Login user of the Elastic server. + * Login user of the Elastic server. If API key is configured, it will be used for + * authentication instead of username/password. */ private String userName; /** - * Login password of the Elastic server. + * Login password of the Elastic server. If API key is configured, it will be used for + * authentication instead of username/password. */ private String password; @@ -74,6 +76,12 @@ public class ElasticProperties extends StepRegistryProperties { */ private String pipeline; + /** + * Base64-encoded credentials string. If configured, it will be used for + * authentication instead of username/password. + */ + private String apiKeyCredentials; + public String getHost() { return this.host; } @@ -146,4 +154,12 @@ public class ElasticProperties extends StepRegistryProperties { this.pipeline = pipeline; } + public String getApiKeyCredentials() { + return this.apiKeyCredentials; + } + + public void setApiKeyCredentials(String apiKeyCredentials) { + this.apiKeyCredentials = apiKeyCredentials; + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesConfigAdapter.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesConfigAdapter.java index ba5a7f3a5e..8c265aa67a 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesConfigAdapter.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesConfigAdapter.java @@ -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); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesConfigAdapterTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesConfigAdapterTests.java index 46e4371d56..8e53133703 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesConfigAdapterTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesConfigAdapterTests.java @@ -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"); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesTests.java index 7e692f0a7f..cc99c02ca6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticPropertiesTests.java @@ -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()); } }