Skip to content

Commit

Permalink
Using new api to read config value instead of direct reading it from …
Browse files Browse the repository at this point in the history
…apm agent

Optimized package import
  • Loading branch information
monu-k2io committed Sep 25, 2023
1 parent c682cd6 commit a6e4119
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package java.io;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.FileHelper;
import com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper;
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.FileOperation;
Expand All @@ -14,9 +12,6 @@
import java.util.ArrayList;
import java.util.List;

import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.DEFAULT;
import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.LOW_SEVERITY_HOOKS_ENABLED;

@Weave(type = MatchType.BaseClass, originalName = "java.io.File")
public abstract class File_Instrumentation {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.newrelic.agent.security.instrumentation.random.java.io;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.FileHelper;
import com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper;
Expand All @@ -14,9 +13,6 @@
import java.util.ArrayList;
import java.util.List;

import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.DEFAULT;
import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.LOW_SEVERITY_HOOKS_ENABLED;

@Weave(type = MatchType.BaseClass, originalName = "java.io.File")
public abstract class File_Instrumentation {
public abstract String getName();
Expand All @@ -25,7 +21,7 @@ public abstract class File_Instrumentation {

public boolean exists() {
boolean isFileLockAcquired = acquireFileLockIfPossible();
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();

AbstractOperation operation = null;
if (isOwaspHookEnabled && isFileLockAcquired && !FileHelper.skipExistsEvent(this.getName()) && LowSeverityHelper.isOwaspHookProcessingNeeded()) {
Expand All @@ -35,7 +31,9 @@ public boolean exists() {
try {
returnVal = Weaver.callOriginal();
} finally {
registerExitOperation(isFileLockAcquired, operation);
if (isOwaspHookEnabled) {
registerExitOperation(isFileLockAcquired, operation);
}
if (isFileLockAcquired) {
releaseFileLock();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.newrelic.agent.security.instrumentation.random.java.security;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper;
import com.newrelic.api.agent.security.schema.AbstractOperation;
Expand All @@ -16,14 +15,11 @@
import java.security.KeyPairGenerator;
import java.security.Provider;

import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.DEFAULT;
import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.LOW_SEVERITY_HOOKS_ENABLED;

@Weave(type = MatchType.ExactClass, originalName = "java.security.KeyPairGenerator")
public class KeyPairGenerator_Instrumentation {
public static KeyPairGenerator getInstance(String algorithm) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, StringUtils.EMPTY, KeyPairGenerator.class.getName(), "getInstance", "KEYPAIRGENERATOR");
}
Expand All @@ -40,7 +36,7 @@ public static KeyPairGenerator getInstance(String algorithm) {

public static KeyPairGenerator getInstance(String algorithm, String provider) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, provider, KeyPairGenerator.class.getName(), "getInstance", "KEYPAIRGENERATOR");
}
Expand All @@ -57,7 +53,7 @@ public static KeyPairGenerator getInstance(String algorithm, String provider) {

public static KeyPairGenerator getInstance(String algorithm, Provider provider) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, provider.getClass().getSimpleName(), KeyPairGenerator.class.getName(), "getInstance", "KEYPAIRGENERATOR");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.newrelic.agent.security.instrumentation.random.java.security;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper;
import com.newrelic.api.agent.security.schema.AbstractOperation;
Expand All @@ -15,15 +14,12 @@
import java.security.MessageDigest;
import java.security.Provider;

import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.DEFAULT;
import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.LOW_SEVERITY_HOOKS_ENABLED;

@Weave(type = MatchType.ExactClass, originalName = "java.security.MessageDigest")
public class MessageDigest_Instrumentation {

public static MessageDigest getInstance(String algorithm) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, StringUtils.EMPTY, MessageDigest.class.getName(), "getInstance");
}
Expand All @@ -40,7 +36,7 @@ public static MessageDigest getInstance(String algorithm) {

public static MessageDigest getInstance(String algorithm, String provider) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, provider, MessageDigest.class.getName(), "getInstance");
}
Expand All @@ -57,7 +53,7 @@ public static MessageDigest getInstance(String algorithm, String provider) {

public static MessageDigest getInstance(String algorithm, Provider provider) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, provider.getClass().getSimpleName(), MessageDigest.class.getName(), "getInstance");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.newrelic.agent.security.instrumentation.random.java.util;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper;
Expand All @@ -15,8 +14,6 @@

import java.security.SecureRandom;

import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.DEFAULT;
import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.LOW_SEVERITY_HOOKS_ENABLED;
import static com.newrelic.agent.security.instrumentation.random.RandomUtils.SECURE_RANDOM;
import static com.newrelic.agent.security.instrumentation.random.RandomUtils.WEAK_RANDOM;

Expand All @@ -26,7 +23,7 @@ public class Random_Instrumentation {
public int nextInt() {
boolean isLockAcquired = acquireLockIfPossible(hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(getClass().getName(), "nextInt");
Expand All @@ -48,7 +45,7 @@ public int nextInt() {
public int nextInt(int bound) {
boolean isLockAcquired = acquireLockIfPossible(hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(getClass().getName(), "nextInt");
Expand All @@ -70,7 +67,7 @@ public int nextInt(int bound) {
public void nextBytes(byte[] bytes) {
boolean isLockAcquired = acquireLockIfPossible(hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(getClass().getName(), "nextBytes");
Expand All @@ -90,7 +87,7 @@ public void nextBytes(byte[] bytes) {
public long nextLong() {
boolean isLockAcquired = acquireLockIfPossible(hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(getClass().getName(), "nextLong");
Expand All @@ -112,7 +109,7 @@ public long nextLong() {
public float nextFloat() {
boolean isLockAcquired = acquireLockIfPossible(hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(getClass().getName(), "nextFloat");
Expand All @@ -134,7 +131,7 @@ public float nextFloat() {
public double nextDouble() {
boolean isLockAcquired = acquireLockIfPossible(hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(getClass().getName(), "nextDouble");
Expand All @@ -156,7 +153,7 @@ public double nextDouble() {
public double nextGaussian() {
boolean isLockAcquired = acquireLockIfPossible(hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(getClass().getName(), "nextGaussian");
Expand All @@ -178,7 +175,7 @@ public double nextGaussian() {
public boolean nextBoolean() {
boolean isLockAcquired = acquireLockIfPossible(hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(getClass().getName(), "nextBoolean");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.newrelic.agent.security.instrumentation.random.javax.crypto;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper;
import com.newrelic.api.agent.security.schema.AbstractOperation;
Expand All @@ -15,14 +14,11 @@

import java.security.Provider;

import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.DEFAULT;
import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.LOW_SEVERITY_HOOKS_ENABLED;

@Weave(type = MatchType.ExactClass, originalName = "javax.crypto.Cipher")
public class Cipher_Instrumentation {
public static final Cipher_Instrumentation getInstance(String algorithm) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, StringUtils.EMPTY, Cipher_Instrumentation.class.getName(), "getInstance", "CIPHER");
}
Expand All @@ -39,7 +35,7 @@ public static final Cipher_Instrumentation getInstance(String algorithm) {

public static final Cipher_Instrumentation getInstance(String transformation, Provider provider) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(transformation, provider.getClass().getSimpleName(), Cipher_Instrumentation.class.getName(), "getInstance", "CIPHER");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.newrelic.agent.security.instrumentation.random.javax.crypto;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper;
import com.newrelic.api.agent.security.schema.AbstractOperation;
Expand All @@ -15,14 +14,11 @@

import java.security.Provider;

import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.DEFAULT;
import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.LOW_SEVERITY_HOOKS_ENABLED;

@Weave(type = MatchType.ExactClass, originalName = "javax.crypto.KeyGenerator")
public class KeyGenerator_Instrumentation {
public static final KeyGenerator_Instrumentation getInstance(String algorithm) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, StringUtils.EMPTY, KeyGenerator_Instrumentation.class.getName(), "getInstance", "KEYGENERATOR");
}
Expand All @@ -39,7 +35,7 @@ public static final KeyGenerator_Instrumentation getInstance(String algorithm) {

public static final KeyGenerator_Instrumentation getInstance(String algorithm, String provider) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, provider, KeyGenerator_Instrumentation.class.getName(), "getInstance", "KEYGENERATOR");
}
Expand All @@ -56,7 +52,7 @@ public static final KeyGenerator_Instrumentation getInstance(String algorithm, S

public static final KeyGenerator_Instrumentation getInstance(String algorithm, Provider provider) {
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
operation = preprocessSecurityHook(algorithm, provider.getClass().getSimpleName(), KeyGenerator_Instrumentation.class.getName(), "getInstance", "KEYGENERATOR");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package javax.servlet.http;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.security.NewRelicSecurity;
import com.newrelic.api.agent.security.instrumentation.helpers.GenericHelper;
import com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper;
Expand All @@ -13,16 +12,13 @@
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.DEFAULT;
import static com.newrelic.api.agent.security.instrumentation.helpers.LowSeverityHelper.LOW_SEVERITY_HOOKS_ENABLED;

@Weave(type = MatchType.Interface, originalName = "javax.servlet.http.HttpServletResponse")
public class HttpServletResponse_Instrumentation {

public void addCookie(Cookie cookie){
boolean isLockAcquired = acquireLockIfPossible(cookie.hashCode());
AbstractOperation operation = null;
boolean isOwaspHookEnabled = NewRelic.getAgent().getConfig().getValue(LOW_SEVERITY_HOOKS_ENABLED, DEFAULT);
boolean isOwaspHookEnabled = NewRelicSecurity.getAgent().isLowPriorityInstrumentationEnabled();
if (isOwaspHookEnabled && LowSeverityHelper.isOwaspHookProcessingNeeded()){
if (isLockAcquired)
operation = preprocessSecurityHook(cookie, getClass().getName(), "addCookie");
Expand Down
Loading

0 comments on commit a6e4119

Please sign in to comment.