Skip to content

Commit

Permalink
Removed weaving of membered variables
Browse files Browse the repository at this point in the history
  • Loading branch information
IshikaDawda committed Oct 9, 2023
1 parent c9f71ff commit 83d2181
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion instrumentation-security/cassandra-datastax-3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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.datastax.cassandra:cassandra-driver-core:3.8.0")
implementation("com.datastax.cassandra:cassandra-driver-core:3.0.0")

testImplementation("org.cassandraunit:cassandra-unit:3.11.2.0")
testImplementation("com.github.jbellis:jamm:0.3.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

@Weave(type = MatchType.ExactClass, originalName = "com.datastax.driver.core.SessionManager")
abstract class SessionManager_Instrumentation {
final Cluster cluster = Weaver.callOriginal();
Configuration configuration() {
return Weaver.callOriginal();
}
public ResultSetFuture executeAsync(Statement statement) {
boolean isLockAcquired = CassandraUtils.acquireLockIfPossible(hashCode());
boolean isLockAcquired = CassandraUtils.acquireLockIfPossible(statement.hashCode());
ResultSetFuture result;
AbstractOperation cqlOperation = null;

Expand All @@ -22,14 +24,14 @@ public ResultSetFuture executeAsync(Statement statement) {
}

if(isLockAcquired){
cqlOperation = CassandraUtils.preProcessSecurityHook(statement, cluster.getConfiguration().getCodecRegistry(), this.getClass().getName());
cqlOperation = CassandraUtils.preProcessSecurityHook(statement, configuration().getCodecRegistry(), this.getClass().getName());
if(cqlOperation != null){
NewRelicSecurity.getAgent().registerOperation(cqlOperation);
}
}
} finally {
if(isLockAcquired){
CassandraUtils.releaseLock(hashCode());
CassandraUtils.releaseLock(statement.hashCode());
}
}
CassandraUtils.registerExitOperation(isLockAcquired, cqlOperation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,36 @@
import com.newrelic.api.agent.security.schema.operation.SQLOperation;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.WeaveAllConstructors;
import com.newrelic.api.agent.weaver.Weaver;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

@Weave(type= MatchType.ExactClass, originalName = "com.datastax.driver.core.SimpleStatement")
public abstract class SimpleStatement_Instrumentation {
private final String query = Weaver.callOriginal();
private final Object[] values = Weaver.callOriginal();
private final Map<String, Object> namedValues = Weaver.callOriginal();
@WeaveAllConstructors
public SimpleStatement_Instrumentation() {

public SimpleStatement_Instrumentation(String query, Object... values) {
boolean isLockAcquired = CassandraUtils.acquireLockIfPossible(hashCode());

try{
if(isLockAcquired){
SQLOperation cqlOperation = new SQLOperation(this.getClass().getName(), CassandraUtils.METHOD_EXECUTE_ASYNC);
cqlOperation.setQuery(query);
cqlOperation.setCaseType(VulnerabilityCaseType.NOSQL_DB_COMMAND);
cqlOperation.setDbName(CassandraUtils.EVENT_CATEGORY);
Map<String, String> localParams = setParams(values);
cqlOperation.setParams(localParams);
NewRelicSecurity.getAgent().getSecurityMetaData().addCustomAttribute(
CassandraUtils.NR_SEC_CUSTOM_ATTRIB_CQL_STMT + hashCode(), cqlOperation);
}
} finally {
if(isLockAcquired){
CassandraUtils.releaseLock(hashCode());
}
}
}

public SimpleStatement_Instrumentation(String query, Map<String, Object> values){
boolean isLockAcquired = CassandraUtils.acquireLockIfPossible(hashCode());

try{
Expand All @@ -28,7 +44,7 @@ public SimpleStatement_Instrumentation() {
cqlOperation.setQuery(query);
cqlOperation.setCaseType(VulnerabilityCaseType.NOSQL_DB_COMMAND);
cqlOperation.setDbName(CassandraUtils.EVENT_CATEGORY);
Map<String, String> localParams = setParams();
Map<String, String> localParams = setParams(values);
cqlOperation.setParams(localParams);
NewRelicSecurity.getAgent().getSecurityMetaData().addCustomAttribute(
CassandraUtils.NR_SEC_CUSTOM_ATTRIB_CQL_STMT + hashCode(), cqlOperation);
Expand All @@ -39,21 +55,24 @@ public SimpleStatement_Instrumentation() {
}
}
}
private Map<String, String> setParams() {
private Map<String, String> setParams(Object... values) {
Map<String, String> params = new HashMap<>();
try{
if(values != null){
for(int i = 0; i < values.length; i++){
if(!(values[i] instanceof ByteBuffer)){
params.put(String.valueOf(i), String.valueOf(values[i]));
}
for(int i = 0; i < values.length; i++){
if(!(values[i] instanceof ByteBuffer)){
params.put(String.valueOf(i), String.valueOf(values[i]));
}
}
if(namedValues != null){
for( Map.Entry<String, Object> namedVal: namedValues.entrySet()) {
if(!(namedVal.getValue() instanceof ByteBuffer)){
params.put(namedVal.getKey(), String.valueOf(namedVal.getValue()));
}
} catch (Exception ignored){
}
return params;
}
private Map<String, String> setParams(Map<String, Object> values) {
Map<String, String> params = new HashMap<>();
try{
for( Map.Entry<String, Object> namedVal: values.entrySet()) {
if(!(namedVal.getValue() instanceof ByteBuffer)){
params.put(namedVal.getKey(), String.valueOf(namedVal.getValue()));
}
}
} catch (Exception ignored){
Expand Down

0 comments on commit 83d2181

Please sign in to comment.