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

OZG-4453 enhance logging

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