Password Management
Password Management
Section titled “Password Management”DBCrust provides comprehensive password management for all supported database types through the universal .dbcrust file system. This system works like PostgreSQL’s .pgpass but extends support to all databases with automatic encryption and cross-platform compatibility.
🔐 Overview
Section titled “🔐 Overview”The password management system provides:
- Universal Support: Works with PostgreSQL, MySQL, MongoDB, Elasticsearch, ClickHouse, SQLite
- Automatic Lookup: Searches for passwords when connecting without credentials
- Authentication Retry: Prompts for password on auth failure and offers to save it
- Secure Encryption: Machine-specific encryption ensures passwords only work on the machine where they were saved
- Cross-Platform: Works on Linux, macOS, and Windows with platform-specific security features
📁 Password File Format
Section titled “📁 Password File Format”The .dbcrust file uses the format:
database_type:host:port:database:username:passwordExamples
Section titled “Examples”# PostgreSQL - plaintext passwordpostgresql:localhost:5432:myapp:dbuser:mypassword
# MySQL - encrypted password (prefixed with 'enc:')mysql:db.example.com:3306:webapp:admin:enc:a1b2c3d4e5f6789...
# MongoDB clustermongodb:cluster.mongodb.net:27017:analytics:analyst:secret123
# Wildcard matching - any host, port, user for specific databasepostgresql:*:*:testdb:*:development_password
# Elasticsearch with custom portelasticsearch:es.cluster.com:9200:logs:elastic_user:elastic_pass
# ClickHouseclickhouse:ch.example.com:8123:analytics:analyst:ch_passwordWildcard Support
Section titled “Wildcard Support”Use * to match any value for that field:
# Match any host/port for testdb databasepostgresql:*:*:testdb:testuser:testpass
# Match any database for specific user on hostmysql:localhost:*:*:admin:admin_pass
# Match any user on localhost PostgreSQLpostgresql:localhost:5432:myapp:*:shared_password🗂️ File Location
Section titled “🗂️ File Location”| Platform | Default Location | Custom Location |
|---|---|---|
| Linux/macOS | ~/.dbcrust | $DBCRUST_PASSFILE |
| Windows | %USERPROFILE%\.dbcrust | %DBCRUST_PASSFILE% |
Security
Section titled “Security”- Unix/macOS: File permissions automatically set to
0600(readable/writable by owner only) - Windows: Relies on NTFS directory security and file system permissions
🔄 Automatic Password Flow
Section titled “🔄 Automatic Password Flow”DBCrust automatically manages passwords through this flow:
graph TD A[Connect to Database] --> B{Password in URL?} B -->|Yes| C[Use Provided Password] B -->|No| D[Lookup in .dbcrust] D --> E{Password Found?} E -->|Yes| F[Try Connection with Found Password] E -->|No| G[Prompt for Password] F --> H{Connection Success?} H -->|Yes| I[Connected Successfully] H -->|No| G C --> J{Connection Success?} J -->|Yes| I J -->|No| G G --> K[Try Connection with Entered Password] K --> L{Connection Success?} L -->|Yes| M[Save Password to .dbcrust?] L -->|No| N[Authentication Failed] M --> O[Password Saved Encrypted] O --> IExample Flow
Section titled “Example Flow”# 1. Connect without password❯ dbcrust postgres://postgres@localhost:5432/postgres
# 2. Authentication fails, automatic prompt appears🔐 Password: [hidden input]
# 3. Password accepted, automatically saved✅ Password saved to .dbcrust file (encrypted)✓ Successfully connected to database
# 4. Next time: automatic connection using saved password❯ dbcrust postgres://postgres@localhost:5432/postgres✓ Successfully connected to database # Uses saved password🛠️ Interactive Commands
Section titled “🛠️ Interactive Commands”\savepass - Save Password
Section titled “\savepass - Save Password”Interactively save a password to the .dbcrust file:
postgres@myapp=> \savepassDatabase type: postgresqlHost: localhostPort: 5432Database name: myappUsername: postgresPassword: [hidden input]Password saved for postgresql:postgres@localhost:5432/myapp (encrypted)Features:
- Interactive prompts for all connection details
- Default values provided based on common database ports
- Passwords automatically encrypted
- Updates existing entries if found
\listpass - List Stored Passwords
Section titled “\listpass - List Stored Passwords”Display all stored credentials without showing the actual passwords:
postgres@myapp=> \listpassSaved passwords: postgresql:postgres@localhost:5432/myapp mysql:admin@db.example.com:3306/webapp mongodb:analyst@cluster.mongodb.net:27017/analytics elasticsearch:elastic@es.cluster.com:9200/logs\deletepass - Delete Password
Section titled “\deletepass - Delete Password”Interactively select and remove a stored password:
postgres@myapp=> \deletepassSelect password to delete: 1. postgresql:postgres@localhost:5432/myapp 2. mysql:admin@db.example.com:3306/webapp 3. mongodb:analyst@cluster.mongodb.net:27017/analytics
Selection: 2Password deleted: mysql:admin@db.example.com:3306/webapp\encryptpass - Encrypt Passwords
Section titled “\encryptpass - Encrypt Passwords”Convert all plaintext passwords in the .dbcrust file to encrypted format:
postgres@myapp=> \encryptpassEncrypted 3 password(s) in .dbcrust file.When to use:
- After manually editing the
.dbcrustfile with plaintext passwords - Migrating from an old system
- Converting existing plaintext entries to encrypted format
🔒 Encryption Details
Section titled “🔒 Encryption Details”Machine-Specific Keys
Section titled “Machine-Specific Keys”Passwords are encrypted using AES-256-GCM with machine-specific keys derived from:
Linux
- Machine ID from
/etc/machine-idor/var/lib/dbus/machine-id - User home directory path
- Hostname, username, user ID
- Fixed salt for additional security
macOS
- Hardware UUID via
ioregorsystem_profiler - User home directory path
- Hostname, username
- Fixed salt for additional security
Windows
- Machine GUID via PowerShell or WMI
- User home directory path
- Hostname, USERNAME environment variable
- Fixed salt for additional security
Security Features
Section titled “Security Features”- Machine-Specific: Encrypted passwords only work on the machine where they were created
- User-Specific: Incorporates user identity into the encryption key
- Salt Protection: Fixed salt prevents rainbow table attacks
- Memory Safety: Passwords cleared from memory after use
- Auto-Detection: System automatically detects encrypted vs plaintext passwords
Password Format
Section titled “Password Format”# Plaintext passwordpostgresql:localhost:5432:myapp:user:mypassword
# Encrypted password (prefixed with 'enc:')postgresql:localhost:5432:myapp:user:enc:a1b2c3d4e5f6789abcdef...🌐 Cross-Platform Usage
Section titled “🌐 Cross-Platform Usage”Linux Example
Section titled “Linux Example”# Create .dbcrust fileecho "postgresql:localhost:5432:myapp:postgres:mypass" > ~/.dbcrustchmod 600 ~/.dbcrust
# Connect automaticallydbcrust postgres://postgres@localhost:5432/myappmacOS Example
Section titled “macOS Example”# Same as Linuxecho "postgresql:localhost:5432:myapp:postgres:mypass" > ~/.dbcrustchmod 600 ~/.dbcrust
# Connect automaticallydbcrust postgres://postgres@localhost:5432/myappWindows Example
Section titled “Windows Example”# Create .dbcrust file in user profileSet-Content -Path "$env:USERPROFILE\.dbcrust" -Value "postgresql:localhost:5432:myapp:postgres:mypass"
# Connect automaticallydbcrust postgres://postgres@localhost:5432/myapp🔧 Advanced Configuration
Section titled “🔧 Advanced Configuration”Custom Password File Location
Section titled “Custom Password File Location”Set the DBCRUST_PASSFILE environment variable to use a custom location:
Linux/macOS
export DBCRUST_PASSFILE="/path/to/my/custom/passwords"dbcrust postgres://postgres@localhost:5432/myappWindows
$env:DBCRUST_PASSFILE = "C:\path\to\my\custom\passwords"dbcrust postgres://postgres@localhost:5432/myappEscaping Special Characters
Section titled “Escaping Special Characters”If your credentials contain colons (:) or backslashes (\), escape them:
# Host with colon in namepostgresql:host\\:with\\:colons:5432:db:user:pass
# Password with colonpostgresql:localhost:5432:db:user:pass\\:with\\:colon
# Database name with backslashpostgresql:localhost:5432:db\\name:user:passwordTeam Workflows
Section titled “Team Workflows”For team environments, consider these patterns:
- Individual Password Files: Each team member maintains their own
.dbcrust - Shared Credentials: Use environment variables for shared database credentials
- Role-Based Access: Different
.dbcrustentries for different database roles
# Example team setuppostgresql:prod.example.com:5432:myapp:readonly:readonly_passpostgresql:prod.example.com:5432:myapp:admin:admin_passpostgresql:staging.example.com:5432:myapp:developer:dev_pass🐛 Troubleshooting
Section titled “🐛 Troubleshooting”Common Issues
Section titled “Common Issues”Password Not Found
Section titled “Password Not Found”# Check file exists and has correct permissionsls -la ~/.dbcrust
# Verify file formatcat ~/.dbcrust
# Check for typos in connection parametersPermission Denied (Unix/macOS)
Section titled “Permission Denied (Unix/macOS)”# Fix file permissionschmod 600 ~/.dbcrustEncryption/Decryption Errors
Section titled “Encryption/Decryption Errors”# Usually indicates the password was encrypted on a different machine# Re-save the password on the current machine:dbcrust postgres://postgres@localhost:5432/myapp# Enter password when prompted - it will be re-encrypted for this machineConnection Still Fails
Section titled “Connection Still Fails”# Verify the stored credentials work manuallydbcrust postgres://username:password@host:port/database
# Check if authentication error detection is workingdbcrust postgres://wronguser@localhost:5432/postgres# Should prompt for passwordDebug Mode
Section titled “Debug Mode”Enable debug logging to troubleshoot password lookup:
# In ~/.config/dbcrust/config.toml[logging]level = "debug"console_output = trueThen check the logs for password lookup activity:
DEBUG [dbcrust] Looking up password in .dbcrust fileDEBUG [dbcrust] Found password in .dbcrust fileDEBUG [dbcrust] Connection successful with looked-up password🔗 Related Features
Section titled “🔗 Related Features”- Vault Integration: For dynamic database credentials
- Session Management: Save complete connection configurations
- Security: Overall security best practices
- Configuration: General configuration options