Skip to content

Commit

Permalink
fixup! More randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
jfreden committed Jan 11, 2024
1 parent dbd1b98 commit a053184
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1325,9 +1325,10 @@ private static void withBaseUpdateApiKeyFields(final XContentBuilder builder, fi
// because it replaces any metadata previously associated with the API key
builder.field("metadata", baseUpdateApiKeyRequest.getMetadata());
}
if (baseUpdateApiKeyRequest.getExpiration() != null) {
builder.field("expiration", baseUpdateApiKeyRequest.getExpiration());
}
builder.field(
"expiration",
baseUpdateApiKeyRequest.getExpiration() != null ? baseUpdateApiKeyRequest.getExpiration().toString() : null
);
}

private static void withRoleDescriptor(XContentBuilder builder, RoleDescriptor roleDescriptor) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException
CapturingLogger.output(logger.getName(), Level.INFO).clear();

final String keyId = randomAlphaOfLength(10);
final TimeValue newExpiration = ApiKeyTests.randomFutureExpirationTime();
final TimeValue newExpiration = randomFrom(ApiKeyTests.randomFutureExpirationTime(), null);
final var updateApiKeyRequest = new UpdateApiKeyRequest(
keyId,
randomBoolean() ? null : keyRoleDescriptors,
Expand All @@ -638,12 +638,12 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException
final var expectedUpdateKeyAuditEventString = String.format(
Locale.ROOT,
"""
"change":{"apikey":{"id":"%s","type":"rest"%s%s,"expiration":"%s"}}\
"change":{"apikey":{"id":"%s","type":"rest"%s%s,"expiration":%s}}\
""",
keyId,
updateApiKeyRequest.getRoleDescriptors() == null ? "" : "," + roleDescriptorsStringBuilder,
updateApiKeyRequest.getMetadata() == null ? "" : Strings.format(",\"metadata\":%s", metadataWithSerialization.serialization()),
updateApiKeyRequest.getExpiration()
updateApiKeyRequest.getExpiration() == null ? null : Strings.format("\"%s\"", newExpiration)
);
output = CapturingLogger.output(logger.getName(), Level.INFO);
assertThat(output.size(), is(2));
Expand Down Expand Up @@ -672,7 +672,7 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException
final var expectedBulkUpdateKeyAuditEventString = String.format(
Locale.ROOT,
"""
"change":{"apikeys":{"ids":[%s],"type":"rest"%s%s}}\
"change":{"apikeys":{"ids":[%s],"type":"rest"%s%s,"expiration":null}}\
""",
bulkUpdateApiKeyRequest.getIds().stream().map(s -> Strings.format("\"%s\"", s)).collect(Collectors.joining(",")),
bulkUpdateApiKeyRequest.getRoleDescriptors() == null ? "" : "," + roleDescriptorsStringBuilder,
Expand Down Expand Up @@ -877,7 +877,7 @@ public void testSecurityConfigChangeEventForCrossClusterApiKeys() throws IOExcep
updateMetadataWithSerialization = randomApiKeyMetadataWithSerialization();
}

final TimeValue newExpiration = ApiKeyTests.randomFutureExpirationTime();
final TimeValue newExpiration = randomFrom(ApiKeyTests.randomFutureExpirationTime(), null);
final var updateRequest = new UpdateCrossClusterApiKeyRequest(
createRequest.getId(),
updateAccess,
Expand All @@ -889,12 +889,12 @@ public void testSecurityConfigChangeEventForCrossClusterApiKeys() throws IOExcep
final String expectedUpdateAuditEventString = String.format(
Locale.ROOT,
"""
"change":{"apikey":{"id":"%s","type":"cross_cluster"%s%s,"expiration":"%s"}}\
"change":{"apikey":{"id":"%s","type":"cross_cluster"%s%s,"expiration":%s}}\
""",
createRequest.getId(),
updateAccess == null ? "" : ",\"role_descriptors\":" + accessWithSerialization.serialization(),
updateRequest.getMetadata() == null ? "" : Strings.format(",\"metadata\":%s", updateMetadataWithSerialization.serialization()),
newExpiration
newExpiration == null ? null : String.format("\"%s\"", newExpiration)
);

output = CapturingLogger.output(logger.getName(), Level.INFO);
Expand Down

0 comments on commit a053184

Please sign in to comment.