Skip to content

Commit 5711b41

Browse files
committed
some tests for ScriptRule
1 parent 47d1191 commit 5711b41

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

src/test/java/org/elasticsearch/rest/action/readonlyrest/acl/test/ACLTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@
2828

2929
public class ACLTest {
3030
private static ACL acl;
31+
3132
public static ACL mkACL(String fileName) {
32-
ACL _acl = null;
33+
Settings s = getSettings(fileName);
34+
return new ACL(s);
35+
}
36+
37+
static Settings getSettings(String fileName) {
3338
try {
3439
byte[] encoded = Files.readAllBytes(Paths.get(System.getProperty("user.dir") + fileName));
3540
String str = Charsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString();
36-
Settings s = Settings.builder().loadFromSource(str).build();
37-
_acl = new ACL(s);
41+
return Settings.builder().loadFromSource(str).build();
3842
} catch (IOException e) {
3943
e.printStackTrace();
44+
throw new Error();
4045
}
41-
return _acl;
46+
4247
}
4348

4449
@BeforeClass
@@ -50,6 +55,7 @@ public static RequestContext mockReq(String uri, String address, String apiKey,
5055
RestRequest r = mock(RestRequest.class, RETURNS_DEEP_STUBS);
5156
when(r.method()).thenReturn(method);
5257
when(r.uri()).thenReturn(uri);
58+
when(r.path()).thenReturn(uri);
5359
when(r.getRemoteAddress()).thenReturn(new InetSocketAddress(address, 80));
5460
when(r.header("X-Forwarded-For")).thenReturn(xForwardedForHeader);
5561
when(r.header("X-Api-Key")).thenReturn(apiKey);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package org.elasticsearch.rest.action.readonlyrest.acl.test;
2+
3+
import org.elasticsearch.common.settings.Settings;
4+
import org.elasticsearch.plugin.readonlyrest.acl.ACL;
5+
import org.elasticsearch.plugin.readonlyrest.acl.RequestContext;
6+
import org.elasticsearch.plugin.readonlyrest.acl.blocks.Block;
7+
import org.elasticsearch.plugin.readonlyrest.acl.blocks.BlockExitResult;
8+
import org.elasticsearch.rest.RestRequest;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertTrue;
14+
15+
public class ScriptACLTest {
16+
private static Settings s = null;
17+
private static RequestContext rc = null;
18+
19+
@BeforeClass
20+
public static void setUpBeforeClass() throws Throwable {
21+
s = ACLTest.getSettings("/src/test/script_test_rules.yml");
22+
rc = ACLTest.mockReq("/path", "1.1.1.1", "", "", 0, RestRequest.Method.PUT, null, new String[]{"index1"}, "action");
23+
24+
}
25+
26+
private static ACL setScript(String script) {
27+
s = Settings.builder().put(s).put("readonlyrest.access_control_rules.0.script", script).build();
28+
return new ACL(s);
29+
}
30+
31+
@Test
32+
public final void testActionIsRead() throws Throwable {
33+
BlockExitResult res = setScript("function onRequest(rc){\n" +
34+
" print('hello: ' + rc.toString());\n" +
35+
" if(" +
36+
" rc.getAction() == 'action'" +
37+
" ){ return true;} return false;}").check(rc);
38+
assertTrue(res.isMatch());
39+
assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
40+
assertEquals("1", res.getBlock().getName());
41+
}
42+
43+
@Test
44+
public final void testOAIsRead() throws Throwable {
45+
BlockExitResult res = setScript("function onRequest(rc){\n" +
46+
" print('hello: ' + rc.toString());\n" +
47+
" if(" +
48+
" rc.getRemoteAddress() == '1.1.1.1'" +
49+
" ){ return true;} return false;}").check(rc);
50+
assertTrue(res.isMatch());
51+
assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
52+
assertEquals("1", res.getBlock().getName());
53+
}
54+
55+
@Test
56+
public final void testIndicesIsRead() throws Throwable {
57+
BlockExitResult res = setScript("function onRequest(rc){\n" +
58+
" print('hello: ' + rc.toString());\n" +
59+
" if(" +
60+
" rc.getIndices().length == 1 && rc.getIndices()[0] == 'index1' " +
61+
" ){ return true;} return false;}").check(rc);
62+
assertTrue(res.isMatch());
63+
assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
64+
assertEquals("1", res.getBlock().getName());
65+
}
66+
67+
@Test
68+
public final void testMethodIsRead() throws Throwable {
69+
BlockExitResult res = setScript("function onRequest(rc){\n" +
70+
" print('hello: ' + rc.toString());\n" +
71+
" if(" +
72+
" rc.getRequest().method().toString() == 'PUT'" +
73+
" ){ return true;} return false;}").check(rc);
74+
assertTrue(res.isMatch());
75+
assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
76+
assertEquals("1", res.getBlock().getName());
77+
}
78+
79+
@Test
80+
public final void testPathIsRead() throws Throwable {
81+
BlockExitResult res = setScript("function onRequest(rc){\n" +
82+
" print('hello: ' + rc.toString());\n" +
83+
" if(" +
84+
" rc.getRequest().path() == '/path'" +
85+
" ){ return true;} return false;}").check(rc);
86+
assertTrue(res.isMatch());
87+
assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
88+
assertEquals("1", res.getBlock().getName());
89+
}
90+
91+
@Test
92+
public final void testContentIsRead() throws Throwable {
93+
BlockExitResult res = setScript("function onRequest(rc){\n" +
94+
" print('hello: ' + rc.toString());\n" +
95+
" if(" +
96+
" rc.getContent() == 'test'" +
97+
" ){ return true;} return false;}").check(rc);
98+
assertTrue(res.isMatch());
99+
assertTrue(res.getBlock().getPolicy() == Block.Policy.ALLOW);
100+
assertEquals("1", res.getBlock().getName());
101+
}
102+
103+
104+
105+
}

src/test/script_test_rules.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
readonlyrest:
2+
enable: true
3+
access_control_rules:
4+
5+
- name: 1
6+
type: allow

0 commit comments

Comments
 (0)