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-299885] Instrumentation support for GraphQL #315

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The agent version.
agentVersion=1.4.1
jsonVersion=1.2.5
jsonVersion=1.2.9
# Updated exposed NR APM API version.
nrAPIVersion=8.12.0

Expand Down
22 changes: 22 additions & 0 deletions instrumentation-security/graphql-java-16.2/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
dependencies {
implementation(project(":newrelic-security-api"))
implementation("com.newrelic.agent.java:newrelic-api:${nrAPIVersion}")
implementation("com.newrelic.agent.java:newrelic-weaver-api:${nrAPIVersion}")
implementation("com.graphql-java:graphql-java:16.2")
}

jar {
manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.security.graphql-java-16.2' }
}

verifyInstrumentation {
passesOnly('com.graphql-java:graphql-java:[16.0,)')
excludeRegex('com.graphql-java:graphql-java:(0.0.0|201|202).*')
excludeRegex('com.graphql-java:graphql-java:.*(vTEST|-beta|-alpha1|-nf-execution|-rc|-TEST).*')
exclude('com.graphql-java:graphql-java:15.0')
}

site {
title 'GraphQL Java'
type 'Framework'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package graphql;

import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.schema.HttpRequest;
import com.newrelic.api.agent.security.schema.HttpRequestCustomDataTypeEnum;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.language.Document;
import graphql.schema.GraphQLSchema;

import java.util.concurrent.CompletableFuture;

@Weave(originalName = "graphql.GraphQL", type = MatchType.ExactClass)
public class GraphQL_Instrumentation {

private CompletableFuture<ExecutionResult> execute(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) {
try {
if (NewRelicSecurity.isHookProcessingActive()) {
HttpRequest request = NewRelicSecurity.getAgent().getSecurityMetaData().getRequest();
if (executionInput.getQuery() != null && !executionInput.getQuery().isEmpty()) {
request.getCustomDataType().put("*.query", HttpRequestCustomDataTypeEnum.GRAPHQL_QUERY.name());
}
if (executionInput.getVariables() != null && !executionInput.getVariables().isEmpty()) {
request.getCustomDataType().put("*.variables", HttpRequestCustomDataTypeEnum.GRAPHQL_VARIABLE.name());
}
}
} catch (Exception ignored) {}
return Weaver.callOriginal();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package graphql;

import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;

@Weave(originalName = "graphql.ParseAndValidate", type = MatchType.ExactClass)
public class ParseAndValidate_Instrumentation {

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ public class HttpRequest {
private Map<String, String> pathParameterMap;

private boolean isRequestParsed;

private boolean isGrpc;

private String route;

private Map<String, String> customDataType;

public HttpRequest() {
this.clientIP = StringUtils.EMPTY;
this.body = new StringBuilder();
Expand All @@ -54,6 +58,7 @@ public HttpRequest() {
this.isRequestParsed = false;
this.isGrpc = false;
this.route = StringUtils.EMPTY;
this.customDataType = new HashMap<>();
}

public HttpRequest(HttpRequest servletInfo) {
Expand All @@ -71,6 +76,7 @@ public HttpRequest(HttpRequest servletInfo) {
this.isRequestParsed = servletInfo.isRequestParsed;
this.isGrpc = servletInfo.isGrpc;
this.route = servletInfo.route;
this.customDataType = servletInfo.customDataType;
}

public String getMethod() {
Expand Down Expand Up @@ -233,6 +239,11 @@ public void setRoute(String segment, boolean isAlreadyServlet) {
this.route = Paths.get(this.route, formatedSegment).normalize().toString();
}
}

public Map<String, String> getCustomDataType() {
return customDataType;
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.newrelic.api.agent.security.schema;

public enum HttpRequestCustomDataTypeEnum {
GRAPHQL_QUERY,
GRAPHQL_VARIABLE,

NONE;
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,4 @@ include 'instrumentation:solr-5.1.0'
include 'instrumentation:solr-7.0.0'
include 'instrumentation:solr-8.0.0'
include 'instrumentation:solr-9.0.0'
include 'instrumentation:graphql-java-16.2'
Loading