-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
IdentityService.java
74 lines (64 loc) · 2.39 KB
/
IdentityService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.identity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchException;
import org.opensearch.common.annotation.InternalApi;
import org.opensearch.common.settings.Settings;
import org.opensearch.identity.noop.NoopIdentityPlugin;
import org.opensearch.identity.tokens.TokenManager;
import org.opensearch.plugins.IdentityAwarePlugin;
import org.opensearch.plugins.IdentityPlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.threadpool.ThreadPool;
import java.util.List;
import java.util.stream.Collectors;
/**
* Identity and access control for OpenSearch
*
* @opensearch.internal
* */
@InternalApi
public class IdentityService {
private static final Logger log = LogManager.getLogger(IdentityService.class);
private final Settings settings;
private final IdentityPlugin identityPlugin;
public IdentityService(final Settings settings, final ThreadPool threadPool, final List<IdentityPlugin> identityPlugins) {
this.settings = settings;
if (identityPlugins.size() == 0) {
log.debug("Identity plugins size is 0");
identityPlugin = new NoopIdentityPlugin(threadPool);
} else if (identityPlugins.size() == 1) {
log.debug("Identity plugins size is 1");
identityPlugin = identityPlugins.get(0);
} else {
throw new OpenSearchException(
"Multiple identity plugins are not supported, found: "
+ identityPlugins.stream().map(Object::getClass).map(Class::getName).collect(Collectors.joining(","))
);
}
}
/**
* Gets the current Subject
*/
public Subject getCurrentSubject() {
return identityPlugin.getCurrentSubject();
}
/**
* Gets the token manager
*/
public TokenManager getTokenManager() {
return identityPlugin.getTokenManager();
}
public void initializeIdentityAwarePlugins(final List<IdentityAwarePlugin> identityAwarePlugins) {
if (identityAwarePlugins != null) {
for (IdentityAwarePlugin plugin : identityAwarePlugins) {
PluginSubject pluginSubject = identityPlugin.getPluginSubject((Plugin) plugin);
plugin.assignSubject(pluginSubject);
}
}
}
}