Skip to content

Commit 77214fb

Browse files
authored
Merge pull request #25 from rimolive/rmartine-cloud-1162
Add a token based implementation for kube-ping
2 parents 59a4421 + 49bae15 commit 77214fb

File tree

2 files changed

+124
-8
lines changed

2 files changed

+124
-8
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

kube/src/main/java/org/openshift/ping/kube/KubePing.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import org.jgroups.conf.ClassConfigurator;
3333
import org.openshift.ping.common.OpenshiftPing;
3434
import org.openshift.ping.common.stream.CertificateStreamProvider;
35-
import org.openshift.ping.common.stream.InsecureStreamProvider;
3635
import org.openshift.ping.common.stream.StreamProvider;
36+
import org.openshift.ping.common.stream.TokenStreamProvider;
3737

3838
/**
3939
* @author <a href="mailto:[email protected]">Ales Justin</a>
@@ -88,7 +88,7 @@ public class KubePing extends OpenshiftPing {
8888
private String clientKeyAlgo = "RSA";
8989

9090
@Property
91-
private String caCertFile;
91+
private String caCertFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
9292

9393
@Property
9494
private String saTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token";
@@ -166,12 +166,9 @@ public void init() throws Exception {
166166
mHost = getSystemEnv(new String[]{getSystemEnvName("MASTER_HOST"), "KUBERNETES_SERVICE_HOST"}, masterHost, true);
167167
mPort = getSystemEnvInt(new String[]{getSystemEnvName("MASTER_PORT"), "KUBERNETES_SERVICE_PORT"}, masterPort);
168168
String saToken = readFileToString(getSystemEnv(getSystemEnvName("SA_TOKEN_FILE"), saTokenFile, true));
169-
if (saToken != null) {
170-
// curl -k -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
171-
// https://172.30.0.2:443/api/v1/namespaces/dward/pods?labelSelector=application%3Deap-app
172-
headers.put("Authorization", "Bearer " + saToken);
173-
}
174-
streamProvider = new InsecureStreamProvider();
169+
String lCaCertFile = getSystemEnv(new String[]{getSystemEnvName("CA_CERT_FILE"), "KUBERNETES_CA_CERTIFICATE_FILE"}, caCertFile, true);
170+
171+
streamProvider = new TokenStreamProvider(saToken, lCaCertFile);
175172
}
176173
String ver = getSystemEnv(getSystemEnvName("API_VERSION"), apiVersion, true);
177174
String url = String.format("%s://%s:%s/api/%s", mProtocol, mHost, mPort, ver);

0 commit comments

Comments
 (0)