Search in sources :

Example 6 with ForceDisconnectException

use of com.newrelic.agent.ForceDisconnectException in project newrelic-java-agent by newrelic.

the class LaspPoliciesConfigTest method noDisconnect.

@Test
public void noDisconnect() throws ParseException, ForceDisconnectException {
    String policiesString = "{" + "\"record_sql\":                    { \"enabled\": false, \"required\": false },\n" + "\"attributes_include\":            { \"enabled\": false, \"required\": false },\n" + "\"allow_raw_exception_messages\":  { \"enabled\": false, \"required\": false },\n" + "\"custom_events\":                 { \"enabled\": false, \"required\": false },\n" + "\"custom_parameters\":             { \"enabled\": false, \"required\": false },\n" + "\"custom_instrumentation_editor\": { \"enabled\": false, \"required\": false },\n" + "\"message_parameters\":            { \"enabled\": false, \"required\": false },\n" + "\"attributes_include\":            { \"enabled\": false, \"required\": false },\n" + "\"job_arguments\":                 { \"enabled\": false, \"required\": false }}";
    JSONObject policiesJson = createPolicies(policiesString);
    try {
        LaspPolicies.validatePolicies(policiesJson);
    } catch (ForceDisconnectException ex) {
        fail();
    }
}
Also used : ForceDisconnectException(com.newrelic.agent.ForceDisconnectException) JSONObject(org.json.simple.JSONObject) Test(org.junit.Test)

Example 7 with ForceDisconnectException

use of com.newrelic.agent.ForceDisconnectException in project newrelic-java-agent by newrelic.

the class LaspPoliciesConfigTest method jobArgumentsRequired.

@Test
public void jobArgumentsRequired() throws ParseException {
    String policiesString = "{" + "\"job_arguments\":      { \"enabled\": true, \"required\": true },\n" + "\"record_sql\":                    { \"enabled\": false, \"required\": false },\n" + "\"attributes_include\":            { \"enabled\": false, \"required\": false },\n" + "\"allow_raw_exception_messages\":  { \"enabled\": false, \"required\": false },\n" + "\"custom_events\":                 { \"enabled\": false, \"required\": false },\n" + "\"custom_parameters\":             { \"enabled\": false, \"required\": false },\n" + "\"custom_instrumentation_editor\": { \"enabled\": false, \"required\": false },\n" + "\"message_parameters\":            { \"enabled\": false, \"required\": false },\n" + "\"attributes_include\":            { \"enabled\": false, \"required\": false }}";
    JSONObject policiesJson = createPolicies(policiesString);
    try {
        LaspPolicies.validatePolicies(policiesJson);
        fail();
    } catch (ForceDisconnectException ex) {
    }
}
Also used : ForceDisconnectException(com.newrelic.agent.ForceDisconnectException) JSONObject(org.json.simple.JSONObject) Test(org.junit.Test)

Example 8 with ForceDisconnectException

use of com.newrelic.agent.ForceDisconnectException in project newrelic-java-agent by newrelic.

the class LaspPoliciesConfigTest method unknownRequired.

@Test
public void unknownRequired() throws ParseException {
    String policiesString = "{" + "\"totally_required_yo\":      { \"enabled\": true, \"required\": true },\n" + "\"record_sql\":                    { \"enabled\": false, \"required\": false },\n" + "\"attributes_include\":            { \"enabled\": false, \"required\": false },\n" + "\"allow_raw_exception_messages\":  { \"enabled\": false, \"required\": false },\n" + "\"custom_events\":                 { \"enabled\": false, \"required\": false },\n" + "\"custom_parameters\":             { \"enabled\": false, \"required\": false },\n" + "\"custom_instrumentation_editor\": { \"enabled\": false, \"required\": false },\n" + "\"message_parameters\":            { \"enabled\": false, \"required\": false },\n" + "\"attributes_include\":            { \"enabled\": false, \"required\": false },\n" + "\"job_arguments\":                 { \"enabled\": false, \"required\": false }}";
    JSONObject policiesJson = createPolicies(policiesString);
    try {
        LaspPolicies.validatePolicies(policiesJson);
        fail();
    } catch (ForceDisconnectException ex) {
    }
}
Also used : ForceDisconnectException(com.newrelic.agent.ForceDisconnectException) JSONObject(org.json.simple.JSONObject) Test(org.junit.Test)

Example 9 with ForceDisconnectException

use of com.newrelic.agent.ForceDisconnectException in project newrelic-java-agent by newrelic.

the class LaspPolicies method validatePolicies.

public static Map<String, Boolean> validatePolicies(JSONObject policies) throws ForceDisconnectException {
    if (policies == null) {
        return Collections.emptyMap();
    }
    Agent.LOG.log(Level.INFO, "LASP Policies received from server side: {0}", policies);
    Map<String, Boolean> validatedPolicies = new HashMap<>();
    Set<Map.Entry<String, JSONObject>> policyEntries = policies.entrySet();
    for (Map.Entry<String, JSONObject> entry : policyEntries) {
        String key = entry.getKey();
        JSONObject value = entry.getValue();
        if ((Boolean) value.get(REQUIRED) && !KNOWN_LASP_POLICIES.contains(key)) {
            throw new ForceDisconnectException("Found unknown policy that is required: " + key);
        }
        // only store known/applicable policies
        if (KNOWN_LASP_POLICIES.contains(key)) {
            validatedPolicies.put(key, (Boolean) value.get(ENABLED));
        }
    }
    // check to see if known policies is missing a policy the agent expected
    for (String knownPolicy : KNOWN_LASP_POLICIES) {
        if (!validatedPolicies.containsKey(knownPolicy)) {
            throw new ForceDisconnectException("Did not receive all policies on preconnect");
        }
    }
    return validatedPolicies;
}
Also used : ForceDisconnectException(com.newrelic.agent.ForceDisconnectException) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ForceDisconnectException (com.newrelic.agent.ForceDisconnectException)9 JSONObject (org.json.simple.JSONObject)7 Test (org.junit.Test)7 MockServiceManager (com.newrelic.agent.MockServiceManager)1 ServiceManager (com.newrelic.agent.service.ServiceManager)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1