Skip to content

feat: support standard AWS env vars and default credential chain#395

Open
purva-8 wants to merge 3 commits intousesend:mainfrom
purva-8:feat/standard-aws-env-vars
Open

feat: support standard AWS env vars and default credential chain#395
purva-8 wants to merge 3 commits intousesend:mainfrom
purva-8:feat/standard-aws-env-vars

Conversation

@purva-8
Copy link
Copy Markdown

@purva-8 purva-8 commented May 2, 2026

Summary

Fixes #316

  • Rename AWS_ACCESS_KEYAWS_ACCESS_KEY_ID and AWS_SECRET_KEYAWS_SECRET_ACCESS_KEY to match the AWS standard naming convention used by the SDK, CLI, and IAM tooling
  • Make credentials optional: both vars are now z.string().optional() in the env schema
  • Default credential chain: when the vars are absent, the credentials object is omitted from SESv2Client, STSClient, and SNSClient — the AWS SDK automatically falls back to its default provider chain (IAM roles, ECS/EKS task roles, EC2 instance profiles, ~/.aws/credentials, etc.)
  • Backward compatibility: the runtimeEnv falls back to the old names (?? process.env.AWS_ACCESS_KEY) so existing deployments don't break without a config change

Files changed

File Change
apps/web/src/env.js Rename schema keys, make optional, add backward-compat fallback
apps/web/src/server/aws/ses.ts Conditionally pass credentials to SESv2Client / STSClient
apps/web/src/server/aws/sns.ts Conditionally pass credentials to SNSClient
apps/web/src/test/setup/setup-env.ts Update test env keys
apps/web/.env.test.example Update example keys
.env.example Update example keys
.env.selfhost.example Update example keys, improve comment

Test plan

  • Existing deployments using the old AWS_ACCESS_KEY / AWS_SECRET_KEY names continue to work (backward-compat fallback)
  • Deployments using the new AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY names work
  • Deployments on ECS/EKS/EC2 with an IAM role and no credential env vars can boot and send email via the default provider chain
  • Unit tests pass (pnpm test)

Summary by cubic

Switches to AWS-standard env vars AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY and uses the AWS default credential chain when unset. Adds a shared credential helper that errors on partial configs; keeps backward compatibility and only passes credentials to SESv2, STS, and SNS when both vars are set.

  • New Features

    • Credentials are optional; SDK default chain is used when unset.
    • New getAwsCredentialOptions centralizes logic for SESv2Client, STSClient, and SNSClient.
    • Throws if only one of AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY is set.
  • Migration

    • Rename AWS_ACCESS_KEYAWS_ACCESS_KEY_ID and AWS_SECRET_KEYAWS_SECRET_ACCESS_KEY.
    • On IAM-role deployments, omit these vars to rely on the default chain.
    • Existing setups using old names keep working; migrate when convenient.

Written for commit 5c816be. Summary will update on new commits.

Summary by CodeRabbit

  • Chores
    • Renamed AWS credential environment variables from AWS_ACCESS_KEY/AWS_SECRET_KEY to AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY across examples and test configs.
    • Updated runtime validation to accept the new variables while falling back to the legacy names for compatibility.
    • Added credential-handling checks to ensure both new variables are set together (or omitted) to avoid misconfiguration.

Replace non-standard AWS_ACCESS_KEY / AWS_SECRET_KEY with the AWS-standard
AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY. The old names are kept as
fallbacks in the runtimeEnv for backward compatibility.

Both vars are now optional. When omitted, the credentials object is not
passed to SESv2Client, STSClient, or SNSClient — the AWS SDK then falls
back to its default provider chain (IAM roles, ECS task roles, instance
profiles, etc.), which is the recommended approach for cloud-native deployments.

Closes usesend#316

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 2, 2026 12:24
@vercel
Copy link
Copy Markdown

vercel Bot commented May 2, 2026

@purva-8 is attempting to deploy a commit to the kmkoushik's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 2, 2026

Walkthrough

This pull request updates AWS credential variable naming conventions across the codebase. Environment configuration files (.env.example, .env.selfhost.example, and apps/web/.env.test.example) are modified to use standard AWS naming (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY instead of AWS_ACCESS_KEY and AWS_SECRET_KEY). The environment schema in apps/web/src/env.js is updated to validate these new optional variables with fallback support for legacy names. AWS service clients in ses.ts and sns.ts are refactored to conditionally inject credentials using the new variable names. Test environment setup is similarly updated to populate the new credential variable names.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: supporting standard AWS environment variables and enabling use of the default credential chain.
Linked Issues check ✅ Passed All coding requirements from issue #316 are met: standard AWS env vars are supported, credentials are optional, and the default credential chain can be used.
Out of Scope Changes check ✅ Passed All changes directly support the PR objectives of renaming AWS variables, making credentials optional, and maintaining backward compatibility.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 6/8 reviews remaining, refill in 9 minutes and 41 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the web app’s AWS credential handling to support standard AWS environment variable names and to allow the AWS SDK default credential provider chain when static credentials aren’t provided (fixing #316).

Changes:

  • Renames AWS_ACCESS_KEY / AWS_SECRET_KEY to AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY (with legacy fallback).
  • Makes AWS credential env vars optional and conditionally omits the credentials override for AWS SDK clients.
  • Updates test setup and example env files to reflect the new variable names.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
apps/web/src/env.js Updates env schema keys and adds legacy fallback in runtimeEnv.
apps/web/src/server/aws/ses.ts Conditionally passes credentials to STSClient and SESv2Client.
apps/web/src/server/aws/sns.ts Conditionally passes credentials to SNSClient.
apps/web/src/test/setup/setup-env.ts Updates test env defaults to new AWS credential keys.
apps/web/.env.test.example Updates example AWS credential keys for tests.
.env.example Updates example AWS credential keys for local/dev.
.env.selfhost.example Updates example keys and clarifies optional credential chain usage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apps/web/src/env.js Outdated
Comment thread apps/web/src/env.js
Comment thread apps/web/src/server/aws/ses.ts Outdated
Comment thread apps/web/src/server/aws/sns.ts Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
apps/web/src/server/aws/ses.ts (1)

33-35: ⚡ Quick win

Extract the repeated credential-options construction to a shared helper.

The same conditional spread appears three times: STS client (Line 33–35), SES client (Line 52–54), and the SNS client in sns.ts (Line 13–15). If the logic ever needs to add sessionToken or change the guard condition, all three sites must be updated in sync.

♻️ Proposed refactor

Add a shared helper (e.g. in apps/web/src/server/aws/credentials.ts):

import { env } from "~/env";

export function getAwsCredentialOptions() {
  if (env.AWS_ACCESS_KEY_ID && env.AWS_SECRET_ACCESS_KEY) {
    return {
      credentials: {
        accessKeyId: env.AWS_ACCESS_KEY_ID,
        secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
      },
    };
  }
  return {};
}

Then in ses.ts:

-    ...(env.AWS_ACCESS_KEY_ID && env.AWS_SECRET_ACCESS_KEY
-      ? { credentials: { accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY } }
-      : {}),
+    ...getAwsCredentialOptions(),

Apply the same replacement in the second occurrence in ses.ts and in sns.ts.

Also applies to: 52-54

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/web/src/server/aws/ses.ts` around lines 33 - 35, Extract the repeated
conditional credential spread into a shared helper (e.g.
getAwsCredentialOptions) that reads env.AWS_ACCESS_KEY_ID and
env.AWS_SECRET_ACCESS_KEY and returns either { credentials: { accessKeyId,
secretAccessKey } } or {}; then replace the inline conditional spread used when
constructing the STS client, SES client (the block containing
...(env.AWS_ACCESS_KEY_ID && env.AWS_SECRET_ACCESS_KEY ? { credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY }
} : {}) in ses.ts) and the SNS client in sns.ts with a call to
getAwsCredentialOptions(), ensuring the helper is exported from a common module
(e.g. credentials.ts) and imported where needed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@apps/web/src/server/aws/ses.ts`:
- Around line 33-35: Extract the repeated conditional credential spread into a
shared helper (e.g. getAwsCredentialOptions) that reads env.AWS_ACCESS_KEY_ID
and env.AWS_SECRET_ACCESS_KEY and returns either { credentials: { accessKeyId,
secretAccessKey } } or {}; then replace the inline conditional spread used when
constructing the STS client, SES client (the block containing
...(env.AWS_ACCESS_KEY_ID && env.AWS_SECRET_ACCESS_KEY ? { credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY }
} : {}) in ses.ts) and the SNS client in sns.ts with a call to
getAwsCredentialOptions(), ensuring the helper is exported from a common module
(e.g. credentials.ts) and imported where needed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 59ef1c51-59a4-4cf8-9644-0f3ad9c00c0f

📥 Commits

Reviewing files that changed from the base of the PR and between 964bbf9 and 83258bf.

📒 Files selected for processing (7)
  • .env.example
  • .env.selfhost.example
  • apps/web/.env.test.example
  • apps/web/src/env.js
  • apps/web/src/server/aws/ses.ts
  • apps/web/src/server/aws/sns.ts
  • apps/web/src/test/setup/setup-env.ts

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/web/src/env.js`:
- Around line 34-35: The environment schema currently allows AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY to be set independently which lets code in
apps/web/src/server/aws/ses.ts and apps/web/src/server/aws/sns.ts silently fall
back to the default provider when only one credential is present; update the zod
schema for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to validate they are
either both present or both absent (e.g., using a .refine or a custom validator)
so the app fails fast with a clear error when only one credential is provided,
and ensure any code that reads these vars (SES/SNS initialization) relies on
that validated pair rather than tolerating partial config.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d7aa3fb3-6172-4517-80bd-b5a93802008a

📥 Commits

Reviewing files that changed from the base of the PR and between 83258bf and 4277fb8.

📒 Files selected for processing (1)
  • apps/web/src/env.js

Comment thread apps/web/src/env.js
…al-config guard

- Move the credential spread logic into a single credentials.ts helper
  so SESv2Client, STSClient, and SNSClient all share one implementation
- Throw a clear error if only one of AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY
  is set, preventing silent fallback to the default provider chain with a
  half-configured environment

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/web/src/server/aws/credentials.ts`:
- Around line 4-19: The credentials helper currently only returns accessKeyId
and secretAccessKey, which breaks temporary AWS creds; add support for
AWS_SESSION_TOKEN by adding AWS_SESSION_TOKEN to your env.js schema and
runtimeEnv, then update the return object in
apps/web/src/server/aws/credentials.ts to conditionally include sessionToken
(e.g., when env.AWS_SESSION_TOKEN is present, add credentials.sessionToken =
env.AWS_SESSION_TOKEN) alongside accessKeyId and secretAccessKey, and update
test fixtures and .env.example to include an optional AWS_SESSION_TOKEN entry so
temporary credential flows are exercised.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2d02e951-f64f-4566-99ee-986b878bc362

📥 Commits

Reviewing files that changed from the base of the PR and between 4277fb8 and 5c816be.

📒 Files selected for processing (3)
  • apps/web/src/server/aws/credentials.ts
  • apps/web/src/server/aws/ses.ts
  • apps/web/src/server/aws/sns.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/web/src/server/aws/sns.ts
  • apps/web/src/server/aws/ses.ts

Comment on lines +4 to +19
const hasKey = !!env.AWS_ACCESS_KEY_ID;
const hasSecret = !!env.AWS_SECRET_ACCESS_KEY;

if (hasKey !== hasSecret) {
throw new Error(
"AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must both be set or both be omitted"
);
}

if (hasKey) {
return {
credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID!,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY!,
},
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify whether AWS_SESSION_TOKEN is modeled and propagated in current PR surface.
# Expected: references in env schema/mapping + credentials helper + (optionally) env examples.
rg -n -C3 '\bAWS_SESSION_TOKEN\b' \
  apps/web/src/env.js \
  apps/web/src/server/aws/credentials.ts \
  apps/web/src/server/aws/ses.ts \
  apps/web/src/server/aws/sns.ts \
  .env.example \
  .env.selfhost.example \
  apps/web/.env.test.example || true

Repository: usesend/useSend

Length of output: 41


🏁 Script executed:

# First, let's find the actual env schema/configuration file
fd -e js -e ts -e json apps/web/src/ | grep -i env | head -20

Repository: usesend/useSend

Length of output: 417


🏁 Script executed:

# Let's examine the credentials.ts file in full
cat -n apps/web/src/server/aws/credentials.ts

Repository: usesend/useSend

Length of output: 713


🏁 Script executed:

# Check the env.js or env schema structure
fd -type f -name "env*" apps/web/src/ | head -10

Repository: usesend/useSend

Length of output: 230


🏁 Script executed:

# Find env configuration files more carefully
find apps/web/src -name "env*" -type f | head -20

Repository: usesend/useSend

Length of output: 79


🏁 Script executed:

# Also search for where env is exported from
rg -l "export.*env" apps/web/src --type ts --type js | head -10

Repository: usesend/useSend

Length of output: 196


🏁 Script executed:

# Look for AWS configuration in existing code
rg "AWS_ACCESS_KEY_ID|AWS_SECRET_ACCESS_KEY" apps/web/src --type ts --type js -A 2 -B 2

Repository: usesend/useSend

Length of output: 2484


🏁 Script executed:

# Read the full env.js file
cat -n apps/web/src/env.js

Repository: usesend/useSend

Length of output: 7193


🏁 Script executed:

# Check AWS SDK documentation expectations for sessionToken
# Look for any existing usage or patterns of sessionToken in the codebase
rg "sessionToken|session_token" apps/web --type ts --type js

Repository: usesend/useSend

Length of output: 41


🏁 Script executed:

# Check how AWS SDK is being used for SES and SNS
cat -n apps/web/src/server/aws/ses.ts | head -50

Repository: usesend/useSend

Length of output: 1938


🏁 Script executed:

# Look for any auth-related issues or comments in AWS files
rg "auth|sign|credential|temporary|session" apps/web/src/server/aws/ --type ts -i

Repository: usesend/useSend

Length of output: 781


🏁 Script executed:

# Check AWS SDK version/package being used
rg "@aws-sdk|aws-sdk" apps/web/package.json

Repository: usesend/useSend

Length of output: 265


Add AWS_SESSION_TOKEN support to credentials helper for temporary AWS credentials.

AWS SDK v3 requires sessionToken when using temporary credentials (from STS, IAM roles, or AssumeRole). The current implementation only handles accessKeyId and secretAccessKey, which works for long-lived IAM credentials but will fail with temporary credentials.

Add AWS_SESSION_TOKEN as an optional environment variable in env.js schema and runtimeEnv, then conditionally include it in the returned credentials object in credentials.ts. Update test setup and .env.example files as needed.

Suggested patch
 export function getAwsCredentialOptions() {
   const hasKey = !!env.AWS_ACCESS_KEY_ID;
   const hasSecret = !!env.AWS_SECRET_ACCESS_KEY;
+  const hasSessionToken = !!env.AWS_SESSION_TOKEN;

   if (hasKey !== hasSecret) {
     throw new Error(
       "AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must both be set or both be omitted"
     );
   }

   if (hasKey) {
     return {
       credentials: {
         accessKeyId: env.AWS_ACCESS_KEY_ID!,
         secretAccessKey: env.AWS_SECRET_ACCESS_KEY!,
+        ...(hasSessionToken ? { sessionToken: env.AWS_SESSION_TOKEN! } : {}),
       },
     };
   }
   return {};
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/web/src/server/aws/credentials.ts` around lines 4 - 19, The credentials
helper currently only returns accessKeyId and secretAccessKey, which breaks
temporary AWS creds; add support for AWS_SESSION_TOKEN by adding
AWS_SESSION_TOKEN to your env.js schema and runtimeEnv, then update the return
object in apps/web/src/server/aws/credentials.ts to conditionally include
sessionToken (e.g., when env.AWS_SESSION_TOKEN is present, add
credentials.sessionToken = env.AWS_SESSION_TOKEN) alongside accessKeyId and
secretAccessKey, and update test fixtures and .env.example to include an
optional AWS_SESSION_TOKEN entry so temporary credential flows are exercised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feat: Avoid hardcoding AWS credentials & support standard AWS env variables

2 participants