Skip to content

Commit 694781e

Browse files
skkosuri-amznadityaj1107VachaShahdblockbowenlan-amzn
authored andcommitted
Backport 1.1 changes to 1.x (opensearch-project#72)
* Add Commits related to Snapshot build of Common Utils on 1.1 (opensearch-project#67) * Using 1.1 snapshot version for OpenSearch (opensearch-project#57) Signed-off-by: Vacha <vachshah@amazon.com> * Build snapshot build by default with the same version as OpenSearch. (opensearch-project#58) Signed-off-by: dblock <dblock@amazon.com> * Update build.gradle to reflect 1.1.0.0 version Co-authored-by: Vacha <vachshah@amazon.com> Co-authored-by: Daniel Doubrovkine (dB.) <dblock@dblock.org> * Build snapshot build by default with the same version as OpenSearch. (opensearch-project#58) (opensearch-project#69) Signed-off-by: dblock <dblock@amazon.com> * Adding an utility method that allows consumers to set custom thread context property in InjectSecurity class (opensearch-project#47) (opensearch-project#70) Signed-off-by: Ravi Thaluru <ravi1092@gmail.com> Co-authored-by: Ravi <6005951+thalurur@users.noreply.github.com> * Add release notes for version 1.1.0.0 * Add release notes for version 1.1.0.0 Co-authored-by: Aditya Jindal <13850971+aditjind@users.noreply.github.com> Co-authored-by: Vacha <vachshah@amazon.com> Co-authored-by: Daniel Doubrovkine (dB.) <dblock@dblock.org> Co-authored-by: Bowen Lan <62091230+bowenlan-amzn@users.noreply.github.com> Co-authored-by: Ravi <6005951+thalurur@users.noreply.github.com> Signed-off-by: AWSHurneyt <hurneyt@amazon.com>
1 parent 800c0d5 commit 694781e

File tree

5 files changed

+94
-7
lines changed

5 files changed

+94
-7
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ jobs:
3434
ref: '1.x'
3535
- name: Build OpenSearch
3636
working-directory: ./OpenSearch
37-
run: ./gradlew publishToMavenLocal -Dbuild.snapshot=false
37+
run: ./gradlew publishToMavenLocal
3838

3939
# common-utils
4040
- name: Build and Test
4141
run: |
42-
./gradlew build -Dopensearch.version=1.1.0
42+
./gradlew build -Dopensearch.version=1.1.0-SNAPSHOT
4343
4444
- name: Publish to Maven Local
4545
run: |
46-
./gradlew publishToMavenLocal -Dopensearch.version=1.1.0
46+
./gradlew publishToMavenLocal -Dopensearch.version=1.1.0-SNAPSHOT
4747
4848
- name: Upload Coverage Report
4949
uses: codecov/codecov-action@v1

build.gradle

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
buildscript {
1313
ext {
1414
opensearch_group = "org.opensearch"
15-
opensearch_version = System.getProperty("opensearch.version", "1.1.0")
15+
opensearch_version = System.getProperty("opensearch.version", "1.1.0-SNAPSHOT")
1616
kotlin_version = System.getProperty("kotlin.version", "1.4.32")
1717
}
1818

@@ -44,7 +44,17 @@ repositories {
4444
jcenter()
4545
}
4646

47-
group 'org.opensearch.commons'
47+
ext {
48+
isSnapshot = "true" == System.getProperty("build.snapshot", "true")
49+
}
50+
51+
allprojects {
52+
group 'org.opensearch.commons'
53+
version = opensearch_version - '-SNAPSHOT' + '.0'
54+
if (isSnapshot) {
55+
version += "-SNAPSHOT"
56+
}
57+
}
4858

4959
sourceCompatibility = 1.8
5060

@@ -146,8 +156,6 @@ task javadocJar(type: Jar) {
146156
from javadoc.destinationDir
147157
}
148158

149-
version '1.1.0.0'
150-
151159
publishing {
152160
publications {
153161
shadow(MavenPublication) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Version 1.1.0.0 Release Notes
2+
3+
Compatible with OpenSearch 1.1.0
4+
5+
### Enhancements
6+
7+
* Adding an utility method that allows consumers to set custom thread context property in InjectSecurity class ([#70](https://github.com/opensearch-project/common-utils/pull/70))
8+

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)