Skip to content
Open
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
Expand Up @@ -17,6 +17,10 @@ public class OSVariables {

private File rootDir;

private File logFile;

private File initLogFile;

public String getOs() {
return os;
}
Expand Down Expand Up @@ -89,19 +93,20 @@ public void setSnapshotDir(String snapshotDir) {
this.snapshotDir = snapshotDir;
}

/*public String getPolicyConfigPath() {
return policyConfigPath;
public File getLogFile() {
return logFile;
}

public void setLogFile(File logFile) {
this.logFile = logFile;
}

public void setPolicyConfigPath(String policyConfigPath) {
this.policyConfigPath = policyConfigPath;
public File getInitLogFile() {
return initLogFile;
}

public String getK2RootDir() {
return k2RootDir;
public void setInitLogFile(File initLogFile) {
this.initLogFile = initLogFile;
}

public void setK2RootDir(String k2RootDir) {
this.k2RootDir = k2RootDir;
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.newrelic.agent.security.instrumentator.os.OsVariablesInstance;
import com.newrelic.agent.security.instrumentator.utils.AgentUtils;
import com.newrelic.agent.security.intcodeagent.models.javaagent.LogMessage;
import com.newrelic.agent.security.intcodeagent.properties.K2JALogProperties;
import com.newrelic.agent.security.intcodeagent.websocket.EventSendPool;
import com.newrelic.agent.security.intcodeagent.websocket.JsonConverter;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
Expand Down Expand Up @@ -42,28 +41,19 @@ private FileLoggerThreadPool() throws IOException {
public void initialiseLogger() {
// load the settings
osVariables = OsVariablesInstance.getInstance().getOsVariables();
int queueSize = 15000;
int maxPoolSize = 1;
int corePoolSize = 1;
long keepAliveTime = 600;

TimeUnit timeUnit = TimeUnit.SECONDS;
try {
if(LogFileHelper.isLoggingToStdOut()){
this.isLoggingToStdOut = true;
}
} catch (NumberFormatException e){}
LogAppender appender = selectAppender();

if(!isLoggingToStdOut && StringUtils.isBlank(osVariables.getLogDirectory())) {
isLoggingActive = false;
isInitLoggingActive = false;
return;
if(appender == LogAppender.CONSOLE) {
this.isLoggingToStdOut = true;
}



int queueSize = 15000;
int maxPoolSize = 1;
int corePoolSize = 1;
long keepAliveTime = 600;
boolean allowCoreThreadTimeOut = false;
executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit,
executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(queueSize), new EventAbortPolicy()) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
Expand All @@ -89,6 +79,23 @@ public Thread newThread(Runnable r) {
});
}

private LogAppender selectAppender() {
if(LogFileHelper.isLoggingToStdOut()){
return LogAppender.CONSOLE;
}

if (StringUtils.isBlank(osVariables.getLogDirectory())) {
return LogAppender.CONSOLE;
}

osVariables.setLogFile(LogFileHelper.createLogFile("java-security-collector.log"));
if(osVariables.getLogFile() == null) {
return LogAppender.CONSOLE;
}
osVariables.setInitLogFile(LogFileHelper.createLogFile("java-security-collector-init.log"));
return LogAppender.FILE;
}


public void shutDownThreadPoolExecutor() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class InitLogWriter implements Runnable {

private static final String STR_COLON = " : ";

public static final String LOGS = "logs";

public static final String THREAD_NAME_TEMPLATE = " [%s] [%s] ";

private static final String LOG_FILE_INITIATED_MSG = "Init Log File initiated.\n";
Expand Down Expand Up @@ -66,50 +64,30 @@ public class InitLogWriter implements Runnable {

private static BufferedWriter writer;

private static File currentLogFile;

private String threadName;

private static OSVariables osVariables = OsVariablesInstance.getInstance().getOsVariables();

static {
if(FileLoggerThreadPool.getInstance().isLoggingToStdOut){
writer = new BufferedWriter(new OutputStreamWriter(System.out));
} else {
fileName = new File(osVariables.getLogDirectory(), "java-security-collector-init.log").getAbsolutePath();
currentLogFile = new File(fileName);
File currentLogFile = osVariables.getInitLogFile();
if (currentLogFile != null) {
fileName = currentLogFile.getAbsolutePath();
currentLogFileName = fileName;
createLogFile();
}
}

private static Boolean createLogFile() {
try {
CommonUtils.forceMkdirs(currentLogFile.getParentFile().toPath(), DIRECTORY_PERMISSION);
System.out.println("New Relic Security Agent: Writing InitLogs to log file:"+currentLogFile);
currentLogFile.setReadable(true, false);
writer = new BufferedWriter(new FileWriter(currentLogFileName, true));
writer.write(LOG_FILE_INITIATED_MSG);
writer.flush();
maxFileSize = FileLoggerThreadPool.getInstance().maxfilesize;

// k2.log.handler.maxfilesize=10
// k2.log.handler.maxfilesize.unit=MB
if (!osVariables.getWindows()) {
Files.setPosixFilePermissions(currentLogFile.toPath(), PosixFilePermissions.fromString(IUtilConstants.FILE_PERMISSIONS));
}
writer.write(String.format(LOG_CONFIGURED_SUCCESSFULLY_MSG, LogLevel.getLevelName(defaultLogLevel), maxFileSize));
writer.flush();
return true;
} catch (Throwable e) {
FileLoggerThreadPool.getInstance().setInitLoggingActive(false);
String tmpDir = System.getProperty("java.io.tmpdir");
System.err.println("[NR-CSEC-JA] Init Log : "+e.getMessage()+" Please find the error in " + tmpDir + File.separator + "NR-CSEC-Logger.err");
try {
e.printStackTrace(new PrintStream(tmpDir + File.separator + "NR-CSEC-Logger.err"));
} catch (FileNotFoundException ex) {
writer = new BufferedWriter(new FileWriter(currentLogFileName, true));
writer.write(LOG_FILE_INITIATED_MSG);
writer.write(String.format(LOG_CONFIGURED_SUCCESSFULLY_MSG, LogLevel.getLevelName(defaultLogLevel), maxFileSize));
writer.flush();
} catch (IOException e) {
String tmpDir = System.getProperty("java.io.tmpdir");
System.err.println("[NR-CSEC-JA] CSEC Log : "+e.getMessage()+" Please find the error in " + tmpDir + File.separator + "NR-CSEC-Logger.err");
try {
e.printStackTrace(new PrintStream(tmpDir + File.separator + "NR-CSEC-Logger.err"));
} catch (FileNotFoundException ex) {
}
}
return false;
System.out.println("New Relic Security Agent: Writing to InitLogs file:"+currentLogFile);
maxFileSize = FileLoggerThreadPool.getInstance().maxfilesize;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.newrelic.agent.security.intcodeagent.filelogging;

public enum LogAppender {
FILE,
CONSOLE,
BOTH
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
import com.newrelic.agent.security.AgentInfo;
import com.newrelic.agent.security.instrumentator.os.OsVariablesInstance;
import com.newrelic.agent.security.intcodeagent.properties.K2JALogProperties;
import com.newrelic.agent.security.intcodeagent.utils.CommonUtils;
import com.newrelic.agent.security.util.IUtilConstants;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.schema.StringUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.FileFilterUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermissions;
import java.time.Instant;
Expand All @@ -47,8 +45,12 @@ public class LogFileHelper {
private static final Integer DEFAULT_LOG_FILE_LIMIT = 0;

public static boolean isLoggingToStdOut() {
String logFileName = NewRelic.getAgent().getConfig().getValue(LogFileHelper.LOG_FILE_NAME, LogFileHelper.DEFAULT_LOG_FILE_NAME);
return StringUtils.equalsIgnoreCase(logFileName, STDOUT);
try {
String logFileName = NewRelic.getAgent().getConfig().getValue(LogFileHelper.LOG_FILE_NAME, LogFileHelper.DEFAULT_LOG_FILE_NAME);
return StringUtils.equalsIgnoreCase(logFileName, STDOUT);
} catch (ClassCastException | NumberFormatException e){
return false;
}
}

public static int logFileCount() {
Expand Down Expand Up @@ -113,4 +115,26 @@ public static void performDailyRollover(){
}

}

public static File createLogFile(String logFileName) {
File logFile = new File(OsVariablesInstance.getInstance().getOsVariables().getLogDirectory(), logFileName);
try {
CommonUtils.forceMkdirs(logFile.getParentFile().toPath(), IUtilConstants.DIRECTORY_PERMISSION);
FileUtils.touch(logFile);
logFile.setReadable(true, false);
logFile.setWritable(true, false);
if (!OsVariablesInstance.getInstance().getOsVariables().getWindows()) {
Files.setPosixFilePermissions(logFile.toPath(), PosixFilePermissions.fromString(IUtilConstants.FILE_PERMISSIONS));
}
return logFile;
} catch (IOException e) {
String tmpDir = System.getProperty("java.io.tmpdir");
System.err.println("[NR-CSEC-JA] CSEC Log : "+e.getMessage()+" Please find the error in " + tmpDir + File.separator + "NR-CSEC-Logger.err");
try {
e.printStackTrace(new PrintStream(tmpDir + File.separator + "NR-CSEC-Logger.err"));
} catch (FileNotFoundException ex) {
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public class LogWriter implements Runnable {

private static final String STR_COLON = " : ";

public static final String LOGS = "logs";

public static final String THREAD_NAME_TEMPLATE = " [%s] [%s] ";
public static final String CAUSED_BY = "Caused by: ";

Expand Down Expand Up @@ -60,50 +58,33 @@ public class LogWriter implements Runnable {

private static BufferedWriter writer;

private static File currentLogFile;

private String threadName;

private static OSVariables osVariables = OsVariablesInstance.getInstance().getOsVariables();
private String logTime;

private static boolean createLogFile() {

try {
CommonUtils.forceMkdirs(currentLogFile.getParentFile().toPath(), IUtilConstants.DIRECTORY_PERMISSION);
System.out.println("New Relic Security Agent: Writing to log file:"+currentLogFile);
currentLogFile.setReadable(true, false);
writer = new BufferedWriter(new FileWriter(currentLogFileName, true));

maxFileSize = FileLoggerThreadPool.getInstance().maxfilesize;

if (!osVariables.getWindows()) {
Files.setPosixFilePermissions(currentLogFile.toPath(), PosixFilePermissions.fromString(IUtilConstants.FILE_PERMISSIONS));
}

} catch (Throwable e) {
if (FileLoggerThreadPool.getInstance().isLoggingActive()) {
FileLoggerThreadPool.getInstance().setLoggingActive(false);
}
String tmpDir = System.getProperty("java.io.tmpdir");
System.err.println("[NR-CSEC-JA] CSEC Log : "+e.getMessage()+" Please find the error in " + tmpDir + File.separator + "NR-CSEC-Logger.err");
static {
File currentLogFile = osVariables.getLogFile();
boolean logActive = true;
if (currentLogFile != null) {
fileName = currentLogFile.getAbsolutePath();
currentLogFileName = fileName;
try {
e.printStackTrace(new PrintStream(tmpDir + File.separator + "NR-CSEC-Logger.err"));
} catch (FileNotFoundException ex) {
writer = new BufferedWriter(new FileWriter(currentLogFileName, true));
} catch (IOException e) {
logActive = false;
String tmpDir = System.getProperty("java.io.tmpdir");
System.err.println("[NR-CSEC-JA] CSEC Log : "+e.getMessage()+" Please find the error in " + tmpDir + File.separator + "NR-CSEC-Logger.err");
try {
e.printStackTrace(new PrintStream(tmpDir + File.separator + "NR-CSEC-Logger.err"));
} catch (FileNotFoundException ex) {
}
}
return false;
System.out.println("New Relic Security Agent: Writing to log file:"+currentLogFile);
maxFileSize = FileLoggerThreadPool.getInstance().maxfilesize;
}
return true;
}

static {
if(FileLoggerThreadPool.getInstance().isLoggingToStdOut){
if(FileLoggerThreadPool.getInstance().isLoggingToStdOut || !logActive){
writer = new BufferedWriter(new OutputStreamWriter(System.out));
} else {
fileName = new File(osVariables.getLogDirectory(), "java-security-collector.log").getAbsolutePath();
currentLogFile = new File(fileName);
currentLogFileName = fileName;
createLogFile();
}
}

Expand Down
Loading