summaryrefslogtreecommitdiffstats
path: root/security-utils/src/main/java/com/yahoo/security/tls/TlsManager.java
blob: bade19939827b48559f538576e83e502f1fc17c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security.tls;

import com.yahoo.security.KeyStoreBuilder;
import com.yahoo.security.KeyStoreType;
import com.yahoo.security.KeyUtils;
import com.yahoo.security.X509CertificateUtils;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.ref.WeakReference;
import java.nio.file.Path;
import java.security.KeyStore;
import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

class TlsManager {
    private static final Logger log = Logger.getLogger(TlsManager.class.getName());
    private static final Duration UPDATE_PERIOD = Duration.ofHours(1);

    private final MutableX509TrustManager trustManager;
    private final MutableX509KeyManager keyManager;
    private final ScheduledExecutorService scheduler;
    private TransportSecurityOptions options;

    private static void reloadTrustManager(TransportSecurityOptions options, MutableX509TrustManager trustManager) {
        if (options.getCaCertificatesFile().isPresent()) {
            trustManager.updateTruststore(loadTruststore(options.getCaCertificatesFile().get()));
        } else {
            trustManager.useDefaultTruststore();
        }
    }

    private static void reloadKeyManager(TransportSecurityOptions options, MutableX509KeyManager keyManager) {
        if (options.getPrivateKeyFile().isPresent() && options.getCertificatesFile().isPresent()) {
            keyManager.updateKeystore(loadKeystore(options.getPrivateKeyFile().get(), options.getCertificatesFile().get()), new char[0]);
        } else {
            keyManager.useDefaultKeystore();
        }
    }
    private static KeyStore loadTruststore(Path caCertificateFile) {
        try {
            return KeyStoreBuilder.withType(KeyStoreType.PKCS12)
                    .withCertificateEntries("cert", X509CertificateUtils.certificateListFromPem(com.yahoo.vespa.jdk8compat.Files.readString(caCertificateFile)))
                    .build();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private static KeyStore loadKeystore(Path privateKeyFile, Path certificatesFile) {
        try {
            return KeyStoreBuilder.withType(KeyStoreType.PKCS12)
                    .withKeyEntry(
                            "default",
                            KeyUtils.fromPemEncodedPrivateKey(com.yahoo.vespa.jdk8compat.Files.readString(privateKeyFile)),
                            X509CertificateUtils.certificateListFromPem(com.yahoo.vespa.jdk8compat.Files.readString(certificatesFile)))
                    .build();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    // Note: no reference to outer class (directly or indirectly) to ensure trust/key managers are eventually GCed once
    // there are no more use of the outer class and the underlying SSLContext
    private static class CryptoMaterialReloader implements Runnable {

        private final Path tlsOptionsConfigFile;
        private final ScheduledExecutorService scheduler;
        private final WeakReference<TlsManager> tlsManager;

        CryptoMaterialReloader(Path tlsOptionsConfigFile,
                               ScheduledExecutorService scheduler,
                               TlsManager tlsManager) {
            this.tlsOptionsConfigFile = tlsOptionsConfigFile;
            this.scheduler = scheduler;
            this.tlsManager = new WeakReference<>(tlsManager);
        }

        @Override
        public void run() {
            try {
                TlsManager tlsManager = this.tlsManager.get();
                if (tlsManager == null) {
                    scheduler.shutdown();
                    return;
                }
                TransportSecurityOptions options = TransportSecurityOptions.fromJsonFile(tlsOptionsConfigFile);
                reloadTrustManager(options, tlsManager.getTrustManager());
                MutableX509KeyManager keyManager = tlsManager.getKeyManager();
                reloadKeyManager(options, keyManager);
            } catch (Throwable t) {
                log.log(Level.SEVERE, String.format("Failed to reload crypto material (path='%s'): %s", tlsOptionsConfigFile, t.getMessage()), t);
            }
        }
    }

    // Static class to ensure no reference to outer class is contained
    private static class ReloaderThreadFactory implements ThreadFactory {
        Path fileName;
        ReloaderThreadFactory(Path fileName) {
            this.fileName = fileName;
        }
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r, "tls-context-reloader:" + fileName.toString() );
            thread.setDaemon(true);
            return thread;
        }
    }

    TlsManager(Path tlsOptionsConfigFile) {
        trustManager = new MutableX509TrustManager();
        keyManager = new MutableX509KeyManager();
        options = TransportSecurityOptions.fromJsonFile(tlsOptionsConfigFile);
        reloadTrustManager(options, trustManager);
        reloadKeyManager(options, keyManager);
        scheduler = Executors.newSingleThreadScheduledExecutor(new ReloaderThreadFactory(tlsOptionsConfigFile));
        this.scheduler.scheduleAtFixedRate(new CryptoMaterialReloader(tlsOptionsConfigFile, scheduler, this),
                UPDATE_PERIOD.getSeconds()/*initial delay*/,
                UPDATE_PERIOD.getSeconds(),
                TimeUnit.SECONDS);
    }
    MutableX509TrustManager getTrustManager() {
        return trustManager;
    }

    MutableX509KeyManager getKeyManager() {
        return keyManager;
    }

    TransportSecurityOptions getOptions() {
        return options;
    }
}