Skip to content

Commit

Permalink
Add non-null to store even non-default values in serialization (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithvm authored Aug 20, 2020
1 parent a0691eb commit 301e02e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.amazon.opendistroforelasticsearch.security.support.WildcardMatcher;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
Expand Down Expand Up @@ -80,6 +81,7 @@
* }
* }
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AuditConfig {

public static final List<String> DEFAULT_IGNORED_USERS = Collections.singletonList("kibanaserver");
Expand Down Expand Up @@ -126,6 +128,7 @@ public static AuditConfig from(final Settings settings) {
* Filter represents set of filtering configuration settings for audit logging.
* Audit logger will use these settings to determine what audit logs are to be generated.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class Filter {
@VisibleForTesting
public static final Filter DEFAULT = Filter.from(Settings.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
Expand Down Expand Up @@ -72,6 +73,7 @@
* Audit Logger uses this configuration to post compliance audit logs.
*/
@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ComplianceConfig {

private static final Logger log = LogManager.getLogger(ComplianceConfig.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,58 @@ private void testMap(final String patchResource, final Map<String, List<String>>
}
}

@Test
public void testPatchRequest() throws Exception {
setupWithRestRoles();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

// update with non-default configuration
AuditConfig auditConfig = new AuditConfig(true, AuditConfig.Filter.from(
ImmutableMap.<String, Object>builder()
.put("enable_rest", false)
.put("disabled_rest_categories", Collections.emptyList())
.put("enable_transport", false)
.put("disabled_transport_categories", Collections.emptyList())
.put("resolve_bulk_requests", false)
.put("resolve_indices", false)
.put("log_request_body", false)
.put("exclude_sensitive_headers", false)
.put("ignore_users", Collections.emptyList())
.put("ignore_requests", Collections.emptyList())
.build())
, ComplianceConfig.from(
ImmutableMap.<String, Object>builder()
.put("enabled", true)
.put("external_config", false)
.put("internal_config", false)
.put("read_metadata_only", false)
.put("read_watched_fields", Collections.emptyMap())
.put("read_ignore_users", Collections.emptyList())
.put("write_metadata_only", true)
.put("write_log_diffs", true)
.put("write_watched_indices", Collections.emptyList())
.put("write_ignore_users", Collections.emptyList())
.build(), Settings.EMPTY));
final String payload = DefaultObjectMapper.writeValueAsString(auditConfig, false);

// update config
RestHelper.HttpResponse response = rh.executePutRequest(CONFIG_ENDPOINT, payload);
assertEquals(HttpStatus.SC_OK, response.getStatusCode());

// make patch request
response = rh.executePatchRequest(ENDPOINT, "[{\"op\": \"add\",\"path\": \"" + "/config/enabled" + "\",\"value\": " + true + "}]");
assertEquals(HttpStatus.SC_OK, response.getStatusCode());

// get config
response = rh.executeGetRequest(ENDPOINT);
assertEquals(HttpStatus.SC_OK, response.getStatusCode());
final JsonNode configNode = readTree(response.getBody()).get("config");

// verify configs are same
assertEquals(readTree(payload), configNode);
}

private String getTestPayload() {
return "{" +
"\"enabled\":true," +
Expand Down

0 comments on commit 301e02e

Please sign in to comment.