|
1 | 1 | import log from '../../logging.js'; |
2 | 2 | import CONFIG from '../../config.loader.js'; |
3 | 3 |
|
| 4 | + // --------------------------------------------------------------------------- |
| 5 | + // Redis cache engine. |
| 6 | + // |
| 7 | + // Standard mode uses the `redis` client; cluster mode uses `redis-clustr`. |
| 8 | + // |
| 9 | + // AWS ElastiCache auth (Plane fork extension): |
| 10 | + // When ELASTICACHE_SECRET_ARN is set, the Redis auth token (and optionally |
| 11 | + // host/port) is fetched from AWS Secrets Manager and injected into the |
| 12 | + // connection options. A background timer re-fetches the secret every |
| 13 | + // AWS_SECRET_CACHE_TTL seconds and rebuilds the client when the token |
| 14 | + // rotates, so reconnections use fresh credentials without a restart. |
| 15 | + // Mirrors plane-ee/apps/silo/src/env.ts resolveRedisUrl()/resolveSecrets(). |
| 16 | + // --------------------------------------------------------------------------- |
| 17 | + |
4 | 18 | var client; |
| 19 | + var currentToken; |
| 20 | + |
| 21 | + // True only when the pod has IRSA or EKS Pod Identity credentials available. |
| 22 | + // Mirrors silo's resolveSecrets() gate (env.ts) so that non-EKS deployments |
| 23 | + // never attempt to reach AWS Secrets Manager even if ELASTICACHE_SECRET_ARN |
| 24 | + // happens to be set in the environment. Static AWS_ACCESS_KEY_ID creds are |
| 25 | + // intentionally NOT accepted here. |
| 26 | + function hasAwsManagedCredentials() { |
| 27 | + return Boolean( |
| 28 | + (process.env.AWS_ROLE_ARN || '').trim() || // IRSA |
| 29 | + process.env.AWS_WEB_IDENTITY_TOKEN_FILE || // IRSA (token file) |
| 30 | + process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI || // EKS Pod Identity |
| 31 | + process.env.AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE // EKS Pod Identity (token file) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + // True when secret resolution should run at all: ARN configured AND the pod |
| 36 | + // has IRSA / Pod Identity credentials to read it with. |
| 37 | + function secretResolutionEnabled() { |
| 38 | + return Boolean(process.env.ELASTICACHE_SECRET_ARN) && hasAwsManagedCredentials(); |
| 39 | + } |
| 40 | + |
| 41 | + // Resolve the auth token (and host/port) from AWS Secrets Manager. |
| 42 | + // Returns null when secret resolution is not enabled (no ARN, or no |
| 43 | + // IRSA/Pod Identity credentials present). |
| 44 | + async function resolveSecretValues() { |
| 45 | + if (!secretResolutionEnabled()) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + var region = process.env.AWS_REGION || 'us-east-1'; |
| 50 | + var { getSecret } = await import('../aws-secrets.js'); |
| 51 | + var secret = await getSecret(process.env.ELASTICACHE_SECRET_ARN, region, true); |
| 52 | + |
| 53 | + var tokenKey = process.env.REDIS_AUTH_TOKEN_KEY || 'REDIS_AUTH_TOKEN'; |
| 54 | + var hostKey = process.env.REDIS_HOST_KEY || 'REDIS_HOST'; |
| 55 | + var portKey = process.env.REDIS_PORT_KEY || 'REDIS_PORT'; |
| 56 | + |
| 57 | + return { |
| 58 | + token: typeof secret[tokenKey] === 'string' ? secret[tokenKey] : undefined, |
| 59 | + host: typeof secret[hostKey] === 'string' ? secret[hostKey] : undefined, |
| 60 | + port: secret[portKey] !== undefined ? Number(secret[portKey]) : undefined, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + // Merge resolved secret values into the configured connection options. |
| 65 | + // Existing config / IFRAMELY_REDIS_* values take precedence for host/port/TLS; |
| 66 | + // only the auth token is always taken from the secret. |
| 67 | + function buildOptions(secretVals) { |
| 68 | + |
| 69 | + if (CONFIG.REDIS_MODE === 'cluster') { |
| 70 | + var clusterOptions = { ...(CONFIG.REDIS_CLUSTER_OPTIONS || {}) }; |
| 71 | + if (secretVals && secretVals.token) { |
| 72 | + // redis-clustr forwards `redisOptions` to node_redis v2's |
| 73 | + // createClient (see redis-clustr@1.7.0 -> redis ^2.6.0). node_redis |
| 74 | + // v2 uses `auth_pass`; `password` is a newer alias, so set both. |
| 75 | + clusterOptions.redisOptions = { |
| 76 | + ...(clusterOptions.redisOptions || {}), |
| 77 | + auth_pass: secretVals.token, |
| 78 | + password: secretVals.token, |
| 79 | + }; |
| 80 | + } |
| 81 | + return clusterOptions; |
| 82 | + } |
| 83 | + |
| 84 | + var options = { ...(CONFIG.REDIS_OPTIONS || {}) }; |
| 85 | + var socket = { ...(options.socket || {}) }; |
5 | 86 |
|
6 | | - if (CONFIG.REDIS_MODE === 'cluster') { |
7 | | - const pkg = await import('redis-clustr'); |
8 | | - const RedisClustr = pkg.default; |
9 | | - client = new RedisClustr(CONFIG.REDIS_CLUSTER_OPTIONS); |
10 | | - } else { |
11 | | - var pkg = await import('redis'); |
12 | | - client = pkg.createClient(CONFIG.REDIS_OPTIONS); |
13 | | - await client.connect(); |
| 87 | + if (secretVals) { |
| 88 | + if (secretVals.token) { |
| 89 | + options.password = secretVals.token; |
| 90 | + } |
| 91 | + // Fill host/port from the secret only when not already provided via |
| 92 | + // config or IFRAMELY_REDIS_HOST/PORT (do not force TLS here). |
| 93 | + if (socket.host === undefined && secretVals.host !== undefined) { |
| 94 | + socket.host = secretVals.host; |
| 95 | + } |
| 96 | + if (socket.port === undefined && secretVals.port !== undefined) { |
| 97 | + socket.port = secretVals.port; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + options.socket = socket; |
| 102 | + return options; |
| 103 | + } |
| 104 | + |
| 105 | + async function createAndConnect() { |
| 106 | + var secretVals = await resolveSecretValues(); |
| 107 | + var options = buildOptions(secretVals); |
| 108 | + |
| 109 | + var newClient; |
| 110 | + if (CONFIG.REDIS_MODE === 'cluster') { |
| 111 | + const pkg = await import('redis-clustr'); |
| 112 | + const RedisClustr = pkg.default; |
| 113 | + newClient = new RedisClustr(options); |
| 114 | + } else { |
| 115 | + var pkg = await import('redis'); |
| 116 | + newClient = pkg.createClient(options); |
| 117 | + await newClient.connect(); |
| 118 | + } |
| 119 | + |
| 120 | + currentToken = secretVals && secretVals.token; |
| 121 | + return newClient; |
| 122 | + } |
| 123 | + |
| 124 | + // Surface a misconfiguration: ARN set but no managed credentials to use it. |
| 125 | + if (process.env.ELASTICACHE_SECRET_ARN && !hasAwsManagedCredentials()) { |
| 126 | + log(' -- Redis: ELASTICACHE_SECRET_ARN is set but no IRSA / EKS Pod Identity credentials detected — skipping AWS Secrets Manager.'); |
| 127 | + } |
| 128 | + |
| 129 | + // Initial connection. |
| 130 | + client = await createAndConnect(); |
| 131 | + |
| 132 | + // Background credential refresh: rebuild the client when the token rotates. |
| 133 | + if (secretResolutionEnabled()) { |
| 134 | + var ttlMs = parseInt(process.env.AWS_SECRET_CACHE_TTL || '300', 10) * 1000; |
| 135 | + if (ttlMs > 0) { |
| 136 | + var refreshTimer = setInterval(async function () { |
| 137 | + try { |
| 138 | + var secretVals = await resolveSecretValues(); |
| 139 | + var newToken = secretVals && secretVals.token; |
| 140 | + if (newToken && newToken !== currentToken) { |
| 141 | + var oldClient = client; |
| 142 | + client = await createAndConnect(); |
| 143 | + log(' -- Redis: rebuilt client after credential rotation'); |
| 144 | + try { |
| 145 | + if (oldClient && oldClient.quit) { |
| 146 | + await oldClient.quit(); |
| 147 | + } |
| 148 | + } catch (quitErr) { |
| 149 | + log(' -- Redis: error closing old client ' + quitErr); |
| 150 | + } |
| 151 | + } |
| 152 | + } catch (err) { |
| 153 | + log(' -- Redis: failed to refresh credentials ' + err); |
| 154 | + } |
| 155 | + }, ttlMs); |
| 156 | + // Allow the process to exit even if the timer is still running. |
| 157 | + refreshTimer.unref(); |
| 158 | + } |
14 | 159 | } |
15 | 160 |
|
16 | 161 | export async function set(key, data, options) { |
|
0 commit comments