forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opensearch-project#2616 preliminary implementation of getTokenManager…
… for extensions backwards compatibility using BWC_PLUGIN_MODE extension setting Signed-off-by: Sam <[email protected]>
- Loading branch information
1 parent
2fc41ed
commit f439e3d
Showing
3 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/org/opensearch/security/auth/SecurityTokenManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.security.auth; | ||
|
||
import org.greenrobot.eventbus.Subscribe; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.transport.TransportAddress; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.common.util.set.Sets; | ||
import org.opensearch.identity.tokens.AuthToken; | ||
import org.opensearch.identity.tokens.BasicAuthToken; | ||
import org.opensearch.identity.tokens.TokenManager; | ||
import org.opensearch.security.auditlog.AuditLog; | ||
import org.opensearch.security.http.XFFResolver; | ||
import org.opensearch.security.securityconf.ConfigModel; | ||
import org.opensearch.security.support.ConfigConstants; | ||
import org.opensearch.security.user.User; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.util.*; | ||
|
||
public class SecurityTokenManager implements TokenManager { | ||
|
||
Boolean extensionBwcCompatMode; | ||
User user; | ||
ConfigModel configModel; | ||
Set<String> mappedRoles; | ||
UserInjector userInjector; | ||
|
||
@Subscribe | ||
public void onConfigModelChanged(ConfigModel configModel) { | ||
this.configModel = configModel; | ||
} | ||
|
||
public SecurityTokenManager( | ||
ThreadContext threadContext, | ||
ThreadPool threadPool, | ||
final XFFResolver xffResolver, | ||
AuditLog auditLog, | ||
Settings settings | ||
) { | ||
this.userInjector = new UserInjector(settings, threadPool, auditLog, xffResolver); | ||
this.extensionBwcCompatMode = settings.getAsBoolean(ConfigConstants.EXTENSIONS_BWC_PLUGIN_MODE, ConfigConstants.EXTENSIONS_BWC_PLUGIN_MODE_DEFAULT); | ||
this.user = threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_USER); | ||
if (user == null) { | ||
user = userInjector.getInjectedUser(); | ||
} | ||
final TransportAddress caller = threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_REMOTE_ADDRESS); | ||
this.mappedRoles = configModel.mapSecurityRoles(user, caller); | ||
} | ||
|
||
@Override | ||
public AuthToken issueToken(String audience) { | ||
|
||
if (extensionBwcCompatMode) { | ||
StringJoiner joiner = new StringJoiner("|"); | ||
joiner.add(user.getName()); | ||
joiner.add(String.join(",", user.getRoles())); | ||
joiner.add(String.join(",", Sets.union(user.getSecurityRoles(), mappedRoles))); | ||
|
||
return new BasicAuthToken(joiner.toString() + "This is the Token including the encrypted backend roles"); | ||
} else { | ||
return new BasicAuthToken("This is standard Token without the roles"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters