|
12 | 12 | import java.util.List; |
13 | 13 | import java.util.Map; |
14 | 14 | import java.util.Objects; |
15 | | -import java.util.function.UnaryOperator; |
16 | 15 | import org.apache.hadoop.conf.Configuration; |
17 | 16 | import org.apache.hadoop.security.GroupMappingServiceProvider; |
18 | 17 | import org.apache.hadoop.util.Lists; |
19 | 18 | import org.slf4j.Logger; |
20 | 19 | import org.slf4j.LoggerFactory; |
21 | 20 |
|
22 | 21 | public class StackableGroupMapper implements GroupMappingServiceProvider { |
| 22 | + |
| 23 | + private static final Logger LOG = LoggerFactory.getLogger(StackableGroupMapper.class); |
23 | 24 | public static final String OPA_MAPPING_URL_PROP = "hadoop.security.group.mapping.opa.url"; |
24 | | - public static final String OPA_MAPPING_GROUP_NAME_PROP = |
| 25 | + private static final String OPA_MAPPING_GROUP_NAME_PROP = |
25 | 26 | "hadoop.security.group.mapping.opa.list.name"; |
26 | 27 | // response base field: see https://www.openpolicyagent.org/docs/latest/rest-api/#response-message |
27 | | - public static final String OPA_RESULT_FIELD = "result"; |
28 | | - public final String mappingGroupName; |
29 | | - private final Logger LOG = LoggerFactory.getLogger(StackableGroupMapper.class); |
| 28 | + private static final String OPA_RESULT_FIELD = "result"; |
| 29 | + private final String mappingGroupName; |
| 30 | + |
30 | 31 | private final HttpClient httpClient = HttpClient.newHttpClient(); |
31 | 32 | private final ObjectMapper json; |
32 | | - private URI opaUri = null; |
| 33 | + private URI opaUri; |
33 | 34 |
|
34 | 35 | public enum HadoopConfig { |
35 | 36 | INSTANCE; |
36 | | - private Configuration configuration = new Configuration(); |
| 37 | + private final Configuration configuration = new Configuration(); |
| 38 | + |
37 | 39 | public Configuration getConfiguration() { |
38 | 40 | return this.configuration; |
39 | 41 | } |
@@ -83,11 +85,11 @@ public StackableGroupMapper() { |
83 | 85 | public List<String> getGroups(String user) throws IOException { |
84 | 86 | LOG.info("Calling StackableGroupMapper.getGroups for user [{}]", user); |
85 | 87 |
|
86 | | - HttpResponse<String> response = null; |
87 | 88 | OpaQuery query = new OpaQuery(new OpaQuery.OpaQueryInput(user)); |
88 | 89 | String body = json.writeValueAsString(query); |
89 | 90 |
|
90 | 91 | LOG.debug("Request body [{}]", body); |
| 92 | + HttpResponse<String> response = null; |
91 | 93 | try { |
92 | 94 | response = |
93 | 95 | httpClient.send( |
@@ -120,24 +122,23 @@ public List<String> getGroups(String user) throws IOException { |
120 | 122 | List<String> rawGroups = (List<String>) result.get(this.mappingGroupName); |
121 | 123 |
|
122 | 124 | for (String rawGroup : rawGroups) { |
123 | | - groups.add(stripSlashes.apply(rawGroup)); |
| 125 | + groups.add(stripSlashes(rawGroup)); |
124 | 126 | } |
125 | 127 |
|
126 | 128 | LOG.info("Groups for [{}]: [{}]", user, groups); |
127 | 129 |
|
128 | 130 | return groups; |
129 | 131 | } |
130 | 132 |
|
131 | | - private static final UnaryOperator<String> stripSlashes = |
132 | | - s -> { |
133 | | - if (s.startsWith("/")) { |
134 | | - s = s.substring(1); |
135 | | - } |
136 | | - if (s.endsWith("/")) { |
137 | | - s = s.substring(0, s.length() - 1); |
138 | | - } |
139 | | - return s; |
140 | | - }; |
| 133 | + private static String stripSlashes(String s) { |
| 134 | + if (s.startsWith("/")) { |
| 135 | + s = s.substring(1); |
| 136 | + } |
| 137 | + if (s.endsWith("/")) { |
| 138 | + s = s.substring(0, s.length() - 1); |
| 139 | + } |
| 140 | + return s; |
| 141 | + } |
141 | 142 |
|
142 | 143 | /** Caches groups, no need to do that for this provider */ |
143 | 144 | @Override |
|
0 commit comments