-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the hazelcast discovery to use the service account and https. #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,21 @@ | |
import com.hazelcast.core.Hazelcast; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.security.cert.X509Certificate; | ||
import java.security.KeyManagementException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.X509TrustManager; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.CommandLineRunner; | ||
|
@@ -43,46 +55,70 @@ public class HazelcastDiscoveryController implements CommandLineRunner { | |
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
static class Address { | ||
|
||
public String IP; | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
static class Subset { | ||
|
||
public List<Address> addresses; | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
static class Endpoints { | ||
|
||
public List<Subset> subsets; | ||
} | ||
|
||
private static String getServiceAccountToken() throws IOException { | ||
String file = "/var/run/secrets/kubernetes.io/serviceaccount/token"; | ||
return new String(Files.readAllBytes(Paths.get(file))); | ||
} | ||
|
||
private static String getEnvOrDefault(String var, String def) { | ||
final String val = System.getenv(var); | ||
return (val == null || val.isEmpty()) | ||
? def | ||
: val; | ||
} | ||
|
||
// TODO: Load the CA cert when it is available on all platforms. | ||
private static TrustManager[] trustAll = new TrustManager[] { | ||
new X509TrustManager() { | ||
public void checkServerTrusted(X509Certificate[] certs, String authType) {} | ||
public void checkClientTrusted(X509Certificate[] certs, String authType) {} | ||
public X509Certificate[] getAcceptedIssuers() { return null; } | ||
} | ||
}; | ||
private static HostnameVerifier trustAllHosts = new HostnameVerifier() { | ||
public boolean verify(String hostname, SSLSession session) { | ||
return true; | ||
} | ||
}; | ||
|
||
@Override | ||
public void run(String... args) { | ||
final String hostName = getEnvOrDefault("KUBERNETES_RO_SERVICE_HOST", | ||
"localhost"); | ||
final String hostPort = getEnvOrDefault("KUBERNETES_RO_SERVICE_PORT", | ||
"8080"); | ||
String serviceName = getEnvOrDefault("HAZELCAST_SERVICE", "hazelcast"); | ||
String path = "/api/v1beta3/namespaces/default/endpoints/"; | ||
final String host = "http://" + hostName + ":" + hostPort; | ||
final String host = "https://kubernetes.default.cluster.local"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last time I checked kubernetes.local was the default domain. Has this changed? Also, what happens if the user specifies a different domain? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it was changed in: I suppose we could customize it, but given the auth stuff, it's going to be fairly complicated to get it all working correctly. |
||
log.info("Asking k8s registry at {}..", host); | ||
|
||
final List<String> hazelcastEndpoints = new CopyOnWriteArrayList<>(); | ||
|
||
try { | ||
String token = getServiceAccountToken(); | ||
|
||
SSLContext ctx = SSLContext.getInstance("SSL"); | ||
ctx.init(null, trustAll, new SecureRandom()); | ||
|
||
URL url = new URL(host + path + serviceName); | ||
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(); | ||
// TODO: remove this when and replace with CA cert loading, when the CA is propogated | ||
// to all nodes on all platforms. | ||
conn.setSSLSocketFactory(ctx.getSocketFactory()); | ||
conn.setHostnameVerifier(trustAllHosts); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO to remove once a CA is available There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
conn.addRequestProperty("Authorization", "Bearer " + token); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
Endpoints endpoints = mapper.readValue(url, Endpoints.class); | ||
Endpoints endpoints = mapper.readValue(conn.getInputStream(), Endpoints.class); | ||
if (endpoints != null) { | ||
// Here is a problem point, endpoints.endpoints can be null in first node cases. | ||
if (endpoints.subsets != null && !endpoints.subsets.isEmpty()) { | ||
|
@@ -92,7 +128,7 @@ public void run(String... args) { | |
}); | ||
} | ||
} | ||
} catch (IOException ex) { | ||
} catch (IOException | NoSuchAlgorithmException | KeyManagementException ex) { | ||
log.warn("Request to Kubernetes API failed", ex); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this file provisioned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's automatically included in all pods now. See:
kubernetes/kubernetes#7101
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this present in 0.17.0? Cause
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't. 0.17.1 has just been tagged. Let me give it a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's in 0.17.1
Brendan
On May 21, 2015 1:48 PM, "Paulo Pires" notifications@github.com wrote: