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
+ }
0 commit comments