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

NR-293073: API Endpoint support for Weblogic Server #305

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
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
@@ -0,0 +1,50 @@
package com.newrelic.agent.security.instrumentation.weblogic;


import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.instrumentation.helpers.URLMappingsHelper;
import com.newrelic.api.agent.security.schema.ApplicationURLMapping;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import weblogic.servlet.internal.WebAppServletContext;

import javax.servlet.ServletRegistration;
import java.util.Collection;
import java.util.Map;

public class HttpServletHelper {

public static final String WEBLOGIC_12_2 = "WEBLOGIC-12.2";

public static void gatherURLMappings( WebAppServletContext servletContext) {
try {
Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
for (ServletRegistration servletRegistration : servletRegistrations.values()) {
for (String s : servletRegistration.getMappings()) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(URLMappingsHelper.WILDCARD, s, servletRegistration.getClassName()));
}
}
getJSPMappings(servletContext, URLMappingsHelper.SEPARATOR);
} catch (Exception e){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, WEBLOGIC_12_2, e.getMessage()), e, HttpServletHelper.class.getName());
}
}

private static void getJSPMappings(WebAppServletContext servletContext, String dir) {
try {
if(dir.endsWith(URLMappingsHelper.SEPARATOR)){
Collection<String> resourcePaths = servletContext.getResourcePaths(dir);
for (String path : resourcePaths) {
if(path.endsWith(URLMappingsHelper.SEPARATOR)) {
getJSPMappings(servletContext, path);
}
else if(path.endsWith(".jsp") || path.endsWith(".jspx") || path.endsWith(".JSP") || path.endsWith(".JSPX")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(URLMappingsHelper.WILDCARD, path));
}
}
}
} catch (Exception e){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, WEBLOGIC_12_2, e.getMessage()), e, HttpServletHelper.class.getName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package weblogic.servlet.internal;

import com.newrelic.agent.security.instrumentation.weblogic.HttpServletHelper;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

import javax.servlet.ServletRegistration;
import java.util.Map;
import java.util.Set;

@Weave
public final class WebAppServletContext {

void start() throws Exception {
Weaver.callOriginal();
HttpServletHelper.gatherURLMappings(this);
}

public Map<String, ? extends ServletRegistration> getServletRegistrations() {
return Weaver.callOriginal();
}

public Set<String> getResourcePaths(String path) {
return Weaver.callOriginal();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.newrelic.agent.security.instrumentation.weblogic12;


import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.instrumentation.helpers.URLMappingsHelper;
import com.newrelic.api.agent.security.schema.ApplicationURLMapping;
import com.newrelic.api.agent.security.utils.logging.LogLevel;
import weblogic.servlet.internal.WebAppServletContext;

import javax.servlet.ServletRegistration;
import java.util.Collection;
import java.util.Map;

public class HttpServletHelper {

public static final String WEBLOGIC_12 = "WEBLOGIC-12";

public static void gatherURLMappings( WebAppServletContext servletContext) {
try {
Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
getJSPMappings(servletContext, URLMappingsHelper.SEPARATOR);

for (ServletRegistration servletRegistration : servletRegistrations.values()) {
for (String s : servletRegistration.getMappings()) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(URLMappingsHelper.WILDCARD, s, servletRegistration.getClassName()));
}
}
} catch (Exception e){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, WEBLOGIC_12, e.getMessage()), e, HttpServletHelper.class.getName());
}
}

private static void getJSPMappings(WebAppServletContext servletContext, String dir) {
try {
if(dir.endsWith(URLMappingsHelper.SEPARATOR)){
Collection<String> resourcePaths = servletContext.getResourcePaths(dir);
for (String path : resourcePaths) {
if(path.endsWith(URLMappingsHelper.SEPARATOR)) {
getJSPMappings(servletContext, path);
}
else if(path.endsWith(".jsp") || path.endsWith(".jspx") || path.endsWith(".JSP") || path.endsWith(".JSPX")) {
URLMappingsHelper.addApplicationURLMapping(new ApplicationURLMapping(URLMappingsHelper.WILDCARD, path));
}
}
}
} catch (Exception e){
NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format(GenericHelper.ERROR_WHILE_GETTING_APP_ENDPOINTS, WEBLOGIC_12, e.getMessage()), e, HttpServletHelper.class.getName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package weblogic.servlet.internal;

import com.newrelic.agent.security.instrumentation.weblogic12.HttpServletHelper;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

import javax.servlet.ServletRegistration;
import java.util.Map;
import java.util.Set;

@Weave
public final class WebAppServletContext {

void start() throws Exception {
Weaver.callOriginal();
HttpServletHelper.gatherURLMappings(this);
}

public Map<String, ? extends ServletRegistration> getServletRegistrations() {
return Weaver.callOriginal();
}

public Set<String> getResourcePaths(String path) {
return Weaver.callOriginal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
import java.util.concurrent.ConcurrentHashMap;

public class URLMappingsHelper {

public static final String SEPARATOR = "/";

public static final String WILDCARD = "*";

public static final String subResourceSegment = "/*";

private static Set<ApplicationURLMapping> mappings = ConcurrentHashMap.newKeySet();
private static final Set<ApplicationURLMapping> mappings = ConcurrentHashMap.newKeySet();

private static final Set<String> defaultHandlers = new HashSet<String>() {{
add("org.eclipse.jetty.jsp.JettyJspServlet");
Expand All @@ -47,15 +48,24 @@ public class URLMappingsHelper {
add("org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet");
add("org.codehaus.groovy.grails.web.pages.GroovyPagesServlet");
add("org.codehaus.groovy.grails.web.servlet.ErrorHandlingServlet");
add("weblogic.corba.iiop.http.TunnelLoginServlet");
add("weblogic.corba.iiop.http.TunnelSendServlet");
add("weblogic.corba.iiop.http.TunnelCloseServlet");
add("weblogic.corba.iiop.http.TunnelRecvServlet");
add("weblogic.rjvm.http.TunnelLoginServlet");
add("weblogic.rjvm.http.TunnelRecvServlet");
add("weblogic.rjvm.http.TunnelSendServlet");
add("weblogic.rjvm.http.TunnelCloseServlet");
add("weblogic.servlet.ClasspathServlet");
}};

public static Set<ApplicationURLMapping> getApplicationURLMappings() {
return mappings;
}

private static Set<Integer> handlers = ConcurrentHashMap.newKeySet();
private static final Set<Integer> handlers = ConcurrentHashMap.newKeySet();

private static Set<RouteSegments> routeSegments = new TreeSet<>(new RouteComparator());
private static final Set<RouteSegments> routeSegments = new TreeSet<>(new RouteComparator());

public static Set<Integer> getHandlersHash() {
return handlers;
Expand All @@ -66,8 +76,11 @@ public static Set<RouteSegments> getRouteSegments() {
}

public static void addApplicationURLMapping(ApplicationURLMapping mapping) {
if (mapping.getHandler() == null || (mapping.getHandler() != null && !defaultHandlers.contains(mapping.getHandler()))) {
mappings.add(mapping);
if (mapping.getHandler() != null && defaultHandlers.contains(mapping.getHandler())) {
return;
}
mappings.add(mapping);
if (mapping.getHandler() == null) {
generateRouteSegments(mapping.getPath());
}
if (mapping.getHandler() != null){
Expand Down
Loading