|
| 1 | +/* |
| 2 | + * Copyright 2022 Thunderberry. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package nl.altindag.server.service; |
| 17 | + |
| 18 | +import nl.altindag.ssl.SSLFactory; |
| 19 | +import nl.altindag.ssl.util.SSLFactoryUtils; |
| 20 | +import org.jboss.logging.Logger; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.UncheckedIOException; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.attribute.BasicFileAttributes; |
| 27 | +import java.time.Instant; |
| 28 | +import java.time.ZoneOffset; |
| 29 | +import java.time.ZonedDateTime; |
| 30 | + |
| 31 | +public class FileBasedSslUpdateService { |
| 32 | + |
| 33 | + private static final Logger LOGGER = Logger.getLogger(FileBasedSslUpdateService.class); |
| 34 | + |
| 35 | + private static final Path identityPath = Path.of("/path/to/your/identity.jks"); |
| 36 | + private static final Path trustStorePath = Path.of("/path/to/your/truststore.jks"); |
| 37 | + private static final char[] identityPassword = "secret".toCharArray(); |
| 38 | + private static final char[] trustStorePassword = "secret".toCharArray(); |
| 39 | + |
| 40 | + private ZonedDateTime lastModifiedTimeIdentityStore = ZonedDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC); |
| 41 | + private ZonedDateTime lastModifiedTimeTrustStore = ZonedDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC); |
| 42 | + |
| 43 | + private final SSLFactory baseSslFactory; |
| 44 | + |
| 45 | + public FileBasedSslUpdateService(SSLFactory baseSslFactory) { |
| 46 | + this.baseSslFactory = baseSslFactory; |
| 47 | + LOGGER.info("Started listening for any changes on the keystore and truststore files..."); |
| 48 | + } |
| 49 | + |
| 50 | + public void updateSslMaterial() { |
| 51 | + try { |
| 52 | + if (Files.exists(identityPath) && Files.exists(trustStorePath)) { |
| 53 | + BasicFileAttributes identityAttributes = Files.readAttributes(identityPath, BasicFileAttributes.class); |
| 54 | + BasicFileAttributes trustStoreAttributes = Files.readAttributes(trustStorePath, BasicFileAttributes.class); |
| 55 | + |
| 56 | + boolean identityUpdated = lastModifiedTimeIdentityStore.isBefore(ZonedDateTime.ofInstant(identityAttributes.lastModifiedTime().toInstant(), ZoneOffset.UTC)); |
| 57 | + boolean trustStoreUpdated = lastModifiedTimeTrustStore.isBefore(ZonedDateTime.ofInstant(trustStoreAttributes.lastModifiedTime().toInstant(), ZoneOffset.UTC)); |
| 58 | + |
| 59 | + if (identityUpdated && trustStoreUpdated) { |
| 60 | + LOGGER.info("Keystore files have been changed. Trying to read the file content and preparing to update the ssl material"); |
| 61 | + |
| 62 | + SSLFactory updatedSslFactory = SSLFactory.builder() |
| 63 | + .withIdentityMaterial(identityPath, identityPassword) |
| 64 | + .withTrustMaterial(trustStorePath, trustStorePassword) |
| 65 | + .build(); |
| 66 | + |
| 67 | + SSLFactoryUtils.reload(baseSslFactory, updatedSslFactory); |
| 68 | + |
| 69 | + lastModifiedTimeIdentityStore = ZonedDateTime.ofInstant(identityAttributes.lastModifiedTime().toInstant(), ZoneOffset.UTC); |
| 70 | + lastModifiedTimeTrustStore = ZonedDateTime.ofInstant(trustStoreAttributes.lastModifiedTime().toInstant(), ZoneOffset.UTC); |
| 71 | + |
| 72 | + LOGGER.info("Updating ssl material finished"); |
| 73 | + } |
| 74 | + } |
| 75 | + } catch (IOException e) { |
| 76 | + throw new UncheckedIOException(e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments