aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/tls/ControllerSslContextFactoryProvider.java
blob: 09f8de403789c4da6d2d35e5df4b62284fa40c28 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.tls;

import com.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.container.jdisc.secretstore.SecretStore;
import com.yahoo.jdisc.http.ssl.SslContextFactoryProvider;
import com.yahoo.security.KeyStoreBuilder;
import com.yahoo.security.KeyStoreType;
import com.yahoo.security.KeyUtils;
import com.yahoo.security.X509CertificateUtils;
import com.yahoo.vespa.hosted.controller.tls.config.TlsConfig;
import org.eclipse.jetty.util.ssl.SslContextFactory;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Configures the controller's HTTPS connector with certificate and private key from a secret store.
 *
 * @author mpolden
 */
@SuppressWarnings("unused") // Injected
public class ControllerSslContextFactoryProvider extends AbstractComponent implements SslContextFactoryProvider {

    private final SecretStore secretStore;
    private final TlsConfig config;
    private final SslContextFactory sslContextFactory;

    @Inject
    public ControllerSslContextFactoryProvider(SecretStore secretStore, TlsConfig config) {
        this.secretStore = Objects.requireNonNull(secretStore, "secretStore must be non-null");
        this.config = Objects.requireNonNull(config, "config must be non-null");
        this.sslContextFactory = create();
    }

    @Override
    public SslContextFactory getInstance(String containerId, int port) {
        return sslContextFactory;
    }

    /** Create a SslContextFactory backed by an in-memory key and trust store */
    private SslContextFactory create() {
        if (!Files.isReadable(Paths.get(config.caTrustStore()))) {
            throw new IllegalArgumentException("CA trust store file is not readable: " + config.caTrustStore());
        }
        SslContextFactory factory = new SslContextFactory();

        // Do not exclude TLS_RSA_* ciphers
        String[] excludedCiphers = Arrays.stream(factory.getExcludeCipherSuites())
                                         .filter(cipherPattern -> !cipherPattern.equals("^TLS_RSA_.*$"))
                                         .toArray(String[]::new);
        factory.setExcludeCipherSuites(excludedCiphers);
        factory.setWantClientAuth(true);

        // Trust store containing CA trust store from file
        factory.setTrustStore(KeyStoreBuilder.withType(KeyStoreType.JKS)
                                             .fromFile(Paths.get(config.caTrustStore()))
                                             .build());

        // Key store containing key pair from secret store
        factory.setKeyStore(KeyStoreBuilder.withType(KeyStoreType.JKS)
                                           .withKeyEntry(getClass().getSimpleName(), privateKey(), certificates())
                                           .build());

        factory.setKeyStorePassword("");
        return factory;
    }

    /** Get private key from secret store */
    private PrivateKey privateKey() {
        return KeyUtils.fromPemEncodedPrivateKey(secretStore.getSecret(config.privateKeySecret()));
    }

    /**
     * Get certificate from secret store. If certificate secret contains multiple certificates, e.g. intermediate
     * certificates, the entire chain will be read
     */
    private List<X509Certificate> certificates() {
        return X509CertificateUtils.certificateListFromPem(secretStore.getSecret(config.certificateSecret()));
    }

}