Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* 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
Expand All @@ -23,13 +23,14 @@
/**
* Defines audit message structure.
*/
public class AuditMessage implements Message {

private String message;
private Throwable throwable;
public final class AuditMessage implements Message {

public AuditMessage(){
private final String message;
private final Throwable throwable;

private AuditMessage(String message, Throwable throwable) {
this.message = message;
this.throwable = throwable;
}

@Override
Expand All @@ -52,26 +53,6 @@ public Throwable getThrowable() {
return throwable;
}

/**
* Use when there are custom string to be added to default msg.
* @param customMessage custom string
*/
private void appendMessage(String customMessage) {
this.message += customMessage;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public void setThrowable(Throwable throwable) {
this.throwable = throwable;
}

/**
* Builder class for AuditMessage.
*/
Expand All @@ -83,10 +64,6 @@ public static class Builder {
private Map<String, String> params;
private String ret;

public Builder(){

}

public Builder setUser(String usr){
this.user = usr;
return this;
Expand All @@ -97,8 +74,8 @@ public Builder atIp(String ipAddr){
return this;
}

public Builder forOperation(String operation){
this.op = operation;
public Builder forOperation(AuditAction action) {
this.op = action.getAction();
return this;
}

Expand All @@ -107,8 +84,8 @@ public Builder withParams(Map<String, String> args){
return this;
}

public Builder withResult(String result){
this.ret = result;
public Builder withResult(AuditEventStatus result) {
this.ret = result.getStatus();
return this;
}

Expand All @@ -118,11 +95,9 @@ public Builder withException(Throwable ex){
}

public AuditMessage build(){
AuditMessage auditMessage = new AuditMessage();
auditMessage.message = "user=" + this.user + " | ip=" + this.ip + " | " +
String message = "user=" + this.user + " | ip=" + this.ip + " | " +
"op=" + this.op + " " + this.params + " | " + "ret=" + this.ret;
auditMessage.throwable = this.throwable;
return auditMessage;
return new AuditMessage(message, throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* 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
Expand Down Expand Up @@ -29,6 +29,9 @@
import java.util.List;
import java.util.Map;

import static org.apache.hadoop.ozone.audit.AuditEventStatus.FAILURE;
import static org.apache.hadoop.ozone.audit.AuditEventStatus.SUCCESS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
Expand All @@ -42,9 +45,6 @@ public class TestOzoneAuditLogger {
private static final AuditLogger AUDIT =
new AuditLogger(AuditLoggerType.OMLOGGER);

private static final String SUCCESS = AuditEventStatus.SUCCESS.name();
private static final String FAILURE = AuditEventStatus.FAILURE.name();

private static final Map<String, String> PARAMS =
new DummyEntity().toAuditMap();

Expand All @@ -55,7 +55,7 @@ public class TestOzoneAuditLogger {
new AuditMessage.Builder()
.setUser(USER)
.atIp(IP_ADDRESS)
.forOperation(DummyAction.CREATE_VOLUME.name())
.forOperation(DummyAction.CREATE_VOLUME)
.withParams(PARAMS)
.withResult(FAILURE)
.withException(null).build();
Expand All @@ -64,7 +64,7 @@ public class TestOzoneAuditLogger {
new AuditMessage.Builder()
.setUser(USER)
.atIp(IP_ADDRESS)
.forOperation(DummyAction.CREATE_VOLUME.name())
.forOperation(DummyAction.CREATE_VOLUME)
.withParams(PARAMS)
.withResult(SUCCESS)
.withException(null).build();
Expand All @@ -73,7 +73,7 @@ public class TestOzoneAuditLogger {
new AuditMessage.Builder()
.setUser(USER)
.atIp(IP_ADDRESS)
.forOperation(DummyAction.READ_VOLUME.name())
.forOperation(DummyAction.READ_VOLUME)
.withParams(PARAMS)
.withResult(FAILURE)
.withException(null).build();
Expand All @@ -82,7 +82,7 @@ public class TestOzoneAuditLogger {
new AuditMessage.Builder()
.setUser(USER)
.atIp(IP_ADDRESS)
.forOperation(DummyAction.READ_VOLUME.name())
.forOperation(DummyAction.READ_VOLUME)
.withParams(PARAMS)
.withResult(SUCCESS)
.withException(null).build();
Expand Down Expand Up @@ -127,12 +127,12 @@ public void verifyDefaultLogLevelForFailure() throws IOException {

@Test
public void messageIncludesAllParts() {
String message = WRITE_FAIL_MSG.getMessage();
String message = WRITE_FAIL_MSG.getFormattedMessage();
assertTrue(message, message.contains(USER));
assertTrue(message, message.contains(IP_ADDRESS));
assertTrue(message, message.contains(DummyAction.CREATE_VOLUME.name()));
assertTrue(message, message.contains(PARAMS.toString()));
assertTrue(message, message.contains(FAILURE));
assertTrue(message, message.contains(FAILURE.getStatus()));
}

/**
Expand Down Expand Up @@ -174,6 +174,6 @@ private void verifyNoLog() throws IOException {
File file = new File("audit.log");
List<String> lines = FileUtils.readLines(file, (String)null);
// When no log entry is expected, the log file must be empty
assertTrue(lines.size() == 0);
assertEquals(0, lines.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -608,26 +608,27 @@ private void audit(AuditAction action, EventType eventType,
@Override
public AuditMessage buildAuditMessageForSuccess(AuditAction op,
Map<String, String> auditMap) {

return new AuditMessage.Builder()
.setUser(null)
.atIp(null)
.forOperation(op.getAction())
.forOperation(op)
.withParams(auditMap)
.withResult(AuditEventStatus.SUCCESS.toString())
.withException(null)
.withResult(AuditEventStatus.SUCCESS)
.build();
}

//TODO: use GRPC to fetch user and ip details
@Override
public AuditMessage buildAuditMessageForFailure(AuditAction op,
Map<String, String> auditMap, Throwable throwable) {

return new AuditMessage.Builder()
.setUser(null)
.atIp(null)
.forOperation(op.getAction())
.forOperation(op)
.withParams(auditMap)
.withResult(AuditEventStatus.FAILURE.toString())
.withResult(AuditEventStatus.FAILURE)
.withException(throwable)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.scm.ScmConfigKeys;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.Server;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.http.client.methods.HttpRequestBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -224,4 +226,9 @@ public static File getDBPath(Configuration conf, String key) {
HddsConfigKeys.OZONE_METADATA_DIRS);
return ServerUtils.getOzoneMetaDirPath(conf);
}

public static String getRemoteUserName() {
UserGroupInformation remoteUser = Server.getRemoteUser();
return remoteUser != null ? remoteUser.getUserName() : null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license
* agreements. See the NOTICE file distributed with this work for additional
Expand Down Expand Up @@ -65,6 +65,7 @@
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.OZONE_SCM_HANDLER_COUNT_DEFAULT;
import static org.apache.hadoop.hdds.scm.ScmConfigKeys.OZONE_SCM_HANDLER_COUNT_KEY;
import static org.apache.hadoop.hdds.scm.server.StorageContainerManager.startRpcServer;
import static org.apache.hadoop.hdds.server.ServerUtils.getRemoteUserName;
import static org.apache.hadoop.hdds.server.ServerUtils.updateRPCListenAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -336,29 +337,26 @@ public List<DatanodeDetails> sortDatanodes(List<String> nodes,
@Override
public AuditMessage buildAuditMessageForSuccess(
AuditAction op, Map<String, String> auditMap) {

return new AuditMessage.Builder()
.setUser((Server.getRemoteUser() == null) ? null :
Server.getRemoteUser().getUserName())
.atIp((Server.getRemoteIp() == null) ? null :
Server.getRemoteIp().getHostAddress())
.forOperation(op.getAction())
.setUser(getRemoteUserName())
.atIp(Server.getRemoteAddress())
.forOperation(op)
.withParams(auditMap)
.withResult(AuditEventStatus.SUCCESS.toString())
.withException(null)
.withResult(AuditEventStatus.SUCCESS)
.build();
}

@Override
public AuditMessage buildAuditMessageForFailure(AuditAction op, Map<String,
String> auditMap, Throwable throwable) {

return new AuditMessage.Builder()
.setUser((Server.getRemoteUser() == null) ? null :
Server.getRemoteUser().getUserName())
.atIp((Server.getRemoteIp() == null) ? null :
Server.getRemoteIp().getHostAddress())
.forOperation(op.getAction())
.setUser(getRemoteUserName())
.atIp(Server.getRemoteAddress())
.forOperation(op)
.withParams(auditMap)
.withResult(AuditEventStatus.FAILURE.toString())
.withResult(AuditEventStatus.FAILURE)
.withException(throwable)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license
* agreements. See the NOTICE file distributed with this work for additional
Expand Down Expand Up @@ -63,7 +63,6 @@
import org.apache.hadoop.ozone.audit.SCMAction;
import org.apache.hadoop.hdds.scm.protocol.StorageContainerLocationProtocolServerSideTranslatorPB;
import org.apache.hadoop.ozone.protocolPB.ProtocolMessageMetrics;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -87,6 +86,7 @@
.OZONE_SCM_HANDLER_COUNT_DEFAULT;
import static org.apache.hadoop.hdds.scm.ScmConfigKeys
.OZONE_SCM_HANDLER_COUNT_KEY;
import static org.apache.hadoop.hdds.server.ServerUtils.getRemoteUserName;
import static org.apache.hadoop.hdds.server.ServerUtils.updateRPCListenAddress;
import static org.apache.hadoop.hdds.scm.server.StorageContainerManager
.startRpcServer;
Expand Down Expand Up @@ -181,8 +181,7 @@ public void join() throws InterruptedException {

@VisibleForTesting
public String getRpcRemoteUsername() {
UserGroupInformation user = ProtobufRpcEngine.Server.getRemoteUser();
return user == null ? null : user.getUserName();
return getRemoteUserName();
}

@Override
Expand Down Expand Up @@ -564,29 +563,26 @@ private Set<DatanodeDetails> queryNodeState(HddsProtos.NodeState nodeState) {
@Override
public AuditMessage buildAuditMessageForSuccess(
AuditAction op, Map<String, String> auditMap) {

return new AuditMessage.Builder()
.setUser((Server.getRemoteUser() == null) ? null :
Server.getRemoteUser().getUserName())
.atIp((Server.getRemoteIp() == null) ? null :
Server.getRemoteIp().getHostAddress())
.forOperation(op.getAction())
.setUser(getRemoteUserName())
.atIp(Server.getRemoteAddress())
.forOperation(op)
.withParams(auditMap)
.withResult(AuditEventStatus.SUCCESS.toString())
.withException(null)
.withResult(AuditEventStatus.SUCCESS)
.build();
}

@Override
public AuditMessage buildAuditMessageForFailure(AuditAction op, Map<String,
String> auditMap, Throwable throwable) {

return new AuditMessage.Builder()
.setUser((Server.getRemoteUser() == null) ? null :
Server.getRemoteUser().getUserName())
.atIp((Server.getRemoteIp() == null) ? null :
Server.getRemoteIp().getHostAddress())
.forOperation(op.getAction())
.setUser(getRemoteUserName())
.atIp(Server.getRemoteAddress())
.forOperation(op)
.withParams(auditMap)
.withResult(AuditEventStatus.FAILURE.toString())
.withResult(AuditEventStatus.FAILURE)
.withException(throwable)
.build();
}
Expand Down
Loading