summaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identity/SiaIdentityProvider.java
blob: 4981b80998fa6c610bd931f9bc827cbdf559555e (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.identity;

import com.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.security.KeyStoreType;
import com.yahoo.security.SslContextBuilder;
import com.yahoo.security.tls.AutoReloadingX509KeyManager;
import com.yahoo.vespa.athenz.api.AthenzIdentity;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.athenz.utils.SiaUtils;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * A {@link ServiceIdentityProvider} that provides the credentials stored on file system.
 *
 * @author mortent
 * @author bjorncs
 */
public class SiaIdentityProvider extends AbstractComponent implements ServiceIdentityProvider {

    private final AutoReloadingX509KeyManager keyManager;
    private final SSLContext sslContext;
    private final AthenzIdentity service;

    @Inject
    public SiaIdentityProvider(SiaProviderConfig config) {
        this(new AthenzService(config.athenzDomain(), config.athenzService()),
             SiaUtils.getPrivateKeyFile(Paths.get(config.keyPathPrefix()), new AthenzService(config.athenzDomain(), config.athenzService())).toFile(),
             SiaUtils.getCertificateFile(Paths.get(config.keyPathPrefix()), new AthenzService(config.athenzDomain(), config.athenzService())).toFile(),
             new File(config.trustStorePath()),
             config.trustStoreType());
    }

    public SiaIdentityProvider(AthenzIdentity service,
                               Path siaPath,
                               File trustStoreFile) {
        this(service,
             SiaUtils.getPrivateKeyFile(siaPath, service).toFile(),
             SiaUtils.getCertificateFile(siaPath, service).toFile(),
             trustStoreFile,
             SiaProviderConfig.TrustStoreType.Enum.jks);
    }

    public SiaIdentityProvider(AthenzIdentity service,
                               File privateKeyFile,
                               File certificateFile,
                               File trustStoreFile,
                               SiaProviderConfig.TrustStoreType.Enum trustStoreType) {
        this.service = service;
        this.keyManager = AutoReloadingX509KeyManager.fromPemFiles(privateKeyFile.toPath(), certificateFile.toPath());
        this.sslContext = createIdentitySslContext(keyManager, trustStoreFile.toPath(), trustStoreType);
    }

    @Override
    public AthenzIdentity identity() {
        return service;
    }

    @Override
    public SSLContext getIdentitySslContext() {
        return sslContext;
    }

    private static SSLContext createIdentitySslContext(AutoReloadingX509KeyManager keyManager, Path trustStoreFile,
                                                       SiaProviderConfig.TrustStoreType.Enum trustStoreType) {
        var builder = new SslContextBuilder();
        if (trustStoreType == SiaProviderConfig.TrustStoreType.Enum.pem) {
            builder = builder.withTrustStore(trustStoreFile);
        } else if (trustStoreType == SiaProviderConfig.TrustStoreType.Enum.jks) {
            builder = builder.withTrustStore(trustStoreFile, KeyStoreType.JKS);
        } else {
            throw new IllegalArgumentException("Unsupported trust store type: " + trustStoreType);
        }
        return builder.withKeyManager(keyManager).build();
    }

    @Override
    public void deconstruct() {
        keyManager.close();
    }

}