Skip to content

Commit b9d5f9e

Browse files
committed
map input to json
1 parent 5815f1e commit b9d5f9e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<version>1.7.36</version>
3434
<scope>provided</scope>
3535
</dependency>
36+
<dependency>
37+
<groupId>com.fasterxml.jackson.core</groupId>
38+
<artifactId>jackson-databind</artifactId>
39+
<version>2.12.7.1</version>
40+
<scope>provided</scope>
41+
</dependency>
3642
<dependency>
3743
<groupId>junit</groupId>
3844
<artifactId>junit</artifactId>

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package tech.stackable.hadoop;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
36
import org.apache.hadoop.conf.Configuration;
47
import org.apache.hadoop.security.GroupMappingServiceProvider;
58
import org.apache.hadoop.util.Lists;
@@ -18,9 +21,32 @@ public class StackableGroupMapper implements GroupMappingServiceProvider {
1821
private final Logger LOG = LoggerFactory.getLogger(StackableGroupMapper.class);
1922
private final Configuration configuration;
2023
private final HttpClient httpClient = HttpClient.newHttpClient();
24+
private final ObjectMapper json;
2125

2226
public StackableGroupMapper() {
2327
this.configuration = new Configuration();
28+
this.json = new ObjectMapper()
29+
// https://github.com/stackabletech/trino-opa-authorizer/issues/24
30+
// OPA server can send other fields, such as `decision_id`` when enabling decision logs
31+
// We could add all the fields we *currently* know, but it's more future-proof to ignore any unknown fields.
32+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
33+
// do not include null values
34+
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
35+
}
36+
37+
private static class OpaQuery {
38+
public OpaQueryInput input;
39+
40+
public OpaQuery(OpaQueryInput input) {
41+
this.input = input;
42+
}
43+
}
44+
45+
public class OpaQueryInput {
46+
public final String username;
47+
public OpaQueryInput(String user) {
48+
this.username = user;
49+
}
2450
}
2551

2652
/**
@@ -42,7 +68,9 @@ public List<String> getGroups(String user) throws IOException {
4268
URI opaUri = URI.create(opaMappingUrl);
4369
HttpResponse<String> response = null;
4470

45-
String body = String.format("{\"input\":{\"username\": \"%s\"}}", user);
71+
OpaQuery query = new OpaQuery(new OpaQueryInput(user));
72+
String body = json.writeValueAsString(query);
73+
4674
LOG.info("Request body [{}]", body);
4775
try {
4876
response = httpClient.send(

0 commit comments

Comments
 (0)