Skip to content

Commit 908a988

Browse files
committed
Towards more search types
1 parent a0abe49 commit 908a988

File tree

3 files changed

+100
-22
lines changed

3 files changed

+100
-22
lines changed

src/main/java/io/tinyauth/elasticsearch/ActionIndicesAdaptor.java

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package io.tinyauth.elasticsearch;
1919

20-
import java.util.Arrays;
2120
import java.util.List;
2221
import java.util.Set;
2322
import java.util.HashSet;
@@ -27,6 +26,9 @@
2726
import java.lang.reflect.Method;
2827
import java.lang.reflect.InvocationTargetException;
2928

29+
import java.util.stream.Collectors;
30+
import java.util.stream.Stream;
31+
3032
import org.apache.logging.log4j.Logger;
3133

3234
import org.elasticsearch.action.ActionRequest;
@@ -49,33 +51,91 @@
4951

5052
public class ActionIndicesAdaptor {
5153
private static final Logger logger = Loggers.getLogger(ActionIndicesAdaptor.class);
52-
private ArrayList<Method> methods;
5354

54-
public ActionIndicesAdaptor() {
55-
methods = new ArrayList<Method>();
55+
private String partition;
56+
private String service;
57+
private String region;
5658

57-
for (Method m: this.getClass().getMethods()) {
58-
if (m.getName() != "extractIndices")
59-
continue;
59+
private List<Method> methods;
6060

61-
if (!m.getGenericReturnType().toString().equals("java.util.Set<java.lang.String>"))
62-
continue;
61+
public ActionIndicesAdaptor(String partition, String service, String region) {
62+
this.partition = partition;
63+
this.service = service;
64+
this.region = region;
6365

64-
logger.error(m);
65-
methods.add(m);
66-
}
67-
// Collections.sort(methods, methodComparator);
66+
this.methods = Stream.of(this.getClass().getMethods())
67+
.filter(m -> m.getName() == "extractIndices")
68+
.filter(m -> m.getParameterTypes().length == 1)
69+
.filter(m -> ActionRequest.class.isAssignableFrom(m.getParameterTypes()[0]))
70+
.filter(m -> m.getGenericReturnType().toString().equals("java.util.Set<java.lang.String>"))
71+
.sorted((left, right) -> {
72+
Class<?> leftType = left.getParameterTypes()[0];
73+
Class<?> rightType = right.getParameterTypes()[0];
74+
75+
if (leftType.isAssignableFrom(rightType))
76+
return 1;
77+
78+
if (rightType.isAssignableFrom(leftType))
79+
return -1;
80+
81+
return leftType.getName().compareTo(rightType.getName());
82+
})
83+
.collect(Collectors.toList());
84+
85+
logger.error(this.methods);
86+
}
87+
88+
private String formatArn(String resourceType, String resource) {
89+
return String.join(":",
90+
"arn",
91+
partition,
92+
service,
93+
region,
94+
"",
95+
resourceType + "/" + resource
96+
);
97+
}
98+
99+
public Set<String> extractIndices(MultiGetRequest req) {
100+
return Stream.of(req.getItems())
101+
.flatMap(ir -> Stream.of(ir.indices()))
102+
.map(idx -> formatArn("index", idx))
103+
.collect(Collectors.toSet());
104+
}
105+
106+
public Set<String> extractIndices(MultiSearchRequest req) {
107+
return Stream.of(req.requests())
108+
.flatMap(ir -> Stream.of(ir.indices()))
109+
.map(idx -> formatArn("index", idx))
110+
.collect(Collectors.toSet());
111+
}
112+
113+
/*public Set<String> extractIndices(MultiTermVectorsRequest req) {
114+
return Stream.of(req.getItems())
115+
.flatMap(ir -> Stream.of(ir.indices()))
116+
.map(idx -> formatArn("index", idx))
117+
.collect(Collectors.toSet());
118+
}*/
119+
120+
/*public Set<String> extractIndices(BulkRequest req) {
121+
return Stream.of(req.requests())
122+
.flatMap(ir -> Stream.of(getIndices(ir)))
123+
.collect(Collectors.toSet());
124+
}*/
125+
126+
public Set<String> extractIndices(DeleteRequest req) {
127+
return Stream.of(req.indices()).map(idx -> formatArn("index", idx)).collect(Collectors.toSet());
128+
}
129+
130+
public Set<String> extractIndices(IndexRequest req) {
131+
return Stream.of(req.indices()).map(idx -> formatArn("index", idx)).collect(Collectors.toSet());
68132
}
69133

70134
public Set<String> extractIndices(SearchRequest req) {
71-
logger.error("SearchRequest");
72-
Set<String> idxs = new HashSet<String>();
73-
Collections.addAll(idxs, req.indices());
74-
return idxs;
135+
return Stream.of(req.indices()).map(idx -> formatArn("index", idx)).collect(Collectors.toSet());
75136
}
76137

77138
public Set<String> extractIndices(ActionRequest req) {
78-
logger.error("ActionRequest");
79139
return new HashSet<String>();
80140
}
81141

@@ -98,12 +158,12 @@ public Set<String> getIndices(ActionRequest req) {
98158
} catch (NullPointerException e) {
99159
logger.error("NullPointerException");
100160
} catch (ExceptionInInitializerError e) {
101-
logger.error("NullPointerException");
161+
logger.error("ExceptionInInitializerError");
102162
}
103163
}
104164
}
105165

106-
logger.error("Unable to find adaptor for request");
166+
logger.error("Unable to find adaptor for request. This is a bug!");
107167
return new HashSet<String>();
108168
}
109169
}

src/main/java/io/tinyauth/elasticsearch/TinyauthActionFilter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@
9191
public class TinyauthActionFilter extends AbstractComponent implements ActionFilter {
9292

9393
private static final Logger logger = Loggers.getLogger(TinyauthActionFilter.class);
94-
private static final ActionIndicesAdaptor indexExtractor = new ActionIndicesAdaptor();
94+
private final ActionIndicesAdaptor indexExtractor;
9595

9696
private final ThreadPool threadPool;
9797

98+
private String partition;
9899
private String serviceName;
100+
private String region;
99101
private String endpoint;
100102
private String accessKeyId;
101103
private String secretAccessKey;
@@ -105,10 +107,14 @@ public TinyauthActionFilter(Settings settings, ThreadPool threadPool) {
105107
super(settings);
106108
this.threadPool = threadPool;
107109

110+
partition = settings.get("tinyauth.partition", "tinyauth");
108111
serviceName = settings.get("tinyauth.service_name", "es");
112+
region = settings.get("tinyauth.region", "default");
109113
endpoint = settings.get("tinyauth.endpoint");
110114
accessKeyId = settings.get("tinyauth.access_key_id");
111115
secretAccessKey = settings.get("tinyauth.secret_access_key");
116+
117+
indexExtractor = new ActionIndicesAdaptor(partition, serviceName, region);
112118
}
113119

114120
@Override
@@ -179,7 +185,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void app
179185
logger.error("IO error while building auth request for " + action);
180186
}
181187

182-
Unirest.post(endpoint + "v1/authorize")
188+
Unirest.post(endpoint + "v1/batch-authorize-by-token")
183189
.basicAuth(accessKeyId, secretAccessKey)
184190
.header("accept", "application/json")
185191
.header("content-type", "application/json")

src/main/java/io/tinyauth/elasticsearch/TinyauthPlugin.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@
3939

4040
public class TinyauthPlugin extends Plugin implements ActionPlugin {
4141

42+
public static final Setting<String> PARTITION_SETTING = Setting.simpleString(
43+
"tinyauth.partition",
44+
Property.NodeScope
45+
);
46+
4247
public static final Setting<String> SERVICE_NAME_SETTING = Setting.simpleString(
4348
"tinyauth.service_name",
4449
Property.NodeScope
4550
);
4651

52+
public static final Setting<String> REGION_SETTING = Setting.simpleString(
53+
"tinyauth.region",
54+
Property.NodeScope
55+
);
56+
4757
public static final Setting<String> ENDPOINT_SETTING = Setting.simpleString(
4858
"tinyauth.endpoint",
4959
Property.NodeScope
@@ -62,7 +72,9 @@ public class TinyauthPlugin extends Plugin implements ActionPlugin {
6272
@Override
6373
public List<Setting<?>> getSettings() {
6474
return Arrays.asList(
75+
PARTITION_SETTING,
6576
SERVICE_NAME_SETTING,
77+
REGION_SETTING,
6678
ENDPOINT_SETTING,
6779
ACCESS_KEY_ID_SETTING,
6880
SECRET_ACCESS_KEY_SETTING

0 commit comments

Comments
 (0)