|
| 1 | +# SCRAM-SHA-256 Authentication |
| 2 | + |
| 3 | +SCRAM-SHA-256 (Salted Challenge Response Authentication Mechanism) is a password authentication method in PostgreSQL that provides better security than traditional MD5 authentication. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +SCRAM-SHA-256 authentication offers several advantages: |
| 8 | +- **Stronger security**: Uses SHA-256 instead of MD5 |
| 9 | +- **Salt protection**: Prevents rainbow table attacks |
| 10 | +- **Iteration count**: Makes brute force attacks more difficult |
| 11 | +- **Mutual authentication**: Both client and server verify each other |
| 12 | + |
| 13 | +## Password Format |
| 14 | + |
| 15 | +SCRAM-SHA-256 passwords have this specific format: |
| 16 | +``` |
| 17 | +SCRAM-SHA-256$<iteration_count>:<salt>$<StoredKey>:<ServerKey> |
| 18 | +``` |
| 19 | + |
| 20 | +Example: |
| 21 | +``` |
| 22 | +SCRAM-SHA-256$4096:27klCUc487uwvJVGKI5YNA==$6K2Y+S3YBlpfRNrLROoO2ulWmnrQoRlGI1GqpNRq0T0=:y4esBVjK/hMtxDB5aWN4ynS1SnQcT1TFTqV0J/snls4= |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage with Chef |
| 26 | + |
| 27 | +### Creating Users with SCRAM-SHA-256 Passwords |
| 28 | + |
| 29 | +When you have a pre-computed SCRAM-SHA-256 password hash: |
| 30 | + |
| 31 | +```ruby |
| 32 | +postgresql_role 'secure_user' do |
| 33 | + encrypted_password 'SCRAM-SHA-256$4096:27klCUc487uwvJVGKI5YNA==$6K2Y+S3YBlpfRNrLROoO2ulWmnrQoRlGI1GqpNRq0T0=:y4esBVjK/hMtxDB5aWN4ynS1SnQcT1TFTqV0J/snls4=' |
| 34 | + login true |
| 35 | + action :create |
| 36 | +end |
| 37 | +``` |
| 38 | + |
| 39 | +### Automatic Character Escaping |
| 40 | + |
| 41 | +The cookbook automatically handles escaping of special characters (`$`) in SCRAM-SHA-256 passwords. You don't need to manually escape these characters - the cookbook will handle this transparently. |
| 42 | + |
| 43 | +**Before (manual escaping required):** |
| 44 | +```ruby |
| 45 | +postgresql_role 'user1' do |
| 46 | + # Manual escaping was required |
| 47 | + password 'SCRAM-SHA-256$4096:salt$key:server'.gsub('$', '\$') |
| 48 | + action [:create, :update] |
| 49 | +end |
| 50 | +``` |
| 51 | + |
| 52 | +**Now (automatic escaping):** |
| 53 | +```ruby |
| 54 | +postgresql_role 'user1' do |
| 55 | + # No manual escaping needed |
| 56 | + encrypted_password 'SCRAM-SHA-256$4096:salt$key:server' |
| 57 | + action [:create, :update] |
| 58 | +end |
| 59 | +``` |
| 60 | + |
| 61 | +## Configuring Authentication |
| 62 | + |
| 63 | +To use SCRAM-SHA-256 authentication, configure the access method: |
| 64 | + |
| 65 | +```ruby |
| 66 | +postgresql_access 'scram authentication' do |
| 67 | + type 'host' |
| 68 | + database 'all' |
| 69 | + user 'myuser' |
| 70 | + address '127.0.0.1/32' |
| 71 | + auth_method 'scram-sha-256' |
| 72 | +end |
| 73 | +``` |
| 74 | + |
| 75 | +## Password Generation |
| 76 | + |
| 77 | +### Using PostgreSQL |
| 78 | + |
| 79 | +Generate a SCRAM-SHA-256 password directly in PostgreSQL: |
| 80 | + |
| 81 | +```sql |
| 82 | +-- Set password for existing user (PostgreSQL will hash it) |
| 83 | +ALTER ROLE myuser PASSWORD 'plaintext_password'; |
| 84 | + |
| 85 | +-- Check the generated hash |
| 86 | +SELECT rolpassword FROM pg_authid WHERE rolname = 'myuser'; |
| 87 | +``` |
| 88 | + |
| 89 | +### Using Ruby |
| 90 | + |
| 91 | +Generate a SCRAM-SHA-256 hash using the `scram-sha-256` gem: |
| 92 | + |
| 93 | +```ruby |
| 94 | +require 'scram-sha-256' |
| 95 | + |
| 96 | +# Generate hash with default iteration count (4096) |
| 97 | +password_hash = ScramSha256.hash_password('my_plain_password') |
| 98 | + |
| 99 | +# Generate hash with custom iteration count |
| 100 | +password_hash = ScramSha256.hash_password('my_plain_password', 8192) |
| 101 | +``` |
| 102 | + |
| 103 | +### Using Python |
| 104 | + |
| 105 | +Generate a SCRAM-SHA-256 hash using Python: |
| 106 | + |
| 107 | +```python |
| 108 | +import hashlib |
| 109 | +import hmac |
| 110 | +import base64 |
| 111 | +import secrets |
| 112 | + |
| 113 | +def generate_scram_sha256(password, salt=None, iterations=4096): |
| 114 | + if salt is None: |
| 115 | + salt = secrets.token_bytes(16) |
| 116 | + |
| 117 | + # Implementation details would go here |
| 118 | + # This is a simplified example |
| 119 | + pass |
| 120 | +``` |
| 121 | + |
| 122 | +## Common Use Cases |
| 123 | + |
| 124 | +### Control Panel Integration |
| 125 | + |
| 126 | +When integrating with control panels that pre-hash passwords: |
| 127 | + |
| 128 | +```ruby |
| 129 | +# Control panel provides pre-hashed password |
| 130 | +hashed_password = control_panel.get_user_password_hash(username) |
| 131 | + |
| 132 | +postgresql_role username do |
| 133 | + encrypted_password hashed_password |
| 134 | + login true |
| 135 | + createdb user_permissions.include?('createdb') |
| 136 | + action [:create, :update] |
| 137 | +end |
| 138 | +``` |
| 139 | + |
| 140 | +### Migration from MD5 |
| 141 | + |
| 142 | +When migrating from MD5 to SCRAM-SHA-256: |
| 143 | + |
| 144 | +```ruby |
| 145 | +# First, configure SCRAM-SHA-256 authentication |
| 146 | +postgresql_access 'upgrade to scram' do |
| 147 | + type 'host' |
| 148 | + database 'all' |
| 149 | + user 'all' |
| 150 | + address '127.0.0.1/32' |
| 151 | + auth_method 'scram-sha-256' |
| 152 | +end |
| 153 | + |
| 154 | +# Users will need to reset their passwords |
| 155 | +# The new passwords will automatically use SCRAM-SHA-256 |
| 156 | +``` |
| 157 | + |
| 158 | +## Troubleshooting |
| 159 | + |
| 160 | +### Common Issues |
| 161 | + |
| 162 | +1. **Password mangling**: If you see passwords with missing `$` characters, ensure you're using this cookbook version that includes automatic escaping. |
| 163 | + |
| 164 | +2. **Authentication failures**: Verify that: |
| 165 | + - The `pg_hba.conf` is configured for `scram-sha-256` |
| 166 | + - The password hash format is correct |
| 167 | + - The user has login privileges |
| 168 | + |
| 169 | +3. **Iteration count**: Higher iteration counts (e.g., 8192 or 16384) provide better security but require more CPU time. |
| 170 | + |
| 171 | +### Debugging |
| 172 | + |
| 173 | +Check the PostgreSQL logs for authentication details: |
| 174 | + |
| 175 | +```bash |
| 176 | +tail -f /var/log/postgresql/postgresql-*.log |
| 177 | +``` |
| 178 | + |
| 179 | +Verify user configuration: |
| 180 | + |
| 181 | +```sql |
| 182 | +SELECT rolname, rolcanlogin, rolpassword |
| 183 | +FROM pg_authid |
| 184 | +WHERE rolname = 'your_username'; |
| 185 | +``` |
| 186 | + |
| 187 | +## Security Recommendations |
| 188 | + |
| 189 | +1. **Use high iteration counts**: 4096 is the minimum; consider 8192 or higher for sensitive applications. |
| 190 | +2. **Enforce SCRAM-SHA-256**: Disable MD5 authentication entirely when possible. |
| 191 | +3. **Regular password rotation**: Implement password rotation policies. |
| 192 | +4. **Monitor authentication**: Log and monitor authentication attempts. |
| 193 | + |
| 194 | +## References |
| 195 | + |
| 196 | +- [PostgreSQL SCRAM-SHA-256 Documentation](https://www.postgresql.org/docs/current/auth-password.html) |
| 197 | +- [RFC 7677: SCRAM-SHA-256 and SCRAM-SHA-256-PLUS](https://tools.ietf.org/html/rfc7677) |
| 198 | +- [PostgreSQL Security Best Practices](https://www.postgresql.org/docs/current/auth-methods.html) |
0 commit comments