-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve support for 'exec' credentials. (#774)
- Loading branch information
1 parent
8a39035
commit 2ad5b96
Showing
4 changed files
with
83 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Diagnostics; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using k8s.Exceptions; | ||
using k8s.KubeConfigModels; | ||
using Microsoft.Rest; | ||
|
||
namespace k8s.Authentication | ||
{ | ||
public class ExecTokenProvider : ITokenProvider | ||
{ | ||
private readonly ExternalExecution exec; | ||
private ExecCredentialResponse response; | ||
|
||
public ExecTokenProvider(ExternalExecution exec) | ||
{ | ||
this.exec = exec; | ||
} | ||
|
||
private bool NeedsRefresh() | ||
{ | ||
if (response?.Status == null) | ||
{ | ||
return true; | ||
} | ||
|
||
if (response.Status.Expiry == null) | ||
{ | ||
return false; | ||
} | ||
|
||
return DateTime.UtcNow.AddSeconds(30) > response.Status.Expiry; | ||
} | ||
|
||
public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken) | ||
{ | ||
if (NeedsRefresh()) | ||
{ | ||
await RefreshToken().ConfigureAwait(false); | ||
} | ||
|
||
return new AuthenticationHeaderValue("Bearer", response.Status.Token); | ||
} | ||
|
||
private async Task RefreshToken() | ||
{ | ||
response = | ||
await Task.Run(() => KubernetesClientConfiguration.ExecuteExternalCommand(this.exec)).ConfigureAwait(false); | ||
} | ||
} | ||
} |
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
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