Skip to content

Commit ba427e9

Browse files
committed
add TokenFileAUth which reloads token if it expires
1 parent 9bd8ed6 commit ba427e9

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Net.Http.Headers;
3+
using System.IO;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Microsoft.Rest;
7+
8+
namespace k8s.Authentication
9+
{
10+
public class TokenFileAuth : ITokenProvider
11+
{
12+
private string _token;
13+
internal string _token_file { get; set; }
14+
internal DateTime _token_expires_at { get; set; }
15+
16+
public TokenFileAuth(string token_file)
17+
{
18+
_token_file = token_file;
19+
}
20+
21+
public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken)
22+
{
23+
if (_token_expires_at < DateTime.UtcNow)
24+
{
25+
_token = File.ReadAllText(_token_file).Trim();
26+
_token_expires_at = DateTime.UtcNow.AddSeconds(60);
27+
}
28+
return new AuthenticationHeaderValue("Bearer", _token);
29+
}
30+
}
31+
}

src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using k8s.Authentication;
34
using k8s.Exceptions;
45

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

45-
var token = File.ReadAllText(Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName));
4646
var rootCAFile = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName);
4747
var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST");
4848
var port = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT");
4949

5050
return new KubernetesClientConfiguration
5151
{
5252
Host = new UriBuilder("https", host, Convert.ToInt32(port)).ToString(),
53-
AccessToken = token,
53+
TokenProvider = new TokenFileAuth(Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName)),
5454
SslCaCerts = CertUtils.LoadPemFileCert(rootCAFile),
5555
};
5656
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using FluentAssertions;
5+
using k8s.Authentication;
6+
using Xunit;
7+
8+
namespace k8s.Tests
9+
{
10+
public class TokenFileAuthTests
11+
{
12+
[OperatingSystemDependentFact(Exclude = OperatingSystem.OSX | OperatingSystem.Windows)]
13+
public async Task Token()
14+
{
15+
var auth = new TokenFileAuth("assets/token1");
16+
var result = await auth.GetAuthenticationHeaderAsync(CancellationToken.None);
17+
result.Scheme.Should().Be("Bearer");
18+
result.Parameter.Should().Be("token1");
19+
20+
auth._token_file = "assets/token2";
21+
result = await auth.GetAuthenticationHeaderAsync(CancellationToken.None);
22+
result.Scheme.Should().Be("Bearer");
23+
result.Parameter.Should().Be("token1");
24+
25+
auth._token_expires_at = DateTime.UtcNow;
26+
result = await auth.GetAuthenticationHeaderAsync(CancellationToken.None);
27+
result.Scheme.Should().Be("Bearer");
28+
result.Parameter.Should().Be("token2");
29+
}
30+
}
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
token1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
token2

0 commit comments

Comments
 (0)