Skip to content

Commit

Permalink
Resurrect the previous format alongside the new one
Browse files Browse the repository at this point in the history
  • Loading branch information
albertzaharovits committed Sep 20, 2018
1 parent bb5928f commit 1ca9d19
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 56 deletions.
38 changes: 38 additions & 0 deletions x-pack/plugin/core/src/main/config/log4j2.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
appender.deprecated_audit_rolling.type = RollingFile
appender.deprecated_audit_rolling.name = deprecated_audit_rolling
appender.deprecated_audit_rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_access.log
appender.deprecated_audit_rolling.layout.type = PatternLayout
appender.deprecated_audit_rolling.layout.pattern = \
[%d{ISO8601}]\
%varsNotEmpty{ [%map{host.ip}]}\
%varsNotEmpty{ [%map{host.name}]}\
%varsNotEmpty{ [%map{node.name}]}\
\ [%map{event.type}]\
\ [%map{event.action}]\
\ \ \
%varsNotEmpty{realm=[%map{realm}], }\
origin_type=[%map{origin.type}]\
%varsNotEmpty{, origin_address=[%map{origin.address}]}\
%varsNotEmpty{, principal=[%map{user.name}]}\
%varsNotEmpty{, realm=[%map{user.realm}]}\
%varsNotEmpty{, run_by_principal=[%map{user.run_by.name}]}\
%varsNotEmpty{, run_as_principal=[%map{user.run_as.name}]}\
%varsNotEmpty{, run_by_realm=[%map{user.run_by.realm}]}\
%varsNotEmpty{, run_as_realm=[%map{user.run_as.realm}]}\
%varsNotEmpty{, roles=[%map{deprecated.user.roles}]}\
%varsNotEmpty{, action=[%map{action}]}\
%varsNotEmpty{, indices=[%map{deprecated.indices}]}\
%varsNotEmpty{, request=[%map{request.name}]}\
%varsNotEmpty{, transport_profile=[%map{transport.profile}]}\
%varsNotEmpty{, rule=[%map{rule}]}\
%varsNotEmpty{, uri=[%map{deprecated.uri}]}\
%varsNotEmpty{, opaque_id=[%map{opaque_id}]}\
%varsNotEmpty{, request_body=[%map{request.body}]}\
%n
appender.deprecated_audit_rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_access-%d{yyyy-MM-dd}.log
appender.deprecated_audit_rolling.policies.type = Policies
appender.deprecated_audit_rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.deprecated_audit_rolling.policies.time.interval = 1
appender.deprecated_audit_rolling.policies.time.modulate = true

appender.audit_rolling.type = RollingFile
appender.audit_rolling.name = audit_rolling
appender.audit_rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_audit.log
Expand Down Expand Up @@ -67,6 +104,7 @@ appender.audit_rolling.policies.time.modulate = true
logger.xpack_security_audit_logfile.name = org.elasticsearch.xpack.security.audit.logfile.LoggingAuditTrail
logger.xpack_security_audit_logfile.level = info
logger.xpack_security_audit_logfile.appenderRef.audit_rolling.ref = audit_rolling
logger.xpack_security_audit_logfile.appenderRef.deprecated_audit_rolling.ref = deprecated_audit_rolling
logger.xpack_security_audit_logfile.additivity = false

logger.xmlsig.name = org.apache.xml.security.signature.XMLSignature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
public class CapturingLogger {

private static final String IMPLICIT_APPENDER_NAME = "__implicit";

/**
* Constructs a new {@link CapturingLogger} named as the fully qualified name of
* the invoking method. One name can be assigned to a single logger globally, so
Expand All @@ -52,42 +54,88 @@ public static Logger newCapturingLogger(final Level level, @Nullable StringLayou
final String name = caller.getClassName() + "." + caller.getMethodName() + "." + level.toString();
final Logger logger = ESLoggerFactory.getLogger(name);
Loggers.setLevel(logger, level);
final MockAppender appender = new MockAppender(name, layout);
attachNewMockAppender(logger, IMPLICIT_APPENDER_NAME, layout);
return logger;
}

public static void attachNewMockAppender(final Logger logger, final String appenderName, @Nullable StringLayout layout)
throws IllegalAccessException {
final MockAppender appender = new MockAppender(buildAppenderName(logger.getName(), appenderName), layout);
appender.start();
Loggers.addAppender(logger, appender);
return logger;
}

private static MockAppender getMockAppender(final String name) {
private static String buildAppenderName(final String loggerName, final String appenderName) {
// appender name also has to be unique globally (logging context globally)
return loggerName + "." + appenderName;
}

private static MockAppender getMockAppender(final String loggerName, final String appenderName) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
final LoggerConfig loggerConfig = config.getLoggerConfig(name);
return (MockAppender) loggerConfig.getAppenders().get(name);
final LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
final String mockAppenderName = buildAppenderName(loggerName, appenderName);
return (MockAppender) loggerConfig.getAppenders().get(mockAppenderName);
}

/**
* Checks if the logger's appender has captured any events.
* Checks if the logger's appender(s) has captured any events.
*
* @param name
* @param loggerName
* The unique global name of the logger.
* @param appenderNames
* Names of other appenders nested under this same logger.
* @return {@code true} if no event has been captured, {@code false} otherwise.
*/
public static boolean isEmpty(final String name) {
final MockAppender appender = getMockAppender(name);
return appender.isEmpty();
public static boolean isEmpty(final String loggerName, final String... appenderNames) {
// check if implicit appender is empty
final MockAppender implicitAppender = getMockAppender(loggerName, IMPLICIT_APPENDER_NAME);
assert implicitAppender != null;
if (false == implicitAppender.isEmpty()) {
return false;
}
if (null == appenderNames) {
return true;
}
// check if any named appenders are empty
for (String appenderName : appenderNames) {
final MockAppender namedAppender = getMockAppender(loggerName, appenderName);
if (namedAppender != null && false == namedAppender.isEmpty()) {
return false;
}
}
return true;
}

/**
* Gets the captured events for a logger by its name. Events are those of the
* implicit appender of the logger.
*
* @param loggerName
* The unique global name of the logger.
* @param level
* The priority level of the captured events to be returned.
* @return A list of captured events formated to {@code String}.
*/
public static List<String> output(final String loggerName, final Level level) {
return output(loggerName, IMPLICIT_APPENDER_NAME, level);
}

/**
* Gets the captured events for a logger by its name.
* Gets the captured events for a logger and an appender by their respective
* names. There is a one to many relationship between loggers and appenders.
*
* @param name
* @param loggerName
* The unique global name of the logger.
* @param appenderName
* The name of an appender associated with the {@code loggerName}
* logger.
* @param level
* The priority level of the captured events to be returned.
* @return A list of captured events formated to {@code String}.
*/
public static List<String> output(final String name, final Level level) {
final MockAppender appender = getMockAppender(name);
public static List<String> output(final String loggerName, final String appenderName, final Level level) {
final MockAppender appender = getMockAppender(loggerName, appenderName);
return appender.output(level);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ LogEntryBuilder withRestUri(RestRequest request) {
if (queryStringIndex > -1) {
logEntry.with(URL_QUERY_FIELD_NAME, request.uri().substring(queryStringIndex + 1, queryStringLength));
}
// deprecated uri format
logEntry.with("deprecated.uri", request.uri());
return this;
}

Expand All @@ -626,10 +628,16 @@ LogEntryBuilder withRunAsSubject(Authentication authentication) {

LogEntryBuilder withRestOrigin(RestRequest request) {
assert LOCAL_ORIGIN_FIELD_VALUE.equals(logEntry.get(ORIGIN_TYPE_FIELD_NAME)); // this is the default
final InetSocketAddress socketAddress = request.getHttpChannel().getRemoteAddress();
final String formattedAddress;
final SocketAddress socketAddress = request.getRemoteAddress();
if (socketAddress instanceof InetSocketAddress) {
formattedAddress = NetworkAddress.format(((InetSocketAddress) socketAddress));
} else {
formattedAddress = socketAddress.toString();
}
if (socketAddress != null) {
logEntry.with(ORIGIN_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE)
.with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(socketAddress));
.with(ORIGIN_ADDRESS_FIELD_NAME, formattedAddress);
}
// fall through to local_node default
return this;
Expand Down Expand Up @@ -700,6 +708,8 @@ LogEntryBuilder with(String key, String value) {
LogEntryBuilder with(String key, String[] values) {
if (values != null) {
logEntry.with(key, toQuotedJsonArray(values));
// deprecated format required for bwc
logEntry.with("deprecated." + key, Strings.arrayToCommaDelimitedString(values));
}
return this;
}
Expand Down
Loading

0 comments on commit 1ca9d19

Please sign in to comment.