Skip to content
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
19 changes: 5 additions & 14 deletions containers/api-proxy/anthropic-oidc-token-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const { mintGitHubOidcToken, httpPost } = require('./github-oidc');
const {
BaseOidcTokenProvider,
REFRESH_FACTOR,
MIN_REFRESH_MARGIN_SECS,
} = require('./oidc-token-provider-base');

/**
Expand Down Expand Up @@ -87,18 +85,7 @@ class AnthropicOidcTokenProvider extends BaseOidcTokenProvider {

const { access_token, expires_in } = await this._exchangeForAnthropicToken(oidcJwt);

const now = Math.floor(Date.now() / 1000);
this._cachedToken = access_token;
this._expiresAt = now + expires_in;

const refreshInSecs = Math.max(
0,
Math.min(
expires_in * REFRESH_FACTOR,
expires_in - MIN_REFRESH_MARGIN_SECS
)
);
this._scheduleRefresh(Math.floor(refreshInSecs * 1000));
this._storeAndScheduleRefresh(access_token, expires_in);
}

async _doRefresh() {
Expand All @@ -109,6 +96,10 @@ class AnthropicOidcTokenProvider extends BaseOidcTokenProvider {
return this._cachedToken;
}

_setCachedValue(value) {
this._cachedToken = value;
}

_getInitSuccessLogContext() {
return {
audience: this._oidcAudience,
Expand Down
19 changes: 5 additions & 14 deletions containers/api-proxy/aws-oidc-token-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
const { mintGitHubOidcToken, httpGet } = require('./github-oidc');
const {
BaseOidcTokenProvider,
REFRESH_FACTOR,
MIN_REFRESH_MARGIN_SECS,
} = require('./oidc-token-provider-base');

/**
Expand Down Expand Up @@ -164,18 +162,7 @@ class AwsOidcTokenProvider extends BaseOidcTokenProvider {

const { credentials, expires_in } = await this._assumeRoleWithWebIdentity(oidcJwt);

const now = Math.floor(Date.now() / 1000);
this._cachedCredentials = credentials;
this._expiresAt = now + expires_in;

const refreshInSecs = Math.max(
0,
Math.min(
expires_in * REFRESH_FACTOR,
expires_in - MIN_REFRESH_MARGIN_SECS
)
);
this._scheduleRefresh(Math.floor(refreshInSecs * 1000));
this._storeAndScheduleRefresh(credentials, expires_in);
}

async _doRefresh() {
Expand All @@ -186,6 +173,10 @@ class AwsOidcTokenProvider extends BaseOidcTokenProvider {
return this._cachedCredentials;
}

_setCachedValue(value) {
this._cachedCredentials = value;
}

_getInitSuccessLogContext() {
return {
role_arn: this._roleArn,
Expand Down
19 changes: 5 additions & 14 deletions containers/api-proxy/gcp-oidc-token-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
const { mintGitHubOidcToken, httpPost } = require('./github-oidc');
const {
BaseOidcTokenProvider,
REFRESH_FACTOR,
MIN_REFRESH_MARGIN_SECS,
} = require('./oidc-token-provider-base');

/**
Expand Down Expand Up @@ -158,18 +156,7 @@ class GcpOidcTokenProvider extends BaseOidcTokenProvider {
expiresIn = federatedExpiresIn;
}

const now = Math.floor(Date.now() / 1000);
this._cachedToken = accessToken;
this._expiresAt = now + expiresIn;

const refreshInSecs = Math.max(
0,
Math.min(
expiresIn * REFRESH_FACTOR,
expiresIn - MIN_REFRESH_MARGIN_SECS
)
);
this._scheduleRefresh(Math.floor(refreshInSecs * 1000));
this._storeAndScheduleRefresh(accessToken, expiresIn);
}

async _doRefresh() {
Expand All @@ -180,6 +167,10 @@ class GcpOidcTokenProvider extends BaseOidcTokenProvider {
return this._cachedToken;
}

_setCachedValue(value) {
this._cachedToken = value;
}

_getInitSuccessLogContext() {
return {
workload_identity_provider: this._workloadIdentityProvider,
Expand Down
28 changes: 28 additions & 0 deletions containers/api-proxy/oidc-token-provider-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ class BaseOidcTokenProvider {
scheduleRefresh(this, delayMs, () => this._doRefresh(), this._providerPrefix);
}

/**
* Store the new cached value, record expiry, and schedule the next refresh.
* @param {unknown} value - token string or credentials object to cache
* @param {number} expiresIn - token lifetime in seconds
*/
_storeAndScheduleRefresh(value, expiresIn) {
const now = Math.floor(Date.now() / 1000);
this._setCachedValue(value);
this._expiresAt = now + expiresIn;

const refreshInSecs = Math.max(
0,
Math.min(
expiresIn * REFRESH_FACTOR,
expiresIn - MIN_REFRESH_MARGIN_SECS
)
);
this._scheduleRefresh(Math.floor(refreshInSecs * 1000));
}

/** @param {number} ms */
_sleep(ms) {
return sleep(ms);
Expand All @@ -103,6 +123,14 @@ class BaseOidcTokenProvider {
throw new Error('_getCachedValue() must be implemented by subclasses');
}

/**
* @abstract
* @param {unknown} value
*/
_setCachedValue(value) {
throw new Error('_setCachedValue() must be implemented by subclasses');
}

/**
* @abstract
* @returns {Record<string, unknown>}
Expand Down
20 changes: 5 additions & 15 deletions containers/api-proxy/oidc-token-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
const { mintGitHubOidcToken, httpPost } = require('./github-oidc');
const {
BaseOidcTokenProvider,
REFRESH_FACTOR,
MIN_REFRESH_MARGIN_SECS,
} = require('./oidc-token-provider-base');

/**
Expand Down Expand Up @@ -140,19 +138,7 @@ class OidcTokenProvider extends BaseOidcTokenProvider {
const oidcJwt = await this._mintGitHubOidcToken();
const { access_token, expires_in } = await this._exchangeForAzureToken(oidcJwt);

const now = Math.floor(Date.now() / 1000);
this._cachedToken = access_token;
this._expiresAt = now + expires_in;

// Schedule proactive refresh
const refreshInSecs = Math.max(
0,
Math.min(
expires_in * REFRESH_FACTOR,
expires_in - MIN_REFRESH_MARGIN_SECS
)
);
this._scheduleRefresh(Math.floor(refreshInSecs * 1000));
this._storeAndScheduleRefresh(access_token, expires_in);
}

/**
Expand All @@ -174,6 +160,10 @@ class OidcTokenProvider extends BaseOidcTokenProvider {
return this._cachedToken;
}

_setCachedValue(value) {
this._cachedToken = value;
}

_getInitSuccessLogContext() {
return {
tenant_id: this._tenantId,
Expand Down
Loading