Search in sources :

Example 1 with CertificateStreamProvider

use of org.jgroups.protocols.kubernetes.stream.CertificateStreamProvider in project jgroups-kubernetes by jgroups-extras.

the class CertsTest method testCerts.

@Test
public void testCerts() throws Exception {
    String clientCertFile = getValue("KUBERNETES_CLIENT_CERTIFICATE_FILE");
    String clientKeyFile = getValue("KUBERNETES_CLIENT_KEY_FILE");
    String clientKeyPassword = getValue("KUBERNETES_CLIENT_KEY_PASSWORD");
    String clientKeyAlgo = getValue("KUBERNETES_CLIENT_KEY_ALGO");
    String caCertFile = getValue("KUBERNETES_CA_CERTIFICATE_FILE");
    if (clientCertFile == null) {
        return;
    }
    CertificateStreamProvider certStreamProvider = new CertificateStreamProvider(clientCertFile, clientKeyFile, clientKeyPassword, clientKeyAlgo, caCertFile);
    String k8s_master = getValue("KUBERNETES_MASTER");
    String apiVersion = getValue("API_VERSION", "v1beta1");
    String op = getValue("OP", "pods");
    try (InputStream is = certStreamProvider.openStream(String.format("%s/api/%s/%s", k8s_master, apiVersion, op), null, 0, 0)) {
        int x;
        while ((x = is.read()) != -1) {
            System.out.print((char) x);
        }
    }
}
Also used : CertificateStreamProvider(org.jgroups.protocols.kubernetes.stream.CertificateStreamProvider) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 2 with CertificateStreamProvider

use of org.jgroups.protocols.kubernetes.stream.CertificateStreamProvider in project jgroups-kubernetes by jgroups-extras.

the class KUBE_PING method init.

public void init() throws Exception {
    super.init();
    tp_bind_port = transport.getBindPort();
    if (tp_bind_port <= 0)
        throw new IllegalArgumentException(String.format("%s only works with  %s.bind_port > 0", KUBE_PING.class.getSimpleName(), transport.getClass().getSimpleName()));
    checkDeprecatedProperties();
    if (namespace == null) {
        log.warn("namespace not set; clustering disabled");
        // no further initialization necessary
        return;
    }
    log.info("namespace %s set; clustering enabled", namespace);
    Map<String, String> headers = new HashMap<>();
    StreamProvider streamProvider;
    if (clientCertFile != null) {
        if (masterProtocol == null)
            masterProtocol = "http";
        streamProvider = new CertificateStreamProvider(clientCertFile, clientKeyFile, clientKeyPassword, clientKeyAlgo, caCertFile);
    } else {
        String saToken = readFileToString(saTokenFile);
        if (saToken != null) {
            // curl -k -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
            // https://172.30.0.2:443/api/v1/namespaces/dward/pods?labelSelector=application%3Deap-app
            headers.put("Authorization", "Bearer " + saToken);
        }
        streamProvider = new TokenStreamProvider(saToken, caCertFile);
    }
    String url = String.format("%s://%s:%s/api/%s", masterProtocol, masterHost, masterPort, apiVersion);
    client = new Client(url, headers, connectTimeout, readTimeout, operationAttempts, operationSleep, streamProvider, log);
    log.debug("KubePING configuration: " + toString());
}
Also used : CertificateStreamProvider(org.jgroups.protocols.kubernetes.stream.CertificateStreamProvider) StreamProvider(org.jgroups.protocols.kubernetes.stream.StreamProvider) TokenStreamProvider(org.jgroups.protocols.kubernetes.stream.TokenStreamProvider) CertificateStreamProvider(org.jgroups.protocols.kubernetes.stream.CertificateStreamProvider) TokenStreamProvider(org.jgroups.protocols.kubernetes.stream.TokenStreamProvider) Utils.readFileToString(org.jgroups.protocols.kubernetes.Utils.readFileToString)

Aggregations

CertificateStreamProvider (org.jgroups.protocols.kubernetes.stream.CertificateStreamProvider)2 InputStream (java.io.InputStream)1 Utils.readFileToString (org.jgroups.protocols.kubernetes.Utils.readFileToString)1 StreamProvider (org.jgroups.protocols.kubernetes.stream.StreamProvider)1 TokenStreamProvider (org.jgroups.protocols.kubernetes.stream.TokenStreamProvider)1 Test (org.junit.Test)1