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

OZG-4453 enhance logging

parent f06ec33c
Branches
Tags
No related merge requests found
...@@ -19,9 +19,9 @@ import co.elastic.clients.transport.rest_client.RestClientTransport; ...@@ -19,9 +19,9 @@ 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 io.fabric8.kubernetes.api.model.Secret;
import lombok.extern.java.Log; import lombok.extern.log4j.Log4j2;
@Log @Log4j2
@Configuration @Configuration
public class ElasticsearchClientConfiguration { public class ElasticsearchClientConfiguration {
...@@ -33,7 +33,7 @@ public class ElasticsearchClientConfiguration { ...@@ -33,7 +33,7 @@ public class ElasticsearchClientConfiguration {
@Bean @Bean
@Scope("singleton") @Scope("singleton")
ElasticsearchClient createElasticsearchClient() { ElasticsearchClient createElasticsearchClient() {
log.info("Create elasticsearch client..."); LOG.info("Create elasticsearch client...");
var credentialsProvider = createCredentialsProvider(elasticSearchProperties.getServer().getSecretDataKey(), getPassword()); var credentialsProvider = createCredentialsProvider(elasticSearchProperties.getServer().getSecretDataKey(), getPassword());
var restClient = buildRestClient(credentialsProvider); var restClient = buildRestClient(credentialsProvider);
var transport = createRestClientTransport(restClient); var transport = createRestClientTransport(restClient);
...@@ -43,7 +43,6 @@ public class ElasticsearchClientConfiguration { ...@@ -43,7 +43,6 @@ public class ElasticsearchClientConfiguration {
private BasicCredentialsProvider createCredentialsProvider(String userName, String password) { private BasicCredentialsProvider createCredentialsProvider(String userName, String password) {
var credentialsProvider = new BasicCredentialsProvider(); var credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
log.info(String.format("use credentials: username: %s, password: %s", userName, password));
return credentialsProvider; return credentialsProvider;
} }
...@@ -54,8 +53,10 @@ public class ElasticsearchClientConfiguration { ...@@ -54,8 +53,10 @@ public class ElasticsearchClientConfiguration {
} }
private HttpHost createHttpHost() { private HttpHost createHttpHost() {
log.info(String.format("use host: %s with port: %s and scheme: %s", elasticSearchProperties.getServer().getHost(), elasticSearchProperties.getServer().getPort(), elasticSearchProperties.getServer().getScheme())); LOG.info(String.format("ElasticSearch config: host: %s with port: %s and scheme: %s", elasticSearchProperties.getServer().getHost(),
return new HttpHost(elasticSearchProperties.getServer().getHost(), elasticSearchProperties.getServer().getPort(), elasticSearchProperties.getServer().getScheme()); elasticSearchProperties.getServer().getPort(), elasticSearchProperties.getServer().getScheme()));
return new HttpHost(elasticSearchProperties.getServer().getHost(), elasticSearchProperties.getServer().getPort(),
elasticSearchProperties.getServer().getScheme());
} }
private RestClientTransport createRestClientTransport(RestClient restClient) { private RestClientTransport createRestClientTransport(RestClient restClient) {
...@@ -63,16 +64,16 @@ public class ElasticsearchClientConfiguration { ...@@ -63,16 +64,16 @@ public class ElasticsearchClientConfiguration {
} }
String getPassword() { String getPassword() {
log.info(String.format("get password from secret: %s in namespace %s", elasticSearchProperties.getServer().getSecretName(), elasticSearchProperties.getServer().getNamespace())); LOG.debug(String.format("get password from secret: %s in namespace %s", elasticSearchProperties.getServer().getSecretName(),
elasticSearchProperties.getServer().getNamespace()));
var secret = getCredentialsSecret(); var secret = getCredentialsSecret();
log.info(String.format("secret data: %s", secret.getData()));
var password = getPasswordFromSecret(secret); var password = getPasswordFromSecret(secret);
log.info(String.format("used password: %s", password));
return password; return password;
} }
private Secret getCredentialsSecret() { private Secret getCredentialsSecret() {
return kubernetesService.getSecretResource(elasticSearchProperties.getServer().getNamespace(), elasticSearchProperties.getServer().getSecretName()).get(); return kubernetesService
.getSecretResource(elasticSearchProperties.getServer().getNamespace(), elasticSearchProperties.getServer().getSecretName()).get();
} }
private String getPasswordFromSecret(Secret secret) { private String getPasswordFromSecret(Secret secret) {
......
package de.ozgcloud.operator.common.elasticsearch; package de.ozgcloud.operator.common.elasticsearch;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Level;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -13,9 +12,9 @@ import co.elastic.clients.elasticsearch.security.PutUserRequest; ...@@ -13,9 +12,9 @@ import co.elastic.clients.elasticsearch.security.PutUserRequest;
import de.ozgcloud.operator.PutRoleRequestData; import de.ozgcloud.operator.PutRoleRequestData;
import de.ozgcloud.operator.PutUserRequestData; import de.ozgcloud.operator.PutUserRequestData;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log; import lombok.extern.log4j.Log4j2;
@Log @Log4j2
@RequiredArgsConstructor @RequiredArgsConstructor
@Component @Component
public class ElasticsearchRemoteService { public class ElasticsearchRemoteService {
...@@ -24,24 +23,22 @@ public class ElasticsearchRemoteService { ...@@ -24,24 +23,22 @@ public class ElasticsearchRemoteService {
public boolean existsIndex(String index) throws Exception { public boolean existsIndex(String index) throws Exception {
try { try {
log.info("Exists index " + index + "..."); LOG.debug("Test if elasticsearch index {0} exits.", index);
var exists = client.indices().exists(builder -> builder.index(index)).value(); var exists = client.indices().exists(builder -> builder.index(index)).value();
log.info("Exists index: " + exists); LOG.debug("Elasticsearch index exists: {0}", exists);
return exists; return exists;
} catch (ElasticsearchException | IOException e) { } catch (ElasticsearchException | IOException e) {
log.log(Level.SEVERE, "Error checking index '" + index + "': " + e); throw new RuntimeException("Error checking index '" + index, e);
throw e;
} }
} }
public void createIndex(String indexName) throws Exception { public void createIndex(String indexName) throws Exception {
try { try {
log.info("Create index " + indexName + "..."); LOG.info("Create elasticsearch index {0}", indexName);
client.indices().create(builder -> builder.index(indexName)); client.indices().create(builder -> builder.index(indexName));
log.info("Create index successful."); LOG.info("Create elasticsearch index {0} successful", indexName);
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "Create index failed." + e); throw new RuntimeException("Create elasticsearch index " + indexName + "failed.", e);
throw e;
} }
} }
...@@ -51,12 +48,11 @@ public class ElasticsearchRemoteService { ...@@ -51,12 +48,11 @@ public class ElasticsearchRemoteService {
public void createSecurityRole(PutRoleRequestData requestData) throws Exception { public void createSecurityRole(PutRoleRequestData requestData) throws Exception {
try { try {
log.info("Create SecurityRole " + requestData.getName() + "..."); LOG.info("Create elasticsearch role {0}", requestData.getName());
client.security().putRole(createPutRoleRequest(requestData)); client.security().putRole(createPutRoleRequest(requestData));
log.info("Create SecurityRole successful."); LOG.info("Create elasticsearch role {0} successful", requestData.getName());
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "Create SecurityRole failed." + e); throw new RuntimeException("Create elasticsearch role " + requestData.getName() + "failed.", e);
throw e;
} }
} }
...@@ -84,12 +80,11 @@ public class ElasticsearchRemoteService { ...@@ -84,12 +80,11 @@ public class ElasticsearchRemoteService {
public void createSecurityUser(PutUserRequestData requestData) throws Exception { public void createSecurityUser(PutUserRequestData requestData) throws Exception {
try { try {
log.info("Create SecurityUser " + requestData.getUsername() + "..."); LOG.info("Create elasticsearch user {0}", requestData.getUsername());
client.security().putUser(createPutUserRequest(requestData)); client.security().putUser(createPutUserRequest(requestData));
log.info("Create SecurityUser successful."); LOG.info("Create elasticsearch user {0} successful", requestData.getUsername());
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "Create SecurityUser failed." + e); throw new RuntimeException("Create elasticsearch user " + requestData.getUsername() + "failed.", e);
throw e;
} }
} }
...@@ -107,34 +102,31 @@ public class ElasticsearchRemoteService { ...@@ -107,34 +102,31 @@ public class ElasticsearchRemoteService {
public void deleteIndex(String indexName) throws Exception { public void deleteIndex(String indexName) throws Exception {
try { try {
log.info("Delete index " + indexName + "..."); LOG.info("Delete elasticsearch index {0}", indexName);
client.indices().delete(builder -> builder.index(indexName)); client.indices().delete(builder -> builder.index(indexName));
log.info("Delete index successful."); LOG.info("Delete elasticsearch index {0} successful", indexName);
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "Delete index failed." + e); throw new RuntimeException("Delete elasticsearch index " + indexName + "failed.", e);
throw e;
} }
} }
public void deleteSecurityRole(String roleName) throws Exception { public void deleteSecurityRole(String roleName) throws Exception {
try { try {
log.info("Delete security role " + roleName + "..."); LOG.info("Delete elasticsearch role {0}", roleName);
client.security().deleteRole(builder -> builder.name(roleName)); client.security().deleteRole(builder -> builder.name(roleName));
log.info("Delete security role successful."); LOG.info("Delete elasticsearch role {0} successful", roleName);
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "Delete security role failed." + e); throw new RuntimeException("Delete elasticsearch role " + roleName + "failed.", e);
throw e;
} }
} }
public void deleteSecurityUser(String userName) throws Exception { public void deleteSecurityUser(String userName) throws Exception {
try { try {
log.info("Delete security user " + userName + "..."); LOG.info("Delete elasticsearch user {0}", userName);
client.security().deleteUser(builder -> builder.username(userName)); client.security().deleteUser(builder -> builder.username(userName));
log.info("Delete security user successful."); LOG.info("Delete elasticsearch user {0} successful", userName);
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "Delete security user failed." + e); throw new RuntimeException("Delete elasticsearch user " + userName + "failed.", e);
throw e;
} }
} }
} }
\ 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