-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Refactor: Merge TokenFile auth with refresh auth #3817
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-client:master
from
yue9944882:refactor/merge-token-file-auth
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions
19
util/src/main/java/io/kubernetes/client/util/exception/TokenAquisitionException.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,19 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package io.kubernetes.client.util.exception; | ||
|
||
public class TokenAquisitionException extends RuntimeException { | ||
public TokenAquisitionException(Exception exception) { | ||
super(exception); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -42,17 +42,19 @@ class TokenFileAuthenticationTest { | |
static WireMockExtension apiServer = | ||
WireMockExtension.newInstance().options(options().dynamicPort()).build(); | ||
|
||
private ApiClient apiClient; | ||
|
||
@BeforeEach | ||
void setup() { | ||
final ApiClient client = new ApiClient(); | ||
client.setBasePath("http://localhost:" + apiServer.getPort()); | ||
this.auth = new TokenFileAuthentication(SERVICEACCOUNT_TOKEN1_PATH); | ||
this.auth.provide(client); | ||
Configuration.setDefaultApiClient(client); | ||
this.apiClient = new ApiClient(); | ||
this.apiClient.setBasePath("http://localhost:" + apiServer.getPort()); | ||
Configuration.setDefaultApiClient(this.apiClient); | ||
} | ||
|
||
@Test | ||
void tokenProvided() throws ApiException { | ||
void tokenProvidedTokenNormalFileReadShouldWork() throws ApiException { | ||
Authentication authn = new TokenFileAuthentication(SERVICEACCOUNT_TOKEN1_PATH); | ||
authn.provide(this.apiClient); | ||
apiServer.stubFor( | ||
get(urlPathEqualTo("/api/v1/pods")).willReturn(okForContentType("application/json", | ||
"{\"items\":[]}"))); | ||
|
@@ -63,19 +65,6 @@ void tokenProvided() throws ApiException { | |
1, | ||
getRequestedFor(urlPathEqualTo("/api/v1/pods")) | ||
.withHeader("Authorization", equalTo("Bearer token1"))); | ||
|
||
this.auth.setFile(SERVICEACCOUNT_TOKEN2_PATH); | ||
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. these unit tests are no longer needed as it was originally for testing token expiry/rotation behavior. now it's decoupled/delegated to the RefreshAuthentication class. |
||
api.listPodForAllNamespaces().execute(); | ||
apiServer.verify( | ||
2, | ||
getRequestedFor(urlPathEqualTo("/api/v1/pods")) | ||
.withHeader("Authorization", equalTo("Bearer token1"))); | ||
|
||
this.auth.setExpiry(Instant.now().minusSeconds(1)); | ||
api.listPodForAllNamespaces().execute(); | ||
apiServer.verify( | ||
1, | ||
getRequestedFor(urlPathEqualTo("/api/v1/pods")) | ||
.withHeader("Authorization", equalTo("Bearer token2"))); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should probably convert this to some sort of non-RuntimeException like "FailedTokenAquisitionException" and handle that inside the
RefreshAuthentication.java
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.
That sounds like a good idea but I ended up adding the exception conversion as a runtime/unchecked exception in the parent class
RefreshAuthentication
. I also tried making it a checked exception in the token supplier function however it will break backward compatibility so I implemented the unchecked exception in the new commit. one example that compatibility would be broken is here where we're loading the token eagerly inRefreshAuthentication
:so the new checked exception would have to be added to the constructor which will require all places referencing it adding explicit handling the checked exception. does that make sense?