Skip to content

Commit 4c6b52d

Browse files
authored
Metadata of API key authentication must have roleDescriptor keys (#84203)
This PR removes conditional check for metadata keys related to API key role descriptors. API key authentication must always have these keys for it to work. The PR adds assertions for these keys and fixes relevant tests. Relates: #82639
1 parent 883b312 commit 4c6b52d

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -696,43 +696,39 @@ private static Map<String, Object> maybeRewriteMetadataForApiKeyRoleDescriptors(
696696
Map<String, Object> metadata = authentication.getMetadata();
697697
// If authentication type is API key, regardless whether it has run-as, the metadata must contain API key role descriptors
698698
if (authentication.isAuthenticatedWithApiKey()) {
699+
assert metadata.containsKey(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY)
700+
: "metadata must contain role descriptor for API key authentication";
701+
assert metadata.containsKey(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)
702+
: "metadata must contain limited role descriptor for API key authentication";
699703
if (authentication.getVersion().onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES)
700704
&& streamVersion.before(VERSION_API_KEY_ROLES_AS_BYTES)) {
701705
metadata = new HashMap<>(metadata);
702-
if (metadata.containsKey(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY)) {
706+
metadata.put(
707+
AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY,
708+
convertRoleDescriptorsBytesToMap((BytesReference) metadata.get(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY))
709+
);
710+
metadata.put(
711+
AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY,
712+
convertRoleDescriptorsBytesToMap(
713+
(BytesReference) metadata.get(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)
714+
)
715+
);
716+
} else if (authentication.getVersion().before(VERSION_API_KEY_ROLES_AS_BYTES)
717+
&& streamVersion.onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES)) {
718+
metadata = new HashMap<>(metadata);
703719
metadata.put(
704720
AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY,
705-
convertRoleDescriptorsBytesToMap((BytesReference) metadata.get(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY))
721+
convertRoleDescriptorsMapToBytes(
722+
(Map<String, Object>) metadata.get(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY)
723+
)
706724
);
707-
}
708-
if (metadata.containsKey(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)) {
709725
metadata.put(
710726
AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY,
711-
convertRoleDescriptorsBytesToMap(
712-
(BytesReference) metadata.get(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)
727+
convertRoleDescriptorsMapToBytes(
728+
(Map<String, Object>) metadata.get(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)
713729
)
714730
);
715731
}
716-
} else if (authentication.getVersion().before(VERSION_API_KEY_ROLES_AS_BYTES)
717-
&& streamVersion.onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES)) {
718-
metadata = new HashMap<>(metadata);
719-
if (metadata.containsKey(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY)) {
720-
metadata.put(
721-
AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY,
722-
convertRoleDescriptorsMapToBytes(
723-
(Map<String, Object>) metadata.get(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY)
724-
)
725-
);
726-
}
727-
if (metadata.containsKey(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)) {
728-
metadata.put(
729-
AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY,
730-
convertRoleDescriptorsMapToBytes(
731-
(Map<String, Object>) metadata.get(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)
732-
)
733-
);
734-
}
735-
}
736732
}
737733
return metadata;
738734
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.core.security.authc;
99

1010
import org.elasticsearch.Version;
11+
import org.elasticsearch.common.bytes.BytesArray;
1112
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1213
import org.elasticsearch.common.io.stream.StreamInput;
1314
import org.elasticsearch.common.settings.Settings;
@@ -414,6 +415,9 @@ public static Authentication randomApiKeyAuthentication(User user, String apiKey
414415
final HashMap<String, Object> metadata = new HashMap<>();
415416
metadata.put(AuthenticationField.API_KEY_ID_KEY, apiKeyId);
416417
metadata.put(AuthenticationField.API_KEY_NAME_KEY, randomBoolean() ? null : randomAlphaOfLengthBetween(1, 16));
418+
metadata.put(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY, new BytesArray("{}"));
419+
metadata.put(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, new BytesArray("""
420+
{"x":{"cluster":["all"],"indices":[{"names":["index*"],"privileges":["all"]}]}}"""));
417421
return Authentication.newApiKeyAuthentication(AuthenticationResult.success(user, metadata), randomAlphaOfLengthBetween(3, 8))
418422
.maybeRewriteForOlderVersion(version);
419423
}

0 commit comments

Comments
 (0)