|
| 1 | +# Database Encryption Rollback Utility |
| 2 | + |
| 3 | +This utility provides functionality to revert encrypted data in multiple database tables back to plain text format. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The rollback utility performs the following operations: |
| 8 | +1. Reads encrypted data from specified columns in supported tables |
| 9 | +2. Decrypts the data using the stored encryption key from the `attributes` table |
| 10 | +3. Stores the decrypted data back as plain text in the same columns |
| 11 | + |
| 12 | +## Supported Tables |
| 13 | + |
| 14 | +| Table | Encrypted Columns | Data Type | |
| 15 | +|-------|------------------|-----------| |
| 16 | +| `cluster` | `config` | EncryptedMap | |
| 17 | +| `gitops_config` | `token` | EncryptedString | |
| 18 | +| `docker_artifact_store` | `aws_secret_accesskey`, `password` | EncryptedString | |
| 19 | +| `git_provider` | `password`, `ssh_private_key`, `access_token` | EncryptedString | |
| 20 | +| `remote_connection_config` | `ssh_password`, `ssh_auth_key` | EncryptedString | |
| 21 | + |
| 22 | +## Files |
| 23 | + |
| 24 | +- `rollback_service.go` - Core rollback service with methods for all supported tables |
| 25 | +- `main.go` - Command-line executable for running rollback operations |
| 26 | +- `rollback_service_test.go` - Unit tests |
| 27 | +- `run_rollback.sh` - Convenient shell script with safety checks |
| 28 | +- `README.md` - This documentation file |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +### Prerequisites |
| 33 | + |
| 34 | +1. Ensure the encryption key exists in the `attributes` table |
| 35 | +2. Set up the required environment variables for database connection |
| 36 | +3. Have appropriate database permissions to read and update the cluster table |
| 37 | + |
| 38 | +### Environment Variables |
| 39 | + |
| 40 | +```bash |
| 41 | +export PG_ADDR="127.0.0.1" # PostgreSQL address |
| 42 | +export PG_PORT="5432" # PostgreSQL port |
| 43 | +export PG_USER="your_username" # PostgreSQL username |
| 44 | +export PG_PASSWORD="your_password" # PostgreSQL password |
| 45 | +export PG_DATABASE="orchestrator" # PostgreSQL database name |
| 46 | +``` |
| 47 | + |
| 48 | +### Running the Utility |
| 49 | + |
| 50 | +#### Rollback All Tables |
| 51 | + |
| 52 | +```bash |
| 53 | +cd common-lib/securestore/rollback |
| 54 | +go run *.go |
| 55 | +``` |
| 56 | + |
| 57 | +#### Rollback Specific Table |
| 58 | + |
| 59 | +```bash |
| 60 | +go run *.go -table=cluster |
| 61 | +go run *.go -table=gitops_config |
| 62 | +go run *.go -table=docker_artifact_store |
| 63 | +go run *.go -table=git_provider |
| 64 | +go run *.go -table=remote_connection_config |
| 65 | +``` |
| 66 | + |
| 67 | +#### Rollback Specific Record |
| 68 | + |
| 69 | +```bash |
| 70 | +go run *.go -table=cluster -id=123 |
| 71 | +go run *.go -table=docker_artifact_store -id=abc-def-123 |
| 72 | +go run *.go -table=gitops_config -id=456 |
| 73 | +``` |
| 74 | + |
| 75 | +#### Validate Rollback Results |
| 76 | + |
| 77 | +```bash |
| 78 | +go run *.go -validate # Validate all tables |
| 79 | +go run *.go -table=cluster -validate # Validate specific table |
| 80 | +``` |
| 81 | + |
| 82 | +#### Use Different Database |
| 83 | + |
| 84 | +```bash |
| 85 | +go run *.go -database=mydb |
| 86 | +``` |
| 87 | + |
| 88 | +#### Show Help |
| 89 | + |
| 90 | +```bash |
| 91 | +go run *.go -help |
| 92 | +``` |
| 93 | + |
| 94 | +### Command Line Options |
| 95 | + |
| 96 | +- `-database string` - Database name to connect to (default: "orchestrator") |
| 97 | +- `-table string` - Table to rollback (cluster, gitops_config, docker_artifact_store, git_provider, remote_connection_config, all) (default: "all") |
| 98 | +- `-id string` - Specific record ID to rollback (optional) |
| 99 | +- `-validate` - Validate rollback results |
| 100 | +- `-help` - Show help message |
| 101 | + |
| 102 | +### Using the Shell Script |
| 103 | + |
| 104 | +The shell script provides additional safety features: |
| 105 | + |
| 106 | +```bash |
| 107 | +# Interactive rollback with confirmation |
| 108 | +./run_rollback.sh |
| 109 | + |
| 110 | +# Rollback specific table |
| 111 | +./run_rollback.sh -t cluster |
| 112 | + |
| 113 | +# Rollback specific record |
| 114 | +./run_rollback.sh -t cluster -i 123 |
| 115 | + |
| 116 | +# Validate results |
| 117 | +./run_rollback.sh -v |
| 118 | + |
| 119 | +# Dry run (show what would be executed) |
| 120 | +./run_rollback.sh --dry-run |
| 121 | +``` |
| 122 | + |
| 123 | +## Code Structure |
| 124 | + |
| 125 | +The rollback utility is implemented as a single Go package with the following files: |
| 126 | + |
| 127 | +- `rollback_service.go` - Core rollback service implementation for all tables |
| 128 | +- `main.go` - Command-line interface and main function |
| 129 | +- `rollback_service_test.go` - Unit tests |
| 130 | +- `go.mod` - Go module definition |
| 131 | + |
| 132 | +All files are in the same `package main` to create a single executable. |
| 133 | + |
| 134 | +## How It Works |
| 135 | + |
| 136 | +### Encryption Detection |
| 137 | + |
| 138 | +The utility uses the `EncryptedMap.Scan()` method to detect if data is encrypted: |
| 139 | +- If the data can be successfully scanned as an `EncryptedMap`, it's considered encrypted |
| 140 | +- If scanning fails, the data is assumed to be already in plain text format |
| 141 | + |
| 142 | +### Decryption Process |
| 143 | + |
| 144 | +1. The utility loads the encryption key from the `attributes` table |
| 145 | +2. For each cluster with config data: |
| 146 | + - Attempts to scan the config as an `EncryptedMap` |
| 147 | + - If successful, the `Scan()` method automatically decrypts the data |
| 148 | + - The decrypted data is then marshaled to JSON and stored back |
| 149 | + |
| 150 | +### Safety Features |
| 151 | + |
| 152 | +- **Non-destructive**: If data is already in plain text, it's left unchanged |
| 153 | +- **Validation**: Provides validation functionality to verify rollback success |
| 154 | +- **Logging**: Comprehensive logging for monitoring progress and debugging |
| 155 | +- **Error handling**: Continues processing other clusters even if one fails |
| 156 | + |
| 157 | +## Error Handling |
| 158 | + |
| 159 | +The utility handles various error scenarios: |
| 160 | +- Database connection failures |
| 161 | +- Missing encryption keys |
| 162 | +- Invalid encrypted data |
| 163 | +- JSON marshaling errors |
| 164 | +- Database update failures |
| 165 | + |
| 166 | +Each error is logged with appropriate context, and the utility continues processing remaining clusters. |
| 167 | + |
| 168 | +## Validation |
| 169 | + |
| 170 | +The validation feature checks that all cluster configs are valid JSON after rollback: |
| 171 | +- Attempts to unmarshal each config as JSON |
| 172 | +- Reports any configs that are not valid JSON |
| 173 | +- Provides summary of validation results |
| 174 | + |
| 175 | +## Security Considerations |
| 176 | + |
| 177 | +- The utility requires access to the encryption key stored in the database |
| 178 | +- Ensure proper database permissions are in place |
| 179 | +- Consider backing up the database before running the rollback |
| 180 | +- The rollback operation converts encrypted data to plain text permanently |
| 181 | + |
| 182 | +## Troubleshooting |
| 183 | + |
| 184 | +### Common Issues |
| 185 | + |
| 186 | +1. **"encryption key not found"** |
| 187 | + - Ensure the encryption key exists in the attributes table |
| 188 | + - Run the encryption key setup if needed |
| 189 | + |
| 190 | +2. **Database connection errors** |
| 191 | + - Verify environment variables are set correctly |
| 192 | + - Check database connectivity and permissions |
| 193 | + |
| 194 | +3. **"Failed to scan as encrypted data"** |
| 195 | + - This is usually normal and indicates data is already in plain text |
| 196 | + - Check logs to confirm the data is being handled correctly |
| 197 | + |
| 198 | +### Logging |
| 199 | + |
| 200 | +The utility provides detailed logging at different levels: |
| 201 | +- `INFO`: General progress and summary information |
| 202 | +- `WARN`: Non-critical issues (e.g., data already in plain text) |
| 203 | +- `ERROR`: Critical errors that prevent processing |
| 204 | +- `DEBUG`: Detailed information for troubleshooting (when enabled) |
0 commit comments