org.bdware.doip.audit.writer.HashWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
The newest version!
package org.bdware.doip.audit.writer;
import com.google.gson.Gson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.auditrepo.AutoAuditDO;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.endpoint.client.ClientConfig;
import org.bdware.doip.endpoint.client.DoipClientImpl;
import org.bdware.irp.irplib.core.IrpMessage;
import org.bdware.irp.irplib.core.IrpMessageCode;
//only log hash to audit proxy;
public class HashWriter extends LogWriter {
private static final Logger LOGGER = LogManager.getLogger(HashWriter.class);
DoipClientImpl doipClient;
AuditConfig auditConfg;
public HashWriter(AuditConfig config) {
super();
doipClient = new DoipClientImpl();
auditConfg = config;
if (config.getAuditUrl() != null)
doipClient.connect(ClientConfig.fromUrl(config.getAuditUrl()));
}
@Override
public void write(AuditLog log) {
try {
LOGGER.info(new Gson().toJson(log));
if (!doipClient.isConnected()) {
doipClient.reconnect(auditConfg.getAuditUrl());
}
doipClient.sendRawMessage(AutoAuditDO.updateMsg(auditConfg.getAuditDoid(), log.asJson()), emptyCallback);
} catch (Throwable e) {
e.printStackTrace();
}
}
public AuditLog extract(EndpointContext ctx, final IrpMessage request, final IrpMessage response) {
try {
AuditProtocalLog log = new AuditProtocalLog();
log.time = System.currentTimeMillis();
log.doid = new AuditProtocalLog.DoidInfo();
extractDOID(log.doid, request.getDoid());
log.eventType = "irp";
log.op = IrpMessageCode.toOpStr(request.header.opCode);
log.status_code = IrpMessageCode.getResponseCodeInfo(response.header.responseCode);
log.request_hash = calculateHash(request);
log.response_hash = calculateHash(response);
log.setClientPubkey(getSigner(request));
log.setClientIP(ctx.getClientIP());
log.setClientDoId(ctx.getClientDoId());
log.setServerPubkey(getSigner(response));
log.setServerIP(ctx.getServerIP());
log.setServerDoId(ctx.getServerDoId());
return log;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public AuditLog extract(EndpointContext ctx, DoipMessage request, DoipMessage response) {
try {
AuditProtocalLog log = new AuditProtocalLog();
log.time = System.currentTimeMillis();
log.doid = new AuditProtocalLog.DoidInfo();
extractDOID(log.doid, request.header.parameters.id);
log.eventType = "doip";
log.op = request.header.parameters.operation;
log.status_code = response.header.parameters.response.getName();
log.request_hash = calculateHash(request);
log.response_hash = calculateHash(response);
log.setClientPubkey(getSigner(request));
log.setClientIP(ctx.getClientIP());
log.setClientDoId(ctx.getClientDoId());
log.setServerPubkey(getSigner(response));
log.setServerIP(ctx.getServerIP());
log.setServerDoId(ctx.getServerDoId());
return log;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}