-
Notifications
You must be signed in to change notification settings - Fork 93
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
InjectSecurity - inject User object in UserInfo in threadContext #396
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,14 @@ | |
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USE_INJECTED_USER_FOR_PLUGINS; | ||
|
||
import java.util.List; | ||
import java.util.StringJoiner; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.Strings; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.commons.authuser.User; | ||
|
||
/** | ||
* For background jobs usage only. User or Roles injection can be done using transport layer only. | ||
|
@@ -104,7 +106,7 @@ public void inject(final String user, final List<String> roles) { | |
|
||
/** | ||
* Injects user. | ||
* @param user | ||
* @param user name | ||
*/ | ||
public void injectUser(final String user) { | ||
if (Strings.isNullOrEmpty(user)) { | ||
|
@@ -115,8 +117,27 @@ public void injectUser(final String user) { | |
threadContext.putTransient(INJECTED_USER, user); | ||
log.debug("{}, InjectSecurity - inject roles: {}", Thread.currentThread().getName(), id); | ||
} else { | ||
log.error("{}, InjectSecurity- most likely thread context corruption : {}", Thread.currentThread().getName(), id); | ||
log.error("{}, InjectSecurity - most likely thread context corruption : {}", Thread.currentThread().getName(), id); | ||
} | ||
} | ||
|
||
/** | ||
* Injects user object into user info. | ||
* @param user | ||
*/ | ||
public void injectUserInfo(final User user) { | ||
if (user == null) { | ||
return; | ||
} | ||
if (threadContext.getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT) != null) { | ||
log.error("{}, InjectSecurity - most likely thread context corruption : {}", Thread.currentThread().getName(), id); | ||
return; | ||
} | ||
StringJoiner joiner = new StringJoiner("|"); | ||
joiner.add(user.getName()); | ||
joiner.add(java.lang.String.join(",", user.getBackendRoles())); | ||
joiner.add(java.lang.String.join(",", user.getRoles())); | ||
Comment on lines
+146
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there already a method to reuse for combining these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't find one. ISM has one in SecurityUtils (generateUserString) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the source of where this ThreadContext header is populated from the security plugin: https://github.com/opensearch-project/security/blob/main/src/main/java/org/opensearch/security/privileges/PrivilegesEvaluator.java#L197-L209 This is done in the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cwperks thanks for checking on this. If there's any possible regression in the future, please help track it in some issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bowenlan-amzn No regression that I can see. I saw this PR and wanted to get familiar with what it was introducing. I thought I'd leave a fly-by comment for future reference in case anyone wants to know where this threadcontext header originally comes from. |
||
threadContext.putTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT, joiner.toString()); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we log in the error message the value present in
threadContext.getTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the error message may not be that useful.
Can you add in the comment of this method that this should only be used within plugin after stash the context and injecting user permission? I think this is the only use case of this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eirsep @bowenlan-amzn
Added comments, better logging and unit test