From ba263e0df9dcdd6990cb041bee84de5a9886f18e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 6 Jun 2024 14:59:10 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20`Certificate.of(=E2=80=A6)`=20factory=20m?= =?UTF-8?q?ethod=20retaining=20the=20private=20key.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes gh-857 --- .../vault/support/CertificateBundle.java | 2 +- .../support/CertificateBundleUnitTests.java | 22 ++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java b/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java index 3a763797b..4e7900da2 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java @@ -95,7 +95,7 @@ public static CertificateBundle of(String serialNumber, String certificate, Stri Assert.hasText(privateKey, "Private key must not be empty"); return new CertificateBundle(serialNumber, certificate, issuingCaCertificate, - Collections.singletonList(issuingCaCertificate), null, privateKey, null); + Collections.singletonList(issuingCaCertificate), privateKey, privateKey, null); } /** diff --git a/spring-vault-core/src/test/java/org/springframework/vault/support/CertificateBundleUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/support/CertificateBundleUnitTests.java index 3054ba7c0..1355f4009 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/support/CertificateBundleUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/support/CertificateBundleUnitTests.java @@ -15,6 +15,8 @@ */ package org.springframework.vault.support; +import static org.assertj.core.api.Assertions.*; + import java.io.IOException; import java.net.URL; import java.security.KeyFactory; @@ -27,8 +29,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.assertj.core.api.Assertions.*; - /** * Unit tests for {@link CertificateBundle}. * @@ -97,6 +97,18 @@ void invalidEcKeySpecShouldThrowException() { assertThat(bundle.getPrivateKeySpec()).isNotNull(); } + @Test + void shouldReturnPrivateKey() { + + String serialNumber = "aserialnumber"; + String certificate = "certificate"; + String caCertificate = "caCertificate"; + String privateKey = "aprivatekey"; + + CertificateBundle bundle = CertificateBundle.of(serialNumber, certificate, caCertificate, privateKey); + assertThat(bundle.getPrivateKey()).isNotNull(); + } + @Test void getAsKeystore() throws Exception { @@ -115,9 +127,9 @@ void getAsKeystore() throws Exception { } @ParameterizedTest - @ValueSource(strings = { "certificate-response-rsa-pem.json", "certificate-response-rsa-der.json", + @ValueSource(strings = {"certificate-response-rsa-pem.json", "certificate-response-rsa-der.json", "certificate-response-rsa-pembundle.json", "certificate-response-ec-pem.json", - "certificate-response-ec-der.json", "certificate-response-ec-pembundle.json" }) + "certificate-response-ec-der.json", "certificate-response-ec-pembundle.json"}) void createKeystore(String path) { CertificateBundle bundle = loadCertificateBundle(path); @@ -132,7 +144,7 @@ void createKeystore(String path) { } @ParameterizedTest - @ValueSource(strings = { "certificate-response-rsa-pem-pkcs8.json", "certificate-response-ec-pem-pkcs8.json" }) + @ValueSource(strings = {"certificate-response-rsa-pem-pkcs8.json", "certificate-response-ec-pem-pkcs8.json"}) void shouldCreateKeystore(String path) { CertificateBundle bundle = loadCertificateBundle(path);