Skip to content

Commit

Permalink
NR-174176: support for Jedis 1.4.0 to 3.0.0 in security agent
Browse files Browse the repository at this point in the history
  • Loading branch information
monu-k2io committed Nov 1, 2023
1 parent 53a01b7 commit 5f035ad
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
24 changes: 24 additions & 0 deletions instrumentation-security/jedis-1.4.0/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
jar {
manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.security.jedis-1.4.0' }
}

dependencies {
implementation(project(":newrelic-security-api"))
implementation("com.newrelic.agent.java:newrelic-api:${nrAPIVersion}")
implementation("com.newrelic.agent.java:newrelic-weaver-api:${nrAPIVersion}")
implementation("redis.clients:jedis:1.4.0")
testImplementation("com.github.codemonstur:embedded-redis:1.0.0")
testImplementation("org.springframework.data:spring-data-redis:1.1.0.RELEASE")
}

verifyInstrumentation {
passesOnly 'redis.clients:jedis:[1.4.0,3.0.0)'
exclude 'redis.clients:jedis:2.7.1'
exclude 'redis.clients:jedis:2.7.2'
excludeRegex 'redis.clients:jedis:.*-(m|rc|RC)[0-9]*'
}

site {
title 'Jedis'
type 'Datastore'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.newrelic.agent.security.instrumentation.jedis_1_4_0;

import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.schema.AbstractOperation;
import com.newrelic.api.agent.security.schema.exceptions.NewRelicSecurityException;
import com.newrelic.api.agent.security.schema.operation.RedisOperation;

import java.util.List;

public class JedisHelper {
public static final String NR_SEC_LOCK_ATTRIB_NAME = "JEDIS_OPERATION_LOCK_";
public static AbstractOperation preprocessSecurityHook(String command, List<Object> args, String klass, String method) {
try {
if (!NewRelicSecurity.isHookProcessingActive() || NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().isEmpty()){
return null;
}
RedisOperation operation = new RedisOperation(klass, method, command, args);
NewRelicSecurity.getAgent().registerOperation(operation);
return operation;
} catch (Throwable e) {
e.printStackTrace();
if (e instanceof NewRelicSecurityException) {
throw e;
}
}
return null;
}

public static void registerExitOperation(boolean isProcessingAllowed, AbstractOperation operation) {
try {
if (operation == null || !isProcessingAllowed || !NewRelicSecurity.isHookProcessingActive() ||
NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().isEmpty()) {
return;
}
NewRelicSecurity.getAgent().registerExitEvent(operation);
} catch (Throwable ignored){}
}

public static void releaseLock(int hashCode) {
try {
GenericHelper.releaseLock(NR_SEC_LOCK_ATTRIB_NAME, hashCode);
} catch (Throwable ignored) {}
}

public static boolean acquireLockIfPossible(int hashCode) {
try {
return GenericHelper.acquireLockIfPossible(NR_SEC_LOCK_ATTRIB_NAME, hashCode);
} catch (Throwable ignored) {}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package redis.clients.jedis;

import com.newrelic.agent.security.instrumentation.jedis_1_4_0.JedisHelper;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.schema.AbstractOperation;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

import java.util.ArrayList;
import java.util.List;

@Weave(type = MatchType.BaseClass, originalName = "redis.clients.jedis.Connection")
public abstract class Connection_Instrumentation {
protected Connection sendCommand(final Protocol.Command cmd, final byte[]... args) {
boolean isLockAcquired = JedisHelper.acquireLockIfPossible(cmd.hashCode());
AbstractOperation operation = null;
if(isLockAcquired && cmd!=null && args!=null) {
List<Object> argList = new ArrayList<>();
for (int i=0; i < args.length; i++) {
Object dataByBytes = NewRelicSecurity.getAgent()
.getSecurityMetaData()
.getCustomAttribute(GenericHelper.NR_SEC_CUSTOM_SPRING_REDIS_ATTR + args[i].hashCode(), Object.class);

if(dataByBytes!=null){
argList.add(dataByBytes);
} else {
argList.add(new String(args[i]));
}
}
operation = JedisHelper.preprocessSecurityHook(cmd.name(), argList, this.getClass().getName(), "sendCommand");
}
Connection returnValue = null;
try {
returnValue = Weaver.callOriginal();
} finally {
if (isLockAcquired) {
JedisHelper.releaseLock(cmd.hashCode());
}
}
JedisHelper.registerExitOperation(isLockAcquired, operation);
return returnValue;
}
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,5 @@ include 'instrumentation:commons-jxpath'
//include 'instrumentation:apache-wicket-6.4'
//include 'instrumentation:apache-wicket-7.0'
//include 'instrumentation:apache-wicket-8.0'
include 'instrumentation:spring-data-redis'
include 'instrumentation:spring-data-redis'
include 'instrumentation:jedis-1.4.0'

0 comments on commit 5f035ad

Please sign in to comment.