+3
-3
@@ -50,7 +50,7 @@ describe('AzureIdentityKubernetesAuthTranslator tests', () => {
|
||||
|
||||
it('should re-use token before expiry', async () => {
|
||||
const authTranslator = new AzureIdentityKubernetesAuthTranslator(
|
||||
new StaticTokenCredential(5 * 60 * 1000),
|
||||
new StaticTokenCredential(20 * 60 * 1000),
|
||||
);
|
||||
|
||||
const response = await authTranslator.decorateClusterDetailsWithAuth(cd);
|
||||
@@ -60,9 +60,9 @@ describe('AzureIdentityKubernetesAuthTranslator tests', () => {
|
||||
expect(response2.serviceAccountToken).toEqual('MY_TOKEN_1');
|
||||
});
|
||||
|
||||
it('should issue new token 2 minutes befory expiry', async () => {
|
||||
it('should issue new token 15 minutes befory expiry', async () => {
|
||||
const authTranslator = new AzureIdentityKubernetesAuthTranslator(
|
||||
new StaticTokenCredential(3 * 60 * 1000), // token expires in 3m
|
||||
new StaticTokenCredential(16 * 60 * 1000), // token expires in 11m
|
||||
);
|
||||
|
||||
const response = await authTranslator.decorateClusterDetailsWithAuth(cd);
|
||||
|
||||
+7
-8
@@ -27,12 +27,11 @@ const aksScope = '6dae42f8-4368-4678-94ff-3960e28e3630/.default'; // This scope
|
||||
export class AzureIdentityKubernetesAuthTranslator
|
||||
implements KubernetesAuthTranslator
|
||||
{
|
||||
private tokenCredential: TokenCredential;
|
||||
private accessToken: AccessToken | null = null;
|
||||
|
||||
constructor(tokenCredential?: TokenCredential) {
|
||||
this.tokenCredential = tokenCredential || new DefaultAzureCredential();
|
||||
}
|
||||
constructor(
|
||||
private readonly tokenCredential: TokenCredential = new DefaultAzureCredential(),
|
||||
) {}
|
||||
|
||||
async decorateClusterDetailsWithAuth(
|
||||
clusterDetails: AzureClusterDetails,
|
||||
@@ -42,7 +41,7 @@ export class AzureIdentityKubernetesAuthTranslator
|
||||
clusterDetails,
|
||||
);
|
||||
|
||||
if (!this.accessToken || this.tokenExpired()) {
|
||||
if (this.tokenExpired()) {
|
||||
this.accessToken = await this.tokenCredential.getToken(aksScope);
|
||||
|
||||
if (!this.accessToken) {
|
||||
@@ -50,15 +49,15 @@ export class AzureIdentityKubernetesAuthTranslator
|
||||
}
|
||||
}
|
||||
|
||||
clusterDetailsWithAuthToken.serviceAccountToken = this.accessToken.token;
|
||||
clusterDetailsWithAuthToken.serviceAccountToken = this.accessToken!.token;
|
||||
return clusterDetailsWithAuthToken;
|
||||
}
|
||||
|
||||
private tokenExpired(): boolean {
|
||||
if (!this.accessToken) return true;
|
||||
|
||||
// Set tokens to expire 2 minutes before its actual expiry time
|
||||
const expiresOn = this.accessToken.expiresOnTimestamp - 2 * 60 * 1000;
|
||||
// Set tokens to expire 15 minutes before its actual expiry time
|
||||
const expiresOn = this.accessToken.expiresOnTimestamp - 15 * 60 * 1000;
|
||||
return Date.now() >= expiresOn;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user