This is an automated email from the ASF dual-hosted git repository. nbonte pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/atlas.git
commit 7dec8f538b6f3f52a4803b63efd3c5b0b41e966c Author: Mandar Ambawane <[email protected]> AuthorDate: Thu Oct 22 14:23:59 2020 +0530 ATLAS-3957 Use Audit framework to capture audit entries for Server Start and Server In Active mode (HA) Signed-off-by: Nikhil P Bonte <[email protected]> --- addons/models/0000-Area0/0010-base_model.json | 8 ++++ ...-base_model_add_atlas_operation_attributes.json | 10 ++++- .../apache/atlas/model/audit/AtlasAuditEntry.java | 4 +- .../resources/solr/core-template/solrconfig.xml | 2 +- .../src/main/java/org/apache/atlas/BeanUtil.java | 38 +++++++++++++++++ .../apache/atlas/web/service/EmbeddedServer.java | 49 ++++++++++++++++++++++ .../org/apache/atlas/web/service/ServiceState.java | 45 +++++++++++++++++++- 7 files changed, 151 insertions(+), 5 deletions(-) diff --git a/addons/models/0000-Area0/0010-base_model.json b/addons/models/0000-Area0/0010-base_model.json index 1de9e57..78ba927 100644 --- a/addons/models/0000-Area0/0010-base_model.json +++ b/addons/models/0000-Area0/0010-base_model.json @@ -37,6 +37,14 @@ { "ordinal": 7, "value": "TYPE_DEF_DELETE" + }, + { + "ordinal": 8, + "value": "SERVER_START" + }, + { + "ordinal": 9, + "value": "SERVER_STATE_ACTIVE" } ] } diff --git a/addons/models/0000-Area0/patches/006-base_model_add_atlas_operation_attributes.json b/addons/models/0000-Area0/patches/006-base_model_add_atlas_operation_attributes.json index a27525e..6d96391 100644 --- a/addons/models/0000-Area0/patches/006-base_model_add_atlas_operation_attributes.json +++ b/addons/models/0000-Area0/patches/006-base_model_add_atlas_operation_attributes.json @@ -20,7 +20,15 @@ { "ordinal": 7, "value": "TYPE_DEF_DELETE" - } + }, + { + "ordinal": 8, + "value": "SERVER_START" + }, + { + "ordinal": 9, + "value": "SERVER_STATE_ACTIVE" + } ] } ] diff --git a/intg/src/main/java/org/apache/atlas/model/audit/AtlasAuditEntry.java b/intg/src/main/java/org/apache/atlas/model/audit/AtlasAuditEntry.java index 9ed4168..98eacfb 100644 --- a/intg/src/main/java/org/apache/atlas/model/audit/AtlasAuditEntry.java +++ b/intg/src/main/java/org/apache/atlas/model/audit/AtlasAuditEntry.java @@ -43,7 +43,9 @@ public class AtlasAuditEntry extends AtlasBaseModelObject implements Serializabl IMPORT_DELETE_REPL("IMPORT_DELETE_REPL"), TYPE_DEF_CREATE("TYPE_DEF_CREATE"), TYPE_DEF_UPDATE("TYPE_DEF_UPDATE"), - TYPE_DEF_DELETE("TYPE_DEF_DELETE"); + TYPE_DEF_DELETE("TYPE_DEF_DELETE"), + SERVER_START("SERVER_START"), + SERVER_STATE_ACTIVE("SERVER_STATE_ACTIVE"); private final String type; diff --git a/test-tools/src/main/resources/solr/core-template/solrconfig.xml b/test-tools/src/main/resources/solr/core-template/solrconfig.xml index 7cbfbd9..94f346f 100644 --- a/test-tools/src/main/resources/solr/core-template/solrconfig.xml +++ b/test-tools/src/main/resources/solr/core-template/solrconfig.xml @@ -434,7 +434,7 @@ --> <lst name="defaults"> <str name="defType">edismax</str> - <str name="qf">3k05_t 35x_t f0l_t i6d_l 7f2d_t 7gn9_t 3r45_s jr9_t 3u9x_t lc5_t mx1_t 7dhh_t iyt_l 3j7p_t 7klh_t 7hfp_t 7i85_t ohx_t 7bwl_l 7cp1_l</str> + <str name="qf">3ll1_t 35x_t f0l_t i6d_l 7f2d_t 7gn9_t 3sp1_s jr9_t 3vut_t lc5_t mx1_t 7dhh_t iyt_l 3j7p_t 7klh_t 7hfp_t 7i85_t ohx_t 7bwl_l 7cp1_l</str> <str name="hl.fl">*</str> <bool name="hl.requireFieldMatch">true</bool> <bool name="lowercaseOperators">true</bool> diff --git a/webapp/src/main/java/org/apache/atlas/BeanUtil.java b/webapp/src/main/java/org/apache/atlas/BeanUtil.java new file mode 100644 index 0000000..ef5a741 --- /dev/null +++ b/webapp/src/main/java/org/apache/atlas/BeanUtil.java @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component +public class BeanUtil implements ApplicationContextAware { + private static ApplicationContext context; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + context = applicationContext; + } + + public static <T> T getBean(Class<T> beanClass) { + return context.getBean(beanClass); + } +} \ No newline at end of file diff --git a/webapp/src/main/java/org/apache/atlas/web/service/EmbeddedServer.java b/webapp/src/main/java/org/apache/atlas/web/service/EmbeddedServer.java index 6985291..bdd660f 100755 --- a/webapp/src/main/java/org/apache/atlas/web/service/EmbeddedServer.java +++ b/webapp/src/main/java/org/apache/atlas/web/service/EmbeddedServer.java @@ -20,7 +20,12 @@ package org.apache.atlas.web.service; import org.apache.atlas.AtlasConfiguration; import org.apache.atlas.AtlasErrorCode; +import org.apache.atlas.BeanUtil; +import org.apache.atlas.RequestContext; import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.audit.AtlasAuditEntry; +import org.apache.atlas.repository.audit.AtlasAuditService; +import org.apache.commons.lang.StringUtils; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -32,6 +37,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Date; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; @@ -43,8 +51,14 @@ public class EmbeddedServer { public static final String ATLAS_DEFAULT_BIND_ADDRESS = "0.0.0.0"; + public static final Date SERVER_START_TIME = new Date(); + protected final Server server; + private AtlasAuditService auditService; + + private ServiceState serviceState; + public EmbeddedServer(String host, int port, String path) throws IOException { int queueSize = AtlasConfiguration.WEBSERVER_QUEUE_SIZE.getInt(); LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize); @@ -96,6 +110,9 @@ public class EmbeddedServer { public void start() throws AtlasBaseException { try { server.start(); + + auditServerStatus(); + server.join(); } catch(Exception e) { throw new AtlasBaseException(AtlasErrorCode.EMBEDDED_SERVER_START, e); @@ -109,4 +126,36 @@ public class EmbeddedServer { LOG.warn("Error during shutdown", e); } } + + private void auditServerStatus() { + auditService = BeanUtil.getBean(AtlasAuditService.class); + serviceState = BeanUtil.getBean(ServiceState.class); + + ServiceState.ServiceStateValue serviceStateValue = serviceState.getState(); + String userName = RequestContext.getCurrentUser(); + + if (userName == null) { + userName = StringUtils.EMPTY; + } + + if (serviceStateValue == ServiceState.ServiceStateValue.ACTIVE) { + String hostName = StringUtils.EMPTY; + String hostAddress = StringUtils.EMPTY; + Date date = new Date(); + + try { + hostName = InetAddress.getLocalHost().getHostName(); + hostAddress = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + LOG.error("Exception occurred during InetAddress retrieval", e); + } + + try { + auditService.add(userName, AtlasAuditEntry.AuditOperation.SERVER_START, hostName + ":" + hostAddress, SERVER_START_TIME, date, null, null, 0); + auditService.add(userName, AtlasAuditEntry.AuditOperation.SERVER_STATE_ACTIVE, hostName + ":" + hostAddress, date, date, null, null, 0); + } catch (AtlasBaseException e) { + LOG.error("Exception occurred during audit", e); + } + } + } } diff --git a/webapp/src/main/java/org/apache/atlas/web/service/ServiceState.java b/webapp/src/main/java/org/apache/atlas/web/service/ServiceState.java index 113b2b2..93e6513 100644 --- a/webapp/src/main/java/org/apache/atlas/web/service/ServiceState.java +++ b/webapp/src/main/java/org/apache/atlas/web/service/ServiceState.java @@ -21,15 +21,24 @@ package org.apache.atlas.web.service; import com.google.common.base.Preconditions; import org.apache.atlas.ApplicationProperties; import org.apache.atlas.AtlasException; +import org.apache.atlas.RequestContext; +import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.ha.HAConfiguration; +import org.apache.atlas.model.audit.AtlasAuditEntry; +import org.apache.atlas.repository.audit.AtlasAuditService; import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.inject.Singleton; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Date; + import static org.apache.atlas.AtlasConstants.ATLAS_MIGRATION_MODE_FILENAME; /** @@ -43,6 +52,9 @@ import static org.apache.atlas.AtlasConstants.ATLAS_MIGRATION_MODE_FILENAME; public class ServiceState { private static final Logger LOG = LoggerFactory.getLogger(ServiceState.class); + @Autowired + AtlasAuditService auditService; + public enum ServiceStateValue { ACTIVE, PASSIVE, @@ -78,9 +90,38 @@ public class ServiceState { } private void setState(ServiceStateValue newState) { - Preconditions.checkState(HAConfiguration.isHAEnabled(configuration), - "Cannot change state as requested, as HA is not enabled for this instance."); + Preconditions.checkState(HAConfiguration.isHAEnabled(configuration), "Cannot change state as requested, as HA is not enabled for this instance."); + state = newState; + + auditServerStatus(); + } + + private void auditServerStatus() { + String userName = RequestContext.getCurrentUser(); + + if (userName == null) { + userName = StringUtils.EMPTY; + } + + if (state == ServiceState.ServiceStateValue.ACTIVE) { + String hostName = StringUtils.EMPTY; + String hostAddress = StringUtils.EMPTY; + Date date = new Date(); + + try { + hostName = InetAddress.getLocalHost().getHostName(); + hostAddress = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + LOG.error("Exception occurred during InetAddress retrieval", e); + } + try { + auditService.add(userName, AtlasAuditEntry.AuditOperation.SERVER_START, hostName + ":" + hostAddress, EmbeddedServer.SERVER_START_TIME, date, null, null, 0); + auditService.add(userName, AtlasAuditEntry.AuditOperation.SERVER_STATE_ACTIVE, hostName + ":" + hostAddress, date, date, null, null, 0); + } catch (AtlasBaseException e) { + LOG.error("Exception occurred during audit", e); + } + } } public void setActive() {
