Skip to content
Snippets Groups Projects
Commit abb2bd8d authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4453 decode password

parent 335908f9
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.operator.common.elasticsearch; package de.ozgcloud.operator.common.elasticsearch;
import java.util.Base64;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
...@@ -16,6 +18,7 @@ import co.elastic.clients.json.jackson.JacksonJsonpMapper; ...@@ -16,6 +18,7 @@ import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport; import co.elastic.clients.transport.rest_client.RestClientTransport;
import de.ozgcloud.operator.OzgCloudElasticsearchProperties; import de.ozgcloud.operator.OzgCloudElasticsearchProperties;
import de.ozgcloud.operator.common.kubernetes.KubernetesRemoteService; import de.ozgcloud.operator.common.kubernetes.KubernetesRemoteService;
import io.fabric8.kubernetes.api.model.Secret;
import lombok.extern.java.Log; import lombok.extern.java.Log;
@Log @Log
...@@ -60,10 +63,24 @@ public class ElasticsearchClientConfiguration { ...@@ -60,10 +63,24 @@ public class ElasticsearchClientConfiguration {
} }
String getPassword() { String getPassword() {
log.info(String.format("get password from secret: %s in namespace %s", elasticSearchProperties.getServer().getNamespace(), elasticSearchProperties.getServer().getSecretName())); log.info(String.format("get password from secret: %s in namespace %s", elasticSearchProperties.getServer().getSecretName(), elasticSearchProperties.getServer().getNamespace()));
var resource = kubernetesService.getSecretResource(elasticSearchProperties.getServer().getNamespace(), elasticSearchProperties.getServer().getSecretName()); var secret = getCredentialsSecret();
var password = MapUtils.getString(resource.get().getStringData(), elasticSearchProperties.getServer().getSecretDataKey()); var password = getPasswordFromSecret(secret);
log.info(String.format("used password: %s", password)); log.info(String.format("used password: %s", password));
return password; return password;
} }
private Secret getCredentialsSecret() {
return kubernetesService.getSecretResource(elasticSearchProperties.getServer().getNamespace(), elasticSearchProperties.getServer().getSecretName()).get();
}
private String getPasswordFromSecret(Secret secret) {
var encodedPassword = MapUtils.getString(secret.getStringData(), elasticSearchProperties.getServer().getSecretDataKey());
return decode(encodedPassword, secret);
}
private String decode(String encodedPassword, Secret secret) {
return new String(Base64.getDecoder().decode(encodedPassword));
}
} }
\ No newline at end of file
package de.ozgcloud.operator.common.elasticsearch;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Base64;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import de.ozgcloud.operator.OzgCloudElasticsearchProperties;
import de.ozgcloud.operator.OzgCloudElasticsearchProperties.OzgCloudElasticsearchServerProperties;
import de.ozgcloud.operator.common.kubernetes.KubernetesRemoteService;
import de.ozgcloud.operator.common.kubernetes.SecretTestFactory;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.dsl.Resource;
class ElasticsearchClientConfigurationTest {
@Spy
@InjectMocks
private ElasticsearchClientConfiguration configuration;
@Mock
private OzgCloudElasticsearchProperties properties;
@Mock
private KubernetesRemoteService kubernetesRemoteService;
@DisplayName("Create elasticsearch client")
@Nested
class TestCreateElasticsearchClient {
private static final String SECRET_DATA_KEY = "dsefsfef";
private static final String SECRET_DATA_VALUE = "testPassword";
private static final String SECRET_DATA_ENCODED_VALUE = encodeStringBase64(SECRET_DATA_VALUE);
private static final Secret SECRET = SecretTestFactory.createBuilder().addToStringData(SECRET_DATA_KEY, SECRET_DATA_ENCODED_VALUE).build();
@Mock
private Resource<Secret> secretResource;
@Mock
private OzgCloudElasticsearchServerProperties serverProperties;
@BeforeEach
void mock() {
when(properties.getServer()).thenReturn(serverProperties);
when(serverProperties.getSecretDataKey()).thenReturn(SECRET_DATA_KEY);
when(kubernetesRemoteService.getSecretResource(any(), any())).thenReturn(secretResource);
when(secretResource.get()).thenReturn(SECRET);
}
@Test
void shouldReturnPasssowrd() {
var password = configuration.getPassword();
assertThat(password).isEqualTo(SECRET_DATA_VALUE);
}
private static String encodeStringBase64(String string) {
return Base64.getEncoder().encodeToString(string.getBytes());
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment