|
12 | 12 | import static org.junit.jupiter.api.Assertions.assertTrue; |
13 | 13 | import static org.opensearch.commons.ConfigConstants.INJECTED_USER; |
14 | 14 | import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_INJECTED_ROLES; |
| 15 | +import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT; |
15 | 16 | import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USE_INJECTED_USER_FOR_PLUGINS; |
16 | 17 |
|
17 | 18 | import java.util.Arrays; |
18 | 19 | import java.util.HashMap; |
| 20 | +import java.util.List; |
19 | 21 |
|
20 | 22 | import org.junit.jupiter.api.Test; |
21 | 23 | import org.opensearch.common.settings.Settings; |
22 | 24 | import org.opensearch.common.util.concurrent.ThreadContext; |
| 25 | +import org.opensearch.commons.authuser.User; |
23 | 26 |
|
24 | 27 | public class InjectSecurityTest { |
25 | 28 |
|
@@ -85,6 +88,42 @@ public void testInjectUser() { |
85 | 88 | assertNull(threadContext.getTransient(INJECTED_USER)); |
86 | 89 | } |
87 | 90 |
|
| 91 | + @Test |
| 92 | + public void testInjectUserInfo() { |
| 93 | + Settings settings = Settings.builder().build(); |
| 94 | + Settings headerSettings = Settings.builder().put("request.headers.default", "1").build(); |
| 95 | + ThreadContext threadContext = new ThreadContext(headerSettings); |
| 96 | + threadContext.putHeader("name", "opendistro"); |
| 97 | + threadContext.putTransient("ctx.name", "plugin"); |
| 98 | + |
| 99 | + assertEquals("1", threadContext.getHeader("default")); |
| 100 | + assertEquals("opendistro", threadContext.getHeader("name")); |
| 101 | + assertEquals("plugin", threadContext.getTransient("ctx.name")); |
| 102 | + |
| 103 | + User user = new User( |
| 104 | + "Bob", |
| 105 | + List.of("backendRole1", "backendRole2"), |
| 106 | + List.of("role1", "role2"), |
| 107 | + List.of("attr1", "attr2"), |
| 108 | + "tenant1" |
| 109 | + ); |
| 110 | + try (InjectSecurity helper = new InjectSecurity("test-name", null, threadContext)) { |
| 111 | + helper.injectUserInfo(user); |
| 112 | + assertEquals("1", threadContext.getHeader("default")); |
| 113 | + assertEquals("opendistro", threadContext.getHeader("name")); |
| 114 | + assertEquals("plugin", threadContext.getTransient("ctx.name")); |
| 115 | + assertNotNull(threadContext.getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)); |
| 116 | + assertEquals( |
| 117 | + "Bob|backendRole1,backendRole2|role1,role2|tenant1", |
| 118 | + threadContext.getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT) |
| 119 | + ); |
| 120 | + } |
| 121 | + assertEquals("1", threadContext.getHeader("default")); |
| 122 | + assertEquals("opendistro", threadContext.getHeader("name")); |
| 123 | + assertEquals("plugin", threadContext.getTransient("ctx.name")); |
| 124 | + assertNull(threadContext.getTransient(OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT)); |
| 125 | + } |
| 126 | + |
88 | 127 | @Test |
89 | 128 | public void testInjectProperty() { |
90 | 129 | Settings settings = Settings.builder().put(OPENSEARCH_SECURITY_USE_INJECTED_USER_FOR_PLUGINS, false).build(); |
|
0 commit comments