Update TokenFileAuth to use async read file method (#631)

Fix typo
Use async method to read file token
This commit is contained in:
Weihan Li
2021-05-22 00:36:10 +08:00
committed by GitHub
parent 29c460d0ed
commit 3ce35a74bf

View File

@@ -18,21 +18,23 @@ namespace k8s.Authentication
TokenFile = tokenFile; TokenFile = tokenFile;
} }
public Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken) public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken)
{ {
if (TokenExpiresAt < DateTime.UtcNow) if (TokenExpiresAt < DateTime.UtcNow)
{ {
token = File.ReadAllText(TokenFile).Trim(); token = await File.ReadAllTextAsync(TokenFile, cancellationToken)
.ContinueWith(r => r.Result.Trim(), cancellationToken)
.ConfigureAwait(false);
// in fact, the token has a expiry of 10 minutes and kubelet // in fact, the token has a expiry of 10 minutes and kubelet
// refreshes it at 8 minutes of its lifetime. setting the expiry // refreshes it at 8 minutes of its lifetime. setting the expiry
// of 1 minute makes sure the token is reloaded regularly so // of 1 minute makes sure the token is reloaded regularly so
// that its actual expiry is after the declared expiry here, // that its actual expiry is after the declared expiry here,
// which is as suffciently true as the time of reading a token // which is as sufficiently true as the time of reading a token
// < 10-8-1 minute. // < 10-8-1 minute.
TokenExpiresAt = DateTime.UtcNow.AddMinutes(1); TokenExpiresAt = DateTime.UtcNow.AddMinutes(1);
} }
return Task.FromResult(new AuthenticationHeaderValue("Bearer", token)); return new AuthenticationHeaderValue("Bearer", token);
} }
} }
} }