UNPKG

@cloud-copilot/iam-collect

Version:

Collect IAM information from AWS Accounts

80 lines 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCredentials = getCredentials; const log_js_1 = require("../utils/log.js"); const coreAuth_js_1 = require("./coreAuth.js"); /** * We cache credentials with a timeout */ const credentialsCache = new Map(); // Currently using a static timeout const CREDENTIAL_CACHE_TIMEOUT = 300 * 1000; /** * We cache requests for credentials to avoid multiple requests for the same accountId and authConfig. */ const credentialRequestCache = {}; /** * Generate a cache key for the given account ID and auth configuration. * * @param accountId the AWS account ID * @param authConfig the authentication configuration, if any * @returns a unique cache key for the credentials */ function credentialsCacheKey(accountId, authConfig) { return authConfig ? `${accountId}:${JSON.stringify(authConfig)}` : accountId; } /** * Get cached credentials for the given cache key, if they exist and are not expired. * * @param cacheKey the cache key to get credentials for * @returns the cached credentials if they exist and are not expired, otherwise undefined */ function getCachedCredentials(cacheKey) { const cached = credentialsCache.get(cacheKey); if (cached && cached.expiration > Date.now()) { return cached.credentials; } credentialsCache.delete(cacheKey); return undefined; } /** * Cache a set of credentials * * @param cacheKey the cache key to use for the credentials * @param credentials the credentials to cache */ function setCachedCredentials(cacheKey, credentials) { const expiration = (0, coreAuth_js_1.now)() + CREDENTIAL_CACHE_TIMEOUT; credentialsCache.set(cacheKey, { expiration, credentials }); } /** * Get credentials for the given account ID and auth configuration. * * @param accountId the AWS account ID for which to get credentials * @param authConfig the authentication configuration to use for the account * @returns new or cached credentials based on the provided account ID and auth configuration */ async function getCredentials(accountId, authConfig) { const cacheKey = credentialsCacheKey(accountId, authConfig); const cachedCredentials = getCachedCredentials(cacheKey); if (cachedCredentials) { log_js_1.log.trace({ accountId }, 'Using cached credentials'); return cachedCredentials; } if (credentialRequestCache[cacheKey] !== undefined) { return credentialRequestCache[cacheKey]; } //Create a new promise and store it in case another request comes in while this one is being processed. return (credentialRequestCache[cacheKey] = (async () => { try { log_js_1.log.trace({ accountId }, 'Creating new credentials'); const newCredentials = await (0, coreAuth_js_1.getNewCredentials)(accountId, authConfig); setCachedCredentials(cacheKey, newCredentials); return newCredentials; } finally { delete credentialRequestCache[cacheKey]; // Clean up the queue regardless of success or failure. } })()); } //# sourceMappingURL=auth.js.map