Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report Error via APM noticeError API on WS connection failure #314

Merged
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
Expand Up @@ -7,6 +7,7 @@
import com.newrelic.agent.security.instrumentator.utils.AgentUtils;
import com.newrelic.agent.security.instrumentator.utils.InstrumentationUtils;
import com.newrelic.agent.security.intcodeagent.filelogging.FileLoggerThreadPool;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import com.newrelic.agent.security.intcodeagent.logging.IAgentConstants;
import com.newrelic.agent.security.intcodeagent.models.config.AgentPolicyParameters;
Expand All @@ -29,6 +30,7 @@
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

public class ControlCommandProcessor implements Runnable {

Expand Down Expand Up @@ -113,6 +115,7 @@ public void run() {
logger.log(LogLevel.SEVERE, controlCommand.getArguments().get(0),
ControlCommandProcessor.class.getSimpleName());
System.err.println(controlCommand.getArguments().get(0));
NewRelic.getAgent().getLogger().log(Level.SEVERE, controlCommand.getArguments().get(0));
InstrumentationUtils.shutdownLogic(true);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.newrelic.agent.security.instrumentator.utils.INRSettingsKey;
import com.newrelic.agent.security.intcodeagent.controlcommand.ControlCommandProcessor;
import com.newrelic.agent.security.intcodeagent.controlcommand.ControlCommandProcessorThreadPool;
import com.newrelic.agent.security.intcodeagent.exceptions.SecurityNoticeError;
import com.newrelic.agent.security.intcodeagent.filelogging.FileLoggerThreadPool;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import com.newrelic.agent.security.intcodeagent.logging.IAgentConstants;
Expand Down Expand Up @@ -37,9 +38,7 @@
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Set;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class WSClient extends WebSocketClient {
Expand All @@ -48,7 +47,7 @@ public class WSClient extends WebSocketClient {
public static final String SENDING_EVENT = "sending event: ";
public static final String UNABLE_TO_SEND_EVENT = "Unable to send event : ";
public static final String ERROR_IN_WSOCK_CONNECTION = "Error in WSock connection : ";
public static final String CONNECTION_CLOSED_BY = "Connection closed by ";
public static final String CONNECTION_CLOSED_BY = "WS Connection closed by ";
public static final String REMOTE_PEER = "remote peer.";
public static final String LOCAL = "local.";
public static final String CODE = " Code: ";
Expand All @@ -70,6 +69,8 @@ public class WSClient extends WebSocketClient {

private WebSocketImpl connection = null;

private Map<String, String> noticeErrorCustomParameters = new HashMap<>();


private SSLContext createSSLContext() throws Exception {
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
Expand All @@ -95,6 +96,7 @@ private SSLContext createSSLContext() throws Exception {

logger.log(caCerts.size() > 0 ? LogLevel.INFO : LogLevel.SEVERE,
String.format("Found %s certificates.", caCerts.size()), WSClient.class.getName());
noticeErrorCustomParameters.put("ca_bundle_count", String.valueOf(caCerts.size()));
// Initialize the keystore
keystore.load(null, null);

Expand Down Expand Up @@ -122,8 +124,10 @@ private InputStream getCaBundleStream() throws IOException {
InputStream inputStream;
String caBundlePath = NewRelic.getAgent().getConfig().getValue(IUtilConstants.NR_SECURITY_CA_BUNDLE_PATH);
if (StringUtils.isNotBlank(caBundlePath)) {
noticeErrorCustomParameters.put("ca_bundle_path", caBundlePath);
inputStream = Files.newInputStream(Paths.get(caBundlePath));
} else {
noticeErrorCustomParameters.put("ca_bundle_path", "internal-pem");
inputStream = CommonUtils.getResourceStreamFromAgentJar("nr-custom-ca.pem");
}
return inputStream;
Expand All @@ -150,6 +154,7 @@ private WSClient() throws URISyntaxException {
Proxy proxy = proxyManager();
if(proxy != null) {
this.setProxy(proxy);
noticeErrorCustomParameters.put("proxy_host", proxy.address().toString());
}
if (StringUtils.startsWithIgnoreCase(AgentConfig.getInstance().getConfig().getK2ServiceInfo().getValidatorServiceEndpointURL(), "wss:")) {
try {
Expand All @@ -159,6 +164,7 @@ private WSClient() throws URISyntaxException {
logger.log(LogLevel.FINER, "Error creating socket factory", e, WSClient.class.getName());
}
}
noticeErrorCustomParameters.put("csec_ws_url", AgentConfig.getInstance().getConfig().getK2ServiceInfo().getValidatorServiceEndpointURL());
logger.log(LogLevel.INFO, String.format("Connecting to WS client %s", AgentConfig.getInstance().getConfig().getK2ServiceInfo().getValidatorServiceEndpointURL()), WSClient.class.getName());
}

Expand Down Expand Up @@ -274,8 +280,10 @@ public void onMessage(String message) {

@Override
public void onClose(int code, String reason, boolean remote) {
logger.log(LogLevel.WARNING, CONNECTION_CLOSED_BY + (remote ? REMOTE_PEER : LOCAL) + CODE + code
+ REASON + reason, WSClient.class.getName());
String message = CONNECTION_CLOSED_BY + (remote ? REMOTE_PEER : LOCAL) + CODE + code
+ REASON + reason;
logger.log(LogLevel.WARNING, message, WSClient.class.getName());
NewRelic.noticeError(new SecurityNoticeError(message), noticeErrorCustomParameters, true);
if (code == CloseFrame.NEVER_CONNECTED) {
return;
}
Expand All @@ -289,6 +297,7 @@ public void onClose(int code, String reason, boolean remote) {

@Override
public void onError(Exception ex) {
NewRelic.noticeError(new SecurityNoticeError(CONNECTION_CLOSED_BY + ex.getClass().getSimpleName(), ex), noticeErrorCustomParameters, true);
logger.logInit(LogLevel.SEVERE, String.format(IAgentConstants.WS_CONNECTION_UNSUCCESSFUL_INFO, AgentConfig
.getInstance().getConfig().getK2ServiceInfo().getValidatorServiceEndpointURL(),
ex.toString(), ex.getCause()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IUtilConstants {

String NR_SECURITY_HOME_APP = "security.is_home_app";

String NR_SECURITY_CA_BUNDLE_PATH = "security.ca_bundle_path";
String NR_SECURITY_CA_BUNDLE_PATH = "ca_bundle_path";
String NR_CSEC_DEBUG_LOGFILE_SIZE = "NR_CSEC_DEBUG_LOGFILE_SIZE";
String NR_CSEC_DEBUG_LOGFILE_MAX_COUNT = "NR_CSEC_DEBUG_LOGFILE_MAX_COUNT";
String LOG_FILE_PATH = "log_file_path";
Expand Down
Loading