|
| 1 | +# Neuron Secrets Management |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Neuron provides Rails-style encrypted credentials management to securely store sensitive configuration like database passwords, API keys, and other secrets. Secrets are encrypted using AES-256-CBC with HMAC authentication and can be safely committed to version control. |
| 6 | + |
| 7 | +## Quick Start |
| 8 | + |
| 9 | +### 1. Generate an Encryption Key |
| 10 | + |
| 11 | +```bash |
| 12 | +# Generate master key |
| 13 | +neuron secrets:key:generate |
| 14 | + |
| 15 | +# Generate environment-specific key |
| 16 | +neuron secrets:key:generate --env=production |
| 17 | +``` |
| 18 | + |
| 19 | +This creates a 256-bit encryption key at: |
| 20 | +- `config/master.key` for base secrets |
| 21 | +- `config/secrets/production.key` for environment-specific secrets |
| 22 | + |
| 23 | +**⚠️ IMPORTANT:** Never commit key files to version control! Add them to `.gitignore` immediately. |
| 24 | + |
| 25 | +### 2. Edit Secrets |
| 26 | + |
| 27 | +```bash |
| 28 | +# Edit base secrets (uses $EDITOR or vi by default) |
| 29 | +neuron secrets:edit |
| 30 | + |
| 31 | +# Edit with specific editor |
| 32 | +neuron secrets:edit --editor=nano |
| 33 | + |
| 34 | +# Edit environment-specific secrets |
| 35 | +neuron secrets:edit --env=production |
| 36 | +``` |
| 37 | + |
| 38 | +This opens your editor with the decrypted YAML content. When you save and exit, the file is automatically re-encrypted. |
| 39 | + |
| 40 | +### 3. View Secrets |
| 41 | + |
| 42 | +```bash |
| 43 | +# Show all secrets |
| 44 | +neuron secrets:show |
| 45 | + |
| 46 | +# Show specific key |
| 47 | +neuron secrets:show --key=database |
| 48 | + |
| 49 | +# Show production secrets (with confirmation) |
| 50 | +neuron secrets:show --env=production |
| 51 | + |
| 52 | +# Skip confirmation for production |
| 53 | +neuron secrets:show --env=production --force |
| 54 | +``` |
| 55 | + |
| 56 | +## File Structure |
| 57 | + |
| 58 | +``` |
| 59 | +config/ |
| 60 | +├── master.key # Base encryption key (gitignored) |
| 61 | +├── secrets.yml.enc # Encrypted base secrets |
| 62 | +└── secrets/ |
| 63 | + ├── production.key # Production key (gitignored) |
| 64 | + ├── production.yml.enc # Encrypted production secrets |
| 65 | + ├── staging.key # Staging key (gitignored) |
| 66 | + └── staging.yml.enc # Encrypted staging secrets |
| 67 | +``` |
| 68 | + |
| 69 | +## Environment Variables |
| 70 | + |
| 71 | +Instead of key files, you can use environment variables: |
| 72 | + |
| 73 | +```bash |
| 74 | +# For master key |
| 75 | +export NEURON_MASTER_KEY=your_64_char_hex_key |
| 76 | + |
| 77 | +# For environment-specific keys |
| 78 | +export NEURON_PRODUCTION_KEY=your_64_char_hex_key |
| 79 | +export NEURON_STAGING_KEY=your_64_char_hex_key |
| 80 | +``` |
| 81 | + |
| 82 | +The system automatically checks for environment variables if key files don't exist. |
| 83 | + |
| 84 | +## CMS Installation Integration |
| 85 | + |
| 86 | +When installing the CMS, use the `--use-secrets` flag to store sensitive configuration in encrypted files: |
| 87 | + |
| 88 | +```bash |
| 89 | +neuron cms:install --use-secrets |
| 90 | +``` |
| 91 | + |
| 92 | +This will: |
| 93 | +1. Generate a master key if one doesn't exist |
| 94 | +2. Split configuration between public (`neuron.yaml`) and encrypted (`secrets.yml.enc`) |
| 95 | +3. Automatically update `.gitignore` to exclude key files |
| 96 | + |
| 97 | +### What Gets Encrypted |
| 98 | + |
| 99 | +Public configuration (`neuron.yaml`): |
| 100 | +- Application name, URL, environment |
| 101 | +- Public settings like timezone, locale |
| 102 | +- Non-sensitive service endpoints |
| 103 | + |
| 104 | +Encrypted secrets (`secrets.yml.enc`): |
| 105 | +- Database passwords |
| 106 | +- API keys and tokens |
| 107 | +- SMTP credentials |
| 108 | +- Session secrets |
| 109 | +- Any sensitive configuration |
| 110 | + |
| 111 | +## Security Best Practices |
| 112 | + |
| 113 | +### Key Management |
| 114 | + |
| 115 | +1. **Never commit key files** - Always add `*.key` to `.gitignore` |
| 116 | +2. **Use different keys per environment** - Don't share production keys with staging/dev |
| 117 | +3. **Rotate keys periodically** - Generate new keys and re-encrypt when team members leave |
| 118 | +4. **Secure key distribution** - Use password managers or secure channels to share keys |
| 119 | + |
| 120 | +### Access Control |
| 121 | + |
| 122 | +1. **Limit key access** - Only give production keys to authorized personnel |
| 123 | +2. **Use environment variables in production** - Avoid key files on production servers |
| 124 | +3. **Audit access** - Log who accesses encrypted secrets in production |
| 125 | + |
| 126 | +### Git Best Practices |
| 127 | + |
| 128 | +```gitignore |
| 129 | +# Encryption keys - NEVER commit these! |
| 130 | +/config/master.key |
| 131 | +/config/secrets/*.key |
| 132 | +/config/*.key |
| 133 | +*.key |
| 134 | +
|
| 135 | +# But DO commit encrypted files |
| 136 | +!/config/secrets.yml.enc |
| 137 | +!/config/secrets/*.yml.enc |
| 138 | +``` |
| 139 | + |
| 140 | +## Command Options |
| 141 | + |
| 142 | +### secrets:key:generate |
| 143 | + |
| 144 | +| Option | Short | Description | |
| 145 | +|--------|-------|------------| |
| 146 | +| `--env` | `-e` | Environment for the key (e.g., production, staging) | |
| 147 | +| `--config` | `-c` | Config directory path (default: config) | |
| 148 | +| `--force` | `-f` | Overwrite existing key file | |
| 149 | +| `--show` | `-s` | Display the generated key | |
| 150 | + |
| 151 | +### secrets:edit |
| 152 | + |
| 153 | +| Option | Short | Description | |
| 154 | +|--------|-------|------------| |
| 155 | +| `--env` | `-e` | Environment to edit (default: base secrets) | |
| 156 | +| `--editor` | | Editor to use (default: $EDITOR or vi) | |
| 157 | +| `--config` | `-c` | Config directory path (default: config) | |
| 158 | + |
| 159 | +### secrets:show |
| 160 | + |
| 161 | +| Option | Short | Description | |
| 162 | +|--------|-------|------------| |
| 163 | +| `--env` | `-e` | Environment to show (default: base secrets) | |
| 164 | +| `--key` | `-k` | Show only specific key/section | |
| 165 | +| `--config` | `-c` | Config directory path (default: config) | |
| 166 | +| `--force` | `-f` | Skip confirmation prompts | |
| 167 | + |
| 168 | +## Troubleshooting |
| 169 | + |
| 170 | +### "Key file not found" |
| 171 | + |
| 172 | +**Problem:** The command can't find the encryption key. |
| 173 | + |
| 174 | +**Solutions:** |
| 175 | +1. Check if the key file exists at the expected location |
| 176 | +2. Set the key as an environment variable |
| 177 | +3. Use `--config` to specify the correct directory |
| 178 | +4. Generate a new key with `secrets:key:generate` |
| 179 | + |
| 180 | +### "Editor exited with error" |
| 181 | + |
| 182 | +**Problem:** The editor command failed. |
| 183 | + |
| 184 | +**Solutions:** |
| 185 | +1. Check your `$EDITOR` environment variable |
| 186 | +2. Use `--editor` to specify a different editor |
| 187 | +3. Ensure the editor is installed and in your PATH |
| 188 | +4. Try a simple editor like `nano` or `vi` |
| 189 | + |
| 190 | +### "Cannot decrypt file" |
| 191 | + |
| 192 | +**Problem:** The encrypted file can't be decrypted. |
| 193 | + |
| 194 | +**Solutions:** |
| 195 | +1. Verify you're using the correct key |
| 196 | +2. Check if the encrypted file is corrupted |
| 197 | +3. Ensure the key matches the one used for encryption |
| 198 | +4. Try using the environment variable instead of key file |
| 199 | + |
| 200 | +## Examples |
| 201 | + |
| 202 | +### Development Setup |
| 203 | + |
| 204 | +```bash |
| 205 | +# Generate development key |
| 206 | +neuron secrets:key:generate |
| 207 | + |
| 208 | +# Add database credentials |
| 209 | +neuron secrets:edit |
| 210 | +# Add in editor: |
| 211 | +# database: |
| 212 | +# host: localhost |
| 213 | +# username: myapp |
| 214 | +# password: dev_password_123 |
| 215 | +# database: myapp_dev |
| 216 | + |
| 217 | +# Verify secrets |
| 218 | +neuron secrets:show --key=database |
| 219 | +``` |
| 220 | + |
| 221 | +### Production Deployment |
| 222 | + |
| 223 | +```bash |
| 224 | +# On deployment server |
| 225 | +export NEURON_PRODUCTION_KEY=<key_from_secure_storage> |
| 226 | + |
| 227 | +# Verify secrets are accessible |
| 228 | +neuron secrets:show --env=production --force |
| 229 | + |
| 230 | +# Start application (secrets automatically loaded) |
| 231 | +php artisan serve |
| 232 | +``` |
| 233 | + |
| 234 | +### Team Collaboration |
| 235 | + |
| 236 | +```bash |
| 237 | +# Team lead generates keys |
| 238 | +neuron secrets:key:generate |
| 239 | +neuron secrets:key:generate --env=staging |
| 240 | +neuron secrets:key:generate --env=production |
| 241 | + |
| 242 | +# Share keys securely (e.g., via password manager) |
| 243 | +# Each team member sets environment variables |
| 244 | +export NEURON_MASTER_KEY=<shared_key> |
| 245 | +export NEURON_STAGING_KEY=<shared_key> |
| 246 | + |
| 247 | +# Team members can now work with secrets |
| 248 | +neuron secrets:show |
| 249 | +neuron secrets:edit --env=staging |
| 250 | +``` |
| 251 | + |
| 252 | +## Migration from Plain Config |
| 253 | + |
| 254 | +If you have existing plain configuration files: |
| 255 | + |
| 256 | +1. Generate a master key: |
| 257 | + ```bash |
| 258 | + neuron secrets:key:generate |
| 259 | + ``` |
| 260 | + |
| 261 | +2. Create initial secrets file: |
| 262 | + ```bash |
| 263 | + neuron secrets:edit |
| 264 | + ``` |
| 265 | + |
| 266 | +3. Move sensitive values from plain config to secrets |
| 267 | + |
| 268 | +4. Update application to read from both sources |
| 269 | + |
| 270 | +5. Commit encrypted file, gitignore key: |
| 271 | + ```bash |
| 272 | + echo "/config/master.key" >> .gitignore |
| 273 | + git add config/secrets.yml.enc .gitignore |
| 274 | + git commit -m "Add encrypted secrets" |
| 275 | + ``` |
| 276 | + |
| 277 | +## Integration with Neuron Components |
| 278 | + |
| 279 | +The secrets system integrates with various Neuron components: |
| 280 | + |
| 281 | +- **CMS**: Database credentials, admin keys |
| 282 | +- **Mail**: SMTP passwords, API tokens |
| 283 | +- **Cache**: Redis passwords, connection strings |
| 284 | +- **Queue**: Connection credentials |
| 285 | +- **Storage**: S3 keys, CDN tokens |
| 286 | + |
| 287 | +Each component automatically loads secrets when available, falling back to plain configuration for non-sensitive values. |
0 commit comments