|
9 | 9 |
|
10 | 10 | package org.elasticsearch.entitlement.qa.test; |
11 | 11 |
|
| 12 | +import org.elasticsearch.core.CheckedRunnable; |
12 | 13 | import org.elasticsearch.core.SuppressForbidden; |
13 | 14 | import org.elasticsearch.entitlement.qa.entitled.EntitledActions; |
14 | 15 |
|
|
26 | 27 | import java.nio.file.Path; |
27 | 28 | import java.nio.file.Paths; |
28 | 29 | import java.nio.file.attribute.UserPrincipal; |
| 30 | +import java.security.GeneralSecurityException; |
| 31 | +import java.security.KeyStore; |
29 | 32 | import java.util.Scanner; |
30 | | - |
| 33 | +import java.util.jar.JarFile; |
| 34 | +import java.util.zip.ZipException; |
| 35 | +import java.util.zip.ZipFile; |
| 36 | + |
| 37 | +import static java.nio.charset.Charset.defaultCharset; |
| 38 | +import static java.util.zip.ZipFile.OPEN_DELETE; |
| 39 | +import static java.util.zip.ZipFile.OPEN_READ; |
| 40 | +import static org.elasticsearch.entitlement.qa.entitled.EntitledActions.createTempFileForWrite; |
31 | 41 | import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.ALWAYS_DENIED; |
32 | 42 | import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS; |
33 | 43 |
|
34 | 44 | @SuppressForbidden(reason = "Explicitly checking APIs that are forbidden") |
| 45 | +@SuppressWarnings("unused") // Called via reflection |
35 | 46 | class FileCheckActions { |
36 | 47 |
|
37 | 48 | static Path testRootDir = Paths.get(System.getProperty("es.entitlements.testdir")); |
@@ -207,21 +218,6 @@ static void fileSetWritableOwner() throws IOException { |
207 | 218 | readWriteFile().toFile().setWritable(true, false); |
208 | 219 | } |
209 | 220 |
|
210 | | - @EntitlementTest(expectedAccess = PLUGINS) |
211 | | - static void createScannerFile() throws FileNotFoundException { |
212 | | - new Scanner(readFile().toFile()); |
213 | | - } |
214 | | - |
215 | | - @EntitlementTest(expectedAccess = PLUGINS) |
216 | | - static void createScannerFileWithCharset() throws IOException { |
217 | | - new Scanner(readFile().toFile(), StandardCharsets.UTF_8); |
218 | | - } |
219 | | - |
220 | | - @EntitlementTest(expectedAccess = PLUGINS) |
221 | | - static void createScannerFileWithCharsetName() throws FileNotFoundException { |
222 | | - new Scanner(readFile().toFile(), "UTF-8"); |
223 | | - } |
224 | | - |
225 | 221 | @EntitlementTest(expectedAccess = PLUGINS) |
226 | 222 | static void createFileInputStreamFile() throws IOException { |
227 | 223 | new FileInputStream(readFile().toFile()).close(); |
@@ -363,5 +359,140 @@ static void filesSetOwner() throws IOException { |
363 | 359 | Files.setOwner(readWriteFile(), owner); // set to existing owner, just trying to execute the method |
364 | 360 | } |
365 | 361 |
|
| 362 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 363 | + static void keystoreGetInstance_FileCharArray() throws IOException { |
| 364 | + try { |
| 365 | + KeyStore.getInstance(readFile().toFile(), new char[0]); |
| 366 | + } catch (GeneralSecurityException expected) { |
| 367 | + return; |
| 368 | + } |
| 369 | + throw new AssertionError("Expected an exception"); |
| 370 | + } |
| 371 | + |
| 372 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 373 | + static void keystoreGetInstance_FileLoadStoreParameter() throws IOException { |
| 374 | + try { |
| 375 | + KeyStore.LoadStoreParameter loadStoreParameter = () -> null; |
| 376 | + KeyStore.getInstance(readFile().toFile(), loadStoreParameter); |
| 377 | + } catch (GeneralSecurityException expected) { |
| 378 | + return; |
| 379 | + } |
| 380 | + throw new AssertionError("Expected an exception"); |
| 381 | + } |
| 382 | + |
| 383 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 384 | + static void keystoreBuilderNewInstance() { |
| 385 | + try { |
| 386 | + KeyStore.Builder.newInstance("", null, readFile().toFile(), null); |
| 387 | + } catch (NullPointerException expected) { |
| 388 | + return; |
| 389 | + } |
| 390 | + throw new AssertionError("Expected an exception"); |
| 391 | + } |
| 392 | + |
| 393 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 394 | + static void zipFile_String() throws IOException { |
| 395 | + expectZipException(() -> new ZipFile(readFile().toString()).close()); |
| 396 | + } |
| 397 | + |
| 398 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 399 | + static void zipFile_StringCharset() throws IOException { |
| 400 | + expectZipException(() -> new ZipFile(readFile().toString(), defaultCharset()).close()); |
| 401 | + } |
| 402 | + |
| 403 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 404 | + static void zipFile_File() throws IOException { |
| 405 | + expectZipException(() -> new ZipFile(readFile().toFile()).close()); |
| 406 | + } |
| 407 | + |
| 408 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 409 | + static void zipFile_FileCharset() throws IOException { |
| 410 | + expectZipException(() -> new ZipFile(readFile().toFile(), defaultCharset()).close()); |
| 411 | + } |
| 412 | + |
| 413 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 414 | + static void zipFile_FileReadOnly() throws IOException { |
| 415 | + expectZipException(() -> new ZipFile(readFile().toFile(), OPEN_READ).close()); |
| 416 | + } |
| 417 | + |
| 418 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 419 | + static void zipFile_FileReadAndDelete() throws IOException { |
| 420 | + expectZipException(() -> new ZipFile(createTempFileForWrite().toFile(), OPEN_READ | OPEN_DELETE).close()); |
| 421 | + } |
| 422 | + |
| 423 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 424 | + static void zipFile_ReadOnlyCharset() throws IOException { |
| 425 | + expectZipException(() -> new ZipFile(readFile().toFile(), OPEN_READ, defaultCharset()).close()); |
| 426 | + } |
| 427 | + |
| 428 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 429 | + static void zipFile_ReadAndDeleteCharset() throws IOException { |
| 430 | + expectZipException(() -> new ZipFile(createTempFileForWrite().toFile(), OPEN_READ | OPEN_DELETE, defaultCharset()).close()); |
| 431 | + } |
| 432 | + |
| 433 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 434 | + static void jarFile_String() throws IOException { |
| 435 | + expectZipException(() -> new JarFile(readFile().toString()).close()); |
| 436 | + } |
| 437 | + |
| 438 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 439 | + static void jarFile_StringBoolean() throws IOException { |
| 440 | + expectZipException(() -> new JarFile(readFile().toString(), false).close()); |
| 441 | + } |
| 442 | + |
| 443 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 444 | + static void jarFile_FileReadOnly() throws IOException { |
| 445 | + expectZipException(() -> new JarFile(readFile().toFile(), false, OPEN_READ).close()); |
| 446 | + } |
| 447 | + |
| 448 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 449 | + static void jarFile_FileReadAndDelete() throws IOException { |
| 450 | + expectZipException(() -> new JarFile(createTempFileForWrite().toFile(), false, OPEN_READ | OPEN_DELETE).close()); |
| 451 | + } |
| 452 | + |
| 453 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 454 | + static void jarFile_FileBooleanReadOnlyVersion() throws IOException { |
| 455 | + expectZipException(() -> new JarFile(readFile().toFile(), false, OPEN_READ, Runtime.version()).close()); |
| 456 | + } |
| 457 | + |
| 458 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 459 | + static void jarFile_FileBooleanReadAndDeleteOnlyVersion() throws IOException { |
| 460 | + expectZipException(() -> new JarFile(createTempFileForWrite().toFile(), false, OPEN_READ | OPEN_DELETE, Runtime.version()).close()); |
| 461 | + } |
| 462 | + |
| 463 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 464 | + static void jarFile_File() throws IOException { |
| 465 | + expectZipException(() -> new JarFile(readFile().toFile()).close()); |
| 466 | + } |
| 467 | + |
| 468 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 469 | + static void jarFileFileBoolean() throws IOException { |
| 470 | + expectZipException(() -> new JarFile(readFile().toFile(), false).close()); |
| 471 | + } |
| 472 | + |
| 473 | + private static void expectZipException(CheckedRunnable<IOException> action) throws IOException { |
| 474 | + try { |
| 475 | + action.run(); |
| 476 | + } catch (ZipException expected) { |
| 477 | + return; |
| 478 | + } |
| 479 | + throw new AssertionError("Expected an exception"); |
| 480 | + } |
| 481 | + |
| 482 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 483 | + static void createScannerFile() throws FileNotFoundException { |
| 484 | + new Scanner(readFile().toFile()); |
| 485 | + } |
| 486 | + |
| 487 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 488 | + static void createScannerFileWithCharset() throws IOException { |
| 489 | + new Scanner(readFile().toFile(), StandardCharsets.UTF_8); |
| 490 | + } |
| 491 | + |
| 492 | + @EntitlementTest(expectedAccess = PLUGINS) |
| 493 | + static void createScannerFileWithCharsetName() throws FileNotFoundException { |
| 494 | + new Scanner(readFile().toFile(), "UTF-8"); |
| 495 | + } |
| 496 | + |
366 | 497 | private FileCheckActions() {} |
367 | 498 | } |
0 commit comments