diff --git a/CHANGELOG.md b/CHANGELOG.md index aa91cb63..59eaac5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Fixed +* Replace the Helper's complete trust bundle set with one TrustStore write per update, remove authorities for absent trust domains, and preserve the live store when preparing a replacement fails. Only certificate aliases of the form `.` are Helper-managed (#492) * Reject X.509-SVID leaf certificates with a root SPIFFE ID path in `X509SvidValidator.verifyChain`, which `SpiffeTrustManager` uses for peer certificates (#510) * Ignore JWKs with an unsupported key type when parsing a JWT bundle instead of rejecting the whole bundle, per the SPIFFE bundle spec (#512) * Serialize `java-spiffe-helper` KeyStore and TrustStore files to memory before writing them, so a store or encoding failure no longer leaves an empty or partial file (#490) diff --git a/java-spiffe-helper/README.md b/java-spiffe-helper/README.md index 3ed13e88..ac9fd223 100644 --- a/java-spiffe-helper/README.md +++ b/java-spiffe-helper/README.md @@ -101,5 +101,16 @@ Issuer: O=SPIFFE, C=US ... ``` -The aliases for the trusted certs are generated using the Trust Domain of the SPIFFE ID in the SAN URI, and adding a -correlative number suffix. \ No newline at end of file +Trusted certificate aliases have the form `.`, for example `example.org.0`. +The index consists entirely of ASCII digits. Certificate entries matching this format are managed by the Helper; +reserve these aliases for Workload API bundles. Other aliases and private key entries are preserved. +If a private-key entry uses an alias needed by a bundle, the trust update fails and preserves the key. The identity +KeyStore may already have been written when this failure occurs. + +Each X.509 context update replaces all Helper-managed authorities with the complete bundle set, removing stale indexes +and authorities for trust domains no longer present. The replacement is prepared in a separate in-memory KeyStore and +the TrustStore is written once per update. If preparing or serializing the replacement fails, the live in-memory +TrustStore and existing file remain unchanged. The replacement becomes the live store only after the write succeeds. + +Files are still written in place, so an I/O failure or crash during writing can leave a partial file. The KeyStore and +TrustStore are written separately; the two files are not updated atomically together. diff --git a/java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStore.java b/java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStore.java index 5424dd67..3bca5fe1 100644 --- a/java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStore.java +++ b/java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStore.java @@ -1,7 +1,11 @@ package io.spiffe.helper.keystore; +import io.spiffe.exception.InvalidSpiffeIdException; +import io.spiffe.spiffeid.TrustDomain; import org.apache.commons.lang3.StringUtils; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -25,7 +29,7 @@ class KeyStore { private final Path keyStoreFilePath; private final KeyStoreType keyStoreType; private final String keyStorePassword; - private final java.security.KeyStore javaKeyStore; + private java.security.KeyStore javaKeyStore; KeyStore( Path keyStoreFilePath, @@ -126,29 +130,79 @@ void storeAuthorityEntry(final AuthorityEntry authorityEntry) throws KeyStoreExc this.flush(); } - void deleteEntriesByAliasPrefix(final String aliasPrefix) throws KeyStoreException { + /** + * Replaces all helper-managed authorities with the complete bundle set, including removing absent trust domains. + * Changes are staged in a separate KeyStore and flushed once before becoming the live in-memory store. + * File writes remain in place, so an I/O failure can still leave a partial file. + */ + void replaceAuthorityEntries(final List authorityEntries) throws KeyStoreException { + java.security.KeyStore replacement = copyKeyStore(); List aliasesToDelete = new ArrayList<>(); - Enumeration aliases = javaKeyStore.aliases(); + Enumeration aliases = replacement.aliases(); while (aliases.hasMoreElements()) { String alias = aliases.nextElement(); - if (alias.startsWith(aliasPrefix)) { + if (replacement.isCertificateEntry(alias) && isAuthorityAlias(alias)) { aliasesToDelete.add(alias); } } for (String alias : aliasesToDelete) { - javaKeyStore.deleteEntry(alias); + replacement.deleteEntry(alias); } - this.flush(); + for (AuthorityEntry authorityEntry : authorityEntries) { + replacement.setCertificateEntry( + authorityEntry.getAlias(), + authorityEntry.getCertificate() + ); + } + + flush(replacement); + javaKeyStore = replacement; + } + + private java.security.KeyStore copyKeyStore() throws KeyStoreException { + try { + ByteArrayOutputStream content = new ByteArrayOutputStream(); + javaKeyStore.store(content, keyStorePassword.toCharArray()); + java.security.KeyStore copy = java.security.KeyStore.getInstance(javaKeyStore.getType(), javaKeyStore.getProvider()); + copy.load(new ByteArrayInputStream(content.toByteArray()), keyStorePassword.toCharArray()); + return copy; + } catch (IOException | GeneralSecurityException e) { + throw new KeyStoreException("KeyStore cannot be copied", e); + } + } + + private boolean isAuthorityAlias(final String alias) { + int separator = alias.lastIndexOf('.'); + if (separator <= 0 || separator == alias.length() - 1) { + return false; + } + + for (int i = separator + 1; i < alias.length(); i++) { + char character = alias.charAt(i); + if (character < '0' || character > '9') { + return false; + } + } + try { + String trustDomain = alias.substring(0, separator); + return TrustDomain.parse(trustDomain).getName().equals(trustDomain); + } catch (InvalidSpiffeIdException | IllegalArgumentException e) { + return false; + } } // Flush KeyStore to disk, to the configured keyStoreFilePath. The KeyStore is serialized to memory before the file // is opened, so a store or encoding failure leaves the existing file as it was. An I/O failure while writing the // bytes can still leave a partial file. private void flush() throws KeyStoreException { + flush(javaKeyStore); + } + + private void flush(final java.security.KeyStore keyStore) throws KeyStoreException { try { - KeyStoreFileWriter.write(keyStoreFilePath, outputStream -> javaKeyStore.store(outputStream, keyStorePassword.toCharArray())); + KeyStoreFileWriter.write(keyStoreFilePath, outputStream -> keyStore.store(outputStream, keyStorePassword.toCharArray())); } catch (KeyStoreException e) { throw e; } catch (IOException | GeneralSecurityException e) { diff --git a/java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStoreHelper.java b/java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStoreHelper.java index 062421c8..fdccc9ae 100644 --- a/java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStoreHelper.java +++ b/java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStoreHelper.java @@ -212,28 +212,24 @@ private void storeX509ContextUpdate(final X509Context update) throws KeyStoreExc keyStore.storePrivateKeyEntry(privateKeyEntry); + List authorityEntries = new ArrayList<>(); for (final Map.Entry entry : update.getX509BundleSet().getBundles().entrySet()) { TrustDomain trustDomain = entry.getKey(); X509Bundle bundle = entry.getValue(); - storeBundle(trustDomain, bundle); + List authorities = sortedAuthorities(bundle); + int index = 0; + for (X509Certificate certificate : authorities) { + authorityEntries.add(AuthorityEntry.builder() + .alias(generateAlias(trustDomain, index)) + .certificate(certificate) + .build()); + index++; + } } - log.log(Level.INFO, "Stored X.509 context update in Java KeyStore"); - } + trustStore.replaceAuthorityEntries(authorityEntries); - private void storeBundle(TrustDomain trustDomain, X509Bundle bundle) throws KeyStoreException { - List authorities = sortedAuthorities(bundle); - trustStore.deleteEntriesByAliasPrefix(generateAliasPrefix(trustDomain)); - - int index = 0; - for (X509Certificate certificate : authorities) { - final AuthorityEntry authorityEntry = AuthorityEntry.builder() - .alias(generateAlias(trustDomain, index)) - .certificate(certificate) - .build(); - trustStore.storeAuthorityEntry(authorityEntry); - index++; - } + log.log(Level.INFO, "Stored X.509 context update in Java KeyStore"); } private List sortedAuthorities(X509Bundle bundle) throws KeyStoreException { diff --git a/java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreHelperTest.java b/java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreHelperTest.java index acbeb028..3dd1861e 100644 --- a/java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreHelperTest.java +++ b/java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreHelperTest.java @@ -17,6 +17,10 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.MockedStatic; import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; import java.io.InputStream; @@ -32,14 +36,22 @@ import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.withSettings; class KeyStoreHelperTest { @@ -48,6 +60,9 @@ class KeyStoreHelperTest { private Path keyStoreFilePath; private Path trustStoreFilePath; + @TempDir + Path tempDirectory; + @BeforeEach void setUp() { workloadApiClient = new WorkloadApiClientStub(); @@ -261,6 +276,65 @@ public void watchX509Context(Watcher watcher) { assertEquals(1, trustStore.size()); } + @ParameterizedTest + @EnumSource(KeyStoreType.class) + void testRun_replacesCompleteBundleSetWithOneWritePerUpdate(KeyStoreType type) throws Exception { + keyStoreFilePath = tempDirectory.resolve("keystore"); + trustStoreFilePath = tempDirectory.resolve("truststore"); + String password = "truststore123"; + X509Context context = workloadApiClient.fetchX509Context(); + X509Bundle localBundle = context.getX509BundleSet().getBundleForTrustDomain(TrustDomain.parse("example.org")); + X509Certificate federatedAuthority = X509CertificateTestUtils + .createRootCA("CN=Federated CA", "spiffe://example.org.uk").getCertificate(); + X509Bundle federatedBundle = new X509Bundle(TrustDomain.parse("example.org.uk"), + Collections.singleton(federatedAuthority)); + AtomicReference bundles = new AtomicReference<>( + X509BundleSet.of(Arrays.asList(localBundle, federatedBundle))); + WorkloadApiClient client = new WorkloadApiClientStub() { + @Override + public void watchX509Context(Watcher watcher) { + watcher.onUpdate(X509Context.of(context.getX509Svids(), bundles.get())); + } + }; + KeyStoreHelper.KeyStoreOptions options = KeyStoreHelper.KeyStoreOptions.builder() + .keyStoreType(type).keyStorePath(keyStoreFilePath).keyStorePass(password) + .trustStorePath(trustStoreFilePath).trustStorePass(password).keyPass(password) + .workloadApiClient(client).build(); + + try (MockedStatic writes = mockStatic(KeyStoreFileWriter.class, + withSettings().defaultAnswer(CALLS_REAL_METHODS).mockMaker("mock-maker-inline")); + KeyStoreHelper helper = KeyStoreHelper.create(options)) { + helper.run(false); + KeyStore stored = loadTrustStore(type, password); + assertEquals(localBundle.getX509Authorities().iterator().next(), stored.getCertificate("example.org.0")); + assertEquals(federatedAuthority, stored.getCertificate("example.org.uk.0")); + assertEquals(2, stored.size()); + writes.verify(() -> KeyStoreFileWriter.write(eq(trustStoreFilePath), any()), times(1)); + + // Removing the shorter trust domain must preserve the longer trust domain's authorities. + bundles.set(X509BundleSet.of(Collections.singleton(federatedBundle))); + helper.run(false); + stored = loadTrustStore(type, password); + assertNull(stored.getCertificate("example.org.0")); + assertEquals(federatedAuthority, stored.getCertificate("example.org.uk.0")); + assertEquals(1, stored.size()); + writes.verify(() -> KeyStoreFileWriter.write(eq(trustStoreFilePath), any()), times(2)); + + bundles.set(X509BundleSet.emptySet()); + helper.run(false); + assertEquals(0, loadTrustStore(type, password).size()); + writes.verify(() -> KeyStoreFileWriter.write(eq(trustStoreFilePath), any()), times(3)); + } + } + + private KeyStore loadTrustStore(KeyStoreType type, String password) throws Exception { + KeyStore stored = KeyStore.getInstance(type.value()); + try (InputStream input = Files.newInputStream(trustStoreFilePath)) { + stored.load(input, password.toCharArray()); + } + return stored; + } + @Test void testCreateHelper_keyStore_trustStore_same_file_throwsException() throws SocketEndpointAddressException { @@ -547,4 +621,4 @@ private void deleteFile(Path file) { //ignore } } -} \ No newline at end of file +} diff --git a/java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreTest.java b/java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreTest.java index c0dcadca..d825b9e6 100644 --- a/java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreTest.java +++ b/java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreTest.java @@ -11,9 +11,13 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; @@ -25,10 +29,16 @@ import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.Arrays; +import java.util.Collections; import static io.spiffe.utils.TestUtils.toUri; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; @@ -41,6 +51,11 @@ public class KeyStoreTest { private X509Bundle x509Bundle; private Path keyStoreFilePath; + @TempDir + Path tempDirectory; + + private static final String STORE_PASSWORD = "truststore123"; + @BeforeEach void setup() throws X509SvidException, URISyntaxException, X509BundleException { x509Svid = X509Svid.load( @@ -111,6 +126,160 @@ void testStoreBundle_in_JKS_KeyStore() throws KeyStoreException, CertificateExce checkBundleEntries(keyStoreFilePath, keyStorePassword, keyStoreType, authority2Alias); } + @ParameterizedTest + @EnumSource(KeyStoreType.class) + void testReplaceAuthorityEntries_preservesUnmanagedAliasesAndReplacesEntries(KeyStoreType type) throws Exception { + KeyStore keyStore = newTestKeyStore(type); + X509Certificate authority = x509Bundle.getX509Authorities().iterator().next(); + String[] managedAliases = {"example.org.0", "example.org.01", "example.org.12", "removed.org.0"}; + String[] unmanagedAliases = {"example.org.1x", "example.org.", "example.org.-1", "example.org.+1", + "example.org.\u0661", "example.org.\uff11", "manual", ".0", "invalid/domain.0"}; + for (String alias : managedAliases) { + keyStore.storeAuthorityEntry(authorityEntry(alias, authority)); + } + for (String alias : unmanagedAliases) { + keyStore.storeAuthorityEntry(authorityEntry(alias, authority)); + } + keyStore.storeAuthorityEntry(authorityEntry("example.org.uk.0", authority)); + + keyStore.replaceAuthorityEntries(Arrays.asList( + authorityEntry("example.org.0", x509Svid.getLeaf()), + authorityEntry("example.org.1", authority), + authorityEntry("example.org.uk.0", authority))); + + java.security.KeyStore stored = loadTestKeyStore(type); + assertEquals(x509Svid.getLeaf(), stored.getCertificate("example.org.0")); + assertEquals(authority, stored.getCertificate("example.org.1")); + assertEquals(authority, stored.getCertificate("example.org.uk.0")); + for (String alias : unmanagedAliases) { + assertEquals(authority, stored.getCertificate(alias), alias); + } + assertNull(stored.getCertificate("example.org.01")); + assertNull(stored.getCertificate("example.org.12")); + assertNull(stored.getCertificate("removed.org.0")); + assertEquals(unmanagedAliases.length + 3, stored.size()); + } + + @ParameterizedTest + @EnumSource(KeyStoreType.class) + void testReplaceAuthorityEntries_successfulReplacementSurvivesLaterFlush(KeyStoreType type) throws Exception { + KeyStore keyStore = newTestKeyStore(type); + X509Certificate authority = x509Bundle.getX509Authorities().iterator().next(); + X509Certificate[] privateKeyChain = {x509Svid.getLeaf(), authority}; + keyStore.storeAuthorityEntry(authorityEntry("example.org.0", authority)); + keyStore.storeAuthorityEntry(authorityEntry("removed.org.0", authority)); + keyStore.storeAuthorityEntry(authorityEntry("example.org.1x", authority)); + keyStore.storePrivateKeyEntry(PrivateKeyEntry.builder().alias("private.org.0") + .privateKey(x509Svid.getPrivateKey()).certificateChain(privateKeyChain) + .password("privatekey123").build()); + + keyStore.replaceAuthorityEntries(Arrays.asList( + authorityEntry("example.org.0", x509Svid.getLeaf()), + authorityEntry("other.org.0", authority))); + keyStore.storeAuthorityEntry(authorityEntry("manual", authority)); + + java.security.KeyStore stored = loadTestKeyStore(type); + assertEquals(x509Svid.getLeaf(), stored.getCertificate("example.org.0")); + assertEquals(authority, stored.getCertificate("other.org.0")); + assertNull(stored.getCertificate("removed.org.0")); + assertEquals(authority, stored.getCertificate("example.org.1x")); + assertEquals(authority, stored.getCertificate("manual")); + assertEquals(x509Svid.getPrivateKey(), stored.getKey("private.org.0", "privatekey123".toCharArray())); + assertArrayEquals(privateKeyChain, stored.getCertificateChain("private.org.0")); + assertEquals(5, stored.size()); + } + + @ParameterizedTest + @EnumSource(KeyStoreType.class) + void testReplaceAuthorityEntries_emptySetRemovesAllManagedAuthorities(KeyStoreType type) throws Exception { + KeyStore keyStore = newTestKeyStore(type); + X509Certificate authority = x509Bundle.getX509Authorities().iterator().next(); + keyStore.storeAuthorityEntry(authorityEntry("example.org.0", authority)); + keyStore.storeAuthorityEntry(authorityEntry("example.org.uk.0", authority)); + keyStore.storeAuthorityEntry(authorityEntry("example.org.1x", authority)); + // Even an indexed alias is not helper-managed when it contains a private key. + keyStore.storePrivateKeyEntry(PrivateKeyEntry.builder().alias("private.org.0") + .privateKey(x509Svid.getPrivateKey()).certificateChain(x509Svid.getChainArray()) + .password("privatekey123").build()); + + keyStore.replaceAuthorityEntries(Collections.emptyList()); + + java.security.KeyStore stored = loadTestKeyStore(type); + assertNull(stored.getCertificate("example.org.0")); + assertNull(stored.getCertificate("example.org.uk.0")); + assertEquals(authority, stored.getCertificate("example.org.1x")); + assertEquals(x509Svid.getPrivateKey(), stored.getKey("private.org.0", "privatekey123".toCharArray())); + assertEquals(2, stored.size()); + } + + @ParameterizedTest + @EnumSource(KeyStoreType.class) + void testReplaceAuthorityEntries_failedInsertionDoesNotChangeLiveStore(KeyStoreType type) throws Exception { + KeyStore keyStore = newTestKeyStore(type); + X509Certificate authority = x509Bundle.getX509Authorities().iterator().next(); + keyStore.storeAuthorityEntry(authorityEntry("example.org.0", authority)); + keyStore.storePrivateKeyEntry(PrivateKeyEntry.builder().alias("private.org.0") + .privateKey(x509Svid.getPrivateKey()).certificateChain(x509Svid.getChainArray()) + .password("privatekey123").build()); + byte[] original = Files.readAllBytes(keyStoreFilePath); + + assertThrows(KeyStoreException.class, () -> keyStore.replaceAuthorityEntries(Arrays.asList( + authorityEntry("example.org.1", authority), + authorityEntry("private.org.0", authority)))); + + assertArrayEquals(original, Files.readAllBytes(keyStoreFilePath)); + // A later successful flush must not persist the failed replacement's deletions or insertions. + keyStore.storeAuthorityEntry(authorityEntry("manual", authority)); + java.security.KeyStore stored = loadTestKeyStore(type); + assertEquals(authority, stored.getCertificate("example.org.0")); + assertFalse(stored.containsAlias("example.org.1")); + assertEquals(x509Svid.getPrivateKey(), stored.getKey("private.org.0", "privatekey123".toCharArray())); + assertEquals(3, stored.size()); + } + + @ParameterizedTest + @EnumSource(KeyStoreType.class) + void testReplaceAuthorityEntries_failedWriteDoesNotChangeLiveStore(KeyStoreType type) throws Exception { + KeyStore keyStore = newTestKeyStore(type); + X509Certificate authority = x509Bundle.getX509Authorities().iterator().next(); + keyStore.storeAuthorityEntry(authorityEntry("example.org.0", authority)); + byte[] original = Files.readAllBytes(keyStoreFilePath); + Files.delete(keyStoreFilePath); + Files.createDirectory(keyStoreFilePath); + + try { + assertThrows(KeyStoreException.class, () -> keyStore.replaceAuthorityEntries( + Collections.singletonList(authorityEntry("other.org.0", authority)))); + } finally { + Files.delete(keyStoreFilePath); + Files.write(keyStoreFilePath, original); + } + + keyStore.storeAuthorityEntry(authorityEntry("manual", authority)); + java.security.KeyStore stored = loadTestKeyStore(type); + assertEquals(authority, stored.getCertificate("example.org.0")); + assertFalse(stored.containsAlias("other.org.0")); + assertEquals(2, stored.size()); + } + + private KeyStore newTestKeyStore(KeyStoreType type) throws KeyStoreException { + keyStoreFilePath = tempDirectory.resolve("truststore"); + return KeyStore.builder().keyStoreFilePath(keyStoreFilePath).keyStoreType(type) + .keyStorePassword(STORE_PASSWORD).build(); + } + + private java.security.KeyStore loadTestKeyStore(KeyStoreType type) throws Exception { + java.security.KeyStore stored = java.security.KeyStore.getInstance(type.value()); + try (InputStream input = Files.newInputStream(keyStoreFilePath)) { + stored.load(input, STORE_PASSWORD.toCharArray()); + } + return stored; + } + + private AuthorityEntry authorityEntry(String alias, X509Certificate certificate) { + return AuthorityEntry.builder().alias(alias).certificate(certificate).build(); + } + @Test void testNewKeyStore_from_existing_file() throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException { String fileName = RandomStringUtils.randomAlphabetic(10);