Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow null name when deserialising API key document #59485

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1030,11 +1030,11 @@ public static final class ApiKeyDoc {
builder.declareLong(constructorArg(), new ParseField("creation_time"));
builder.declareLongOrNull(constructorArg(), -1, new ParseField("expiration_time"));
builder.declareBoolean(constructorArg(), new ParseField("api_key_invalidated"));
builder.declareString(optionalConstructorArg(), new ParseField("api_key_hash"));
builder.declareString(constructorArg(), new ParseField("name"));
builder.declareString(constructorArg(), new ParseField("api_key_hash"));
builder.declareStringOrNull(optionalConstructorArg(), new ParseField("name"));
builder.declareInt(constructorArg(), new ParseField("version"));
ObjectParserHelper<ApiKeyDoc, Void> parserHelper = new ObjectParserHelper<>();
parserHelper.declareRawObject(builder, optionalConstructorArg(), new ParseField("role_descriptors"));
parserHelper.declareRawObject(builder, constructorArg(), new ParseField("role_descriptors"));
parserHelper.declareRawObject(builder, constructorArg(), new ParseField("limited_by_role_descriptors"));
builder.declareObject(constructorArg(), (p, c) -> p.map(), new ParseField("creator"));
PARSER = builder.build();
Expand All @@ -1044,11 +1044,10 @@ public static final class ApiKeyDoc {
final long creationTime;
final long expirationTime;
final Boolean invalidated;
@Nullable
final String hash;
@Nullable
final String name;
final int version;
@Nullable
final BytesReference roleDescriptorsBytes;
final BytesReference limitedByRoleDescriptorsBytes;
final Map<String, Object> creator;
Expand All @@ -1058,10 +1057,10 @@ public ApiKeyDoc(
long creationTime,
long expirationTime,
Boolean invalidated,
@Nullable String hash,
String name,
String hash,
@Nullable String name,
int version,
@Nullable BytesReference roleDescriptorsBytes,
BytesReference roleDescriptorsBytes,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed Nullable and replaced optionalConstructorArg() with constructorArg() for hash and roleDescriptorsBytes fields for clarity:

  • The parser does not allow them to be null without something like builder.declareStringOrNull(...). Therefore the Nullable etc does not have any effects anyway. In another word, the overall code logic is not impacted.
  • Based on the logic in ApiKeyService#newDocument, these two fields can never be null.

BytesReference limitedByRoleDescriptorsBytes,
Map<String, Object> creator) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -800,19 +800,19 @@ public void testCachedApiKeyValidationWillNotBeBlockedByUnCachedApiKey() throws

public void testApiKeyDocDeserialization() throws IOException {
final String apiKeyDocumentSource =
"{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":null,\"api_key_invalidated\":false," +
"{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":1591919944599,\"api_key_invalidated\":false," +
"\"api_key_hash\":\"{PBKDF2}10000$abc\",\"role_descriptors\":{\"a\":{\"cluster\":[\"all\"]}}," +
"\"limited_by_role_descriptors\":{\"limited_by\":{\"cluster\":[\"all\"]," +
"\"metadata\":{\"_reserved\":true},\"type\":\"role\"}}," +
"\"name\":\"key-1\",\"version\":7000099," +
"\"creator\":{\"principal\":\"admin\",\"metadata\":{\"foo\":\"bar\"},\"realm\":\"file1\",\"realm_type\":\"file\"}}\n";
"\"creator\":{\"principal\":\"admin\",\"metadata\":{\"foo\":\"bar\"},\"realm\":\"file1\",\"realm_type\":\"file\"}}";
final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent(XContentHelper.createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
new BytesArray(apiKeyDocumentSource),
XContentType.JSON));
assertEquals("api_key", apiKeyDoc.docType);
assertEquals(1591919944598L, apiKeyDoc.creationTime);
assertEquals(-1L, apiKeyDoc.expirationTime);
assertEquals(1591919944599L, apiKeyDoc.expirationTime);
assertFalse(apiKeyDoc.invalidated);
assertEquals("{PBKDF2}10000$abc", apiKeyDoc.hash);
assertEquals("key-1", apiKeyDoc.name);
Expand All @@ -828,6 +828,22 @@ public void testApiKeyDocDeserialization() throws IOException {
assertEquals("bar", ((Map<String, Object>)creator.get("metadata")).get("foo"));
}

public void testApiKeyDocDeserializationWithNullValues() throws IOException {
final String apiKeyDocumentSource =
"{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":null,\"api_key_invalidated\":false," +
"\"api_key_hash\":\"{PBKDF2}10000$abc\",\"role_descriptors\":{}," +
"\"limited_by_role_descriptors\":{\"limited_by\":{\"cluster\":[\"all\"]}}," +
"\"name\":null,\"version\":7000099," +
"\"creator\":{\"principal\":\"admin\",\"metadata\":{},\"realm\":\"file1\"}}";
final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent(XContentHelper.createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
new BytesArray(apiKeyDocumentSource),
XContentType.JSON));
assertEquals(-1L, apiKeyDoc.expirationTime);
assertNull(apiKeyDoc.name);
assertEquals(new BytesArray("{}"), apiKeyDoc.roleDescriptorsBytes);
}

public static class Utils {
private static final AuthenticationContextSerializer authenticationContextSerializer = new AuthenticationContextSerializer();

Expand Down