aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/ssl/DefaultSslTrustStoreContext.java
blob: c2d91cca3ea6b4fed529e347a6b2c8156e531d4b (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
// 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 org.eclipse.jetty.util.ssl.SslContextFactory;

import java.security.KeyStore;
import java.util.function.Consumer;

/**
 * @author bjorncs
 */
public class DefaultSslTrustStoreContext implements SslTrustStoreContext {

    private final SslContextFactory sslContextFactory;

    public DefaultSslTrustStoreContext(SslContextFactory sslContextFactory) {
        this.sslContextFactory = sslContextFactory;
    }

    @Override
    public void updateTrustStore(KeyStore trustStore) {
        updateTrustStore(trustStore, null);
    }

    @Override
    public void updateTrustStore(KeyStore trustStore, String password) {
        updateTrustStore(sslContextFactory -> {
            sslContextFactory.setTrustStore(trustStore);
            if (password != null) {
                sslContextFactory.setTrustStorePassword(password);
            }
        });
    }

    @Override
    public void updateTrustStore(String trustStorePath, String trustStoreType, String trustStorePassword) {
        updateTrustStore(sslContextFactory -> {
            sslContextFactory.setTrustStorePath(trustStorePath);
            sslContextFactory.setTrustStoreType(trustStoreType);
            if (trustStorePassword != null) {
                sslContextFactory.setTrustStorePassword(trustStorePassword);
            }
        });
    }

    private void updateTrustStore(Consumer<SslContextFactory> reloader) {
        try {
            sslContextFactory.reload(reloader);
        } catch (Exception e) {
            throw new RuntimeException("Could not update truststore: " + e.getMessage(), e);
        }
    }

}