Search in sources :

Example 1 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class JSONLogFieldSyntax method sanitize.

/**
 * Sanitizes the provided JSON value.
 *
 * @param  value  The value to be sanitized.  It must not be {@code null}.
 *
 * @return  A sanitized representation of the provided JSON value.
 */
@NotNull()
private JSONValue sanitize(@NotNull final JSONValue value) {
    if (value instanceof JSONObject) {
        final Map<String, JSONValue> originalFields = ((JSONObject) value).getFields();
        final Map<String, JSONValue> sanitizedFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
        for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
            sanitizedFields.put(e.getKey(), sanitize(e.getValue()));
        }
        return new JSONObject(sanitizedFields);
    } else if (value instanceof JSONArray) {
        final List<JSONValue> originalValues = ((JSONArray) value).getValues();
        final List<JSONValue> sanitizedValues = new ArrayList<>(originalValues.size());
        for (final JSONValue v : originalValues) {
            sanitizedValues.add(sanitize(v));
        }
        return new JSONArray(sanitizedValues);
    } else if (value instanceof JSONString) {
        final String stringValue = ((JSONString) value).stringValue();
        return new JSONString(sanitize(stringValue));
    } else {
        return value;
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 2 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class JSONLogFieldSyntax method redactValue.

/**
 * Retrieves a redacted representation of the provided JSON value.
 *
 * @param  value  The value to be redacted.
 *
 * @return  A redacted representation of the provided JSON value.
 */
@NotNull()
private JSONValue redactValue(@NotNull final JSONValue value) {
    if (value instanceof JSONObject) {
        final Map<String, JSONValue> originalFields = ((JSONObject) value).getFields();
        final Map<String, JSONValue> redactedFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
        for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
            final String fieldName = e.getKey();
            if (shouldRedactOrTokenize(fieldName)) {
                redactedFields.put(fieldName, new JSONString(REDACTED_STRING));
            } else {
                redactedFields.put(fieldName, redactValue(e.getValue()));
            }
        }
        return new JSONObject(redactedFields);
    } else if (value instanceof JSONArray) {
        final List<JSONValue> originalValues = ((JSONArray) value).getValues();
        final List<JSONValue> redactedValues = new ArrayList<>(originalValues.size());
        for (final JSONValue v : originalValues) {
            redactedValues.add(redactValue(v));
        }
        return new JSONArray(redactedValues);
    } else {
        return sanitize(value);
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 3 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class JSONLogsTestCase method createPopulatedMessageObject.

/**
 * Creates a JSON object with an encoded representation of a log message that
 * has values populated for most of the optional common fields.
 *
 * @param  messageType    The message type for the log message.  This must not
 *                        be {@code null}.
 * @param  operationType  The operation type for the log message.  This may be
 *                        {@code null} if it is not an operation log message.
 * @param  fields         The set of additional fields to include in the log
 *                        message.  This must not be {@code null} but may be
 *                        empty.
 *
 * @return  The log message that was created.
 */
@NotNull()
protected static JSONObject createPopulatedMessageObject(@NotNull final AccessLogMessageType messageType, @Nullable final AccessLogOperationType operationType, @NotNull final JSONField... fields) {
    final Map<String, JSONValue> fieldMap = createMinimalFieldMap(messageType, operationType);
    fieldMap.put(PRODUCT_NAME.getFieldName(), new JSONString(DEFAULT_PRODUCT_NAME));
    fieldMap.put(INSTANCE_NAME.getFieldName(), new JSONString(DEFAULT_INSTANCE_NAME));
    fieldMap.put(STARTUP_ID.getFieldName(), new JSONString(DEFAULT_STARTUP_ID));
    fieldMap.put(THREAD_ID.getFieldName(), new JSONNumber(DEFAULT_THREAD_ID));
    fieldMap.put(CONNECTION_ID.getFieldName(), new JSONNumber(DEFAULT_CONNECTION_ID));
    if (operationType != null) {
        fieldMap.put(OPERATION_ID.getFieldName(), new JSONNumber(DEFAULT_OPERATION_ID));
        fieldMap.put(MESSAGE_ID.getFieldName(), new JSONNumber(DEFAULT_MESSAGE_ID));
        fieldMap.put(TRIGGERED_BY_CONNECTION_ID.getFieldName(), new JSONNumber(DEFAULT_TRIGGERED_BY_CONNECTION_ID));
        fieldMap.put(TRIGGERED_BY_OPERATION_ID.getFieldName(), new JSONNumber(DEFAULT_TRIGGERED_BY_OPERATION_ID));
        fieldMap.put(ORIGIN.getFieldName(), new JSONString(DEFAULT_ORIGIN));
        fieldMap.put(REQUESTER_IP_ADDRESS.getFieldName(), new JSONString(DEFAULT_REQUESTER_IP));
        fieldMap.put(REQUESTER_DN.getFieldName(), new JSONString(DEFAULT_REQUESTER_DN));
        fieldMap.put(REQUEST_CONTROL_OIDS.getFieldName(), createArray(DEFAULT_REQUEST_CONTROL_OIDS));
        fieldMap.put(USING_ADMIN_SESSION_WORKER_THREAD.getFieldName(), new JSONBoolean(DEFAULT_USING_ADMIN_SESSION_WORKER_THREAD));
        fieldMap.put(ADMINISTRATIVE_OPERATION.getFieldName(), new JSONString(DEFAULT_ADMIN_OP_MESSAGE));
        fieldMap.put(INTERMEDIATE_CLIENT_REQUEST_CONTROL.getFieldName(), DEFAULT_INTERMEDIATE_CLIENT_REQUEST.getControlObject());
        fieldMap.put(OPERATION_PURPOSE.getFieldName(), DEFAULT_OPERATION_PURPOSE_REQUEST.getControlObject());
        if ((messageType == AccessLogMessageType.FORWARD) || (messageType == AccessLogMessageType.FORWARD_FAILED) || (messageType == AccessLogMessageType.RESULT) || (messageType == AccessLogMessageType.ASSURANCE_COMPLETE)) {
            fieldMap.put(TARGET_HOST.getFieldName(), new JSONString(DEFAULT_FORWARD_TARGET_HOST));
            fieldMap.put(TARGET_PORT.getFieldName(), new JSONNumber(DEFAULT_FORWARD_TARGET_PORT));
            fieldMap.put(TARGET_PROTOCOL.getFieldName(), new JSONString(DEFAULT_FORWARD_TARGET_PROTOCOL));
        }
        if ((messageType == AccessLogMessageType.FORWARD_FAILED) || (messageType == AccessLogMessageType.RESULT) || (messageType == AccessLogMessageType.ASSURANCE_COMPLETE)) {
            fieldMap.put(RESULT_CODE_VALUE.getFieldName(), new JSONNumber(DEFAULT_RESULT_CODE.intValue()));
            fieldMap.put(RESULT_CODE_NAME.getFieldName(), new JSONString(DEFAULT_RESULT_CODE.getName()));
            fieldMap.put(DIAGNOSTIC_MESSAGE.getFieldName(), new JSONString(DEFAULT_DIAGNOSTIC_MESSAGE));
        }
        if ((messageType == AccessLogMessageType.RESULT) || (messageType == AccessLogMessageType.ASSURANCE_COMPLETE) || (messageType == AccessLogMessageType.ENTRY) || (messageType == AccessLogMessageType.REFERENCE) || (messageType == AccessLogMessageType.INTERMEDIATE_RESPONSE)) {
            fieldMap.put(RESPONSE_CONTROL_OIDS.getFieldName(), createArray(DEFAULT_RESPONSE_CONTROL_OIDS));
        }
        if ((messageType == AccessLogMessageType.RESULT) || (messageType == AccessLogMessageType.ASSURANCE_COMPLETE)) {
            fieldMap.put(ADDITIONAL_INFO.getFieldName(), new JSONString(DEFAULT_ADDITIONAL_INFO_MESSAGE));
            fieldMap.put(MATCHED_DN.getFieldName(), new JSONString(DEFAULT_MATCHED_DN));
            fieldMap.put(REFERRAL_URLS.getFieldName(), createArray(DEFAULT_REFERRAL_URLS));
            fieldMap.put(SERVERS_ACCESSED.getFieldName(), createArray(DEFAULT_SERVERS_ACCESSED));
            fieldMap.put(UNCACHED_DATA_ACCESSED.getFieldName(), new JSONBoolean(DEFAULT_UNCACHED_DATA_ACCESSED));
            fieldMap.put(WORK_QUEUE_WAIT_TIME_MILLIS.getFieldName(), new JSONNumber(DEFAULT_WORK_QUEUE_WAIT_TIME_MILLIS));
            fieldMap.put(PROCESSING_TIME_MILLIS.getFieldName(), new JSONNumber(DEFAULT_PROCESSING_TIME_MILLIS));
            fieldMap.put(INTERMEDIATE_RESPONSES_RETURNED.getFieldName(), new JSONNumber(DEFAULT_INTERMEDIATE_RESPONSES_RETURNED));
            fieldMap.put(USED_PRIVILEGES.getFieldName(), createArray(DEFAULT_USED_PRIVILEGES));
            fieldMap.put(PRE_AUTHORIZATION_USED_PRIVILEGES.getFieldName(), createArray(DEFAULT_PRE_AUTHZ_USED_PRIVILEGES));
            fieldMap.put(MISSING_PRIVILEGES.getFieldName(), createArray(DEFAULT_MISSING_PRIVILEGES));
            if (operationType != AccessLogOperationType.ABANDON) {
                fieldMap.put(INTERMEDIATE_CLIENT_RESPONSE_CONTROL.getFieldName(), DEFAULT_INTERMEDIATE_CLIENT_RESPONSE.getControlObject());
            }
            if ((operationType == AccessLogOperationType.ADD) || (operationType == AccessLogOperationType.COMPARE) || (operationType == AccessLogOperationType.DELETE) || (operationType == AccessLogOperationType.MODIFY) || (operationType == AccessLogOperationType.MODDN) || (operationType == AccessLogOperationType.SEARCH)) {
                fieldMap.put(AUTHORIZATION_DN.getFieldName(), new JSONString(DEFAULT_AUTHZ_DN));
            }
            if ((operationType == AccessLogOperationType.ADD) || (operationType == AccessLogOperationType.DELETE) || (operationType == AccessLogOperationType.MODIFY) || (operationType == AccessLogOperationType.MODDN)) {
                fieldMap.put(REPLICATION_CHANGE_ID.getFieldName(), new JSONString(DEFAULT_REPLICATION_CHANGE_ID));
                fieldMap.put(ASSURED_REPLICATION_REQUIREMENTS.getFieldName(), createAssuredReplicationRequirements());
            }
            if ((operationType == AccessLogOperationType.ADD) || (operationType == AccessLogOperationType.DELETE) || (operationType == AccessLogOperationType.MODIFY) || (operationType == AccessLogOperationType.MODDN) || (operationType == AccessLogOperationType.SEARCH)) {
                fieldMap.put(INDEXES_WITH_KEYS_ACCESSED_NEAR_ENTRY_LIMIT.getFieldName(), createArray(DEFAULT_INDEXES_NEAR_ENTRY_LIMIT));
                fieldMap.put(INDEXES_WITH_KEYS_ACCESSED_EXCEEDING_ENTRY_LIMIT.getFieldName(), createArray(DEFAULT_INDEXES_EXCEEDING_ENTRY_LIMIT));
            }
        }
        if (messageType == AccessLogMessageType.ASSURANCE_COMPLETE) {
            fieldMap.put(LOCAL_ASSURANCE_SATISFIED.getFieldName(), new JSONBoolean(DEFAULT_LOCAL_ASSURANCE_SATISFIED));
            fieldMap.put(REMOTE_ASSURANCE_SATISFIED.getFieldName(), new JSONBoolean(DEFAULT_REMOTE_ASSURANCE_SATISFIED));
            fieldMap.put(SERVER_ASSURANCE_RESULTS.getFieldName(), new JSONArray(DEFAULT_ASSURED_REPLICATION_SERVER_RESULTS.get(0).getServerResultObject(), DEFAULT_ASSURED_REPLICATION_SERVER_RESULTS.get(1).getServerResultObject()));
        }
    }
    for (final JSONField field : fields) {
        fieldMap.put(field.getName(), field.getValue());
    }
    return new JSONObject(fieldMap);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONField(com.unboundid.util.json.JSONField) JSONNumber(com.unboundid.util.json.JSONNumber) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Example 4 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class JSONLogFieldSyntaxTestCase method testIncludeFields.

/**
 * Tests the behavior for the syntax when it is configured to only consider
 * a specified set of fields as sensitive, and therefore only those fields
 * will be redacted or tokenized when operating on components.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testIncludeFields() throws Exception {
    final Set<String> includeFields = StaticUtils.setOf("a", "b", "c");
    final JSONLogFieldSyntax syntax = new JSONLogFieldSyntax(10, includeFields, null);
    assertNotNull(syntax.getIncludedSensitiveFields());
    assertEquals(syntax.getIncludedSensitiveFields(), includeFields);
    assertNotNull(syntax.getExcludedSensitiveFields());
    assertTrue(syntax.getExcludedSensitiveFields().isEmpty());
    final JSONObject o = new JSONObject(new JSONField("a", "foo"), new JSONField("b", 5), new JSONField("c", true), new JSONField("d", "e"), new JSONField("f", new JSONObject(new JSONField("a", "g"), new JSONField("h", "i"))), new JSONField("j", new JSONArray(new JSONString("k"), new JSONObject(new JSONField("a", "l"), new JSONField("m", "n")))));
    assertNotNull(syntax.valueToSanitizedString(o));
    assertEquals(syntax.valueToSanitizedString(o), "{ \"a\":\"foo\", " + "\"b\":5, " + "\"c\":true, " + "\"d\":\"e\", " + "\"f\":{ \"a\":\"g\", \"h\":\"i\" }, " + "\"j\":[ \"k\", { \"a\":\"l\", \"m\":\"n\" } ] }");
    assertNotNull(syntax.redactComponents(o));
    assertEquals(syntax.redactComponents(o), "{ \"a\":\"{REDACTED}\", " + "\"b\":\"{REDACTED}\", " + "\"c\":\"{REDACTED}\", " + "\"d\":\"e\", " + "\"f\":{ \"a\":\"{REDACTED}\", \"h\":\"i\" }, " + "\"j\":[ \"k\", { \"a\":\"{REDACTED}\", \"m\":\"n\" } ] }");
    final byte[] pepper = StaticUtils.randomBytes(8, false);
    assertNotNull(syntax.tokenizeComponents(o, pepper));
    final JSONObject tokenizedObject = new JSONObject(syntax.tokenizeComponents(o, pepper));
    assertEquals(tokenizedObject.getFields().size(), 6);
    assertNotNull(tokenizedObject.getFieldAsString("a"));
    assertTrue(tokenizedObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("b"));
    assertTrue(tokenizedObject.getFieldAsString("b").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("c"));
    assertTrue(tokenizedObject.getFieldAsString("c").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedObject.getFieldAsString("d"));
    assertEquals(tokenizedObject.getFieldAsString("d"), "e");
    final JSONObject tokenizedFObject = tokenizedObject.getFieldAsObject("f");
    assertNotNull(tokenizedFObject);
    assertEquals(tokenizedFObject.getFields().size(), 2);
    assertNotNull(tokenizedFObject.getFieldAsString("a"));
    assertTrue(tokenizedFObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertNotNull(tokenizedFObject.getFieldAsString("h"));
    assertEquals(tokenizedFObject.getFieldAsString("h"), "i");
    final List<JSONValue> arrayElements = tokenizedObject.getFieldAsArray("j");
    assertNotNull(arrayElements);
    assertEquals(arrayElements.size(), 2);
    assertEquals(arrayElements.get(0), new JSONString("k"));
    final JSONObject arrayObject = (JSONObject) arrayElements.get(1);
    assertEquals(arrayObject.getFields().size(), 2);
    assertTrue(arrayObject.getFieldAsString("a").startsWith("{TOKENIZED:"));
    assertEquals(arrayObject.getFieldAsString("m"), "n");
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 5 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class JSONObjectFilter method getFilters.

/**
 * Retrieves the value of the specified field from the provided JSON object as
 * a list of JSON object filters.  The specified field must be a top-level
 * field in the JSON object and it must have a value that is an array of
 * JSON objects that represent valid JSON object filters.
 *
 * @param  o          The JSON object to examine.  It must not be
 *                    {@code null}.
 * @param  fieldName  The name of a top-level field in the JSON object that is
 *                    expected to have a value that is an array of JSON
 *                    objects that represent valid JSON object filters.  It
 *                    must not be {@code null}.
 *
 * @return  The list of JSON object filters retrieved from the JSON object.
 *
 * @throws  JSONException  If the object doesn't have the specified field, or
 *                         if the value of that field is not an array of
 *                         JSON objects that represent valid JSON object
 *                         filters.
 */
@NotNull()
protected List<JSONObjectFilter> getFilters(@NotNull final JSONObject o, @NotNull final String fieldName) throws JSONException {
    final JSONValue value = o.getField(fieldName);
    if (value == null) {
        throw new JSONException(ERR_OBJECT_FILTER_MISSING_REQUIRED_FIELD.get(String.valueOf(o), getFilterType(), fieldName));
    }
    if (!(value instanceof JSONArray)) {
        throw new JSONException(ERR_OBJECT_FILTER_VALUE_NOT_ARRAY.get(String.valueOf(o), getFilterType(), fieldName));
    }
    final List<JSONValue> values = ((JSONArray) value).getValues();
    final ArrayList<JSONObjectFilter> filterList = new ArrayList<>(values.size());
    for (final JSONValue arrayValue : values) {
        if (!(arrayValue instanceof JSONObject)) {
            throw new JSONException(ERR_OBJECT_FILTER_ARRAY_ELEMENT_NOT_OBJECT.get(String.valueOf(o), getFilterType(), fieldName));
        }
        final JSONObject filterObject = (JSONObject) arrayValue;
        try {
            filterList.add(decode(filterObject));
        } catch (final JSONException e) {
            Debug.debugException(e);
            throw new JSONException(ERR_OBJECT_FILTER_ARRAY_ELEMENT_NOT_FILTER.get(String.valueOf(o), getFilterType(), String.valueOf(filterObject), fieldName, e.getMessage()), e);
        }
    }
    return filterList;
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(com.unboundid.util.json.JSONException) NotNull(com.unboundid.util.NotNull)

Aggregations

JSONArray (com.unboundid.util.json.JSONArray)98 JSONObject (com.unboundid.util.json.JSONObject)89 JSONString (com.unboundid.util.json.JSONString)77 Test (org.testng.annotations.Test)72 JSONField (com.unboundid.util.json.JSONField)68 JSONValue (com.unboundid.util.json.JSONValue)27 JSONNumber (com.unboundid.util.json.JSONNumber)22 NotNull (com.unboundid.util.NotNull)20 ArrayList (java.util.ArrayList)19 LinkedHashMap (java.util.LinkedHashMap)18 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 PasswordQualityRequirement (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement)7 Entry (com.unboundid.ldap.sdk.Entry)6 Map (java.util.Map)6 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)5 JSONBoolean (com.unboundid.util.json.JSONBoolean)5 JSONException (com.unboundid.util.json.JSONException)5 Date (java.util.Date)4 List (java.util.List)4 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)3