-
Notifications
You must be signed in to change notification settings - Fork 305
add TokenFileAUth which reloads token if it expires #440
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
Merged
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 hidden or 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,37 @@ | ||
using System; | ||
using System.Net.Http.Headers; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Rest; | ||
|
||
namespace k8s.Authentication | ||
{ | ||
public class TokenFileAuth : ITokenProvider | ||
{ | ||
private string _token; | ||
internal string _token_file { get; set; } | ||
internal DateTime _token_expires_at { get; set; } | ||
|
||
public TokenFileAuth(string token_file) | ||
{ | ||
_token_file = token_file; | ||
} | ||
|
||
public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken) | ||
{ | ||
if (_token_expires_at < DateTime.UtcNow) | ||
{ | ||
_token = File.ReadAllText(_token_file).Trim(); | ||
// in fact, the token has a expiry of 10 minutes and kubelet | ||
// refreshes it at 8 minutes of its lifetime. setting the expiry | ||
// of 1 minute makes sure the token is reloaded regularly so | ||
// that its actual expiry is after the declared expiry here, | ||
// which is as suffciently true as the time of reading a token | ||
// < 10-8-1 minute. | ||
_token_expires_at = DateTime.UtcNow.AddMinutes(1); | ||
} | ||
return new AuthenticationHeaderValue("Bearer", _token); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,30 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using k8s.Authentication; | ||
using Xunit; | ||
|
||
namespace k8s.Tests | ||
{ | ||
public class TokenFileAuthTests | ||
{ | ||
public async Task Token() | ||
{ | ||
var auth = new TokenFileAuth("assets/token1"); | ||
var result = await auth.GetAuthenticationHeaderAsync(CancellationToken.None); | ||
result.Scheme.Should().Be("Bearer"); | ||
result.Parameter.Should().Be("token1"); | ||
|
||
auth._token_file = "assets/token2"; | ||
result = await auth.GetAuthenticationHeaderAsync(CancellationToken.None); | ||
result.Scheme.Should().Be("Bearer"); | ||
result.Parameter.Should().Be("token1"); | ||
|
||
auth._token_expires_at = DateTime.UtcNow; | ||
result = await auth.GetAuthenticationHeaderAsync(CancellationToken.None); | ||
result.Scheme.Should().Be("Bearer"); | ||
result.Parameter.Should().Be("token2"); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
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.
Uh oh!
There was an error while loading. Please reload this page.