From f1e0eafcd04f228d7867b6716ff73a4ab14f9284 Mon Sep 17 00:00:00 2001 From: Min Jin Date: Mon, 9 Dec 2024 13:43:03 -0800 Subject: [PATCH] merge TokenFile auth with refresh auth Signed-off-by: Min Jin --- .../credentials/RefreshAuthentication.java | 1 - .../credentials/TokenFileAuthentication.java | 51 +++++-------------- 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/util/src/main/java/io/kubernetes/client/util/credentials/RefreshAuthentication.java b/util/src/main/java/io/kubernetes/client/util/credentials/RefreshAuthentication.java index f03c610cb1..33d88bb5d4 100644 --- a/util/src/main/java/io/kubernetes/client/util/credentials/RefreshAuthentication.java +++ b/util/src/main/java/io/kubernetes/client/util/credentials/RefreshAuthentication.java @@ -28,7 +28,6 @@ // 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. -// TODO: Merge this with TokenFileAuthentication. public class RefreshAuthentication implements Authentication, Interceptor { private Instant expiry; private Duration refreshPeriod; diff --git a/util/src/main/java/io/kubernetes/client/util/credentials/TokenFileAuthentication.java b/util/src/main/java/io/kubernetes/client/util/credentials/TokenFileAuthentication.java index 2afe0df7d7..942cca4f91 100644 --- a/util/src/main/java/io/kubernetes/client/util/credentials/TokenFileAuthentication.java +++ b/util/src/main/java/io/kubernetes/client/util/credentials/TokenFileAuthentication.java @@ -17,6 +17,7 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; +import java.time.Duration; import java.time.Instant; import okhttp3.Interceptor; import okhttp3.OkHttpClient; @@ -26,49 +27,23 @@ // 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 class TokenFileAuthentication extends RefreshAuthentication { 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; + this(file, Duration.ofMinutes(1)); } - public void setExpiry(Instant expiry) { - this.expiry = expiry; + public TokenFileAuthentication(String file, Duration refreshPeriod) { + super(() -> { + return getToken(file); + }, refreshPeriod); } - 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); + private static String getToken(String file) { + try { + return new String(Files.readAllBytes(Paths.get(file)), Charset.defaultCharset()).trim(); + } catch (IOException e) { + throw new RuntimeException("Cannot read file: " + file); + } } - @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); - } }