|
4 | 4 | import static org.hamcrest.CoreMatchers.equalTo; |
5 | 5 | import static org.hamcrest.CoreMatchers.startsWith; |
6 | 6 | import static org.hamcrest.MatcherAssert.assertThat; |
| 7 | +import static org.junit.Assert.assertThrows; |
7 | 8 | import static org.junit.Assert.assertTrue; |
8 | 9 |
|
9 | 10 | import io.jenkins.plugins.casc.SecretSourceResolver.Base64Lookup; |
@@ -521,4 +522,58 @@ public void resolve_trimWithSpacesOnly_becomesEmpty() { |
521 | 522 | environment.set("FOO", " "); |
522 | 523 | assertThat(resolve("${trim:${FOO}}"), equalTo("")); |
523 | 524 | } |
| 525 | + |
| 526 | + @Test |
| 527 | + public void resolve_strictModeEnvVar_throwsExceptionOnMissingVar() { |
| 528 | + environment.set("CASC_STRICT_SECRET_RESOLUTION", "true"); |
| 529 | + |
| 530 | + IllegalStateException exception = assertThrows(IllegalStateException.class, () -> { |
| 531 | + resolve("${MISSING_SECRET_VAR}"); |
| 532 | + }); |
| 533 | + |
| 534 | + assertThat(exception.getMessage(), containsString("MISSING_SECRET_VAR")); |
| 535 | + } |
| 536 | + |
| 537 | + @Test |
| 538 | + public void resolve_strictModeSysProp_throwsExceptionOnMissingVar() { |
| 539 | + System.setProperty("casc.strict.secret.resolution", "true"); |
| 540 | + try { |
| 541 | + IllegalStateException exception = assertThrows(IllegalStateException.class, () -> { |
| 542 | + resolve("${ANOTHER_MISSING_VAR}"); |
| 543 | + }); |
| 544 | + assertThat(exception.getMessage(), containsString("ANOTHER_MISSING_VAR")); |
| 545 | + } finally { |
| 546 | + System.clearProperty("casc.strict.secret.resolution"); |
| 547 | + } |
| 548 | + } |
| 549 | + |
| 550 | + @Test |
| 551 | + public void resolve_strictMode_ignoresExceptionIfDefaultProvided() { |
| 552 | + environment.set("CASC_STRICT_SECRET_RESOLUTION", "true"); |
| 553 | + |
| 554 | + String output = resolve("${MISSING_SECRET_VAR:-my_fallback_value}"); |
| 555 | + |
| 556 | + assertThat(output, equalTo("my_fallback_value")); |
| 557 | + } |
| 558 | + |
| 559 | + @Test |
| 560 | + public void resolve_strictModeSetToFalse_defaultsToEmptyString() { |
| 561 | + environment.set("CASC_STRICT_SECRET_RESOLUTION", "false"); |
| 562 | + |
| 563 | + String output = resolve("${MISSING_SECRET_VAR}"); |
| 564 | + |
| 565 | + assertThat(output, equalTo("")); |
| 566 | + assertTrue(logContains("Configuration import: Found unresolved variable 'MISSING_SECRET_VAR'")); |
| 567 | + } |
| 568 | + |
| 569 | + @Test |
| 570 | + public void resolve_strictMode_multipleVariables_oneMissing_shouldFail() { |
| 571 | + environment.set("CASC_STRICT_SECRET_RESOLUTION", "true"); |
| 572 | + environment.set("FOO", "hello"); |
| 573 | + |
| 574 | + IllegalStateException exception = assertThrows(IllegalStateException.class, () -> { |
| 575 | + resolve("${FOO}:${MISSING}"); |
| 576 | + }); |
| 577 | + assertThat(exception.getMessage(), containsString("MISSING")); |
| 578 | + } |
524 | 579 | } |
0 commit comments