Skip to content

Commit

Permalink
[NR-283915] Route detection support for mule server (#293)
Browse files Browse the repository at this point in the history
* Route detection Support for mule server
  • Loading branch information
IshikaDawda authored Aug 9, 2024
1 parent 68ecd56 commit b36154c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.newrelic.api.agent.security.instrumentation.helpers.URLMappingsHelper;
import com.newrelic.api.agent.security.schema.AgentMetaData;
import com.newrelic.api.agent.security.schema.ApplicationURLMapping;
import com.newrelic.api.agent.security.schema.Framework;
import com.newrelic.api.agent.security.schema.policy.AgentPolicy;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import org.mule.api.processor.MessageProcessor;
import org.mule.module.http.api.HttpHeaders;
import org.mule.module.http.api.listener.HttpListener;
Expand All @@ -29,6 +31,7 @@ public class MuleHelper {
private static final String EMPTY = "";
public static final String LIBRARY_NAME = "MULE-SERVER";
private static final Map<Integer, String> handlerMap = new HashMap<>();
public static final String MULE_3_6 = "MULE-3.6";

public static void processHttpRequestHeader(HttpRequest httpRequest,
com.newrelic.api.agent.security.schema.HttpRequest securityRequest
Expand Down Expand Up @@ -114,10 +117,24 @@ public static void gatherURLMappings(HttpListener messageSource, List<MessagePro
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(method, path, handlerClass));
}
}
}catch (Exception ignored){}
} catch (Exception ignored){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, MULE_3_6, ignored.getMessage()), ignored, MuleHelper.class.getName());
}
}

public static Map<Integer, String> getHandlerMap() {
return handlerMap;
}

// route detection
public static void setRequestRoute(String listenerPath) {
if (NewRelicSecurity.isHookProcessingActive()) {
try {
NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().setRoute(listenerPath);
NewRelicSecurity.getAgent().getSecurityMetaData().getMetaData().setFramework(Framework.MULE);
} catch (Exception e) {
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_ROUTE_FOR_INCOMING_REQUEST, MULE_3_6, e.getMessage()), e, MuleHelper.class.getName());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.newrelic.api.agent.security.schema.SecurityMetaData;
import com.newrelic.api.agent.security.schema.exceptions.NewRelicSecurityException;
import com.newrelic.api.agent.security.schema.operation.RXSSOperation;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
Expand All @@ -24,6 +25,7 @@ public static MuleEvent transform(final HttpRequestContext requestContext, final
{
boolean isLockAcquired = acquireLockIfPossible(requestContext.hashCode());
MuleEvent event;
MuleHelper.setRequestRoute(listenerPath);
if (isLockAcquired) {
preprocessSecurityHook(requestContext);
}
Expand Down Expand Up @@ -82,7 +84,9 @@ private static void preprocessSecurityHook(HttpRequestContext requestContext) {
// TODO: need to update UserClassEntity
ServletHelper.registerUserLevelCode(MuleHelper.LIBRARY_NAME);
securityRequest.setRequestParsed(true);
} catch (Throwable ignored){}
} catch (Throwable ignored){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_GENERATING_HTTP_REQUEST, MuleHelper.MULE_3_6, ignored.getMessage()), ignored, HttpRequestToMuleEvent_Instrumentation.class.getName());
}
}

private static void postProcessSecurityHook() {
Expand All @@ -107,9 +111,11 @@ private static void postProcessSecurityHook() {
ServletHelper.tmpFileCleanUp(NewRelicSecurity.getAgent().getSecurityMetaData().getFuzzRequestIdentifier().getTempFiles());
} catch (Throwable e) {
if(e instanceof NewRelicSecurityException){
e.printStackTrace();
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.SECURITY_EXCEPTION_MESSAGE, MuleHelper.MULE_3_6, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
throw e;
}
NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, MuleHelper.MULE_3_6, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, MuleHelper.MULE_3_6, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import com.newrelic.api.agent.security.schema.SecurityMetaData;
import com.newrelic.api.agent.security.schema.exceptions.NewRelicSecurityException;
import com.newrelic.api.agent.security.schema.operation.RXSSOperation;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import org.mule.module.http.internal.domain.request.HttpRequest;
import org.mule.module.http.internal.domain.request.HttpRequestContext;
import org.mule.module.http.internal.listener.HttpRequestToMuleEvent_Instrumentation;

@Weave(type = MatchType.Interface, originalName = "org.mule.module.http.internal.listener.async.RequestHandler")
public class RequestHandler_Instrumentation {
Expand Down Expand Up @@ -76,7 +78,9 @@ private void preprocessSecurityHook(HttpRequestContext requestContext) {
// TODO: need to update UserClassEntity
ServletHelper.registerUserLevelCode(MuleHelper.LIBRARY_NAME);
securityRequest.setRequestParsed(true);
} catch (Throwable ignored){}
} catch (Throwable ignored){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_GENERATING_HTTP_REQUEST, MuleHelper.MULE_3_6, ignored.getMessage()), ignored, HttpRequestToMuleEvent_Instrumentation.class.getName());
}
}

private void postProcessSecurityHook() {
Expand All @@ -101,9 +105,11 @@ private void postProcessSecurityHook() {
ServletHelper.tmpFileCleanUp(NewRelicSecurity.getAgent().getSecurityMetaData().getFuzzRequestIdentifier().getTempFiles());
} catch (Throwable e) {
if(e instanceof NewRelicSecurityException){
e.printStackTrace();
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.SECURITY_EXCEPTION_MESSAGE, MuleHelper.MULE_3_6, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
throw e;
}
NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, MuleHelper.MULE_3_6, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, MuleHelper.MULE_3_6, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import com.newrelic.api.agent.security.instrumentation.helpers.URLMappingsHelper;
import com.newrelic.api.agent.security.schema.AgentMetaData;
import com.newrelic.api.agent.security.schema.ApplicationURLMapping;
import com.newrelic.api.agent.security.schema.Framework;
import com.newrelic.api.agent.security.schema.policy.AgentPolicy;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import org.mule.api.processor.MessageProcessor;
import org.mule.module.http.api.HttpHeaders;
import org.mule.module.http.api.listener.HttpListener;
import org.mule.module.http.internal.domain.request.HttpRequest;
import org.mule.module.http.internal.listener.ListenerPath;
import org.mule.processor.InvokerMessageProcessor;

import java.util.HashMap;
Expand All @@ -28,6 +31,7 @@ public class MuleHelper {
private static final String EMPTY = "";
public static final String LIBRARY_NAME = "MULE-SERVER";
private static final Map<Integer, String> handlerMap = new HashMap<>();
public static final String MULE_3_7 = "MULE-3.7";

public static void processHttpRequestHeader(HttpRequest httpRequest, com.newrelic.api.agent.security.schema.HttpRequest securityRequest) {
for (String headerName : httpRequest.getHeaderNames()) {
Expand Down Expand Up @@ -108,10 +112,23 @@ public static void gatherURLMappings(HttpListener messageSource, List<MessagePro
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(method, path, handlerClass));
}
}
} catch (Exception ignored){}
} catch (Exception ignored){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, MULE_3_7, ignored.getMessage()), ignored, MuleHelper.class.getName());
}
}

public static Map<Integer, String> getHandlerMap() {
return handlerMap;
}

public static void setRequestRoute(ListenerPath listenerPath) {
if (NewRelicSecurity.isHookProcessingActive()) {
try {
NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().setRoute(listenerPath.getResolvedPath());
NewRelicSecurity.getAgent().getSecurityMetaData().getMetaData().setFramework(Framework.MULE);
} catch (Exception e) {
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_ROUTE_FOR_INCOMING_REQUEST, MULE_3_7, e.getMessage()), e, MuleHelper.class.getName());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.newrelic.api.agent.security.schema.SecurityMetaData;
import com.newrelic.api.agent.security.schema.exceptions.NewRelicSecurityException;
import com.newrelic.api.agent.security.schema.operation.RXSSOperation;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
Expand All @@ -24,6 +25,7 @@ public static MuleEvent transform(final HttpRequestContext requestContext, final
{
boolean isLockAcquired = acquireLockIfPossible(requestContext.hashCode());
MuleEvent event;
MuleHelper.setRequestRoute(listenerPath);
if (isLockAcquired) {
preprocessSecurityHook(requestContext);
}
Expand Down Expand Up @@ -82,7 +84,9 @@ private static void preprocessSecurityHook(HttpRequestContext requestContext) {
// TODO: need to update UserClassEntity
ServletHelper.registerUserLevelCode(MuleHelper.LIBRARY_NAME);
securityRequest.setRequestParsed(true);
} catch (Throwable ignored){}
} catch (Throwable ignored){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_GENERATING_HTTP_REQUEST, MuleHelper.MULE_3_7, ignored.getMessage()), ignored, HttpRequestToMuleEvent_Instrumentation.class.getName());
}
}

private static void postProcessSecurityHook() {
Expand All @@ -107,9 +111,11 @@ private static void postProcessSecurityHook() {
ServletHelper.tmpFileCleanUp(NewRelicSecurity.getAgent().getSecurityMetaData().getFuzzRequestIdentifier().getTempFiles());
} catch (Throwable e) {
if(e instanceof NewRelicSecurityException){
e.printStackTrace();
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.SECURITY_EXCEPTION_MESSAGE, MuleHelper.MULE_3_7, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
throw e;
}
NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, MuleHelper.MULE_3_7, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, MuleHelper.MULE_3_7, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import com.newrelic.api.agent.security.schema.SecurityMetaData;
import com.newrelic.api.agent.security.schema.exceptions.NewRelicSecurityException;
import com.newrelic.api.agent.security.schema.operation.RXSSOperation;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import org.mule.module.http.internal.domain.request.HttpRequest;
import org.mule.module.http.internal.domain.request.HttpRequestContext;
import org.mule.module.http.internal.listener.HttpRequestToMuleEvent_Instrumentation;

@Weave(type = MatchType.Interface, originalName = "org.mule.module.http.internal.listener.async.RequestHandler")
public class RequestHandler_Instrumentation {

public void handleRequest(HttpRequestContext requestContext, HttpResponseReadyCallback responseCallback) {
boolean isLockAcquired = acquireLockIfPossible(requestContext.hashCode());
if (isLockAcquired) {
Expand Down Expand Up @@ -76,7 +79,9 @@ private void preprocessSecurityHook(HttpRequestContext requestContext) {
// TODO: need to update UserClassEntity
ServletHelper.registerUserLevelCode(MuleHelper.LIBRARY_NAME);
securityRequest.setRequestParsed(true);
} catch (Throwable ignored){}
} catch (Throwable ignored){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_GENERATING_HTTP_REQUEST, MuleHelper.MULE_3_7, ignored.getMessage()), ignored, HttpRequestToMuleEvent_Instrumentation.class.getName());
}
}

private void postProcessSecurityHook() {
Expand All @@ -101,9 +106,11 @@ private void postProcessSecurityHook() {
ServletHelper.tmpFileCleanUp(NewRelicSecurity.getAgent().getSecurityMetaData().getFuzzRequestIdentifier().getTempFiles());
} catch (Throwable e) {
if(e instanceof NewRelicSecurityException){
e.printStackTrace();
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.SECURITY_EXCEPTION_MESSAGE, MuleHelper.MULE_3_7, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
throw e;
}
NewRelicSecurity.getAgent().log(LogLevel.SEVERE, String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, MuleHelper.MULE_3_7, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
NewRelicSecurity.getAgent().reportIncident(LogLevel.SEVERE , String.format(GenericHelper.REGISTER_OPERATION_EXCEPTION_MESSAGE, MuleHelper.MULE_3_7, e.getMessage()), e, HttpRequestToMuleEvent_Instrumentation.class.getName());
}
}

Expand Down

0 comments on commit b36154c

Please sign in to comment.