|
15 | 15 | */ |
16 | 16 | package org.springframework.vault.core; |
17 | 17 |
|
18 | | -import java.util.Arrays; |
19 | | -import java.util.Collections; |
20 | | -import java.util.List; |
21 | | - |
| 18 | +import org.assertj.core.api.Assertions; |
22 | 19 | import org.junit.jupiter.api.AfterEach; |
23 | 20 | import org.junit.jupiter.api.BeforeEach; |
24 | 21 | import org.junit.jupiter.api.Test; |
25 | 22 | import org.junit.jupiter.api.extension.ExtendWith; |
26 | | - |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.Arguments; |
| 25 | +import org.junit.jupiter.params.provider.MethodSource; |
27 | 26 | import org.springframework.beans.factory.annotation.Autowired; |
28 | 27 | import org.springframework.test.context.ContextConfiguration; |
29 | 28 | import org.springframework.test.context.junit.jupiter.SpringExtension; |
30 | 29 | import org.springframework.vault.VaultException; |
31 | | -import org.springframework.vault.support.*; |
| 30 | +import org.springframework.vault.support.Ciphertext; |
| 31 | +import org.springframework.vault.support.Hmac; |
| 32 | +import org.springframework.vault.support.Plaintext; |
| 33 | +import org.springframework.vault.support.RawTransitKey; |
| 34 | +import org.springframework.vault.support.Signature; |
| 35 | +import org.springframework.vault.support.SignatureValidation; |
| 36 | +import org.springframework.vault.support.TransitKeyType; |
| 37 | +import org.springframework.vault.support.VaultDecryptionResult; |
| 38 | +import org.springframework.vault.support.VaultEncryptionResult; |
| 39 | +import org.springframework.vault.support.VaultHmacRequest; |
| 40 | +import org.springframework.vault.support.VaultMount; |
| 41 | +import org.springframework.vault.support.VaultSignRequest; |
| 42 | +import org.springframework.vault.support.VaultSignatureVerificationRequest; |
| 43 | +import org.springframework.vault.support.VaultTransitContext; |
| 44 | +import org.springframework.vault.support.VaultTransitKey; |
| 45 | +import org.springframework.vault.support.VaultTransitKeyConfiguration; |
| 46 | +import org.springframework.vault.support.VaultTransitKeyCreationRequest; |
32 | 47 | import org.springframework.vault.util.IntegrationTestSupport; |
33 | 48 | import org.springframework.vault.util.RequiresVaultVersion; |
34 | 49 | import org.springframework.vault.util.Version; |
35 | 50 |
|
36 | | -import static org.assertj.core.api.Assertions.*; |
| 51 | +import java.util.Arrays; |
| 52 | +import java.util.Collections; |
| 53 | +import java.util.List; |
| 54 | +import java.util.stream.IntStream; |
| 55 | +import java.util.stream.Stream; |
| 56 | + |
| 57 | +import static org.assertj.core.api.Assertions.assertThat; |
| 58 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 59 | +import static org.assertj.core.api.Assertions.fail; |
37 | 60 |
|
38 | 61 | /** |
39 | 62 | * Integration tests for {@link VaultTransitTemplate} through |
@@ -327,19 +350,29 @@ void encryptShouldCreateCiphertext() { |
327 | 350 | assertThat(ciphertext).startsWith("vault:v"); |
328 | 351 | } |
329 | 352 |
|
330 | | - @Test |
331 | | - void encryptShouldCreateCiphertextWithNonceAndContext() { |
| 353 | + private static Stream<Arguments> encryptWithKeyVersion() { |
| 354 | + return Stream.of(Arguments.of(1, 1, "v1"), Arguments.of(2, 2, "v2"), Arguments.of(1, 2, ""), |
| 355 | + Arguments.of(2, 1, "v1"), Arguments.of("2", "0", "v2")); |
| 356 | + } |
332 | 357 |
|
333 | | - this.transitOperations.createKey("mykey", |
334 | | - VaultTransitKeyCreationRequest.builder().convergentEncryption(true).derived(true).build()); |
| 358 | + @ParameterizedTest |
| 359 | + @MethodSource |
| 360 | + void encryptWithKeyVersion(int keyVersion, int usedKeyVersionWhileEncrypting, String expectedKeyPrefix) { |
| 361 | + this.transitOperations.createKey("mykey", VaultTransitKeyCreationRequest.builder().build()); |
| 362 | + // rotate the key to get the right version |
| 363 | + IntStream.range(0, keyVersion - 1).forEach(__ -> this.transitOperations.rotate("mykey")); |
335 | 364 |
|
336 | 365 | VaultTransitContext transitRequest = VaultTransitContext.builder() |
337 | | - .context("blubb".getBytes()) // |
338 | | - .nonce("123456789012".getBytes()) // |
| 366 | + .keyVersion(usedKeyVersionWhileEncrypting) |
339 | 367 | .build(); |
340 | 368 |
|
341 | | - String ciphertext = this.transitOperations.encrypt("mykey", "hello-world".getBytes(), transitRequest); |
342 | | - assertThat(ciphertext).startsWith("vault:v1:"); |
| 369 | + try { |
| 370 | + String ciphertext = this.transitOperations.encrypt("mykey", "hello-world".getBytes(), transitRequest); |
| 371 | + assertThat(ciphertext).startsWith("vault:%s:".formatted(expectedKeyPrefix)); |
| 372 | + } |
| 373 | + catch (Exception e) { |
| 374 | + Assertions.assertThat(expectedKeyPrefix).isNullOrEmpty(); |
| 375 | + } |
343 | 376 | } |
344 | 377 |
|
345 | 378 | @Test |
@@ -388,6 +421,38 @@ void decryptShouldCreatePlaintext() { |
388 | 421 | assertThat(plaintext).isEqualTo("hello-world"); |
389 | 422 | } |
390 | 423 |
|
| 424 | + private static Stream<Arguments> decryptWithKeyVersion() { |
| 425 | + return Stream.of(Arguments.of(1, 1, true), Arguments.of(2, 2, true), Arguments.of(1, 2, false), |
| 426 | + Arguments.of(2, 1, true), Arguments.of("2", "0", true)); |
| 427 | + } |
| 428 | + |
| 429 | + @ParameterizedTest |
| 430 | + @MethodSource |
| 431 | + void decryptWithKeyVersion(int keyVersion, int usedKeyVersionWhileEncrypting, boolean shouldPass) { |
| 432 | + this.transitOperations.createKey("mykey"); |
| 433 | + // rotate the key to get the right version |
| 434 | + IntStream.range(0, keyVersion - 1).forEach(__ -> this.transitOperations.rotate("mykey")); |
| 435 | + |
| 436 | + VaultTransitContext transitRequest = VaultTransitContext.builder() |
| 437 | + .keyVersion(usedKeyVersionWhileEncrypting) |
| 438 | + .build(); |
| 439 | + |
| 440 | + try { |
| 441 | + String ciphertext = this.transitOperations |
| 442 | + .encrypt("mykey", Plaintext.of("hello-world").with(transitRequest)) |
| 443 | + .getCiphertext(); |
| 444 | + String plaintext = Plaintext.of(this.transitOperations.decrypt("mykey", ciphertext, transitRequest)) |
| 445 | + .asString(); |
| 446 | + |
| 447 | + assertThat(shouldPass).isTrue(); |
| 448 | + assertThat(plaintext).isEqualTo("hello-world"); |
| 449 | + |
| 450 | + } |
| 451 | + catch (VaultException e) { |
| 452 | + assertThat(shouldPass).isFalse(); |
| 453 | + } |
| 454 | + } |
| 455 | + |
391 | 456 | @Test |
392 | 457 | void decryptShouldCreatePlaintextWithNonceAndContext() { |
393 | 458 |
|
@@ -580,7 +645,7 @@ void shouldBatchDecryptWithWrongContext() { |
580 | 645 | } |
581 | 646 | catch (VaultException e) { |
582 | 647 | assertThat(e).hasMessageContaining("error"); // Vault 1.6 behavior is |
583 | | - // different |
| 648 | + // different |
584 | 649 | } |
585 | 650 | } |
586 | 651 |
|
|
0 commit comments