Skip to content

Commit 4487bf4

Browse files
turettnshivangdoshi07
authored andcommitted
Handle attributes when impersonating user (#23)
* unit-test: make sure attributes work after REST impersonation See #22 for details. * Preserve attributes when impersonating user. When impersonating a user over the REST interface, attributes were being lost. This handles them the same way roles are handled.
1 parent 1f872c8 commit 4487bf4

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/main/java/com/amazon/opendistroforelasticsearch/security/auth/internal/InternalAuthenticationBackend.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.nio.charset.StandardCharsets;
3636
import java.util.Arrays;
3737
import java.util.Collections;
38+
import java.util.HashMap;
3839
import java.util.List;
3940

4041
import org.bouncycastle.crypto.generators.OpenBSDBCrypt;
@@ -88,6 +89,17 @@ public boolean exists(User user) {
8889
user.addRoles(roles);
8990
}
9091

92+
final Settings customAttributes = cfg.getAsSettings(user.getName() + ".attributes");
93+
HashMap<String, String> attributeMap = new HashMap<String, String>();
94+
95+
if(customAttributes != null) {
96+
for(String attributeName: customAttributes.names()) {
97+
attributeMap.put("attr.internal."+attributeName, customAttributes.get(attributeName));
98+
}
99+
}
100+
101+
user.addAttributes(attributeMap);
102+
91103
return true;
92104
}
93105

src/test/java/com/amazon/opendistroforelasticsearch/security/HttpIntegrationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public void testHTTPBasic() throws Exception {
248248
res = rh.executeGetRequest("/_opendistro/_security/authinfo", new BasicHeader("opendistro_security_impersonate_as","knuddel"), encodeBasicHeader("worf", "worf"));
249249
Assert.assertEquals(HttpStatus.SC_OK, res.getStatusCode());
250250
Assert.assertTrue(res.getBody().contains("name=knuddel"));
251+
Assert.assertTrue(res.getBody().contains("attr.internal.test1"));
251252
Assert.assertFalse(res.getBody().contains("worf"));
252253

253254
res = rh.executeGetRequest("/_opendistro/_security/authinfo", new BasicHeader("opendistro_security_impersonate_as","nonexists"), encodeBasicHeader("worf", "worf"));

src/test/resources/internal_users.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ logstash:
106106

107107
knuddel:
108108
hash: _imponly_
109+
attributes:
110+
test1: test2
109111

110112
twitter:
111113
hash: $2a$12$n5nubfWATfQjSYHiWtUyeOxMIxFInUHOAx8VMmGmxFNPGpaBmeB.m

0 commit comments

Comments
 (0)