|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.xpack.core.security.authz.privilege;
|
7 | 7 |
|
| 8 | +import junit.framework.AssertionFailedError; |
8 | 9 | import org.apache.lucene.util.automaton.CharacterRunAutomaton;
|
9 | 10 | import org.elasticsearch.common.util.set.Sets;
|
10 | 11 | import org.elasticsearch.test.ESTestCase;
|
|
17 | 18 | import java.util.Locale;
|
18 | 19 | import java.util.Map;
|
19 | 20 | import java.util.Set;
|
| 21 | +import java.util.function.Supplier; |
20 | 22 |
|
21 | 23 | import static org.elasticsearch.common.Strings.collectionToCommaDelimitedString;
|
22 | 24 | import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
|
|
27 | 29 | public class ApplicationPrivilegeTests extends ESTestCase {
|
28 | 30 |
|
29 | 31 | public void testValidationOfApplicationName() {
|
30 |
| - // too short |
31 |
| - assertValidationFailure("Application names", () -> ApplicationPrivilege.validateApplicationName("ap")); |
32 |
| - // must start with lowercase |
33 |
| - assertValidationFailure("Application names", () -> ApplicationPrivilege.validateApplicationName("App")); |
34 |
| - // must start with letter |
35 |
| - assertValidationFailure("Application names", () -> ApplicationPrivilege.validateApplicationName("1app")); |
36 |
| - // cannot contain special characters |
37 |
| - assertValidationFailure("Application names", |
38 |
| - () -> ApplicationPrivilege.validateApplicationName("app" + randomFrom(":;$#%()+=/'.,".toCharArray()))); |
| 32 | + final String specialCharacters = ":;$#%()+='.{}[]!@^&'"; |
| 33 | + final Supplier<Character> specialCharacter = () -> specialCharacters.charAt(randomInt(specialCharacters.length() - 1)); |
| 34 | + |
| 35 | + assertValidationFailure("a p p", "application name", () -> ApplicationPrivilege.validateApplicationName("a p p")); |
| 36 | + assertValidationFailure("ap", "application name", () -> ApplicationPrivilege.validateApplicationName("ap")); |
| 37 | + for (String app : Arrays.asList( |
| 38 | + "App",// must start with lowercase |
| 39 | + "1app", // must start with letter |
| 40 | + "app" + specialCharacter.get() // cannot contain special characters unless preceded by a "-" or "_" |
| 41 | + )) { |
| 42 | + assertValidationFailure(app, "application name", () -> ApplicationPrivilege.validateApplicationName(app)); |
| 43 | + assertValidationFailure(app, "application name", () -> ApplicationPrivilege.validateApplicationNameOrWildcard(app)); |
| 44 | + } |
39 | 45 |
|
40 | 46 | // no wildcards
|
41 |
| - assertValidationFailure("Application names", () -> ApplicationPrivilege.validateApplicationName("app*")); |
| 47 | + assertValidationFailure("app*", "application names", () -> ApplicationPrivilege.validateApplicationName("app*")); |
42 | 48 | // no special characters with wildcards
|
43 |
| - assertValidationFailure("Application names", |
44 |
| - () -> ApplicationPrivilege.validateApplicationNameOrWildcard("app" + randomFrom((":;$#%()+=/'.,").toCharArray()) + "*")); |
| 49 | + final String appNameWithSpecialCharAndWildcard = "app" + specialCharacter.get() + "*"; |
| 50 | + assertValidationFailure(appNameWithSpecialCharAndWildcard, "application name", |
| 51 | + () -> ApplicationPrivilege.validateApplicationNameOrWildcard(appNameWithSpecialCharAndWildcard)); |
45 | 52 |
|
| 53 | + String appNameWithSpecialChars = "myapp" + randomFrom('-', '_'); |
| 54 | + for (int i = randomIntBetween(1, 12); i > 0; i--) { |
| 55 | + appNameWithSpecialChars = appNameWithSpecialChars + specialCharacter.get(); |
| 56 | + } |
46 | 57 | // these should all be OK
|
47 |
| - assertNoException(() -> ApplicationPrivilege.validateApplicationName("app")); |
48 |
| - assertNoException(() -> ApplicationPrivilege.validateApplicationName("app1")); |
49 |
| - assertNoException(() -> ApplicationPrivilege.validateApplicationName("myApp")); |
50 |
| - assertNoException(() -> ApplicationPrivilege.validateApplicationName("my-App")); |
51 |
| - assertNoException(() -> ApplicationPrivilege.validateApplicationName("my_App")); |
52 |
| - assertNoException(() -> ApplicationPrivilege.validateApplicationNameOrWildcard("app*")); |
| 58 | + for (String app : Arrays.asList("app", "app1", "myApp", "myApp-:;$#%()+='.", "myApp_:;$#%()+='.", appNameWithSpecialChars)) { |
| 59 | + assertNoException(app, () -> ApplicationPrivilege.validateApplicationName(app)); |
| 60 | + assertNoException(app, () -> ApplicationPrivilege.validateApplicationNameOrWildcard(app)); |
| 61 | + } |
53 | 62 | }
|
54 | 63 |
|
55 | 64 | public void testValidationOfPrivilegeName() {
|
56 | 65 | // must start with lowercase
|
57 |
| - assertValidationFailure("privilege names", () -> ApplicationPrivilege.validatePrivilegeName("Read")); |
| 66 | + assertValidationFailure("Read", "privilege names", () -> ApplicationPrivilege.validatePrivilegeName("Read")); |
58 | 67 | // must start with letter
|
59 |
| - assertValidationFailure("privilege names", () -> ApplicationPrivilege.validatePrivilegeName("1read")); |
| 68 | + assertValidationFailure("1read", "privilege names", () -> ApplicationPrivilege.validatePrivilegeName("1read")); |
60 | 69 | // cannot contain special characters
|
61 |
| - final String withSpecialChar = "read" + randomFrom(":;$#%()+=/',".toCharArray()); |
62 |
| - assertValidationFailure("privilege names", () -> ApplicationPrivilege.validatePrivilegeName(withSpecialChar)); |
| 70 | + final String specialChars = ":;$#%()+=/',"; |
| 71 | + final String withSpecialChar = "read" + specialChars.charAt(randomInt(specialChars.length()-1)); |
| 72 | + assertValidationFailure(withSpecialChar, "privilege names", () -> ApplicationPrivilege.validatePrivilegeName(withSpecialChar)); |
63 | 73 |
|
64 | 74 | // these should all be OK
|
65 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeName("read")); |
66 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeName("read1")); |
67 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeName("readData")); |
68 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeName("read-data")); |
69 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeName("read.data")); |
70 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeName("read_data")); |
71 |
| - |
72 |
| - assertValidationFailure("privilege names and action", () -> ApplicationPrivilege.validatePrivilegeOrActionName("r e a d")); |
73 |
| - assertValidationFailure("privilege names and action", () -> ApplicationPrivilege.validatePrivilegeOrActionName("read\n")); |
74 |
| - assertValidationFailure("privilege names and action", () -> ApplicationPrivilege.validatePrivilegeOrActionName("copy®")); |
75 |
| - |
76 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("read")); |
77 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("read1")); |
78 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("readData")); |
79 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("read-data")); |
80 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("read.data")); |
81 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("read_data")); |
82 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("read:*")); |
83 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("read/*")); |
84 |
| - assertNoException(() -> ApplicationPrivilege.validatePrivilegeOrActionName("read/a_b.c-d+e%f#(g)")); |
| 75 | + for (String priv : Arrays.asList("read", "read1", "readData", "read-data", "read.data", "read_data")) { |
| 76 | + assertNoException(priv, () -> ApplicationPrivilege.validatePrivilegeName(priv)); |
| 77 | + assertNoException(priv, () -> ApplicationPrivilege.validatePrivilegeOrActionName(priv)); |
| 78 | + } |
| 79 | + |
| 80 | + for (String priv : Arrays.asList("r e a d", "read\n", "copy®")) { |
| 81 | + assertValidationFailure(priv, "privilege names and action", () -> ApplicationPrivilege.validatePrivilegeOrActionName(priv)); |
| 82 | + } |
85 | 83 |
|
| 84 | + for (String priv : Arrays.asList("read:*", "read/*", "read/a_b.c-d+e%f#(g)")) { |
| 85 | + assertNoException(priv, () -> ApplicationPrivilege.validatePrivilegeOrActionName(priv)); |
| 86 | + } |
86 | 87 | }
|
87 | 88 |
|
88 | 89 | public void testGetPrivilegeByName() {
|
@@ -144,17 +145,22 @@ private String getPrivilegeName(ApplicationPrivilege privilege) {
|
144 | 145 | }
|
145 | 146 | }
|
146 | 147 |
|
147 |
| - private void assertValidationFailure(String messageContent, ThrowingRunnable body) { |
148 |
| - final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, body); |
149 |
| - assertThat(exception.getMessage(), containsString(messageContent)); |
| 148 | + private void assertValidationFailure(String reason,String messageContent, ThrowingRunnable body) { |
| 149 | + final IllegalArgumentException exception; |
| 150 | + try { |
| 151 | + exception = expectThrows(IllegalArgumentException.class, body); |
| 152 | + assertThat(exception.getMessage().toLowerCase(Locale.ROOT), containsString(messageContent.toLowerCase(Locale.ROOT))); |
| 153 | + } catch (AssertionFailedError e) { |
| 154 | + fail(reason + " - " + e.getMessage()); |
| 155 | + } |
150 | 156 | }
|
151 | 157 |
|
152 |
| - private void assertNoException(ThrowingRunnable body) { |
| 158 | + private void assertNoException(String reason, ThrowingRunnable body) { |
153 | 159 | try {
|
154 | 160 | body.run();
|
155 | 161 | // pass
|
156 | 162 | } catch (Throwable e) {
|
157 |
| - Assert.fail("Expected no exception, but got: " + e); |
| 163 | + Assert.fail(reason + " - Expected no exception, but got: " + e); |
158 | 164 | }
|
159 | 165 | }
|
160 | 166 |
|
|
0 commit comments