summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/ssl/DefaultSslKeyStoreConfigurator.java
blob: fb0a5869bb3819331969dabbafcc1eca8cc55d4e (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.ssl;

import com.google.inject.Inject;
import com.yahoo.jdisc.http.ConnectorConfig;
import com.yahoo.jdisc.http.SecretStore;
import com.yahoo.jdisc.http.ssl.pem.PemSslKeyStore;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.util.logging.Logger;

/**
 * @author bjorncs
 */
public class DefaultSslKeyStoreConfigurator implements SslKeyStoreConfigurator {

    private static final Logger log = Logger.getLogger(DefaultSslKeyStoreConfigurator.class.getName());

    private final SecretStore secretStore;
    private final ConnectorConfig.Ssl config;

    @Inject
    public DefaultSslKeyStoreConfigurator(ConnectorConfig config, SecretStore secretStore) {
        validateConfig(config.ssl());
        this.secretStore = secretStore;
        this.config = config.ssl();
    }

    private static void validateConfig(ConnectorConfig.Ssl config) {
        if (!config.enabled()) return;
        switch (config.keyStoreType()) {
            case JKS:
                validateJksConfig(config);
                break;
            case PEM:
                validatePemConfig(config);
                break;
        }
    }

    @Override
    public void configure(SslKeyStoreContext context) {
        if (!config.enabled()) return;
        switch (config.keyStoreType()) {
            case JKS:
                context.updateKeyStore(config.keyStorePath(), "JKS", secretStore.getSecret(config.keyDbKey()));
                break;
            case PEM:
                context.updateKeyStore(createPemKeyStore(config.pemKeyStore()));
                break;
        }
    }

    private static void validateJksConfig(ConnectorConfig.Ssl ssl) {
        if (!ssl.pemKeyStore().keyPath().isEmpty() || ! ssl.pemKeyStore().certificatePath().isEmpty()) {
            throw new IllegalArgumentException("pemKeyStore attributes can not be set when keyStoreType is JKS.");
        }
        if (ssl.keyDbKey().isEmpty()) {
            throw new IllegalArgumentException("Missing password for JKS keystore");
        }
    }

    private static void validatePemConfig(ConnectorConfig.Ssl ssl) {
        if (! ssl.keyStorePath().isEmpty()) {
            throw new IllegalArgumentException("keyStorePath can not be set when keyStoreType is PEM");
        }
        if (!ssl.keyDbKey().isEmpty()) {
            // TODO Make an error once there are separate passwords for truststore and keystore
            log.warning("Encrypted PEM key stores are not supported. Password is only applied to truststore");
        }
        if (ssl.pemKeyStore().certificatePath().isEmpty()) {
            throw new IllegalArgumentException("Missing certificate path.");
        }
        if (ssl.pemKeyStore().keyPath().isEmpty()) {
            throw new IllegalArgumentException("Missing key path.");
        }
    }

    private static KeyStore createPemKeyStore(ConnectorConfig.Ssl.PemKeyStore pemKeyStore) {
        try {
            Path certificatePath = Paths.get(pemKeyStore.certificatePath());
            Path keyPath = Paths.get(pemKeyStore.keyPath());
            return new PemSslKeyStore(certificatePath, keyPath).loadJavaKeyStore();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } catch (Exception e) {
            throw new RuntimeException("Failed setting up key store for " + pemKeyStore.keyPath() + ", " + pemKeyStore.certificatePath(), e);
        }
    }

}