-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NR-174176: support for Jedis 1.4.0 to 3.0.0 in security agent
- Loading branch information
Showing
4 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
52 changes: 52 additions & 0 deletions
52
....0/src/main/java/com/newrelic/agent/security/instrumentation/jedis_1_4_0/JedisHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...on-security/jedis-1.4.0/src/main/java/redis/clients/jedis/Connection_Instrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters