|
| 1 | +# bun.secrets Integration |
| 2 | + |
| 3 | +This project includes a modular `bun.secrets` integration for secure credential storage and management. This allows you to store API keys and other sensitive data in your operating system's native credential manager instead of plain text `.env` files. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +1. **Enable bun.secrets:** |
| 8 | + ```bash |
| 9 | + export USE_BUN_SECRETS=1 |
| 10 | + ``` |
| 11 | + |
| 12 | +2. **Initialize your secrets from existing environment variables:** |
| 13 | + ```bash |
| 14 | + bun run secrets init |
| 15 | + ``` |
| 16 | + |
| 17 | +3. **Manage secrets interactively:** |
| 18 | + ```bash |
| 19 | + bun run secrets |
| 20 | + ``` |
| 21 | + |
| 22 | +## Available Commands |
| 23 | + |
| 24 | +### `bun run secrets` |
| 25 | +Interactive CLI for managing secrets: |
| 26 | +- 📊 Show status |
| 27 | +- 📋 List all secrets |
| 28 | +- 🔐 Set a secret |
| 29 | +- 🔍 Get a secret |
| 30 | +- 🗑️ Delete a secret |
| 31 | +- 🚀 Initialize from environment |
| 32 | + |
| 33 | +### `bun run secrets <action>` |
| 34 | +Direct command execution: |
| 35 | +```bash |
| 36 | +bun run secrets status # Show current status |
| 37 | +bun run secrets list # List all configured secrets |
| 38 | +bun run secrets set # Set a secret value |
| 39 | +bun run secrets get # Retrieve a secret value |
| 40 | +bun run secrets delete # Delete a secret |
| 41 | +bun run secrets init # Initialize from environment variables |
| 42 | +``` |
| 43 | + |
| 44 | +### `bun run test:secrets` |
| 45 | +Test the secrets integration and verify functionality. |
| 46 | + |
| 47 | +## Environment Variables |
| 48 | + |
| 49 | +### USE_BUN_SECRETS |
| 50 | +- `0` or `false`: Use traditional environment variables (default) |
| 51 | +- `1` or `true`: Use bun.secrets for credential storage |
| 52 | + |
| 53 | +When enabled, the application will: |
| 54 | +1. First try to retrieve secrets from bun.secrets |
| 55 | +2. Fall back to environment variables if not found |
| 56 | +3. Store new secrets using bun.secrets |
| 57 | + |
| 58 | +### Supported Secrets |
| 59 | + |
| 60 | +The system supports these secrets by default: |
| 61 | + |
| 62 | +| Secret Key | Description | Environment Variable | |
| 63 | +|------------|-------------|---------------------| |
| 64 | +| `VERCEL_OIDC_TOKEN` | Vercel OIDC authentication token | `VERCEL_OIDC_TOKEN` | |
| 65 | +| `ANTHROPIC_API_KEY` | Anthropic Claude API key | `ANTHROPIC_API_KEY` | |
| 66 | +| `OPENAI_API_KEY` | OpenAI API key | `OPENAI_API_KEY` | |
| 67 | +| `OPENROUTER_API_KEY` | OpenRouter API key | `OPENROUTER_API_KEY` | |
| 68 | + |
| 69 | +## Platform-Specific Storage |
| 70 | + |
| 71 | +- **macOS**: Keychain Services |
| 72 | +- **Linux**: libsecret (GNOME Keyring, KWallet, etc.) |
| 73 | +- **Windows**: Windows Credential Manager |
| 74 | + |
| 75 | +## Usage Examples |
| 76 | + |
| 77 | +### Setting Secrets |
| 78 | + |
| 79 | +```bash |
| 80 | +# Enable bun.secrets |
| 81 | +export USE_BUN_SECRETS=1 |
| 82 | + |
| 83 | +# Set API key interactively |
| 84 | +bun run secrets set |
| 85 | +# Select "ANTHROPIC_API_KEY" from the menu |
| 86 | +# Enter your key when prompted |
| 87 | +``` |
| 88 | + |
| 89 | +### Migrating from .env Files |
| 90 | + |
| 91 | +```bash |
| 92 | +# With your .env file in place and USE_BUN_SECRETS=1 |
| 93 | +bun run secrets init |
| 94 | +# This will copy all environment variables into bun.secrets |
| 95 | +``` |
| 96 | + |
| 97 | +### Programmatic Usage |
| 98 | + |
| 99 | +```typescript |
| 100 | +import { getSecret, setSecret, isBunSecretsEnabled } from "./lib/secrets.ts"; |
| 101 | + |
| 102 | +// Check if bun.secrets is enabled |
| 103 | +if (isBunSecretsEnabled()) { |
| 104 | + // Get a secret |
| 105 | + const apiKey = await getSecret("ANTHROPIC_API_KEY"); |
| 106 | + |
| 107 | + // Set a secret |
| 108 | + await setSecret("CUSTOM_SECRET", "super-secret-value"); |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Security Considerations |
| 113 | + |
| 114 | +- ✅ Credentials are encrypted by the operating system |
| 115 | +- ✅ Access is restricted to the user who stored them |
| 116 | +- ✅ No plain text storage of sensitive data |
| 117 | +- ✅ Memory is zeroed after use |
| 118 | +- ✅ Survives application restarts |
| 119 | + |
| 120 | +## Migration from Environment Variables |
| 121 | + |
| 122 | +1. **Enable bun.secrets:** |
| 123 | + ```bash |
| 124 | + export USE_BUN_SECRETS=1 |
| 125 | + ``` |
| 126 | + |
| 127 | +2. **Initialize from current environment:** |
| 128 | + ```bash |
| 129 | + bun run secrets init |
| 130 | + ``` |
| 131 | + |
| 132 | +3. **Verify migration:** |
| 133 | + ```bash |
| 134 | + bun run secrets list |
| 135 | + ``` |
| 136 | + |
| 137 | +4. **Optional:** Remove sensitive values from .env files |
| 138 | + |
| 139 | +## Custom Secret Definitions |
| 140 | + |
| 141 | +The system is modular and can be extended with custom secrets: |
| 142 | + |
| 143 | +```typescript |
| 144 | +import { DEFAULT_SECRETS, type SecretDefinition } from "./lib/secrets.ts"; |
| 145 | + |
| 146 | +const customSecrets: SecretDefinition = { |
| 147 | + ...DEFAULT_SECRETS, |
| 148 | + MY_CUSTOM_API_KEY: { |
| 149 | + service: "my-app", |
| 150 | + name: "custom-api-key", |
| 151 | + envVar: "MY_CUSTOM_API_KEY", |
| 152 | + }, |
| 153 | +}; |
| 154 | + |
| 155 | +// Use custom secrets |
| 156 | +await setSecret("MY_CUSTOM_API_KEY", "my-secret-value", customSecrets); |
| 157 | +``` |
| 158 | + |
| 159 | +## Troubleshooting |
| 160 | + |
| 161 | +### "bun.secrets is not enabled" |
| 162 | +Set `USE_BUN_SECRETS=1` in your environment. |
| 163 | + |
| 164 | +### "Failed to retrieve secret" |
| 165 | +- Check that the secret exists: `bun run secrets list` |
| 166 | +- Verify the secret name in the configuration |
| 167 | +- Check OS-specific permissions for credential storage |
| 168 | + |
| 169 | +### Linux: "Secret service daemon not running" |
| 170 | +Install and start a secret service: |
| 171 | +```bash |
| 172 | +# Ubuntu/Debian |
| 173 | +sudo apt-get install libsecret-1-0 |
| 174 | +# GNOME Keyring should start automatically |
| 175 | + |
| 176 | +# Or for systems without GUI |
| 177 | +sudo apt-get install gnome-keyring |
| 178 | +eval $(gnome-keyring-daemon --start) |
| 179 | +``` |
| 180 | + |
| 181 | +### Windows: Credential Manager access denied |
| 182 | +Ensure you're running with appropriate permissions to access Windows Credential Manager. |
| 183 | + |
| 184 | +## Best Practices |
| 185 | + |
| 186 | +1. **Enable bun.secrets in development** for better security |
| 187 | +2. **Use environment variables in CI/CD** (bun.secrets is not for production secrets) |
| 188 | +3. **Initialize secrets** when switching between bun.secrets and environment variables |
| 189 | +4. **Regularly check status** with `bun run secrets status` |
| 190 | +5. **Remove old .env values** after successful migration to avoid confusion |
0 commit comments