diff --git a/.env.sample b/.env.sample index 0ed966f..0201f5f 100644 --- a/.env.sample +++ b/.env.sample @@ -29,6 +29,8 @@ MYSQL_ROOT_PASSWORD=mysql-user-root-password # Root password for MySQL container # ======================================== # REDIS CACHE # ======================================== +# Preferred for encrypted ElastiCache; overrides REDIS_HOST and REDIS_PORT. +# REDIS_URL=rediss://your-cache-primary-endpoint:6379 REDIS_HOST=127.0.0.1 REDIS_PORT=6379 # REDIS_PASSWORD= # Uncomment and set if password protection is enabled diff --git a/README.md b/README.md index f61a1e5..f5b76e8 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ The following table summarizes the environment variables used by the application | | **Database (PostgreSQL - Group)** | | | `GROUP_DB_URL` | URL of the group PostgreSQL database | `postgresql://postgres:identitypassword@localhost:5431/group` | | | **Redis Cache** | | +| `REDIS_URL` | Preferred connection URL for both authorization storage and the Nest cache. Use `rediss://` with the AWS endpoint for TLS; overrides `REDIS_HOST`/`REDIS_PORT`. | Unset | | `REDIS_HOST` | Hostname for the Redis cache instance | `127.0.0.1` | | `REDIS_PORT` | Port for the Redis cache instance | `6380` | | | **JWT Validation** | | diff --git a/deploy/REDIS-MIGRATION.md b/deploy/REDIS-MIGRATION.md new file mode 100644 index 0000000..2c0af2b --- /dev/null +++ b/deploy/REDIS-MIGRATION.md @@ -0,0 +1,117 @@ +# PM-6691: encrypted dev identity cache + +Dev identity currently uses `REDIS_HOST=apirediscache.topcoder.com`, whose private +CNAME points to the standalone Redis OSS 6.2.6 `apicache` cluster. The replacement +is the `identity-api-v6-dev-cache` CloudFormation stack/replication group in +account `811668436784`, region `us-east-1`. + +The replacement uses Valkey 9.1, one `cache.t4g.micro` node, cluster mode disabled, +required TLS, encryption at rest, and seven-day automatic snapshot retention. +Its security group admits port 6379 from the existing identity task security +group `sg-afba72dd`. This is the existing single-node dev availability model; +it does not add a replica or Multi-AZ failover. The stack retains a snapshot on +cache replacement/deletion and has termination protection enabled. Its deployed +CloudFormation template is the source of infrastructure configuration. + +The initial restore is from `pm-6691-apicache-20261109`. The original `apicache` +and its DNS record remain available for rollback. The encrypted `v6identity` +cluster is a separate resource and is not used by this change. Keycloak is +outside this migration. + +Prepared on November 9, 2026: + +- Primary endpoint: `master.identity-api-v6-dev-cache.056cmt.use1.cache.amazonaws.com:6379`. +- The new `REDIS_URL` SecureString is version 1. The legacy host and port + parameters were not changed. +- Task revision `identity-api-v6:150` adds only the `REDIS_URL` secret reference + to revision 149. It retains image digest + `sha256:bc31bde69612e9d09394fbc66c26b8990e7913027a9336793330f9e29b3328ce`. + This stages the binding; it does not deploy the code in this PR. +- A disposable ECS task in the identity VPC verified a TLS `PING`, a synthetic + write/read/TTL/delete cycle, and rejection of plaintext connections. The + restored database had 12 keys before and after the test. The task received + no identity application secrets. + +## Configuration and application behavior + +- `/config/identity-api-v6/appvar/REDIS_URL` is a SecureString containing + `rediss://:6379`. Use the AWS endpoint, not the + legacy custom CNAME, so certificate hostname verification succeeds. +- The identity ECS task definition must bind `REDIS_URL` to that exact parameter + ARN. The pinned deployment suite preserves existing secret references; merely + creating the parameter does not inject it into tasks. +- The binding can be staged with the current image, which does not read + `REDIS_URL`. It continues to use unchanged `REDIS_HOST`/`REDIS_PORT`. Do not + overwrite those parameters or the old CNAME before the new code is deployed. +- This PR makes `REDIS_URL` take precedence for both `AuthDataStore` and the Nest + cache. Without it, host/port continue to work for local development and the + current production configuration. Production infrastructure is not changed. +- `rediss://` requires TLS 1.2 or later with certificate and hostname verification. + Neither client disables verification or falls back from TLS to plaintext. +- The Nest cache now uses a Keyv Redis store through `stores`, replacing the + obsolete `cache-manager-ioredis` configuration that was ignored by Nest cache + manager v3. Startup checks connectivity. Cache values are shared across tasks + under `identity-api-v6::`; per-entry TTLs remain milliseconds. +- Authorization entries retain the existing `ap:identity:authorization:` key + format, JSON serialization, and 7,776,000-second (90-day) TTL. + +## Cutover gate before merging/deploying + +The initial restored snapshot is a preparation step, not ongoing replication. +The PR must not be deployed on the assumption that a snapshot stays current. + +1. Recheck the running task revision and confirm only identity will consume the + new `REDIS_URL`. Check the old cache's clients and command activity from inside + the VPC; the investigation's quiet metrics are not proof of no future writes. +2. Pause identity authorization mutations for the final migration window. Check + all Redis databases, counts, key types, and TTLs without logging token values. + Confirm that the old authorization state still matches the restored state. + If it has changed, refresh the replacement from a fresh snapshot or perform + a controlled transfer preserving TTLs before resuming writes. Account for + deletions/revocations as well as newly created sessions. Do not merge until + the replacement reflects the final source state. +3. Merge/deploy this PR to dev. Avoid overlapping old/new writers during cutover; + the existing service normally uses rolling deployments. Keep mutations paused + until all old tasks have drained and the new task is healthy. Existing + process-local OAuth/OTP flows cannot be carried across the restart; exercise + new flows after deployment. +4. Validate authorization creation/read/refresh/logout, OAuth state, password + reset, and email-change/OTP flows. Confirm shared cache behavior across tasks, + encrypted connections, TTLs, and no Redis/certificate errors. Run the normal + identity health check and verify the deployed image digest. +5. Observe before retiring the original cache. Do not delete `apicache`, its DNS + record, or snapshots as part of this PR. Complete retirement before Redis OSS + 6 extended-support charges begin on February 1, 2027. + +## Rollback + +The previous image ignores `REDIS_URL` and still reads the old host/port, so its +configuration remains usable. However, after any writes on the new cache, a +simple image rollback would lose new sessions and could resurrect sessions that +were revoked after cutover. Pause mutations and reconcile state first, or make +an explicit session-invalidation decision. Never switch back to a stale snapshot +while authorization traffic continues. Keep both caches and their snapshots +until the rollback window ends. + +## Verification + +Unit tests cover URL precedence, legacy settings, invalid configuration, verified +TLS options, authorization TTLs, connection failures, and shutdown cleanup: + +```sh +pnpm exec jest --runInBand src/config/redis.config.spec.ts src/api/authorization/auth-data-store.service.spec.ts +``` + +The integration suite requires a disposable Redis/Valkey instance. It writes only +uniquely prefixed synthetic test keys and deletes them, but must never be pointed +at an application cache. It constructs two independent Nest modules and verifies +shared values, expiry in milliseconds, deletion, and the authorization format/TTL. + +```sh +REDIS_TEST_URL=redis://127.0.0.1:16379 pnpm exec jest --config jest.redis.config.js --runInBand +NODE_EXTRA_CA_CERTS=/path/to/test-ca.crt REDIS_TEST_URL=rediss://localhost:16380 pnpm exec jest --config jest.redis.config.js --runInBand +``` + +The TLS test server must present a certificate trusted by `test-ca.crt` and valid +for `localhost` only. The suite verifies that both clients reject the same server +when addressed by `127.0.0.1`. No certificate-verification bypass is used. diff --git a/jest.redis.config.js b/jest.redis.config.js new file mode 100644 index 0000000..af19c8a --- /dev/null +++ b/jest.redis.config.js @@ -0,0 +1,8 @@ +module.exports = { + rootDir: '.', + testRegex: 'test/redis.integration-spec.ts$', + transform: { '^.+\\.(t|j)s$': 'ts-jest' }, + moduleNameMapper: { '^src/(.*)$': '/src/$1' }, + testEnvironment: 'node', + testTimeout: 15000, +}; diff --git a/package.json b/package.json index ade9daa..79058e8 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ }, "dependencies": { "@aws-sdk/client-kms": "^3.1161.0", + "@keyv/redis": "5.1.6", "@nestjs/axios": "^4.0.0", "@nestjs/cache-manager": "^3.0.1", "@nestjs/common": "^11.1.28", @@ -58,7 +59,6 @@ "axios": "^1.20.0", "bcryptjs": "^3.0.2", "cache-manager": "^6.4.3", - "cache-manager-ioredis": "^2.1.0", "class-transformer": "^0.5.1", "class-validator": "^0.14.4", "cors": "^2.8.6", @@ -71,6 +71,7 @@ "ioredis": "^5.6.1", "jsonwebtoken": "^9.0.3", "jwks-rsa": "^3.2.0", + "keyv": "5.6.0", "lodash": "^4.18.1", "passport": "^0.7.0", "passport-custom": "^1.1.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 210ed5c..9d6e171 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -65,12 +65,15 @@ importers: '@aws-sdk/client-kms': specifier: ^3.1161.0 version: 3.1161.0 + '@keyv/redis': + specifier: 5.1.6 + version: 5.1.6(keyv@5.6.0) '@nestjs/axios': specifier: ^4.0.0 version: 4.0.1(@nestjs/common@11.1.28(class-transformer@0.5.1)(class-validator@0.14.4)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1))(axios@1.20.0(debug@4.4.3(supports-color@8.1.1))(supports-color@8.1.1))(rxjs@7.8.2) '@nestjs/cache-manager': specifier: ^3.0.1 - version: 3.0.1(@nestjs/common@11.1.28(class-transformer@0.5.1)(class-validator@0.14.4)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1))(@nestjs/core@11.1.28)(cache-manager@6.4.3)(keyv@5.5.5)(rxjs@7.8.2) + version: 3.0.1(@nestjs/common@11.1.28(class-transformer@0.5.1)(class-validator@0.14.4)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1))(@nestjs/core@11.1.28)(cache-manager@6.4.3)(keyv@5.6.0)(rxjs@7.8.2) '@nestjs/common': specifier: ^11.1.28 version: 11.1.28(class-transformer@0.5.1)(class-validator@0.14.4)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1) @@ -104,9 +107,6 @@ importers: cache-manager: specifier: ^6.4.3 version: 6.4.3 - cache-manager-ioredis: - specifier: ^2.1.0 - version: 2.1.0(supports-color@8.1.1) class-transformer: specifier: ^0.5.1 version: 0.5.1 @@ -143,6 +143,9 @@ importers: jwks-rsa: specifier: ^3.2.0 version: 3.2.0(supports-color@8.1.1) + keyv: + specifier: 5.6.0 + version: 5.6.0 lodash: specifier: 4.18.1 version: 4.18.1 @@ -778,9 +781,6 @@ packages: '@ioredis/commands@1.4.0': resolution: {integrity: sha512-aFT2yemJJo+TZCmieA7qnYGQooOS7QfNmYrzGtsYd3g9j5iDP8AimYYAesf79ohjbLG12XxC4nG5DyEnC88AsQ==} - '@ioredis/commands@1.5.0': - resolution: {integrity: sha512-eUgLqrMf8nJkZxT24JvVRrQya1vZkQh8BBeYNwGDqa5I0VUi8ACx7uFvAaLxintokpTenkK6DASvo/bvNbBGow==} - '@isaacs/nonrecursive-glob-match@1.0.3': resolution: {integrity: sha512-SI7kyKHq4XXDRozw+r9GlRZkia1ZkFrpLW98r2IC2Bpr8YkFGE3fvjzmxCG0x78A++69HmDNrDvSMYBZmHGpKA==} @@ -880,6 +880,12 @@ packages: '@jridgewell/trace-mapping@0.3.9': resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==} + '@keyv/redis@5.1.6': + resolution: {integrity: sha512-eKvW6pspvVaU5dxigaIDZr635/Uw6urTXL3gNbY9WTR8d3QigZQT+r8gxYSEOsw4+1cCBsC4s7T2ptR0WC9LfQ==} + engines: {node: '>= 18'} + peerDependencies: + keyv: ^5.6.0 + '@keyv/serialize@1.1.1': resolution: {integrity: sha512-dXn3FZhPv0US+7dtJsIi2R+c7qWYiReoEh5zUntWCf4oSpMNib8FDhSoed6m3QyZdx5hK7iLFkYk3rNxwt8vTA==} @@ -1198,6 +1204,18 @@ packages: '@prisma/get-platform@6.19.3': resolution: {integrity: sha512-xFj1VcJ1N3MKooOQAGO0W5tsd0W2QzIvW7DD7c/8H14Zmp4jseeWAITm+w2LLoLrlhoHdPPh0NMZ8mfL6puoHA==} + '@redis/client@5.12.1': + resolution: {integrity: sha512-7aPGWeqA3uFm43o19umzdl16CEjK/JQGtSXVPevplTaOU3VJA/rseBC1QvYUz9lLDIMBimc4SW/zrW4S89BaCA==} + engines: {node: '>= 18.19.0'} + peerDependencies: + '@node-rs/xxhash': ^1.1.0 + '@opentelemetry/api': '>=1 <2' + peerDependenciesMeta: + '@node-rs/xxhash': + optional: true + '@opentelemetry/api': + optional: true + '@scarf/scarf@1.4.0': resolution: {integrity: sha512-xxeapPiUXdZAE3che6f3xogoJPeZgig6omHEy1rIY5WVsB3H2BHNnZH+gHG6x91SCWyQCzWGsuL2Hh3ClO5/qQ==} @@ -1912,10 +1930,6 @@ packages: magicast: optional: true - cache-manager-ioredis@2.1.0: - resolution: {integrity: sha512-TCxbp9ceuFveTKWuNaCX8QjoC41rAlHen4s63u9Yd+iXlw3efYmimc/u935PKPxSdhkXpnMes4mxtK3/yb0L4g==} - engines: {node: '>=6.0.0'} - cache-manager@6.4.3: resolution: {integrity: sha512-VV5eq/QQ5rIVix7/aICO4JyvSeEv9eIQuKL5iFwgM2BrcYoE0A/D1mNsAHJAsB0WEbNdBlKkn6Tjz6fKzh/cKQ==} @@ -2238,10 +2252,6 @@ packages: resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} engines: {node: '>=0.4.0'} - denque@1.5.1: - resolution: {integrity: sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==} - engines: {node: '>=0.10'} - denque@2.1.0: resolution: {integrity: sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==} engines: {node: '>=0.10'} @@ -2746,6 +2756,9 @@ packages: resolution: {integrity: sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==} engines: {node: '>= 0.4'} + hookified@1.15.1: + resolution: {integrity: sha512-MvG/clsADq1GPM2KGo2nyfaWVyn9naPiXrqIe4jYjXNZQt238kWyOGrsyc/DmRAQ+Re6yeo6yX/yoNCG5KAEVg==} + html-escaper@2.0.2: resolution: {integrity: sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==} @@ -2825,10 +2838,6 @@ packages: inspect-with-kind@1.0.5: resolution: {integrity: sha512-MAQUJuIo7Xqk8EVNP+6d3CKq9c80hi4tjIbIAT6lmGW9W6WzlHiu9PS8uSuUYU+Do+j1baiFp3H25XEVxDIG2g==} - ioredis@4.30.1: - resolution: {integrity: sha512-17Ed70njJ7wT7JZsdTVLb0j/cmwHwfQCFu+AP6jY7nFKd+CA7MBW7nX121mM64eT8S9ekAVtYYt8nGQPmm3euA==} - engines: {node: '>=6'} - ioredis@5.8.2: resolution: {integrity: sha512-C6uC+kleiIMmjViJINWk80sOQw5lEzse1ZmvD+S/s8p8CWapftSaC+kocGTx6xrbrJ4WmYQGC08ffHLr6ToR6Q==} engines: {node: '>=12.22.0'} @@ -3145,8 +3154,8 @@ packages: keyv@4.5.4: resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==} - keyv@5.5.5: - resolution: {integrity: sha512-FA5LmZVF1VziNc0bIdCSA1IoSVnDCqE8HJIZZv2/W8YmoAM50+tnUgJR/gQZwEeIMleuIOnRnHA/UaZRNeV4iQ==} + keyv@5.6.0: + resolution: {integrity: sha512-CYDD3SOtsHtyXeEORYRx2qBtpDJFjRTGXUtmNEMGyzYOKj1TE3tycdlho7kA1Ufx9OYWZzg52QFBGALTirzDSw==} kind-of@6.0.3: resolution: {integrity: sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==} @@ -3202,9 +3211,6 @@ packages: lodash.defaults@4.2.0: resolution: {integrity: sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==} - lodash.flatten@4.4.0: - resolution: {integrity: sha512-C5N2Z3DgnnKr0LOpv/hKCgKdb7ZZwafIrsesve6lmzvZIRZRGaZ/l6Q8+2W7NaT+ZwO3fFlSCzCzrDCFdJfZ4g==} - lodash.includes@4.3.0: resolution: {integrity: sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==} @@ -3518,10 +3524,6 @@ packages: resolution: {integrity: sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==} engines: {node: '>=10'} - p-map@2.1.0: - resolution: {integrity: sha512-y3b8Kpd8OAN444hxfBbFfj1FY/RjtTd8tzYwhUqNYXx0fXx2iX4maP4Qr6qhIKbQXI02wTLAda4fYUbDagTUFw==} - engines: {node: '>=6'} - p-try@2.2.0: resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==} engines: {node: '>=6'} @@ -5149,8 +5151,6 @@ snapshots: '@ioredis/commands@1.4.0': {} - '@ioredis/commands@1.5.0': {} - '@isaacs/nonrecursive-glob-match@1.0.3': {} '@istanbuljs/load-nyc-config@1.1.0': @@ -5354,6 +5354,16 @@ snapshots: '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.5.5 + '@keyv/redis@5.1.6(keyv@5.6.0)': + dependencies: + '@redis/client': 5.12.1 + cluster-key-slot: 1.1.2 + hookified: 1.15.1 + keyv: 5.6.0 + transitivePeerDependencies: + - '@node-rs/xxhash' + - '@opentelemetry/api' + '@keyv/serialize@1.1.1': {} '@lukeed/csprng@1.1.0': {} @@ -5438,12 +5448,12 @@ snapshots: axios: 1.20.0(debug@4.4.3(supports-color@8.1.1))(supports-color@8.1.1) rxjs: 7.8.2 - '@nestjs/cache-manager@3.0.1(@nestjs/common@11.1.28(class-transformer@0.5.1)(class-validator@0.14.4)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1))(@nestjs/core@11.1.28)(cache-manager@6.4.3)(keyv@5.5.5)(rxjs@7.8.2)': + '@nestjs/cache-manager@3.0.1(@nestjs/common@11.1.28(class-transformer@0.5.1)(class-validator@0.14.4)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1))(@nestjs/core@11.1.28)(cache-manager@6.4.3)(keyv@5.6.0)(rxjs@7.8.2)': dependencies: '@nestjs/common': 11.1.28(class-transformer@0.5.1)(class-validator@0.14.4)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1) '@nestjs/core': 11.1.28(@nestjs/common@11.1.28(class-transformer@0.5.1)(class-validator@0.14.4)(reflect-metadata@0.2.2)(rxjs@7.8.2)(supports-color@8.1.1))(@nestjs/platform-express@11.1.28)(reflect-metadata@0.2.2)(rxjs@7.8.2) cache-manager: 6.4.3 - keyv: 5.5.5 + keyv: 5.6.0 rxjs: 7.8.2 '@nestjs/cli@11.0.14(@swc/cli@0.6.0(@swc/core@1.15.5)(chokidar@4.0.3)(supports-color@8.1.1))(@swc/core@1.15.5)(@types/node@22.19.3)(uglify-js@3.19.3)': @@ -5657,6 +5667,10 @@ snapshots: dependencies: '@prisma/debug': 6.19.3 + '@redis/client@5.12.1': + dependencies: + cluster-key-slot: 1.1.2 + '@scarf/scarf@1.4.0': {} '@sinclair/typebox@0.27.8': {} @@ -6533,15 +6547,9 @@ snapshots: pkg-types: 2.3.0 rc9: 2.1.2 - cache-manager-ioredis@2.1.0(supports-color@8.1.1): - dependencies: - ioredis: 4.30.1(supports-color@8.1.1) - transitivePeerDependencies: - - supports-color - cache-manager@6.4.3: dependencies: - keyv: 5.5.5 + keyv: 5.6.0 cacheable-lookup@7.0.0: {} @@ -6805,8 +6813,6 @@ snapshots: delayed-stream@1.0.0: {} - denque@1.5.1: {} - denque@2.1.0: {} depd@2.0.0: {} @@ -7362,6 +7368,8 @@ snapshots: dependencies: function-bind: 1.1.2 + hookified@1.15.1: {} + html-escaper@2.0.2: {} http-cache-semantics@4.2.0: {} @@ -7442,22 +7450,6 @@ snapshots: dependencies: kind-of: 6.0.3 - ioredis@4.30.1(supports-color@8.1.1): - dependencies: - '@ioredis/commands': 1.5.0 - cluster-key-slot: 1.1.2 - debug: 4.4.3(supports-color@8.1.1) - denque: 1.5.1 - lodash.defaults: 4.2.0 - lodash.flatten: 4.4.0 - lodash.isarguments: 3.1.0 - p-map: 2.1.0 - redis-errors: 1.2.0 - redis-parser: 3.0.0 - standard-as-callback: 2.1.0 - transitivePeerDependencies: - - supports-color - ioredis@5.8.2(supports-color@8.1.1): dependencies: '@ioredis/commands': 1.4.0 @@ -7965,7 +7957,7 @@ snapshots: dependencies: json-buffer: 3.0.1 - keyv@5.5.5: + keyv@5.6.0: dependencies: '@keyv/serialize': 1.1.1 @@ -8006,8 +7998,6 @@ snapshots: lodash.defaults@4.2.0: {} - lodash.flatten@4.4.0: {} - lodash.includes@4.3.0: {} lodash.isarguments@3.1.0: {} @@ -8296,8 +8286,6 @@ snapshots: dependencies: p-limit: 3.1.0 - p-map@2.1.0: {} - p-try@2.2.0: {} parent-module@1.0.1: diff --git a/src/api/authorization/auth-data-store.service.spec.ts b/src/api/authorization/auth-data-store.service.spec.ts index 2c78b36..039aa33 100644 --- a/src/api/authorization/auth-data-store.service.spec.ts +++ b/src/api/authorization/auth-data-store.service.spec.ts @@ -31,6 +31,15 @@ jest.mock('../../shared/util/common.utils', () => ({ })); describe('AuthDataStore', () => { + beforeEach(() => { + jest.clearAllMocks(); + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.clearAllTimers(); + jest.useRealTimers(); + }); let service: AuthDataStore; let mockConfigService: Partial; @@ -97,6 +106,9 @@ describe('AuthDataStore', () => { beforeEach(async () => { mockRedisClient = { + on: jest.fn().mockReturnThis(), + disconnect: jest.fn(), + quit: jest.fn().mockResolvedValue('OK'), ping: jest.fn().mockResolvedValue('PONG'), setex: jest.fn().mockResolvedValue('OK'), get: jest.fn().mockResolvedValue(JSON.stringify(mockAuthResponse)), @@ -129,6 +141,39 @@ describe('AuthDataStore', () => { expect(service['store']).toBeInstanceOf(RedisDataStore); }); + it('uses the TLS URL with certificate verification and the existing auth TTL', async () => { + const store = new RedisDataStore({ + spec: { + url: 'rediss://cache.example.com:6379', + host: 'old.example.com', + port: 6379, + expirySeconds: 7776000, + }, + }); + await store.put(mockAuthResponse); + expect(Redis).toHaveBeenCalledWith( + 'rediss://cache.example.com:6379', + expect.objectContaining({ + tls: { + servername: 'cache.example.com', + rejectUnauthorized: true, + minVersion: 'TLSv1.2', + }, + }), + ); + expect(mockRedisClient.setex).toHaveBeenCalledWith( + 'ap:identity:authorization:123456:test-target', + 7776000, + JSON.stringify(mockAuthResponse), + ); + }); + + it('closes the authorization client when its Nest provider is destroyed', async () => { + await service.get('token', 'target'); + await service.onModuleDestroy(); + expect(mockRedisClient.quit).toHaveBeenCalledTimes(1); + }); + it('should store and retrieve authorization data', async () => { await service.put(mockAuthResponse); const result = await service.get( @@ -176,6 +221,7 @@ describe('AuthDataStore', () => { await expect(failingService.put(mockAuthResponse)).rejects.toThrow(); await expect(failingService.get('token', 'target')).rejects.toThrow(); await expect(failingService.delete('token', 'target')).rejects.toThrow(); + expect(mockRedisClient.disconnect).toHaveBeenCalledTimes(3); }); it('should handle null auth data in put', async () => { diff --git a/src/api/authorization/auth-data-store.service.ts b/src/api/authorization/auth-data-store.service.ts index 1d25022..0aee36b 100644 --- a/src/api/authorization/auth-data-store.service.ts +++ b/src/api/authorization/auth-data-store.service.ts @@ -3,9 +3,10 @@ import Redis, { RedisOptions } from 'ioredis'; import { ConfigurationService } from '../../config/configuration.service'; import { AuthorizationResponse } from '../../dto/authorization/authorization.dto'; import { CommonUtils } from '../../shared/util/common.utils'; +import { getRedisTlsOptions, getRedisUrl } from '../../config/redis.config'; @Injectable() -export class AuthDataStore { +export class AuthDataStore implements OnModuleDestroy { private readonly storeConfig; private readonly store: DataStoreService; @@ -30,6 +31,12 @@ export class AuthDataStore { async delete(token: string, target: string): Promise { await this.store.delete(token, target); } + + async onModuleDestroy(): Promise { + if (this.store instanceof RedisDataStore) { + await this.store.onModuleDestroy(); + } + } } /** @@ -127,25 +134,33 @@ export class RedisDataStore private readonly expirySeconds: number; private readonly redisOptions: RedisOptions; + private readonly redisUrl: string; + constructor(config) { super(); - // create redis connection + this.redisUrl = getRedisUrl(config.spec); this.redisOptions = { - host: config.spec.host || 'localhost', - port: parseInt(config.spec.port) || 6379, + tls: getRedisTlsOptions(this.redisUrl), + connectTimeout: 5000, + commandTimeout: 5000, + maxRetriesPerRequest: 1, }; this.expirySeconds = config.spec.expirySeconds; } private async getRedisClient() { if (!this.redisClient) { - this.redisClient = new Redis(this.redisOptions); + this.redisClient = new Redis(this.redisUrl, this.redisOptions); + this.redisClient.on('error', () => + this.logger.error('Authorization Redis connection failed'), + ); // check connection try { await this.redisClient.ping(); - } catch (error) { + } catch { + this.redisClient.disconnect(); this.redisClient = null; - this.logger.error('Cannot connect to redis.', error); + this.logger.error('Cannot connect to authorization Redis'); throw new Error("authDataStore can't connect to redis"); } } diff --git a/src/app.module.ts b/src/app.module.ts index 1749706..425e97d 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,7 +1,7 @@ import { Module, MiddlewareConsumer, NestModule } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { CacheModule } from '@nestjs/cache-manager'; -import { redisStore } from 'cache-manager-ioredis'; +import { createRedisCacheOptions } from './config/redis-cache.config'; import { EventModule } from './shared/event/event.module'; import { RoleModule } from './api/role/role.module'; import { AuthModule } from './core/auth/auth.module'; @@ -23,14 +23,7 @@ import { TopgearUserRolesModule } from './api/topgear-user-role/topgear-user-rol CacheModule.registerAsync({ isGlobal: true, imports: [ConfigModule], - useFactory: (configService: ConfigService) => ({ - store: redisStore, - host: configService.get('REDIS_HOST', '127.0.0.1'), - port: configService.get('REDIS_PORT', 6379), - ttl: 30 * 24 * 60 * 60 * 1000, // Default TTL: 30 days in milliseconds - // password: configService.get('REDIS_PASSWORD'), - // db: configService.get('REDIS_DB', 0), - }), + useFactory: createRedisCacheOptions, inject: [ConfigService], }), EventModule, diff --git a/src/config/configuration.service.ts b/src/config/configuration.service.ts index 7f0d31c..d71aa9c 100644 --- a/src/config/configuration.service.ts +++ b/src/config/configuration.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; +import { getRedisConnectionConfig } from './redis.config'; @Injectable() export class ConfigurationService { @@ -45,8 +46,7 @@ export class ConfigurationService { return { type: 'redis', spec: { - host: this.configService.get('REDIS_HOST'), - port: this.configService.get('REDIS_PORT'), + ...getRedisConnectionConfig(this.configService), expirySeconds: 7776000, }, }; diff --git a/src/config/redis-cache.config.ts b/src/config/redis-cache.config.ts new file mode 100644 index 0000000..dcabfdf --- /dev/null +++ b/src/config/redis-cache.config.ts @@ -0,0 +1,49 @@ +import { Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { CacheModuleOptions } from '@nestjs/cache-manager'; +import KeyvRedis, { RedisClientOptions } from '@keyv/redis'; +import Keyv from 'keyv'; +import { + getRedisConnectionConfig, + getRedisTlsOptions, + getRedisUrl, +} from './redis.config'; + +export async function createRedisCacheOptions( + config: ConfigService, +): Promise { + const url = getRedisUrl(getRedisConnectionConfig(config)); + const tls = getRedisTlsOptions(url); + const logger = new Logger('RedisCache'); + const socket = { connectTimeout: 5000, reconnectStrategy: false as const }; + const options: RedisClientOptions = { + url, + socket: tls ? { ...socket, ...tls, tls: true } : socket, + // Fail requests promptly; the adapter reconnects on subsequent operations. + disableOfflineQueue: true, + }; + const adapter = new KeyvRedis(options, { + throwOnConnectError: true, + throwOnErrors: true, + }); + const store = new Keyv({ + store: adapter, + namespace: 'identity-api-v6', + useKeyPrefix: false, + throwOnErrors: true, + }); + store.on('error', () => + logger.error('Redis cache connection/operation failed'), + ); + + try { + // A configured but unreachable cache must not silently become process memory. + const client = await adapter.getClient(); + await client.ping(); + } catch { + await adapter.disconnect(true).catch(() => undefined); + throw new Error('Cannot connect to the configured Redis cache'); + } + + return { stores: [store], ttl: 30 * 24 * 60 * 60 * 1000 }; +} diff --git a/src/config/redis.config.spec.ts b/src/config/redis.config.spec.ts new file mode 100644 index 0000000..582467b --- /dev/null +++ b/src/config/redis.config.spec.ts @@ -0,0 +1,52 @@ +import { ConfigService } from '@nestjs/config'; +import { + getRedisConnectionConfig, + getRedisTlsOptions, + getRedisUrl, +} from './redis.config'; + +describe('Redis connection configuration', () => { + it('prefers the TLS URL over the legacy host and port', () => { + const config = new ConfigService({ + REDIS_URL: 'rediss://cache.example.com:6379/0', + REDIS_HOST: 'old.example.com', + REDIS_PORT: '6380', + }); + const url = getRedisUrl(getRedisConnectionConfig(config)); + expect(url).toBe('rediss://cache.example.com:6379/0'); + expect(getRedisTlsOptions(url)).toEqual({ + servername: 'cache.example.com', + minVersion: 'TLSv1.2', + rejectUnauthorized: true, + }); + }); + + it('preserves legacy host/port and plaintext local development', () => { + expect(getRedisUrl({ host: 'localhost', port: '6380' })).toBe( + 'redis://localhost:6380', + ); + expect(getRedisTlsOptions(getRedisUrl({}))).toBeUndefined(); + }); + + it('preserves URL credentials and database without logging them', () => { + const url = 'rediss://default:escaped%40password@cache.example.com:6379/2'; + expect(getRedisUrl({ url })).toBe(url); + }); + + it.each([ + 'https://cache.example.com', + 'cache.example.com:6379', + 'rediss://cache.example.com:0', + 'rediss://cache.example.com:invalid', + 'rediss://cache.example.com/invalid-db', + 'rediss://cache.example.com?tls=false', + 'rediss://cache.example.com#fragment', + ])( + 'rejects malformed URLs without falling back to the old host: %s', + (url) => { + expect(() => getRedisUrl({ url, host: 'old.example.com' })).toThrow( + 'Invalid Redis configuration: use redis:// or rediss://', + ); + }, + ); +}); diff --git a/src/config/redis.config.ts b/src/config/redis.config.ts new file mode 100644 index 0000000..ca79319 --- /dev/null +++ b/src/config/redis.config.ts @@ -0,0 +1,53 @@ +import { ConfigService } from '@nestjs/config'; +import { ConnectionOptions } from 'tls'; + +export interface RedisConnectionConfig { + url?: string; + host?: string; + port?: string | number; +} + +export function getRedisConnectionConfig( + config: ConfigService, +): RedisConnectionConfig { + return { + url: config.get('REDIS_URL'), + host: config.get('REDIS_HOST', '127.0.0.1'), + port: config.get('REDIS_PORT', '6379'), + }; +} + +/** REDIS_URL takes precedence; legacy host/port remain available for local/prod. */ +export function getRedisUrl(config: RedisConnectionConfig): string { + const value = config.url?.trim(); + try { + const url = new URL( + value || `redis://${config.host || '127.0.0.1'}:${config.port || 6379}`, + ); + if ( + !['redis:', 'rediss:'].includes(url.protocol) || + !url.hostname || + url.search || + url.hash || + (url.pathname && !/^\/\d*$/.test(url.pathname)) || + url.port === '0' + ) { + throw new Error('Invalid connection configuration'); + } + return url.toString(); + } catch { + // URL parsing errors can include credentials; never include the supplied URL. + throw new Error('Invalid Redis configuration: use redis:// or rediss://'); + } +} + +export function getRedisTlsOptions(url: string): ConnectionOptions | undefined { + const parsed = new URL(url); + return parsed.protocol === 'rediss:' + ? { + servername: parsed.hostname, + minVersion: 'TLSv1.2', + rejectUnauthorized: true, + } + : undefined; +} diff --git a/test/redis.integration-spec.ts b/test/redis.integration-spec.ts new file mode 100644 index 0000000..307cae6 --- /dev/null +++ b/test/redis.integration-spec.ts @@ -0,0 +1,130 @@ +import { CacheModule, CACHE_MANAGER } from '@nestjs/cache-manager'; +import { ConfigModule, ConfigService } from '@nestjs/config'; +import { Test, TestingModule } from '@nestjs/testing'; +import { Cache } from 'cache-manager'; +import Redis from 'ioredis'; +import { randomUUID } from 'crypto'; +import { createRedisCacheOptions } from '../src/config/redis-cache.config'; +import { getRedisTlsOptions } from '../src/config/redis.config'; +import { ConfigurationService } from '../src/config/configuration.service'; +import { AuthDataStore } from '../src/api/authorization/auth-data-store.service'; + +// Use a disposable Redis/Valkey instance. Never point this suite at application data. +const url = process.env.REDIS_TEST_URL; +const describeRedis = url ? describe : describe.skip; +const itTls = url?.startsWith('rediss://') ? it : it.skip; + +describeRedis('Redis integration', () => { + let first: TestingModule; + let second: TestingModule; + let observer: Redis; + const prefix = `pm6691-test-${randomUUID()}`; + + async function createModule() { + const config = new ConfigService({ REDIS_URL: url }); + return Test.createTestingModule({ + imports: [ + CacheModule.registerAsync({ + imports: [ConfigModule], + useFactory: () => createRedisCacheOptions(config), + }), + ], + providers: [ + AuthDataStore, + { + provide: ConfigurationService, + useValue: new ConfigurationService(config), + }, + ], + }).compile(); + } + + beforeAll(async () => { + observer = new Redis(url, { tls: getRedisTlsOptions(url) }); + await observer.ping(); + first = await createModule(); + second = await createModule(); + }); + + afterAll(async () => { + await first?.close(); + await second?.close(); + await observer?.quit(); + }); + + it('shares cache values across independent Nest containers and deletes them', async () => { + const key = `${prefix}:shared`; + const a = first.get(CACHE_MANAGER); + const b = second.get(CACHE_MANAGER); + try { + await a.set(key, { otp: 'synthetic' }, 5000); + expect(await b.get(key)).toEqual({ otp: 'synthetic' }); + const ttl = await observer.pttl(`identity-api-v6::${key}`); + expect(ttl).toBeGreaterThan(0); + expect(ttl).toBeLessThanOrEqual(5000); + await b.del(key); + expect(await a.get(key)).toBeNull(); + } finally { + await a.del(key); + } + }); + + it('expires cache values using millisecond TTLs', async () => { + const key = `${prefix}:expiry`; + const cache = first.get(CACHE_MANAGER); + await cache.set(key, 'synthetic', 50); + await new Promise((resolve) => setTimeout(resolve, 100)); + expect(await second.get(CACHE_MANAGER).get(key)).toBeNull(); + }); + + it('preserves authorization key format and its 90-day TTL across instances', async () => { + const claims = Buffer.from(JSON.stringify({ userId: prefix })).toString( + 'base64url', + ); + const auth = { token: `e30.${claims}.synthetic`, target: 'test' }; + const a = first.get(AuthDataStore); + const b = second.get(AuthDataStore); + try { + await a.put(auth); + expect(await b.get(auth.token, auth.target)).toEqual(auth); + const ttl = await observer.ttl( + `ap:identity:authorization:${prefix}:test`, + ); + expect(ttl).toBeGreaterThan(7775990); + expect(ttl).toBeLessThanOrEqual(7776000); + await b.delete(auth.token, auth.target); + expect(await a.get(auth.token, auth.target)).toBeNull(); + } finally { + await a.delete(auth.token, auth.target); + } + }); + + it('fails startup when the configured cache cannot be reached', async () => { + await expect( + createRedisCacheOptions( + new ConfigService({ REDIS_URL: 'redis://127.0.0.1:1' }), + ), + ).rejects.toThrow('Cannot connect to the configured Redis cache'); + }); + + itTls( + 'rejects a TLS certificate for a different hostname in both clients', + async () => { + const wrongHostname = new URL(url); + // The disposable test server certificate is valid only for localhost. + wrongHostname.hostname = '127.0.0.1'; + const config = new ConfigService({ REDIS_URL: wrongHostname.toString() }); + await expect(createRedisCacheOptions(config)).rejects.toThrow( + 'Cannot connect to the configured Redis cache', + ); + const auth = new AuthDataStore(new ConfigurationService(config)); + try { + await expect(auth.get('synthetic', 'test')).rejects.toThrow( + "authDataStore can't connect to redis", + ); + } finally { + await auth.onModuleDestroy(); + } + }, + ); +});