From a8f09ca28eedad2f7a9ea22cae46a44a59057796 Mon Sep 17 00:00:00 2001 From: Esta Nagy Date: Sun, 29 Dec 2024 21:39:59 +0100 Subject: [PATCH] Base64 cannot be decoded to KeyStore with Java client (#1296) - Fixes encoder to generate same format as before - Changes end-to-end tests to only accept the right format (and no longer accept Base64Mime) Resolves #1295 {patch} Signed-off-by: Esta Nagy --- .../lowkeyvault/service/certificate/impl/CertContentType.java | 2 +- .../service/certificate/impl/CertContentTypeTest.java | 4 +++- .../lowkeyvault/steps/CertificateStepDefAssertion.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lowkey-vault-app/src/main/java/com/github/nagyesta/lowkeyvault/service/certificate/impl/CertContentType.java b/lowkey-vault-app/src/main/java/com/github/nagyesta/lowkeyvault/service/certificate/impl/CertContentType.java index ffd50a71..344ec76e 100644 --- a/lowkey-vault-app/src/main/java/com/github/nagyesta/lowkeyvault/service/certificate/impl/CertContentType.java +++ b/lowkey-vault-app/src/main/java/com/github/nagyesta/lowkeyvault/service/certificate/impl/CertContentType.java @@ -239,6 +239,6 @@ static byte[] decodeBase64String(final String certificateContent) { } static String encodeAsBase64String(final byte[] bytes) { - return Base64.getMimeEncoder().encodeToString(bytes); + return Base64.getEncoder().encodeToString(bytes); } } diff --git a/lowkey-vault-app/src/test/java/com/github/nagyesta/lowkeyvault/service/certificate/impl/CertContentTypeTest.java b/lowkey-vault-app/src/test/java/com/github/nagyesta/lowkeyvault/service/certificate/impl/CertContentTypeTest.java index 0a029f87..fa8cb407 100644 --- a/lowkey-vault-app/src/test/java/com/github/nagyesta/lowkeyvault/service/certificate/impl/CertContentTypeTest.java +++ b/lowkey-vault-app/src/test/java/com/github/nagyesta/lowkeyvault/service/certificate/impl/CertContentTypeTest.java @@ -19,6 +19,7 @@ import java.security.cert.CertificateEncodingException; import java.util.List; import java.util.Objects; +import java.util.regex.Pattern; import java.util.stream.Stream; import static com.github.nagyesta.lowkeyvault.service.certificate.impl.CertContentType.PEM; @@ -468,7 +469,8 @@ void testEncodeAsBase64StringShouldProduceTheExpectedBase64String(final byte[] i final String actual = CertContentType.encodeAsBase64String(input); //then - Assertions.assertEquals(expected, actual); + final String expectedWithoutLineBreaks = expected.replaceAll(Pattern.quote("\r\n"), ""); + Assertions.assertEquals(expectedWithoutLineBreaks, actual); } @ParameterizedTest diff --git a/lowkey-vault-docker/src/test/java/com/github/nagyesta/lowkeyvault/steps/CertificateStepDefAssertion.java b/lowkey-vault-docker/src/test/java/com/github/nagyesta/lowkeyvault/steps/CertificateStepDefAssertion.java index ca3c914e..0374a0e2 100644 --- a/lowkey-vault-docker/src/test/java/com/github/nagyesta/lowkeyvault/steps/CertificateStepDefAssertion.java +++ b/lowkey-vault-docker/src/test/java/com/github/nagyesta/lowkeyvault/steps/CertificateStepDefAssertion.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.Objects; +import static java.util.Base64.getDecoder; import static java.util.Base64.getMimeDecoder; public class CertificateStepDefAssertion extends CommonAssertions { @@ -305,7 +306,7 @@ private X509Certificate getX509Certificate(final CertificateContentType contentT final CertificateFactory fact = CertificateFactory.getInstance("X.509"); certificate = (X509Certificate) fact.generateCertificate(new ByteArrayInputStream(encodedCertificate)); } else { - final byte[] bytes = getMimeDecoder().decode(value); + final byte[] bytes = getDecoder().decode(value); final KeyStore keyStore = getKeyStore(bytes, DEFAULT_PASSWORD); final String alias = findAlias(keyStore); certificate = (X509Certificate) keyStore.getCertificate(alias);