Skip to content

Commit

Permalink
Merge pull request #311 from newrelic/snapshot-remove-NR-297214
Browse files Browse the repository at this point in the history
[NR-297214]: Removal of status log files
  • Loading branch information
IshikaDawda authored Dec 12, 2024
2 parents 18ee1c5 + 7090225 commit 917dec8
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.newrelic.agent.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.newrelic.agent.security.instrumentator.os.OSVariables;
import com.newrelic.agent.security.instrumentator.os.OsVariablesInstance;
import com.newrelic.agent.security.instrumentator.utils.AgentUtils;
import com.newrelic.agent.security.intcodeagent.exceptions.RestrictionModeException;
import com.newrelic.agent.security.intcodeagent.exceptions.SecurityNoticeError;
import com.newrelic.agent.security.intcodeagent.exceptions.RestrictionModeException;
import com.newrelic.agent.security.intcodeagent.filelogging.FileLoggerThreadPool;
import com.newrelic.agent.security.intcodeagent.models.collectorconfig.AgentMode;
import com.newrelic.agent.security.intcodeagent.models.collectorconfig.ScanControllers;
Expand All @@ -21,13 +18,10 @@
import com.newrelic.agent.security.util.IUtilConstants;
import com.newrelic.api.agent.NewRelic;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -43,8 +37,6 @@

public class AgentConfig {

public static final String CLEANING_STATUS_SNAPSHOTS_FROM_LOG_DIRECTORY_MAX_S_FILE_COUNT_REACHED_REMOVED_S = "Cleaning status-snapshots from snapshots directory, max %s file count reached removed : %s";

public static final String AGENT_JAR_LOCATION = "agent_jar_location";
public static final String AGENT_HOME = "agent_home";
public static final String INVALID_CRON_EXPRESSION_PROVIDED_FOR_IAST_RESTRICTED_MODE = "Invalid cron expression provided for IAST Mode";
Expand Down Expand Up @@ -410,37 +402,6 @@ public String getLogLevel() {
return logLevel;
}

public void createSnapshotDirectory() throws IOException {
if (osVariables.getSnapshotDir() == null){
return;
}
Path snapshotDir = Paths.get(osVariables.getSnapshotDir());
// Remove any file with this name from target.
if (!snapshotDir.toFile().isDirectory()) {
FileUtils.deleteQuietly(snapshotDir.toFile());
}
CommonUtils.forceMkdirs(snapshotDir, DIRECTORY_PERMISSION);
}

private void keepMaxStatusLogFiles(int max) {
Collection<File> statusFiles = FileUtils.listFiles(new File(osVariables.getSnapshotDir()), FileFilterUtils.trueFileFilter(), null);
if (statusFiles.size() >= max) {
File[] sortedStatusFiles = statusFiles.toArray(new File[0]);
Arrays.sort(sortedStatusFiles, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
FileUtils.deleteQuietly(sortedStatusFiles[0]);
logger.log(LogLevel.INFO, String.format(CLEANING_STATUS_SNAPSHOTS_FROM_LOG_DIRECTORY_MAX_S_FILE_COUNT_REACHED_REMOVED_S, max, sortedStatusFiles[0].getAbsolutePath()), AgentConfig.class.getName());
}
}

public void setupSnapshotDir() {
try {
createSnapshotDirectory();
keepMaxStatusLogFiles(100);
} catch (Exception e) {
logger.log(LogLevel.WARNING, String.format("Snapshot directory creation failed !!! Please check file permissions. error:%s ", e.getMessage()), e, AgentConfig.class.getName());
}
}

public String getGroupName() {
return groupName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public void initStatusLogValues() {
AgentUtils.getInstance().getStatusLogValues().put("server-name", NOT_AVAILABLE);
AgentUtils.getInstance().getStatusLogValues().put("app-location", NOT_AVAILABLE);
AgentUtils.getInstance().getStatusLogValues().put("framework", NOT_AVAILABLE);

Map<String, String> statusLogValues = AgentUtils.getInstance().getStatusLogValues();
logger.logInit(LogLevel.INFO, String.format("CSEC HOME: %s, permissions read & write: %s", statusLogValues.get("csec-home"), statusLogValues.get("csec-home-permissions")), AgentInfo.class.getName());
logger.logInit(LogLevel.INFO, String.format("Agent location: %s", statusLogValues.get("agent-location")), AgentInfo.class.getName());
logger.logInit(LogLevel.INFO, String.format("Current working directory: %s, permissions read & write: %s", statusLogValues.get("cwd"), statusLogValues.get("cwd-permissions")), AgentInfo.class.getName());
}

public boolean agentStatTrigger(boolean clean){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class OSVariables {
private String logDirectory;
private String tmpDirectory;

private String snapshotDir;

private String osArch;

private File rootDir;
Expand Down Expand Up @@ -81,14 +79,6 @@ public void setRootDir(File rootDir) {
this.rootDir = rootDir;
}

public String getSnapshotDir() {
return snapshotDir;
}

public void setSnapshotDir(String snapshotDir) {
this.snapshotDir = snapshotDir;
}

/*public String getPolicyConfigPath() {
return policyConfigPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class OsVariablesInstance {
public static final String LANGUAGE_AGENT = "language-agent";

public static final String TMP = "tmp";
public static final String SNAPSHOTS = "snapshots";

private static OsVariablesInstance instance;

Expand All @@ -29,7 +28,6 @@ private OsVariablesInstance() {
if(StringUtils.isNotBlank(AgentConfig.getInstance().getSecurityHome())) {
osVariables.setLogDirectory(Paths.get(AgentConfig.getInstance().getSecurityHome(), LOGS).toString());
osVariables.setTmpDirectory(Paths.get(AgentConfig.getInstance().getSecurityHome(), TMP, LANGUAGE_AGENT, AgentInfo.getInstance().getApplicationUUID()).toString());
osVariables.setSnapshotDir(Paths.get(osVariables.getLogDirectory(), SNAPSHOTS).toString());
}
// osVariables.setPolicyConfigPath(Paths.get(k2root.toString(), CONFIG, LANGUAGE_AGENT).toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void log(LogLevel logLevel, String event, Throwable throwableEvent, Strin
}

public void logInit(LogLevel logLevel, String event, String logSourceClassName) {
postLogMessage(logLevel, event, null, logSourceClassName);
// postLogMessage(logLevel, event, null, logSourceClassName);
if (!isInitLoggingActive || logLevel.getLevel() == 1 || logLevel.getLevel() > InitLogWriter.defaultLogLevel) {
return;
}
Expand All @@ -166,7 +166,7 @@ public void logInit(LogLevel logLevel, String event, String logSourceClassName)
}

public void logInit(LogLevel logLevel, String event, Throwable throwableEvent, String logSourceClassName) {
postLogMessage(logLevel, event, throwableEvent, logSourceClassName);
// postLogMessage(logLevel, event, throwableEvent, logSourceClassName);
if (!isInitLoggingActive || logLevel.getLevel() == 1 || logLevel.getLevel() > InitLogWriter.defaultLogLevel) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,7 @@

public class HealthCheckScheduleThread {

public static final String STATUS_TIMESTAMP = "timestamp";
public static final String CAN_T_WRITE_STATUS_LOG_FILE_S_REASON_S = "Can't write status log file : %s , reason : %s ";
public static final String LAST_5_ERRORS = "last-5-errors";
public static final String LAST_5_HC = "last-5-hc";
public static final String K_2_AGENT_STATUS_LOG = "java-security-collector-status-%s.log";
public static final String LATEST_PROCESS_STATS = "latest-process-stats";
public static final String LATEST_SERVICE_STATS = "latest-service-stats";
public static final String VALIDATOR_SERVER_STATUS = "validator-server-status";
public static final String ENFORCED_POLICY = "enforced-policy";

public static final String WEBSOCKET = "websocket";
public static final String SEPARATOR = ": ";
public static final String CAN_T_CREATE_STATUS_LOG_FILE = "Can't create status log file!!!";
private static HealthCheckScheduleThread instance;

private static final FileLoggerThreadPool logger = FileLoggerThreadPool.getInstance();
Expand Down Expand Up @@ -97,8 +85,6 @@ public void run() {
} catch (Throwable e) {
logger.log(LogLevel.WARNING, "Error while trying to verify connection: ", e,
HealthCheckScheduleThread.class.getName());
} finally {
writeStatusLogFile(sendJaHealthCheck);
}
}
};
Expand Down Expand Up @@ -137,44 +123,6 @@ public boolean cancelTask(boolean forceCancel) {
return false;
}

private void writeStatusLogFile(JAHealthCheck sendJaHealthCheck) {
JAHealthCheck writerHealthCheck = sendJaHealthCheck;
if(writerHealthCheck == null){
writerHealthCheck = AgentInfo.getInstance().getJaHealthCheck();
}
if (osVariables.getSnapshotDir() == null){
return;
}
File statusLog = new File(osVariables.getSnapshotDir(), String.format(K_2_AGENT_STATUS_LOG, AgentInfo.getInstance().getApplicationUUID()));
try {
FileUtils.deleteQuietly(statusLog);
if (statusLog.createNewFile()) {
Map<String, String> substitutes = AgentUtils.getInstance().getStatusLogValues();
substitutes.put(STATUS_TIMESTAMP, Instant.now().toString());
JAHealthCheck finalWriterHealthCheck = writerHealthCheck;
substitutes.put(LATEST_PROCESS_STATS, finalWriterHealthCheck.getStats().keySet().stream()
.map(key -> key + SEPARATOR + finalWriterHealthCheck.getStats().get(key))
.collect(Collectors.joining(StringUtils.LF, StringUtils.EMPTY, StringUtils.EMPTY)));
substitutes.put(LATEST_SERVICE_STATS, finalWriterHealthCheck.getServiceStatus().keySet().stream()
.map(key -> key + SEPARATOR + finalWriterHealthCheck.getServiceStatus().get(key))
.collect(Collectors.joining(StringUtils.LF, StringUtils.EMPTY, StringUtils.EMPTY)));
substitutes.put(LAST_5_ERRORS, StringUtils.joinWith(StringUtils.LF, AgentUtils.getInstance().getStatusLogMostRecentErrors().toArray()));
substitutes.put(LAST_5_HC, StringUtils.joinWith(StringUtils.LF, AgentUtils.getInstance().getStatusLogMostRecentHCs().toArray()));
substitutes.put(VALIDATOR_SERVER_STATUS, finalWriterHealthCheck.getServiceStatus().getOrDefault(WEBSOCKET, StringUtils.EMPTY).toString());
substitutes.put(ENFORCED_POLICY, JsonConverter.toJSON(AgentUtils.getInstance().getAgentPolicy()));
StringSubstitutor substitutor = new StringSubstitutor(substitutes);
FileUtils.writeStringToFile(statusLog, substitutor.replace(IAgentConstants.STATUS_FILE_TEMPLATE), StandardCharsets.UTF_8);
isStatusLoggingActive = true;
} else {
isStatusLoggingActive = false;
logger.log(LogLevel.SEVERE, CAN_T_CREATE_STATUS_LOG_FILE, HealthCheckScheduleThread.class.getName());
}
} catch (IOException e) {
String error = String.format(CAN_T_WRITE_STATUS_LOG_FILE_S_REASON_S, statusLog, e.getMessage());
isStatusLoggingActive = false;
logger.log(LogLevel.SEVERE, error, e, HealthCheckScheduleThread.class.getName());
}
}

private static Map<String, Object> getServiceStatus() {
Map<String, Object> serviceStatus = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void triggerNrSecurity() {
info.initialiseHC();
config.populateAgentPolicy();
config.populateAgentPolicyParameters();
config.setupSnapshotDir();
// config.setupSnapshotDir();
info.initStatusLogValues();
setInitialised(true);
populateLinkingMetadata();
Expand Down

0 comments on commit 917dec8

Please sign in to comment.