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

Improve role cache efficiency for API key roles #58156

Merged
merged 39 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
390c499
Only partially deserialise api key doc during authentication
ywangd Jun 16, 2020
c353aef
Improve role cache efficiency and add bwc for API key auth metadata
ywangd Jun 16, 2020
46cf4fd
Fix tests
ywangd Jun 16, 2020
1319909
Update bwc version
ywangd Jun 16, 2020
b1688f9
checkstyle
ywangd Jun 16, 2020
0f9532b
Fix more tests
ywangd Jun 16, 2020
f1e1285
Add bwc for outgoing requests
ywangd Jun 16, 2020
9832f50
Add more tests for bwc and role cache reuse
ywangd Jun 17, 2020
d2560d8
Add mixed cluster bwc test
ywangd Jun 17, 2020
ca698d7
Apply suggestions from code review
ywangd Jul 1, 2020
cf785c9
fix variable name
ywangd Jul 1, 2020
abcd485
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 1, 2020
d62d8a2
Avoid using deprecated API
ywangd Jul 2, 2020
8148050
Update x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/s…
ywangd Jul 6, 2020
1440bfe
Address feedback
ywangd Jul 6, 2020
cfca9cc
Fix tests
ywangd Jul 6, 2020
50f1706
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 6, 2020
8fb443e
Update x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/s…
ywangd Jul 6, 2020
797c54b
Update method name change
ywangd Jul 6, 2020
552e7b3
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 6, 2020
3cf31f3
Address feedback
ywangd Jul 6, 2020
1300c4d
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 6, 2020
3531a52
Fix tests
ywangd Jul 7, 2020
9e7996d
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 7, 2020
27be381
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 7, 2020
a032325
A challenging merge
ywangd Jul 7, 2020
9395a83
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 8, 2020
0f08a3c
Address feedback
ywangd Jul 8, 2020
5b75e1d
Simplify creator parsing of ApiKeyDoc
ywangd Jul 12, 2020
bedb46f
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 12, 2020
52698f4
Fix merge
ywangd Jul 12, 2020
8543c18
Address feedback
ywangd Jul 13, 2020
c1fba5a
Address more feedback
ywangd Jul 13, 2020
0c991a6
Merge remote-tracking branch 'origin/master' into es-53939-api-key-ro…
ywangd Jul 13, 2020
0ac2a84
Address feedback
ywangd Jul 13, 2020
70307e7
checkstyle
ywangd Jul 13, 2020
8b1153c
Use new version constants where it is applicable
ywangd Jul 13, 2020
121134b
more checkstyle
ywangd Jul 13, 2020
2b20b48
Add one test for ApiKeyDoc.fromXContent
ywangd Jul 13, 2020
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 @@ -210,6 +210,12 @@ public void declareLong(BiConsumer<Value, Long> consumer, ParseField field) {
declareField(consumer, p -> p.longValue(), field, ValueType.LONG);
}

public void declareLongOrNull(BiConsumer<Value, Long> consumer, long nullValue, ParseField field) {
// Using a method reference here angers some compilers
declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.longValue(),
field, ValueType.LONG_OR_NULL);
}

public void declareInt(BiConsumer<Value, Integer> consumer, ParseField field) {
// Using a method reference here angers some compilers
declareField(consumer, p -> p.intValue(), field, ValueType.INT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import org.elasticsearch.ElasticsearchSecurityException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.node.Node;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.Authentication.AuthenticationType;
Expand All @@ -23,14 +26,22 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY;
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY;

/**
* A lightweight utility that can find the current user and authentication information for the local thread.
*/
public class SecurityContext {

public static final Version VERSION_API_KEY_ROLES_AS_BYTES = Version.V_7_9_0;

private final Logger logger = LogManager.getLogger(SecurityContext.class);

private final ThreadContext threadContext;
Expand Down Expand Up @@ -149,8 +160,27 @@ public void executeAfterRewritingAuthentication(Consumer<StoredContext> consumer
final Authentication authentication = getAuthentication();
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
setAuthentication(new Authentication(authentication.getUser(), authentication.getAuthenticatedBy(),
authentication.getLookedUpBy(), version, authentication.getAuthenticationType(), authentication.getMetadata()));
authentication.getLookedUpBy(), version, authentication.getAuthenticationType(),
rewriteMetadataForApiKeyRoleDescriptors(version, authentication)));
consumer.accept(original);
}
}

private Map<String, Object> rewriteMetadataForApiKeyRoleDescriptors(Version streamVersion, Authentication authentication) {
Map<String, Object> metadata = authentication.getMetadata();
if (authentication.getAuthenticationType() == AuthenticationType.API_KEY
&& authentication.getVersion().onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES)
&& streamVersion.before(VERSION_API_KEY_ROLES_AS_BYTES)) {
metadata = new HashMap<>(metadata);
metadata.put(
API_KEY_ROLE_DESCRIPTORS_KEY,
XContentHelper.convertToMap(
(BytesReference) metadata.get(API_KEY_ROLE_DESCRIPTORS_KEY), false, XContentType.JSON).v2());
metadata.put(
API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY,
XContentHelper.convertToMap(
(BytesReference) metadata.get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY), false, XContentType.JSON).v2());
}
return metadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
public final class AuthenticationField {

public static final String AUTHENTICATION_KEY = "_xpack_security_authentication";
public static final String API_KEY_ROLE_DESCRIPTORS_KEY = "_security_api_key_role_descriptors";
public static final String API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY = "_security_api_key_limited_by_role_descriptors";

private AuthenticationField() {}
}
Loading