|
| 1 | +package org.openshift.ping.common.stream; |
| 2 | + |
| 3 | +import static org.openshift.ping.common.Utils.openFile; |
| 4 | + |
| 5 | +import java.io.FileNotFoundException; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.net.URLConnection; |
| 10 | +import java.security.KeyStore; |
| 11 | +import java.security.cert.CertificateFactory; |
| 12 | +import java.security.cert.X509Certificate; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.logging.Level; |
| 15 | +import java.util.logging.Logger; |
| 16 | + |
| 17 | +import javax.net.ssl.HttpsURLConnection; |
| 18 | +import javax.net.ssl.SSLContext; |
| 19 | +import javax.net.ssl.SSLSocketFactory; |
| 20 | +import javax.net.ssl.TrustManager; |
| 21 | +import javax.net.ssl.TrustManagerFactory; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author <a href="mailto:[email protected]">Ricardo Martinelli</a> |
| 25 | + */ |
| 26 | +public class TokenStreamProvider extends BaseStreamProvider { |
| 27 | + |
| 28 | + private static final Logger log = Logger.getLogger(TokenStreamProvider.class.getName()); |
| 29 | + |
| 30 | + private String token; |
| 31 | + |
| 32 | + private String caCertFile; |
| 33 | + |
| 34 | + private SSLSocketFactory factory; |
| 35 | + |
| 36 | + public TokenStreamProvider(String token, String caCertFile) { |
| 37 | + this.token = token; |
| 38 | + this.caCertFile = caCertFile; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public InputStream openStream(String url, Map<String, String> headers, int connectTimeout, int readTimeout) |
| 43 | + throws IOException { |
| 44 | + URLConnection connection = openConnection(url, headers, connectTimeout, readTimeout); |
| 45 | + |
| 46 | + if (connection instanceof HttpsURLConnection) { |
| 47 | + HttpsURLConnection httpsConnection = HttpsURLConnection.class.cast(connection); |
| 48 | + //httpsConnection.setHostnameVerifier(InsecureStreamProvider.INSECURE_HOSTNAME_VERIFIER); |
| 49 | + httpsConnection.setSSLSocketFactory(getSSLSocketFactory()); |
| 50 | + if (log.isLoggable(Level.FINE)) { |
| 51 | + log.fine(String.format("Using HttpsURLConnection with SSLSocketFactory [%s] for url [%s].", factory, url)); |
| 52 | + } |
| 53 | + } else { |
| 54 | + if (log.isLoggable(Level.FINE)) { |
| 55 | + log.fine(String.format("Using URLConnection for url [%s].", url)); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (token != null) { |
| 60 | + // curl -k -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \ |
| 61 | + // https://172.30.0.2:443/api/v1/namespaces/dward/pods?labelSelector=application%3Deap-app |
| 62 | + headers.put("Authorization", "Bearer " + token); |
| 63 | + } |
| 64 | + return connection.getInputStream(); |
| 65 | + } |
| 66 | + |
| 67 | + private TrustManager[] configureCaCert(String caCertFile) throws Exception { |
| 68 | + if (caCertFile != null) { |
| 69 | + try { |
| 70 | + InputStream pemInputStream = openFile(caCertFile); |
| 71 | + try { |
| 72 | + CertificateFactory certFactory = CertificateFactory.getInstance("X509"); |
| 73 | + X509Certificate cert = (X509Certificate)certFactory.generateCertificate(pemInputStream); |
| 74 | + |
| 75 | + KeyStore trustStore = KeyStore.getInstance("JKS"); |
| 76 | + trustStore.load(null); |
| 77 | + |
| 78 | + String alias = cert.getSubjectX500Principal().getName(); |
| 79 | + trustStore.setCertificateEntry(alias, cert); |
| 80 | + |
| 81 | + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 82 | + trustManagerFactory.init(trustStore); |
| 83 | + |
| 84 | + return trustManagerFactory.getTrustManagers(); |
| 85 | + } finally { |
| 86 | + pemInputStream.close(); |
| 87 | + } |
| 88 | + } catch (FileNotFoundException fnfe) { |
| 89 | + log.log(Level.SEVERE, "ca cert file not found: " + caCertFile); |
| 90 | + throw fnfe; |
| 91 | + } catch (Exception e) { |
| 92 | + log.log(Level.SEVERE, "Could not create trust manager for " + caCertFile, e); |
| 93 | + throw e; |
| 94 | + } |
| 95 | + } else { |
| 96 | + log.log(Level.WARNING, "ca cert file undefined"); |
| 97 | + return InsecureStreamProvider.INSECURE_TRUST_MANAGERS; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private SSLSocketFactory getSSLSocketFactory() throws IOException { |
| 102 | + if(this.factory == null) { |
| 103 | + synchronized(this) { |
| 104 | + if(this.factory == null) { |
| 105 | + try { |
| 106 | + TrustManager[] trustManagers = configureCaCert(this.caCertFile); |
| 107 | + SSLContext context = SSLContext.getInstance("TLS"); |
| 108 | + context.init(null, trustManagers, null); |
| 109 | + this.factory = context.getSocketFactory(); |
| 110 | + } catch(Exception e) { |
| 111 | + throw new IOException(e); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return this.factory; |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments