Skip to content

Commit 45bacea

Browse files
Adding an utility method that allows consumers to set custom thread context property in InjectSecurity class (#47) (#70)
Signed-off-by: Ravi Thaluru <ravi1092@gmail.com> Co-authored-by: Ravi <6005951+thalurur@users.noreply.github.com>
1 parent 4750d1c commit 45bacea

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/main/java/org/opensearch/commons/InjectSecurity.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,23 @@ public void injectRoles(final List<String> roles) {
160160
}
161161
}
162162

163+
/**
164+
* Allows one to set the property in threadContext if possible to the value provided. If not possible returns false.
165+
* @param property
166+
* @param value
167+
* @return boolean
168+
*/
169+
public boolean injectProperty(final String property, final Object value) {
170+
if (Strings.isNullOrEmpty(property) || value == null || threadContext.getTransient(property) != null) {
171+
log.debug("{}, InjectSecurity - cannot inject property: {}", Thread.currentThread().getName(), id);
172+
return false;
173+
} else {
174+
threadContext.putTransient(property, value);
175+
log.debug("{}, InjectSecurity - inject property: {}", Thread.currentThread().getName(), id);
176+
return true;
177+
}
178+
}
179+
163180
@Override
164181
public void close() {
165182
if (ctx != null) {

src/test/java/org/opensearch/commons/InjectSecurityTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727
package org.opensearch.commons;
2828

2929
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertFalse;
3031
import static org.junit.jupiter.api.Assertions.assertNotNull;
3132
import static org.junit.jupiter.api.Assertions.assertNull;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
3234
import static org.opensearch.commons.ConfigConstants.INJECTED_USER;
3335
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_INJECTED_ROLES;
3436
import static org.opensearch.commons.ConfigConstants.OPENSEARCH_SECURITY_USE_INJECTED_USER_FOR_PLUGINS;
3537

3638
import java.util.Arrays;
39+
import java.util.Map;
3740

3841
import org.junit.jupiter.api.Test;
3942
import org.opensearch.common.settings.Settings;
@@ -102,4 +105,55 @@ public void testInjectUser() {
102105
assertEquals("plugin", threadContext.getTransient("ctx.name"));
103106
assertNull(threadContext.getTransient(INJECTED_USER));
104107
}
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+
}
105159
}

0 commit comments

Comments
 (0)