Skip to content

Commit e6d2d60

Browse files
authored
use fullname where this can be extracted: add google format version (#6)
1 parent 0c93fa7 commit e6d2d60

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
<cleanthat.version>2.17</cleanthat.version>
3939
<error-prone.version>2.24.1</error-prone.version>
40+
<google-java-format.version>1.19.2</google-java-format.version>
4041

4142
<maven-clean-plugin.version>3.3.2</maven-clean-plugin.version>
4243
<maven-compiler-plugin.version>3.12.1</maven-compiler-plugin.version>

src/main/java/tech/stackable/hadoop/StackableGroupMapper.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,23 @@
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.databind.DeserializationFeature;
66
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import java.io.IOException;
78
import java.net.URI;
89
import java.net.http.HttpClient;
910
import java.net.http.HttpRequest;
1011
import java.net.http.HttpResponse;
11-
import java.util.HashMap;
1212
import java.util.List;
13-
import java.util.Map;
1413
import java.util.Objects;
15-
16-
import com.fasterxml.jackson.databind.type.TypeFactory;
1714
import org.apache.hadoop.conf.Configuration;
1815
import org.apache.hadoop.security.GroupMappingServiceProvider;
16+
import org.apache.hadoop.security.UserGroupInformation;
1917
import org.slf4j.Logger;
2018
import org.slf4j.LoggerFactory;
2119

2220
public class StackableGroupMapper implements GroupMappingServiceProvider {
2321

24-
private static final Logger LOG = LoggerFactory.getLogger(StackableGroupMapper.class);
25-
2622
public static final String OPA_MAPPING_URL_PROP = "hadoop.security.group.mapping.opa.policy.url";
27-
// response base field: see https://www.openpolicyagent.org/docs/latest/rest-api/#response-message
28-
private static final String OPA_RESULT_FIELD = "result";
29-
23+
private static final Logger LOG = LoggerFactory.getLogger(StackableGroupMapper.class);
3024
private final HttpClient httpClient = HttpClient.newHttpClient();
3125
private final ObjectMapper json;
3226
private URI opaUri;
@@ -59,21 +53,36 @@ public StackableGroupMapper() {
5953
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
6054
}
6155

62-
private static class OpaQueryResult {
63-
public List<String> result;
64-
}
65-
6656
/**
67-
* Returns list of groups for a user.
57+
* Returns list of groups for a user. Internally Hadoop will pass the short name to this function,
58+
* but this prevents us from effectively separating users with the same names but with different
59+
* kerberos principals. For this reason the user name is extracted from the UserGroupInformation
60+
* instead (giving us the full name), defaulting to the original name if this is not possible.
6861
*
69-
* @param user get groups for this user
62+
* @param user get groups from the associated user group information for this user
7063
* @return list of groups for a given user
7164
*/
7265
@Override
7366
public List<String> getGroups(String user) {
7467
LOG.info("Calling StackableGroupMapper.getGroups for user \"{}\"", user);
7568

76-
OpaGroupsQuery query = new OpaGroupsQuery(new OpaGroupsQuery.OpaGroupsQueryInput(user));
69+
String workingUser = user;
70+
try {
71+
UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();
72+
LOG.debug(
73+
"Current user [{}] with user-name [{}] and short-name [{}]",
74+
currentUser,
75+
currentUser.getUserName(),
76+
currentUser.getShortUserName());
77+
workingUser = currentUser.getUserName();
78+
} catch (IOException e) {
79+
LOG.warn(
80+
"Unable to extract name from UserGroupInformation, defaulting to \"{}\": {}",
81+
user,
82+
e.getMessage());
83+
}
84+
85+
OpaGroupsQuery query = new OpaGroupsQuery(new OpaGroupsQuery.OpaGroupsQueryInput(workingUser));
7786

7887
String body;
7988
try {
@@ -115,7 +124,7 @@ public List<String> getGroups(String user) {
115124
}
116125
List<String> groups = result.result;
117126

118-
LOG.debug("Groups for \"{}\": {}", user, groups);
127+
LOG.debug("Groups for \"{}\": {}", workingUser, groups);
119128

120129
return groups;
121130
}
@@ -139,4 +148,8 @@ public void cacheGroupsAdd(List<String> groups) {
139148
"ignoring cacheGroupsAdd for groups [{}]: caching should be provided by the policy provider",
140149
groups);
141150
}
151+
152+
private static class OpaQueryResult {
153+
public List<String> result;
154+
}
142155
}

0 commit comments

Comments
 (0)