@@ -0,0 +1,3783 @@
+// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.%0Apackage com.yahoo.vespa.athenz.identity;%0A%0Aimport javax.net.ssl.SSLContext;%0Aimport javax.net.ssl.SSLSocketFactory;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.net.InetAddress;%0Aimport java.net.Socket;%0Aimport java.net.UnknownHostException;%0Aimport java.util.function.Supplier;%0A%0A/**%0A * A %7B@link SSLSocketFactory%7D that creates sockets through the %7B@link SSLContext%7D provided by %7B@link ServiceIdentityProvider%7D.%0A *%0A * Note: the implementation assumes that the set of default/supported cipher suites is static for a given %7B@link ServiceIdentityProvider%7D instance.%0A *%0A * @author bjorncs%0A */%0Apublic class ServiceIdentitySslSocketFactory extends SSLSocketFactory %7B%0A%0A    private final Object monitor = new Object();%0A    private final Supplier%3CSSLContext%3E sslContextSupplier;%0A    private final String%5B%5D defaultCipherSuites;%0A    private final String%5B%5D supportedCipherSuites;%0A    private SSLContext sslContext;%0A    private SSLSocketFactory sslSocketFactory;%0A%0A    public ServiceIdentitySslSocketFactory(ServiceIdentityProvider serviceIdentityProvider) %7B%0A        this(serviceIdentityProvider::getIdentitySslContext);%0A    %7D%0A%0A    public ServiceIdentitySslSocketFactory(Supplier%3CSSLContext%3E sslContextSupplier) %7B%0A        super();%0A        SSLContext sslContext = sslContextSupplier.get();%0A        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();%0A        this.sslContextSupplier = sslContextSupplier;%0A        this.defaultCipherSuites = sslSocketFactory.getDefaultCipherSuites();%0A        this.supportedCipherSuites = sslSocketFactory.getSupportedCipherSuites();%0A        this.sslContext = sslContext;%0A        this.sslSocketFactory = sslSocketFactory;%0A    %7D%0A%0A    private SSLSocketFactory currentSslSocketFactory() %7B%0A        SSLContext currentSslContext = sslContextSupplier.get();%0A        synchronized (monitor) %7B%0A            if (currentSslContext != sslContext) %7B%0A                sslContext = currentSslContext;%0A                sslSocketFactory = currentSslContext.getSocketFactory();%0A            %7D%0A            return sslSocketFactory;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public String%5B%5D getDefaultCipherSuites() %7B%0A        return defaultCipherSuites;%0A    %7D%0A%0A    @Override%0A    public String%5B%5D getSupportedCipherSuites() %7B%0A        return supportedCipherSuites;%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException %7B%0A        return currentSslSocketFactory().createSocket(s, host, port, autoClose);%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(String host, int port) throws IOException, UnknownHostException %7B%0A        return currentSslSocketFactory().createSocket(host, port);%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException %7B%0A        return currentSslSocketFactory().createSocket(host, port, localHost, localPort);%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(InetAddress host, int port) throws IOException %7B%0A        return currentSslSocketFactory().createSocket(host, port);%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException %7B%0A        return currentSslSocketFactory().createSocket(address, port, localAddress, localPort);%0A    %7D%0A%0A    @Override%0A    public Socket createSocket(Socket s, InputStream consumed, boolean autoClose) throws IOException %7B%0A        return currentSslSocketFactory().createSocket(s, consumed, autoClose);%0A    %7D%0A%0A    @Override%0A    public Socket createSocket() throws IOException %7B%0A        return currentSslSocketFactory().createSocket();%0A    %7D%0A%0A%7D%0A

Add SSLSocketFactory backed by ServiceIdentityProvider