|  | 
| 27 | 27 | package org.opensearch.commons; | 
| 28 | 28 | 
 | 
| 29 | 29 | import static org.junit.jupiter.api.Assertions.assertEquals; | 
|  | 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; | 
| 30 | 31 | import static org.junit.jupiter.api.Assertions.assertNotNull; | 
| 31 | 32 | import static org.junit.jupiter.api.Assertions.assertNull; | 
|  | 33 | +import static org.junit.jupiter.api.Assertions.assertTrue; | 
| 32 | 34 | import static org.opensearch.commons.ConfigConstants.INJECTED_USER; | 
| 33 | 35 | import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_INJECTED_ROLES; | 
| 34 | 36 | import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USE_INJECTED_USER_FOR_PLUGINS; | 
| 35 | 37 | 
 | 
| 36 | 38 | import java.util.Arrays; | 
|  | 39 | +import java.util.Map; | 
| 37 | 40 | 
 | 
| 38 | 41 | import org.junit.jupiter.api.Test; | 
| 39 | 42 | import org.opensearch.common.settings.Settings; | 
| @@ -102,4 +105,55 @@ public void testInjectUser() { | 
| 102 | 105 |         assertEquals("plugin", threadContext.getTransient("ctx.name")); | 
| 103 | 106 |         assertNull(threadContext.getTransient(INJECTED_USER)); | 
| 104 | 107 |     } | 
|  | 108 | + | 
|  | 109 | +    @Test | 
|  | 110 | +    public void testInjectProperty() { | 
|  | 111 | +        Settings settings = Settings.builder().put(OPENSEARCH_SECURITY_USE_INJECTED_USER_FOR_PLUGINS, false).build(); | 
|  | 112 | +        Settings headerSettings = Settings.builder().put("request.headers.default", "1").build(); | 
|  | 113 | +        ThreadContext threadContext = new ThreadContext(headerSettings); | 
|  | 114 | +        threadContext.putHeader("name", "opendistro"); | 
|  | 115 | +        threadContext.putTransient("ctx.name", "plugin"); | 
|  | 116 | + | 
|  | 117 | +        assertEquals("1", threadContext.getHeader("default")); | 
|  | 118 | +        assertEquals("opendistro", threadContext.getHeader("name")); | 
|  | 119 | +        assertEquals("plugin", threadContext.getTransient("ctx.name")); | 
|  | 120 | + | 
|  | 121 | +        try (InjectSecurity helper = new InjectSecurity("test-name", settings, threadContext)) { | 
|  | 122 | +            helper.inject("joe", Arrays.asList("ops-role", "logs-role")); | 
|  | 123 | +            assertEquals("1", threadContext.getHeader("default")); | 
|  | 124 | +            assertEquals("opendistro", threadContext.getHeader("name")); | 
|  | 125 | +            assertEquals("plugin", threadContext.getTransient("ctx.name")); | 
|  | 126 | +            assertNotNull(threadContext.getTransient(OPENSEARCH_SECURITY_INJECTED_ROLES)); | 
|  | 127 | +            // cannot inject property that is already set | 
|  | 128 | +            assertFalse(helper.injectProperty(OPENSEARCH_SECURITY_INJECTED_ROLES, "new value")); | 
|  | 129 | +            assertEquals("plugin|ops-role,logs-role", threadContext.getTransient(OPENSEARCH_SECURITY_INJECTED_ROLES)); | 
|  | 130 | +            // cannot inject invalid property/value | 
|  | 131 | +            assertFalse(helper.injectProperty("", "new value")); | 
|  | 132 | +            assertFalse(helper.injectProperty(null, "new value")); | 
|  | 133 | +            assertFalse(helper.injectProperty("property", null)); | 
|  | 134 | +            // can inject non-set valid properties | 
|  | 135 | +            assertTrue(helper.injectProperty("property1", true)); | 
|  | 136 | +            assertTrue(helper.injectProperty("property2", "some value")); | 
|  | 137 | +            assertTrue(helper.injectProperty("property3", "")); | 
|  | 138 | +            assertTrue(helper.injectProperty("property4", Map.of("key", "value"))); | 
|  | 139 | +            // verify the set properties are not null and equal to what was set | 
|  | 140 | +            assertNull(threadContext.getTransient("property")); | 
|  | 141 | +            assertNotNull(threadContext.getTransient("property1")); | 
|  | 142 | +            assertEquals(true, threadContext.getTransient("property1")); | 
|  | 143 | +            assertNotNull(threadContext.getTransient("property2")); | 
|  | 144 | +            assertEquals("some value", threadContext.getTransient("property2")); | 
|  | 145 | +            assertNotNull(threadContext.getTransient("property3")); | 
|  | 146 | +            assertEquals("", threadContext.getTransient("property3")); | 
|  | 147 | +            assertNotNull(threadContext.getTransient("property4")); | 
|  | 148 | +            assertEquals(Map.of("key", "value"), threadContext.getTransient("property4")); | 
|  | 149 | +        } | 
|  | 150 | +        assertEquals("1", threadContext.getHeader("default")); | 
|  | 151 | +        assertEquals("opendistro", threadContext.getHeader("name")); | 
|  | 152 | +        assertEquals("plugin", threadContext.getTransient("ctx.name")); | 
|  | 153 | +        assertNull(threadContext.getTransient(OPENSEARCH_SECURITY_INJECTED_ROLES)); | 
|  | 154 | +        assertNull(threadContext.getTransient("property1")); | 
|  | 155 | +        assertNull(threadContext.getTransient("property2")); | 
|  | 156 | +        assertNull(threadContext.getTransient("property3")); | 
|  | 157 | +        assertNull(threadContext.getTransient("property4")); | 
|  | 158 | +    } | 
| 105 | 159 | } | 
0 commit comments