Tips & Tricks (अपडेट: 2/6/2026)

Claude Code के साथ secrets management: .env से production rotation तक

Claude Code से .env, CI/CD secrets, cloud keys, redacted logs और safe permission boundaries संभालें।

Claude Code के साथ secrets management: .env से production rotation तक

API key, database URL, OAuth client secret और cloud deployment credentials किसी भी real application के लिए जरूरी होते हैं। लेकिन एक गलत copy-paste के बाद यही values Git history, CI logs, screenshots, tickets, README और article draft में लंबे समय तक रह सकती हैं। Claude Code से debugging, migration, deployment या documentation करवाते समय पहला नियम साफ होना चाहिए: Claude Code safety system बनाने में मदद कर सकता है, पर उसे असली secret values देखनी, print करनी, store करनी या docs में दोबारा लिखनी नहीं चाहिए।

Secrets management सिर्फ password छिपाने का नाम नहीं है। इसका मतलब है configuration को code से अलग रखना, local/dev/staging/production को अलग रखना, least privilege यानी जरूरत भर permission देना, app start होते समय values validate करना, logs में sensitive values redact करना और incident से पहले rotation process तैयार रखना। The Twelve-Factor App config principle आज भी सही शुरुआत है: configuration environment से आनी चाहिए, repository में hard-code नहीं होनी चाहिए। Practical setup में .gitignore, .env.example, validated config loader, CI/CD secrets, cloud secret store और rotation checklist भी चाहिए।

flowchart LR
  Dev["Local .env"] --> Loader["Validated config loader"]
  CI["CI/CD secrets"] --> Deploy["Deployment runtime"]
  Store["AWS / GCP / Azure secret store"] --> Deploy
  Deploy --> App["Application process"]
  App --> Logs["Redacted logs"]

1. पहले तय करें कि कौन सी value कहां रहेगी

Beginner गलती यह है कि सभी environment variables को एक जैसा मान लिया जाता है। सरल test है: अगर किसी value के leak होने से कोई पैसा खर्च कर सकता है, email भेज सकता है, data पढ़ या लिख सकता है, deploy कर सकता है या user बनकर login कर सकता है, तो वह secret है। Public base URL, feature flag का नाम या OAuth client ID अक्सर कम sensitive होते हैं क्योंकि अकेले उनसे permission नहीं मिलती।

Use caseExamplesLocal developmentProduction
Email और API integrationSendGrid API key, Stripe secret key, GitHub token.env में test या sandbox keyCI/CD secrets या secret store से inject
DatabaseDATABASE_URL, user, passwordlocal या dev-only userread/write permission अलग और password rotation
Cloud deployAWS/GCP/Azure credentialslong-lived local keys से बचेंOIDC या deploy-only role
OAuth और webhookOAUTH_CLIENT_SECRET, webhook signing secretlocal app अलग बनाएंprod secrets repo से बाहर रखें

मुख्य बात environment separation है। Production Stripe key को local में reuse न करें। Organization-wide GitHub token को CI में न डालें। Deploy credential को administrator permission न दें। यही least privilege है: काम के लिए जितनी permission चाहिए, उतनी ही, सही environment में, और संभव हो तो सीमित समय के लिए।

2. Repository में shape रखें, value नहीं

.env local development के लिए उपयोगी है, लेकिन उसे commit नहीं करना चाहिए। Repository में .env.example रखें, जिसमें variable names, format, source और purpose लिखे हों। Onboarding के लिए इसे environment variables guide के साथ जोड़ें ताकि नए developers बार-बार वही सवाल न पूछें।

# Local secrets
.env
.env.*
!.env.example
!.env.test.example

# Logs and generated output that may contain tokens
npm-debug.log*
yarn-debug.log*
coverage/
dist/
# .env.example - placeholders only, never real values
NODE_ENV=development
APP_BASE_URL=http://localhost:3000

# Use a local database user, not production credentials.
DATABASE_URL=postgres://app_user:replace-me@localhost:5432/app_dev

# Use test/sandbox keys for local development.
SENDGRID_API_KEY=SG.xxxxxx
STRIPE_SECRET_KEY=sk_test_xxxxxx
GITHUB_TOKEN=ghp_xxxxxx

# OAuth secrets must be separated by environment.
OAUTH_CLIENT_ID=local-client-id
OAUTH_CLIENT_SECRET=replace-me

# Deployment reads these from CI or a cloud secret store.
AWS_REGION=ap-northeast-1
DEPLOY_ROLE_ARN=arn:aws:iam::123456789012:role/app-deploy-dev

Claude Code को involve करते समय boundary लिखें: वह .env.example, deployment manifests, package scripts और variable names पढ़ सकता है; पर .env नहीं खोलेगा, real values chat में नहीं लिखेगा और credentials documentation में नहीं डालेगा। अगर असली value चाहिए, तो आप उसे local machine, CI settings या secret store में set करें। Claude Code को सिर्फ variable name बताएं।

3. Node.js में startup validation और log redaction करें

अगर कोई required secret missing है या format गलत है, तो app को startup पर ही fail होना चाहिए। Debug करते समय token print न हो, इसके लिए redaction helper भी चाहिए। नीचे Node.js loader dotenv और envalid से validation करता है और logs के लिए redactConfig देता है।

import { config as loadDotenv } from "dotenv";
import { cleanEnv, str, url } from "envalid";

const envFile = process.env.NODE_ENV === "test" ? ".env.test" : ".env";
loadDotenv({ path: envFile });

const secretKeyPattern = /(KEY|TOKEN|SECRET|PASSWORD|DATABASE_URL|PRIVATE)/i;

const env = cleanEnv(process.env, {
  NODE_ENV: str({
    choices: ["development", "test", "staging", "production"],
    default: "development",
  }),
  APP_BASE_URL: url({ default: "http://localhost:3000" }),
  DATABASE_URL: url(),
  SENDGRID_API_KEY: str(),
  STRIPE_SECRET_KEY: str(),
  GITHUB_TOKEN: str(),
  OAUTH_CLIENT_ID: str(),
  OAUTH_CLIENT_SECRET: str(),
  AWS_REGION: str({ default: "ap-northeast-1" }),
  DEPLOY_ROLE_ARN: str(),
});

export function appConfig() {
  return Object.freeze({
    nodeEnv: env.NODE_ENV,
    appBaseUrl: env.APP_BASE_URL,
    databaseUrl: env.DATABASE_URL,
    sendgridApiKey: env.SENDGRID_API_KEY,
    stripeSecretKey: env.STRIPE_SECRET_KEY,
    githubToken: env.GITHUB_TOKEN,
    oauthClientId: env.OAUTH_CLIENT_ID,
    oauthClientSecret: env.OAUTH_CLIENT_SECRET,
    awsRegion: env.AWS_REGION,
    deployRoleArn: env.DEPLOY_ROLE_ARN,
  });
}

export function redactValue(key, value) {
  if (!secretKeyPattern.test(key)) return value;
  if (!value) return "<empty>";
  const text = String(value);
  if (text.length <= 8) return "<redacted>";
  return `${text.slice(0, 4)}...${text.slice(-4)}`;
}

export function redactConfig(config) {
  return Object.fromEntries(
    Object.entries(config).map(([key, value]) => [key, redactValue(key, value)]),
  );
}

if (process.argv[1] === new URL(import.meta.url).pathname) {
  console.log(redactConfig(appConfig()));
}

Common failure यह है कि incident के समय कोई console.log(process.env) डाल देता है और फिर CI log को ticket या chat में paste कर देता है। अगर वही raw log Claude Code को दिया गया, तो वह secret को test, docs या article में फिर से लिख सकता है। पहले redact करें, फिर मदद मांगें। Screenshot भी upload करने से पहले crop या blur करें।

4. Claude Code के लिए permission boundaries लिखें

Claude Code को अधिकतर real values की जरूरत नहीं होती। उसे variable names, format, redacted error और permission का purpose चाहिए। Security, deploy या debug task शुरू करते समय यह prompt boundary paste करें।

You may inspect .env.example, package.json, deployment manifests, and secret names.
Do not open, print, summarize, store, or copy .env, CI/CD secret values, cloud credentials, production dumps, or screenshots containing tokens.
When you need a value, ask me to set it outside chat and confirm only the variable name.
If a secret appears in command output, stop, redact it, and report which file or command exposed it.
Before changing permissions, explain the least-privilege scope and ask for approval.
Do not paste real secrets into prompts, logs, documentation, code comments, tests, tickets, or articles.

Commands पर भी control रखें। printenv, cat .env, cloud CLI जो credentials दिखाती है, complete CI log dump और screenshot capture confirmation के बिना न चलाएं। .env.example, type definitions, redacted errors और official docs से Claude Code अधिकतर fixes कर सकता है।

5. CI/CD secrets और cloud secret stores को अलग रखें

Practical model यह है: local के लिए .env, pipeline के लिए CI/CD secrets और production runtime के लिए cloud secret store। AWS में AWS Secrets Manager, Google Cloud में Secret Manager, और Azure में Key Vault इस्तेमाल करें। GitHub repositories में secret scanning enable करें ताकि गलती से committed tokens जल्दी मिलें।

name: deploy

on:
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    env:
      NODE_ENV: production
      AWS_REGION: ap-northeast-1
    steps:
      - uses: actions/checkout@v4
      - name: Validate required secret names
        run: test -n "${{ secrets.DEPLOY_ROLE_ARN }}" && test -n "${{ secrets.DATABASE_URL }}"
      - name: Deploy without echoing secrets
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
          DEPLOY_ROLE_ARN: ${{ secrets.DEPLOY_ROLE_ARN }}
        run: npm run deploy

CI में बड़ा pitfall है “echo करके देख लेते हैं।” GitHub Actions कई exact secret values mask करता है, लेकिन derived strings, encoded URLs, JSON, screenshots या unregistered values हमेशा सुरक्षित नहीं रहते। Claude Code से variable names और validation steps मांगें, values नहीं। अगर वह broad permissions सुझाए, तो reason मांगें और scope घटाएं।

6. Rotation को emergency नहीं, routine बनाएं

Rotation सिर्फ leak के बाद नहीं होनी चाहिए। SendGrid, Stripe, GitHub tokens, database passwords, OAuth client secrets, webhook signing secrets और cloud trust policies के लिए owner, environment, consumers, last rotation date और next review date लिखें।

## Secret rotation checklist
- [ ] Identify owner, environment, consumers, and business impact.
- [ ] Create a new secret with the smallest required scope.
- [ ] Store it in CI/CD secrets or the cloud secret store.
- [ ] Deploy one service or job with the new value.
- [ ] Confirm logs and metrics without printing the secret.
- [ ] Revoke the old secret.
- [ ] Scan Git history, tickets, docs, screenshots, and chat snippets.
- [ ] Record the rotation date and next review date.

Claude Code इस checklist को issue template, runbook या migration plan में बदलने के लिए अच्छा है। लेकिन उसे old या new key value न दें। बेहतर prompt है: “SENDGRID_API_KEY की सभी references खोजें और v2 key rollout plan बनाएं।”

7. Publish से पहले failure cases जांचें

इनमें से कोई भी बात सही हो तो publish या deploy रोक दें:

  • .env या .env.production commit हुआ और exposed key revoke नहीं की गई।
  • Screenshot, CI log, error report या article example में token, DB URL या OAuth secret दिखता है।
  • Cloud key को administrator permission मिली है जबकि deploy-only permission काफी थी।
  • Production Stripe, SendGrid, database या GitHub credentials local में reuse हो रहे हैं।
  • OAuth client secret या webhook signing secret का owner और rotation runbook नहीं है।
  • AI को real values दी गईं और उसने उन्हें README, tests, tickets या drafts में दोबारा लिख दिया।

Security incidents अक्सर process gaps से आते हैं। security audit checklist से permissions देखें, security failure cases से reviewer training करें, API development guide से service boundary जांचें, और SendGrid या email automation के लिए email automation article पढ़ें।

8. Team rollout practical रखें

पहले सप्ताह में सभी services को secret store पर migrate करने की कोशिश न करें। एक application चुनें और पूरा path बनाएं: .gitignore, .env.example, validated loader, redacted logs, CI/CD secrets, production secret store और rotation record। फिर common secrets migrate करें: SendGrid, Stripe, GitHub token, DATABASE_URL, OAuth client secret और deploy credentials।

ClaudeCodeLab की training और consulting real repository structure पर काम करती है, लेकिन real secret values नहीं लेती। हम तय करते हैं कि Claude Code कौन सी files inspect कर सकता है, CI/CD secret boundaries design करते हैं, GitHub secret scanning enable करते हैं, AWS/GCP/Azure secret store migration plan बनाते हैं और reusable prompts/runbooks छोड़ते हैं। इससे team अगले दिन code review में template इस्तेमाल कर सकती है।

सार यह है कि secrets management सिर्फ strings छिपाना नहीं है। Values को code से अलग करें, environments isolate करें, startup पर validate करें, logs redact करें, permissions कम करें और rotation practice करें। Claude Code इस system को बनाने और review करने में मदद कर सकता है, पर secrets copy करने की नई जगह नहीं बनना चाहिए।

Masa के sample Node.js app में यह workflow चलाकर देखा गया। Config loader ने missing values पकड़ीं, redactConfig ने CI logs से database URL और API keys छिपाईं, और deploy workflow से unnecessary write permissions हट गईं। एक पुराना screenshot Stripe test key दिखा रहा था, इसलिए publish से पहले key फिर से issue की गई। Practical result यह है: logs, images और drafts को source code जितनी ही सख्ती से review करना होगा।

#claude-code #security #secrets-prabandhan #devops
मुफ़्त

मुफ़्त PDF: Claude Code cheatsheet

Email डालें और commands, review habits तथा safe workflow वाली एक-page PDF पाएँ.

हम आपका data सुरक्षित रखते हैं और spam नहीं भेजते.

Masa

लेखक के बारे में

Masa

Claude Code workflow और team adoption पर काम करने वाला engineer.