Commit 7dec8f53 by Mandar Ambawane Committed by Nikhil Bonte

ATLAS-3957 Use Audit framework to capture audit entries for Server Start and…

ATLAS-3957 Use Audit framework to capture audit entries for Server Start and Server In Active mode (HA) Signed-off-by: 's avatarNikhil P Bonte <nbonte@apache.org>
parent f959d84a
...@@ -37,6 +37,14 @@ ...@@ -37,6 +37,14 @@
{ {
"ordinal": 7, "ordinal": 7,
"value": "TYPE_DEF_DELETE" "value": "TYPE_DEF_DELETE"
},
{
"ordinal": 8,
"value": "SERVER_START"
},
{
"ordinal": 9,
"value": "SERVER_STATE_ACTIVE"
} }
] ]
} }
......
...@@ -20,7 +20,15 @@ ...@@ -20,7 +20,15 @@
{ {
"ordinal": 7, "ordinal": 7,
"value": "TYPE_DEF_DELETE" "value": "TYPE_DEF_DELETE"
} },
{
"ordinal": 8,
"value": "SERVER_START"
},
{
"ordinal": 9,
"value": "SERVER_STATE_ACTIVE"
}
] ]
} }
] ]
......
...@@ -43,7 +43,9 @@ public class AtlasAuditEntry extends AtlasBaseModelObject implements Serializabl ...@@ -43,7 +43,9 @@ public class AtlasAuditEntry extends AtlasBaseModelObject implements Serializabl
IMPORT_DELETE_REPL("IMPORT_DELETE_REPL"), IMPORT_DELETE_REPL("IMPORT_DELETE_REPL"),
TYPE_DEF_CREATE("TYPE_DEF_CREATE"), TYPE_DEF_CREATE("TYPE_DEF_CREATE"),
TYPE_DEF_UPDATE("TYPE_DEF_UPDATE"), 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; private final String type;
......
...@@ -434,7 +434,7 @@ ...@@ -434,7 +434,7 @@
--> -->
<lst name="defaults"> <lst name="defaults">
<str name="defType">edismax</str> <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> <str name="hl.fl">*</str>
<bool name="hl.requireFieldMatch">true</bool> <bool name="hl.requireFieldMatch">true</bool>
<bool name="lowercaseOperators">true</bool> <bool name="lowercaseOperators">true</bool>
......
/**
* 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
...@@ -20,7 +20,12 @@ package org.apache.atlas.web.service; ...@@ -20,7 +20,12 @@ package org.apache.atlas.web.service;
import org.apache.atlas.AtlasConfiguration; import org.apache.atlas.AtlasConfiguration;
import org.apache.atlas.AtlasErrorCode; 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.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.Connector;
import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.HttpConnectionFactory;
...@@ -32,6 +37,9 @@ import org.slf4j.Logger; ...@@ -32,6 +37,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; 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.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -43,8 +51,14 @@ public class EmbeddedServer { ...@@ -43,8 +51,14 @@ public class EmbeddedServer {
public static final String ATLAS_DEFAULT_BIND_ADDRESS = "0.0.0.0"; 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; protected final Server server;
private AtlasAuditService auditService;
private ServiceState serviceState;
public EmbeddedServer(String host, int port, String path) throws IOException { public EmbeddedServer(String host, int port, String path) throws IOException {
int queueSize = AtlasConfiguration.WEBSERVER_QUEUE_SIZE.getInt(); int queueSize = AtlasConfiguration.WEBSERVER_QUEUE_SIZE.getInt();
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize); LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize);
...@@ -96,6 +110,9 @@ public class EmbeddedServer { ...@@ -96,6 +110,9 @@ public class EmbeddedServer {
public void start() throws AtlasBaseException { public void start() throws AtlasBaseException {
try { try {
server.start(); server.start();
auditServerStatus();
server.join(); server.join();
} catch(Exception e) { } catch(Exception e) {
throw new AtlasBaseException(AtlasErrorCode.EMBEDDED_SERVER_START, e); throw new AtlasBaseException(AtlasErrorCode.EMBEDDED_SERVER_START, e);
...@@ -109,4 +126,36 @@ public class EmbeddedServer { ...@@ -109,4 +126,36 @@ public class EmbeddedServer {
LOG.warn("Error during shutdown", e); 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);
}
}
}
} }
...@@ -21,15 +21,24 @@ package org.apache.atlas.web.service; ...@@ -21,15 +21,24 @@ package org.apache.atlas.web.service;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.apache.atlas.ApplicationProperties; import org.apache.atlas.ApplicationProperties;
import org.apache.atlas.AtlasException; 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.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.configuration.Configuration;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.inject.Singleton; 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; import static org.apache.atlas.AtlasConstants.ATLAS_MIGRATION_MODE_FILENAME;
/** /**
...@@ -43,6 +52,9 @@ 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 { public class ServiceState {
private static final Logger LOG = LoggerFactory.getLogger(ServiceState.class); private static final Logger LOG = LoggerFactory.getLogger(ServiceState.class);
@Autowired
AtlasAuditService auditService;
public enum ServiceStateValue { public enum ServiceStateValue {
ACTIVE, ACTIVE,
PASSIVE, PASSIVE,
...@@ -78,9 +90,38 @@ public class ServiceState { ...@@ -78,9 +90,38 @@ public class ServiceState {
} }
private void setState(ServiceStateValue newState) { private void setState(ServiceStateValue newState) {
Preconditions.checkState(HAConfiguration.isHAEnabled(configuration), Preconditions.checkState(HAConfiguration.isHAEnabled(configuration), "Cannot change state as requested, as HA is not enabled for this instance.");
"Cannot change state as requested, as HA is not enabled for this instance.");
state = newState; 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() { public void setActive() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment