Skip to content

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 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/KubernetesClient/Authentication/TokenFileAuth.cs
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using k8s.Authentication;
using k8s.Exceptions;

namespace k8s
Expand Down Expand Up @@ -42,15 +43,14 @@ public static KubernetesClientConfiguration InClusterConfig()
"unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined");
}

var token = File.ReadAllText(Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName));
var rootCAFile = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName);
var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST");
var port = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT");

return new KubernetesClientConfiguration
{
Host = new UriBuilder("https", host, Convert.ToInt32(port)).ToString(),
AccessToken = token,
TokenProvider = new TokenFileAuth(Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName)),
SslCaCerts = CertUtils.LoadPemFileCert(rootCAFile),
};
}
Expand Down
30 changes: 30 additions & 0 deletions tests/KubernetesClient.Tests/TokenFileAuth.cs
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");
}
}
}
1 change: 1 addition & 0 deletions tests/KubernetesClient.Tests/assets/token1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
token1
1 change: 1 addition & 0 deletions tests/KubernetesClient.Tests/assets/token2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
token2