-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #967 from zshihang/master
reload service account token after expiry
- Loading branch information
Showing
6 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
util/src/main/java/io/kubernetes/client/util/credentials/TokenFileAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.kubernetes.client.util.credentials; | ||
|
||
import io.kubernetes.client.openapi.ApiClient; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.time.Instant; | ||
import okhttp3.Interceptor; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
// TODO: prefer OpenAPI backed Auentication once it is available. see details in | ||
// https://github.com/OpenAPITools/openapi-generator/pull/6036. currently, the | ||
// workaround is to hijack the http request. | ||
public class TokenFileAuthentication implements Authentication, Interceptor { | ||
private String file; | ||
private String token; | ||
private Instant expiry; | ||
|
||
public TokenFileAuthentication(String file) { | ||
this.expiry = Instant.MIN; | ||
this.file = file; | ||
} | ||
|
||
private String getToken() { | ||
if (Instant.now().isAfter(this.expiry)) { | ||
try { | ||
this.token = | ||
new String(Files.readAllBytes(Paths.get(this.file)), Charset.defaultCharset()).trim(); | ||
expiry = Instant.now().plusSeconds(60); | ||
} catch (IOException ie) { | ||
throw new RuntimeException("Cannot read file: " + this.file); | ||
} | ||
} | ||
|
||
return this.token; | ||
} | ||
|
||
public void setExpiry(Instant expiry) { | ||
this.expiry = expiry; | ||
} | ||
|
||
public void setFile(String file) { | ||
this.file = file; | ||
} | ||
|
||
@Override | ||
public void provide(ApiClient client) { | ||
OkHttpClient withInterceptor = client.getHttpClient().newBuilder().addInterceptor(this).build(); | ||
client.setHttpClient(withInterceptor); | ||
} | ||
|
||
@Override | ||
public Response intercept(Interceptor.Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
Request newRequest; | ||
newRequest = request.newBuilder().header("Authorization", "Bearer " + getToken()).build(); | ||
return chain.proceed(newRequest); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
util/src/test/java/io/kubernetes/client/util/credentials/TokenFileAuthenticationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.kubernetes.client.util.credentials; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
|
||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import com.google.common.io.Resources; | ||
import io.kubernetes.client.openapi.ApiClient; | ||
import io.kubernetes.client.openapi.ApiException; | ||
import io.kubernetes.client.openapi.Configuration; | ||
import io.kubernetes.client.openapi.apis.CoreV1Api; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class TokenFileAuthenticationTest { | ||
private static final String SERVICEACCOUNT_TOKEN1_PATH = | ||
Resources.getResource("token1").getPath(); | ||
private static final String SERVICEACCOUNT_TOKEN2_PATH = | ||
Resources.getResource("token2").getPath(); | ||
private static final int PORT = 8089; | ||
private TokenFileAuthentication auth; | ||
|
||
@Rule public WireMockRule wireMockRule = new WireMockRule(PORT); | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
final ApiClient client = new ApiClient(); | ||
client.setBasePath("http://localhost:" + PORT); | ||
this.auth = new TokenFileAuthentication(SERVICEACCOUNT_TOKEN1_PATH); | ||
this.auth.provide(client); | ||
Configuration.setDefaultApiClient(client); | ||
} | ||
|
||
@Test | ||
public void testTokenProvided() throws IOException, ApiException { | ||
stubFor( | ||
get(urlPathEqualTo("/api/v1/pods")).willReturn(okForContentType("application/json", "{}"))); | ||
CoreV1Api api = new CoreV1Api(); | ||
|
||
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); | ||
WireMock.verify( | ||
1, | ||
getRequestedFor(urlPathEqualTo("/api/v1/pods")) | ||
.withHeader("Authorization", equalTo("Bearer token1"))); | ||
|
||
this.auth.setFile(SERVICEACCOUNT_TOKEN2_PATH); | ||
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); | ||
WireMock.verify( | ||
2, | ||
getRequestedFor(urlPathEqualTo("/api/v1/pods")) | ||
.withHeader("Authorization", equalTo("Bearer token1"))); | ||
|
||
this.auth.setExpiry(Instant.now().minusSeconds(1)); | ||
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); | ||
WireMock.verify( | ||
1, | ||
getRequestedFor(urlPathEqualTo("/api/v1/pods")) | ||
.withHeader("Authorization", equalTo("Bearer token2"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
token1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
token2 |