1. 本文目標
? ? 本文主要介紹Sentry的審計日志,介紹Sentry審計日志怎么配置,在哪里記錄日志,日志格式。為了幫助讀者更好地了解Sentry審計日志,也會簡單介紹下什么是審計日志,Log4j日志。
2. 審計日志
? ? 審計日志的主要作用是記錄用戶對系統(tǒng)的各種操作行為,通過審計日志,可以對系統(tǒng)進行故障分析,行為分析,安全審計等。普通日志更多記錄:程序運行的錯誤,警告以及全鏈路的追蹤。
3. Log4j日志框架
? ? Log4j是非常流行的日志框架。主要包括Appender, Filter, Layout, Console/File/Socket這4個組件。Appender執(zhí)行日志的輸出,F(xiàn)ilter過濾哪些日志需要輸出,哪些日志不輸出,Layout指定日志的格式,Console/File/Socket指定日志輸出的目的地。
? ? Log4j也非常容易擴展,根據(jù)自己的需要定制自己的日志類。主要是對Appender進行擴展,實現(xiàn)?org.apache.log4j.AppenderSkeleton 類,然后里面改造成你想要的效果。
? ??
4. Sentry審計日志
4.1 審計日志記錄哪些內(nèi)容
? ?主要是這些操作被記錄: create role, drop role, add role to group, delete role from group, grant privilege, revoke privilege
?4.2 Sentry的審計日志:
? ???log4j.logger.sentry.hive.authorization.ddl.logger=INFO, sentryHiveAudit
? ????log4j.appender.sentryHiveAudit=org.apache.sentry.provider.db.log.appender.RollingFileWithoutDeleteAppender ??# set the appender class
? ? log4j.appender.sentryHiveAudit.File=/var/log/sentry/sentryHiveAudit.log ? ? ? ? ?# set the log file location
? ?log4j.appender.sentryHiveAudit.MaxFileSize=5MB??????????????????????????????????????? ???# set the max size for the log file
? ? log4j.appender.sentryHiveAudit.layout= org.apache.log4j.PatternLayout ? ? ? ? # set the layout class
? ?log4j.appender.sentryHiveAudit.layout.ConversionPattern=%m%n ? ? ? ? ? ? ? ? ?# set the message forma
這段配置的意思是,logger是sentryHiveAudit,這個對應(yīng)Appender是org.apache.sentry.provider.db.log.appender.RollingFileWithoutDeleteAppender,意味Sentry的審計日志擴展了Log4j。下面是定義了日志的存儲位置,日志格式等信息。
4.3 審計日志打點位置
? ? 既然審計日志記錄都是用戶操作行為,對于Sentry來說,肯定是在對外的Thrift接口處:SentryPolicyStoreProcessor。例如創(chuàng)建角色接口處打印了審計日志:
@Override
public TCreateSentryRoleResponse create_sentry_role(
TCreateSentryRoleRequest request)throws TException {
final Timer.Context timerContext =sentryMetrics.createRoleTimer.time();
TCreateSentryRoleResponse response =new TCreateSentryRoleResponse();
try {
validateClientVersion(request.getProtocol_version());
authorize(request.getRequestorUserName(),
getRequestorGroups(request.getRequestorUserName()));
CommitContext commitContext =sentryStore.createSentryRole(request.getRoleName());
response.setStatus(Status.OK());
notificationHandlerInvoker.create_sentry_role(commitContext,
request, response);
}catch (SentryAlreadyExistsException e) {
String msg ="Role: " + request +" already exists.";
LOGGER.error(msg, e);
response.setStatus(Status.AlreadyExists(msg, e));
}catch (SentryAccessDeniedException e) {
LOGGER.error(e.getMessage(), e);
response.setStatus(Status.AccessDenied(e.getMessage(), e));
}catch (SentryThriftAPIMismatchException e) {
LOGGER.error(e.getMessage(), e);
response.setStatus(Status.THRIFT_VERSION_MISMATCH(e.getMessage(), e));
}catch (Exception e) {
String msg ="Unknown error for request: " + request +", message: " + e.getMessage();
LOGGER.error(msg, e);
response.setStatus(Status.RuntimeError(msg, e));
}finally {
timerContext.stop();
}
try {
AUDIT_LOGGER.info(JsonLogEntityFactory.getInstance()
.createJsonLogEntity(request, response,conf).toJsonFormatLog());
}catch (Exception e) {
// if any exception, log the exception.
? ? String msg ="Error creating audit log for create role: " + e.getMessage();
LOGGER.error(msg, e);
}
return response;
}
4.4 Sentry日志格式
? ? 審計日志格式如下,以下示例是創(chuàng)建角色時記錄的審計日志:
{"serviceName":"Sentry-Service","userName":"admin","impersonator":"","ipAddress":"/127.0.0.1","operation":"CREATE_ROLE","eventTime":"1588758753463","operationText":"CREATE ROLE test","allowed":"false","databaseName":null,"tableName":null,"column":null,"resourcePath":null,"objectType":"ROLE"}