Skip to content

Commit 33d589c

Browse files
authored
Merge pull request #349 from devtron-labs/rollback-encrypted-data
feat: rollback encrypted data
2 parents 4564399 + 20b19c5 commit 33d589c

957 files changed

Lines changed: 346285 additions & 50 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common-lib/go.mod

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/aws/aws-sdk-go-v2/service/s3 v1.79.4
1818
github.com/aws/smithy-go v1.22.3
1919
github.com/caarlos0/env v3.5.0+incompatible
20+
github.com/devtron-labs/kubelink v0.0.0-20241003100859-1859c74af205
2021
github.com/docker/docker v28.1.1+incompatible
2122
github.com/gammazero/workerpool v1.1.3
2223
github.com/go-pg/pg v6.15.1+incompatible
@@ -41,6 +42,7 @@ require (
4142
k8s.io/api v0.33.0
4243
k8s.io/apiextensions-apiserver v0.33.0
4344
k8s.io/apimachinery v0.33.0
45+
k8s.io/cli-runtime v0.33.0
4446
k8s.io/client-go v0.33.0
4547
k8s.io/kube-aggregator v0.33.0
4648
k8s.io/kubectl v0.33.0
@@ -72,6 +74,7 @@ require (
7274
github.com/aws/aws-sdk-go-v2/service/sso v1.25.3 // indirect
7375
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.30.1 // indirect
7476
github.com/aws/aws-sdk-go-v2/service/sts v1.33.19 // indirect
77+
github.com/blang/semver/v4 v4.0.0 // indirect
7578
github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443 // indirect
7679
github.com/containerd/errdefs v1.0.0 // indirect
7780
github.com/containerd/platforms v0.2.1 // indirect
@@ -80,22 +83,26 @@ require (
8083
github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect
8184
github.com/fsnotify/fsnotify v1.9.0 // indirect
8285
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
86+
github.com/go-errors/errors v1.4.2 // indirect
8387
github.com/go-jose/go-jose/v4 v4.1.0 // indirect
8488
github.com/go-playground/locales v0.14.1 // indirect
8589
github.com/go-playground/universal-translator v0.18.1 // indirect
8690
github.com/google/go-cmp v0.7.0 // indirect
91+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
8792
github.com/hashicorp/errwrap v1.1.0 // indirect
8893
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
8994
github.com/jinzhu/inflection v1.0.0 // indirect
9095
github.com/leodido/go-urn v1.4.0 // indirect
9196
github.com/moby/docker-image-spec v1.3.1 // indirect
9297
github.com/moby/sys/atomicwriter v0.1.0 // indirect
98+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
9399
github.com/morikuni/aec v1.0.0 // indirect
94-
github.com/onsi/ginkgo v1.16.5 // indirect
100+
github.com/nxadm/tail v1.4.8 // indirect
95101
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
96102
github.com/spiffe/go-spiffe/v2 v2.5.0 // indirect
97103
github.com/stretchr/objx v0.5.2 // indirect
98104
github.com/x448/float16 v0.8.4 // indirect
105+
github.com/xlab/treeprint v1.2.0 // indirect
99106
github.com/zeebo/errs v1.4.0 // indirect
100107
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
101108
go.opentelemetry.io/contrib/detectors/gcp v1.36.0 // indirect
@@ -107,9 +114,10 @@ require (
107114
go.opentelemetry.io/proto/otlp v1.6.0 // indirect
108115
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
109116
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
110-
gotest.tools/v3 v3.4.0 // indirect
111117
mellium.im/sasl v0.3.2 // indirect
112118
oras.land/oras-go/v2 v2.5.0 // indirect
119+
sigs.k8s.io/kustomize/api v0.19.0 // indirect
120+
sigs.k8s.io/kustomize/kyaml v0.19.0 // indirect
113121
sigs.k8s.io/randfill v1.0.0 // indirect
114122
)
115123

@@ -129,7 +137,7 @@ require (
129137
github.com/containerd/log v0.1.0 // indirect
130138
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
131139
github.com/docker/cli v28.1.1+incompatible
132-
github.com/docker/go-connections v0.4.0 // indirect
140+
github.com/docker/go-connections v0.5.0 // indirect
133141
github.com/docker/go-units v0.5.0 // indirect
134142
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
135143
github.com/felixge/httpsnoop v1.0.4 // indirect
@@ -177,7 +185,7 @@ require (
177185
google.golang.org/genproto v0.0.0-20250519155744-55703ea1f237 // indirect
178186
google.golang.org/genproto/googleapis/api v0.0.0-20250519155744-55703ea1f237 // indirect
179187
google.golang.org/genproto/googleapis/rpc v0.0.0-20250519155744-55703ea1f237 // indirect
180-
google.golang.org/grpc v1.72.2 // indirect
188+
google.golang.org/grpc v1.72.2
181189
gopkg.in/inf.v0 v0.9.1 // indirect
182190
gopkg.in/yaml.v3 v3.0.1 // indirect
183191
k8s.io/klog/v2 v2.130.1 // indirect

common-lib/go.sum

Lines changed: 24 additions & 46 deletions
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
FROM golang:1.24-alpine AS builder
2+
3+
# Install git for go mod dependencies
4+
RUN apk add --no-cache git
5+
6+
# Create non-root user for build
7+
RUN addgroup -g 1001 -S appgroup && \
8+
adduser -u 1001 -S appuser -G appgroup
9+
10+
# Set working directory
11+
WORKDIR /app
12+
13+
# Copy go mod files
14+
COPY go.mod go.sum ./
15+
16+
# Download dependencies
17+
RUN go mod download
18+
19+
# Copy source code
20+
COPY . .
21+
22+
# Build the application
23+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o rollback .
24+
25+
# Final stage
26+
FROM alpine:latest
27+
28+
# Install ca-certificates for HTTPS requests
29+
RUN apk --no-cache add ca-certificates
30+
31+
# Create non-root user
32+
RUN addgroup -g 1001 -S appgroup && \
33+
adduser -u 1001 -S appuser -G appgroup
34+
35+
# Set working directory
36+
WORKDIR /app
37+
38+
# Copy binary from builder stage
39+
COPY --from=builder /app/rollback .
40+
41+
# Change ownership to non-root user
42+
RUN chown -R appuser:appgroup /app
43+
44+
# Switch to non-root user
45+
USER appuser
46+
47+
# Expose any necessary ports (if needed)
48+
# EXPOSE 8080
49+
50+
# Set entrypoint
51+
ENTRYPOINT ["./rollback"]
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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)
14 MB
Binary file not shown.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module cluster-rollback
2+
3+
go 1.24.0
4+
5+
toolchain go1.24.4
6+
7+
require (
8+
github.com/caarlos0/env v3.5.0+incompatible
9+
github.com/devtron-labs/common-lib v0.0.0-00010101000000-000000000000
10+
github.com/go-pg/pg v6.15.1+incompatible
11+
github.com/sirupsen/logrus v1.9.3
12+
github.com/stretchr/testify v1.10.0
13+
)
14+
15+
require (
16+
github.com/beorn7/perks v1.0.1 // indirect
17+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
18+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
19+
github.com/docker/cli v28.1.1+incompatible // indirect
20+
github.com/google/wire v0.6.0 // indirect
21+
github.com/jinzhu/inflection v1.0.0 // indirect
22+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
23+
github.com/nxadm/tail v1.4.11 // indirect
24+
github.com/onsi/gomega v1.38.2 // indirect
25+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
26+
github.com/prometheus/client_golang v1.22.0 // indirect
27+
github.com/prometheus/client_model v0.6.2 // indirect
28+
github.com/prometheus/common v0.64.0 // indirect
29+
github.com/prometheus/procfs v0.16.1 // indirect
30+
go.uber.org/multierr v1.11.0 // indirect
31+
go.uber.org/zap v1.27.0 // indirect
32+
golang.org/x/crypto v0.38.0 // indirect
33+
golang.org/x/sys v0.35.0 // indirect
34+
google.golang.org/protobuf v1.36.7 // indirect
35+
gopkg.in/yaml.v3 v3.0.1 // indirect
36+
mellium.im/sasl v0.3.2 // indirect
37+
)
38+
39+
replace github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20251015063403-c79706370455

0 commit comments

Comments
 (0)