Search in sources :

Example 1 with GCPAuthenticator

use of io.kubernetes.client.util.authenticators.GCPAuthenticator in project java by kubernetes-client.

the class KubeConfigTest method testGCPAuthProvider.

@Test
public void testGCPAuthProvider() {
    KubeConfig.registerAuthenticator(new GCPAuthenticator());
    try {
        File config = folder.newFile("config");
        FileWriter writer = new FileWriter(config);
        writer.write(GCP_CONFIG);
        writer.flush();
        writer.close();
        KubeConfig kc = KubeConfig.loadKubeConfig(new FileReader(config));
        assertEquals("fake-token", kc.getAccessToken());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex);
    }
}
Also used : GCPAuthenticator(io.kubernetes.client.util.authenticators.GCPAuthenticator) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) File(java.io.File) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with GCPAuthenticator

use of io.kubernetes.client.util.authenticators.GCPAuthenticator in project java by kubernetes-client.

the class KubeConfigTest method testGCPAuthProviderStringDate.

@Test
public void testGCPAuthProviderStringDate() {
    KubeConfig.registerAuthenticator(new GCPAuthenticator());
    try {
        File config = folder.newFile("config");
        FileWriter writer = new FileWriter(config);
        writer.write(GCP_TEST_DATE_STRING);
        writer.flush();
        writer.close();
        KubeConfig kc = KubeConfig.loadKubeConfig(new FileReader(config));
        assertEquals("fake-token", kc.getAccessToken());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex);
    }
}
Also used : GCPAuthenticator(io.kubernetes.client.util.authenticators.GCPAuthenticator) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) File(java.io.File) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with GCPAuthenticator

use of io.kubernetes.client.util.authenticators.GCPAuthenticator in project java by kubernetes-client.

the class KubeConfigTest method testGCPAuthProviderExpiredTokenWithinGCloud.

@Test
public void testGCPAuthProviderExpiredTokenWithinGCloud() {
    String gcpConfigExpiredToken = "apiVersion: v1\n" + "contexts:\n" + "- context:\n" + "    user: gke-cluster\n" + "  name: foo-context\n" + "current-context: foo-context\n" + "users:\n" + "- name: gke-cluster\n" + "  user:\n" + "    auth-provider:\n" + "      config:\n" + "        access-token: fake-token\n" + "        cmd-args: config config-helper --format=json\n" + "        cmd-path: /usr/lib/google-cloud-sdk/bin/gcloud\n" + "        expiry: 1970-01-01T00:00:00Z\n" + "        expiry-key: '{.credential.token_expiry}'\n" + "        token-key: '{.credential.access_token}'\n" + "      name: gcp";
    String fakeExecResult = "{\n" + "  \"credential\": {\n" + "    \"access_token\": \"new-fake-token\",\n" + "    \"id_token\": \"id-fake-token\",\n" + "    \"token_expiry\": \"2121-08-05T02:30:24Z\"\n" + "  }\n" + "}";
    ProcessBuilder mockPB = Mockito.mock(ProcessBuilder.class, Mockito.RETURNS_DEEP_STUBS);
    Process mockProcess = Mockito.mock(Process.class);
    Mockito.when(mockProcess.exitValue()).thenReturn(0);
    Mockito.when(mockProcess.getInputStream()).thenReturn(new ByteArrayInputStream(fakeExecResult.getBytes(StandardCharsets.UTF_8)));
    try {
        Mockito.when(mockPB.command(Mockito.anyList()).start()).thenReturn(mockProcess);
    } catch (IOException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex);
    }
    KubeConfig.registerAuthenticator(new GCPAuthenticator(mockPB, null));
    try {
        KubeConfig kc = KubeConfig.loadKubeConfig(new StringReader(gcpConfigExpiredToken));
        assertEquals("new-fake-token", kc.getAccessToken());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex);
    }
}
Also used : GCPAuthenticator(io.kubernetes.client.util.authenticators.GCPAuthenticator) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with GCPAuthenticator

use of io.kubernetes.client.util.authenticators.GCPAuthenticator in project java by kubernetes-client.

the class KubeConfigTest method testGCPAuthProviderExpiredTokenWithoutGCloud.

@Test
public void testGCPAuthProviderExpiredTokenWithoutGCloud() {
    String gcpConfigExpiredToken = "apiVersion: v1\n" + "contexts:\n" + "- context:\n" + "    user: gke-cluster\n" + "  name: foo-context\n" + "current-context: foo-context\n" + "users:\n" + "- name: gke-cluster\n" + "  user:\n" + "    auth-provider:\n" + "      config:\n" + "        access-token: fake-token\n" + "        expiry: 1970-01-01T00:00:00Z\n" + "      name: gcp";
    String fakeToken = "new-fake-token";
    String fakeTokenExpiry = "2121-08-05T02:30:24Z";
    GoogleCredentials mockGC = Mockito.mock(GoogleCredentials.class);
    Mockito.when(mockGC.getAccessToken()).thenReturn(new AccessToken(fakeToken, Date.from(Instant.parse(fakeTokenExpiry))));
    KubeConfig.registerAuthenticator(new GCPAuthenticator(null, mockGC));
    try {
        KubeConfig kc = KubeConfig.loadKubeConfig(new StringReader(gcpConfigExpiredToken));
        assertEquals(fakeToken, kc.getAccessToken());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex);
    }
}
Also used : GCPAuthenticator(io.kubernetes.client.util.authenticators.GCPAuthenticator) AccessToken(com.google.auth.oauth2.AccessToken) StringReader(java.io.StringReader) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

GCPAuthenticator (io.kubernetes.client.util.authenticators.GCPAuthenticator)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 File (java.io.File)2 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2 StringReader (java.io.StringReader)2 AccessToken (com.google.auth.oauth2.AccessToken)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1