|
29 | 29 | import java.net.HttpURLConnection; |
30 | 30 | import java.net.URL; |
31 | 31 | import java.nio.charset.StandardCharsets; |
| 32 | +import java.util.ArrayList; |
32 | 33 | import java.util.Arrays; |
33 | 34 | import java.util.Collections; |
34 | 35 | import java.util.HashMap; |
| 36 | +import java.util.List; |
35 | 37 | import java.util.Map; |
36 | 38 | import javax.net.ssl.HttpsURLConnection; |
37 | 39 | import javax.net.ssl.SSLContext; |
@@ -185,6 +187,22 @@ public void testAuthCheck_noACL(final AuthSchema authSchema) throws Exception { |
185 | 187 | assertEquals(HttpURLConnection.HTTP_OK, authTestConn.getResponseCode()); |
186 | 188 | } |
187 | 189 |
|
| 190 | + @ParameterizedTest |
| 191 | + @EnumSource(value = AuthSchema.class, names = {"DIGEST"}) |
| 192 | + public void testAuthCheck_noPerms(final AuthSchema authSchema) throws Exception { |
| 193 | + // The extra ACL entry gives Perms.READ perms to the "invalid" |
| 194 | + // DIGEST authInfo---but that should not permit access, as |
| 195 | + // AuthTestCommand requires Perms.ADMIN. |
| 196 | + setupRootACL(authSchema, ZooDefs.Ids.READ_ACL_UNSAFE); |
| 197 | + try { |
| 198 | + final HttpURLConnection authTestConn = sendAuthTestCommandRequest(authSchema, false); |
| 199 | + assertEquals(HttpURLConnection.HTTP_FORBIDDEN, authTestConn.getResponseCode()); |
| 200 | + } finally { |
| 201 | + addAuthInfo(zk, authSchema); |
| 202 | + resetRootACL(zk); |
| 203 | + } |
| 204 | + } |
| 205 | + |
188 | 206 | @Test |
189 | 207 | public void testAuthCheck_invalidServerRequiredConfig() { |
190 | 208 | assertThrows("An active server is required for auth check", |
@@ -300,19 +318,29 @@ public void clearTLS() { |
300 | 318 | } |
301 | 319 |
|
302 | 320 | private void setupRootACL(final AuthSchema authSchema) throws Exception { |
| 321 | + setupRootACL(authSchema, Collections.<ACL>emptyList()); |
| 322 | + } |
| 323 | + |
| 324 | + private void setupRootACL(final AuthSchema authSchema, final List<ACL> extraEntries) throws Exception { |
| 325 | + final List<ACL> aclEntries = new ArrayList<>(); |
| 326 | + |
303 | 327 | switch (authSchema) { |
304 | 328 | case DIGEST: |
305 | | - setupRootACLForDigest(zk); |
| 329 | + aclEntries.addAll(genACLForDigest()); |
306 | 330 | break; |
307 | 331 | case X509: |
308 | | - setupRootACLForX509(zk); |
| 332 | + aclEntries.addAll(genACLForX509()); |
309 | 333 | break; |
310 | 334 | case IP: |
311 | | - setupRootACLForIP(zk); |
| 335 | + aclEntries.addAll(genACLForIP()); |
312 | 336 | break; |
313 | 337 | default: |
314 | 338 | throw new IllegalArgumentException("Unknown auth schema"); |
315 | 339 | } |
| 340 | + |
| 341 | + aclEntries.addAll(extraEntries); |
| 342 | + |
| 343 | + zk.setACL(Commands.ROOT_PATH, aclEntries, -1); |
316 | 344 | } |
317 | 345 |
|
318 | 346 | private HttpURLConnection sendAuthTestCommandRequest(final AuthSchema authSchema, final boolean validAuthInfo) throws Exception { |
@@ -343,22 +371,22 @@ public static void resetRootACL(final ZooKeeper zk) throws Exception { |
343 | 371 | zk.setACL(Commands.ROOT_PATH, OPEN_ACL_UNSAFE, -1); |
344 | 372 | } |
345 | 373 |
|
346 | | - public static void setupRootACLForDigest(final ZooKeeper zk) throws Exception { |
| 374 | + public static List<ACL> genACLForDigest() throws Exception { |
347 | 375 | final String idPassword = String.format("%s:%s", ROOT_USER, ROOT_PASSWORD); |
348 | 376 | final String digest = DigestAuthenticationProvider.generateDigest(idPassword); |
349 | 377 |
|
350 | 378 | final ACL acl = new ACL(ZooDefs.Perms.ALL, new Id(DIGEST_SCHEMA, digest)); |
351 | | - zk.setACL(Commands.ROOT_PATH, Collections.singletonList(acl), -1); |
| 379 | + return Collections.singletonList(acl); |
352 | 380 | } |
353 | 381 |
|
354 | | - private static void setupRootACLForX509(final ZooKeeper zk) throws Exception { |
| 382 | + private static List<ACL> genACLForX509() throws Exception { |
355 | 383 | final ACL acl = new ACL(ZooDefs.Perms.ALL, new Id(X509_SCHEMA, X509_SUBJECT_PRINCIPAL)); |
356 | | - zk.setACL(Commands.ROOT_PATH, Collections.singletonList(acl), -1); |
| 384 | + return Collections.singletonList(acl); |
357 | 385 | } |
358 | 386 |
|
359 | | - private static void setupRootACLForIP(final ZooKeeper zk) throws Exception { |
| 387 | + private static List<ACL> genACLForIP() throws Exception { |
360 | 388 | final ACL acl = new ACL(ZooDefs.Perms.ALL, new Id(IP_SCHEMA, "127.0.0.1")); |
361 | | - zk.setACL(Commands.ROOT_PATH, Collections.singletonList(acl), -1); |
| 389 | + return Collections.singletonList(acl); |
362 | 390 | } |
363 | 391 |
|
364 | 392 | public static void addAuthInfoForDigest(final ZooKeeper zk) { |
|
0 commit comments