Skip to content

Commit 1638094

Browse files
authored
Merge pull request #429 from databacker/encrypt-option
add support to encrypt output files
2 parents dd82814 + ce262c4 commit 1638094

File tree

15 files changed

+1086
-10
lines changed

15 files changed

+1086
-10
lines changed

cmd/dump.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cmd
22

33
import (
44
"context"
5+
"encoding/base64"
56
"fmt"
7+
"os"
68
"strings"
79

810
"github.com/google/uuid"
@@ -13,6 +15,7 @@ import (
1315
"github.com/databacker/api/go/api"
1416
"github.com/databacker/mysql-backup/pkg/compression"
1517
"github.com/databacker/mysql-backup/pkg/core"
18+
"github.com/databacker/mysql-backup/pkg/encrypt"
1619
"github.com/databacker/mysql-backup/pkg/storage"
1720
"github.com/databacker/mysql-backup/pkg/util"
1821
)
@@ -132,6 +135,66 @@ func dumpCmd(passedExecs execs, cmdConfig *cmdConfiguration) (*cobra.Command, er
132135
}
133136
}
134137

138+
// encryption algorithm: check config, then CLI/env var overrides
139+
var (
140+
encryptionAlgo string
141+
encryptionKey []byte
142+
encryptor encrypt.Encryptor
143+
)
144+
if cmdConfig.configuration != nil && dumpConfig != nil && dumpConfig.Encryption != nil {
145+
if dumpConfig.Encryption.Algorithm == nil {
146+
return fmt.Errorf("encryption algorithm must be set in config file")
147+
}
148+
encryptionAlgo = string(*dumpConfig.Encryption.Algorithm)
149+
switch {
150+
case dumpConfig.Encryption.Key != nil && *dumpConfig.Encryption.Key != "" && dumpConfig.Encryption.KeyPath != nil && *dumpConfig.Encryption.KeyPath != "":
151+
return fmt.Errorf("encryption key and path cannot both be set in config file")
152+
case dumpConfig.Encryption.Key != nil && *dumpConfig.Encryption.Key == "" && dumpConfig.Encryption.KeyPath != nil && *dumpConfig.Encryption.KeyPath == "":
153+
return fmt.Errorf("must set at least one of encryption key or path in config file")
154+
case dumpConfig.Encryption.Key != nil && *dumpConfig.Encryption.Key != "":
155+
encryptionKey, err = base64.StdEncoding.DecodeString(*dumpConfig.Encryption.Key)
156+
if err != nil {
157+
return fmt.Errorf("error decoding encryption key from config file: %v", err)
158+
}
159+
case dumpConfig.Encryption.KeyPath != nil && *dumpConfig.Encryption.KeyPath != "":
160+
key, err := os.ReadFile(*dumpConfig.Encryption.KeyPath)
161+
if err != nil {
162+
return fmt.Errorf("error reading encryption key from path: %v", err)
163+
}
164+
encryptionKey = key
165+
}
166+
}
167+
encryptionVar := v.GetString("encryption")
168+
if encryptionVar != "" {
169+
encryptionAlgo = encryptionVar
170+
}
171+
if encryptionAlgo != "" {
172+
keyContent := v.GetString("encryption-key")
173+
keyPath := v.GetString("encryption-key-path")
174+
switch {
175+
case keyContent != "" && keyPath != "":
176+
return fmt.Errorf("encryption key and path cannot both be set in CLI")
177+
case keyContent == "" && keyPath == "":
178+
return fmt.Errorf("must set at least one of encryption key or path in CLI")
179+
case keyContent != "":
180+
encryptionKey, err = base64.StdEncoding.DecodeString(keyContent)
181+
if err != nil {
182+
return fmt.Errorf("error decoding encryption key from CLI flag: %v", err)
183+
}
184+
case keyPath != "":
185+
key, err := os.ReadFile(keyPath)
186+
if err != nil {
187+
return fmt.Errorf("error reading encryption key from path: %v", err)
188+
}
189+
encryptionKey = key
190+
}
191+
192+
encryptor, err = encrypt.GetEncryptor(encryptionAlgo, encryptionKey)
193+
if err != nil {
194+
return fmt.Errorf("failure to get encryptor '%s': %v", encryptionAlgo, err)
195+
}
196+
}
197+
135198
// retention, if enabled
136199
retention := v.GetString("retention")
137200
if retention == "" && cmdConfig.configuration != nil && cmdConfig.configuration.Prune != nil && cmdConfig.configuration.Prune.Retention != nil {
@@ -173,6 +236,7 @@ func dumpCmd(passedExecs execs, cmdConfig *cmdConfiguration) (*cobra.Command, er
173236
DBNames: include,
174237
DBConn: cmdConfig.dbconn,
175238
Compressor: compressor,
239+
Encryptor: encryptor,
176240
Exclude: exclude,
177241
PreBackupScripts: preBackupScripts,
178242
PostBackupScripts: postBackupScripts,
@@ -262,6 +326,10 @@ S3: If it is a URL of the format s3://bucketname/path then it will connect via S
262326
// retention
263327
flags.String("retention", "", "Retention period for backups. Optional. If not specified, no pruning will be done. Can be number of backups or time-based. For time-based, the format is: 1d, 1w, 1m, 1y for days, weeks, months, years, respectively. For number-based, the format is: 1c, 2c, 3c, etc. for the count of backups to keep.")
264328

329+
// encryption options
330+
flags.String("encryption", "", fmt.Sprintf("Encryption algorithm to use, none if blank. Supported are: %s. Format must match the specific algorithm.", strings.Join(encrypt.All, ", ")))
331+
flags.String("encryption-key", "", "Encryption key to use, base64-encoded. Useful for debugging, not recommended for production. If encryption is enabled, and both are provided or neither is provided, returns an error.")
332+
flags.String("encryption-key-path", "", "Path to encryption key file. If encryption is enabled, and both are provided or neither is provided, returns an error.")
265333
return cmd, nil
266334
}
267335

go.mod

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ require (
3131
)
3232

3333
require (
34-
github.com/databacker/api/go/api v0.0.0-20241202154620-01b0380f21cb
34+
github.com/databacker/api/go/api v0.0.0-20250418100420-12e1adda1303
3535
github.com/google/go-cmp v0.6.0
3636
go.opentelemetry.io/otel v1.31.0
3737
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0
@@ -41,10 +41,13 @@ require (
4141
)
4242

4343
require (
44+
filippo.io/age v1.2.1 // indirect
45+
github.com/InfiniteLoopSpace/go_S-MIME v0.0.0-20181221134359-3f58f9a4b2b6 // indirect
4446
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
4547
github.com/containerd/log v0.1.0 // indirect
4648
github.com/distribution/reference v0.6.0 // indirect
4749
github.com/felixge/httpsnoop v1.0.3 // indirect
50+
github.com/github/smimesign v0.2.0 // indirect
4851
github.com/go-logr/logr v1.4.2 // indirect
4952
github.com/go-logr/stdr v1.2.2 // indirect
5053
github.com/golang/protobuf v1.5.4 // indirect
@@ -107,11 +110,11 @@ require (
107110
github.com/spf13/jwalterweatherman v1.0.0 // indirect
108111
github.com/stretchr/objx v0.5.2 // indirect
109112
github.com/subosito/gotenv v1.2.0 // indirect
110-
golang.org/x/crypto v0.36.0
113+
golang.org/x/crypto v0.37.0
111114
golang.org/x/net v0.38.0 // indirect
112-
golang.org/x/sys v0.31.0 // indirect
113-
golang.org/x/text v0.23.0 // indirect
114-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
115+
golang.org/x/sys v0.32.0 // indirect
116+
golang.org/x/text v0.24.0 // indirect
117+
golang.org/x/tools v0.22.0 // indirect
115118
gopkg.in/ini.v1 v1.51.0 // indirect
116119
gopkg.in/yaml.v2 v2.4.0 // indirect
117120
gotest.tools/v3 v3.4.0 // indirect

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
filippo.io/age v1.2.1 h1:X0TZjehAZylOIj4DubWYU1vWQxv9bJpo+Uu2/LGhi1o=
3+
filippo.io/age v1.2.1/go.mod h1:JL9ew2lTN+Pyft4RiNGguFfOpewKwSHm5ayKD/A4004=
24
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
35
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
46
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
57
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
68
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
9+
github.com/InfiniteLoopSpace/go_S-MIME v0.0.0-20181221134359-3f58f9a4b2b6 h1:TkEaE2dfSBN9onWsQ1pC9EVMmVDJqkYWNUwS6+EYxlM=
10+
github.com/InfiniteLoopSpace/go_S-MIME v0.0.0-20181221134359-3f58f9a4b2b6/go.mod h1:yhh4MGRGdTpTET5RhSJx4XNCEkJljP3k8MxTTB3joQA=
711
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
812
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
913
github.com/Microsoft/hcsshim v0.11.7 h1:vl/nj3Bar/CvJSYo7gIQPyRWc9f3c6IeSNavBTSZNZQ=
@@ -56,6 +60,7 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
5660
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
5761
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
5862
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
63+
github.com/certifi/gocertifi v0.0.0-20180118203423-deb3ae2ef261/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
5964
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
6065
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
6166
github.com/cloudsoda/go-smb2 v0.0.0-20231106205947-b0758ecc4c67 h1:KzZU0EMkUm4vX/jPp5d/VttocDpocL/8QP0zyiI9Xiw=
@@ -72,6 +77,10 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
7277
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
7378
github.com/databacker/api/go/api v0.0.0-20241202154620-01b0380f21cb h1:9PthuA+o1wBZuTkNc2LLXQfI5+Myy+ok8nD3bQzd7DA=
7479
github.com/databacker/api/go/api v0.0.0-20241202154620-01b0380f21cb/go.mod h1:bQhbl71Lk1ATni0H+u249hjoQ8ShAdVNcNjnw6z+SbE=
80+
github.com/databacker/api/go/api v0.0.0-20250418091750-e67e3226ca5f h1:vuPsDEgli1S6khpEwY721epJnZiFtPSPHuxyMz9SJUY=
81+
github.com/databacker/api/go/api v0.0.0-20250418091750-e67e3226ca5f/go.mod h1:bQhbl71Lk1ATni0H+u249hjoQ8ShAdVNcNjnw6z+SbE=
82+
github.com/databacker/api/go/api v0.0.0-20250418100420-12e1adda1303 h1:TVLyJzdvDvWIEs1/v6G0rQPpZeUsArQ7skzicjfCV8I=
83+
github.com/databacker/api/go/api v0.0.0-20250418100420-12e1adda1303/go.mod h1:bQhbl71Lk1ATni0H+u249hjoQ8ShAdVNcNjnw6z+SbE=
7584
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7685
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7786
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -96,6 +105,8 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
96105
github.com/geoffgarside/ber v1.1.0 h1:qTmFG4jJbwiSzSXoNJeHcOprVzZ8Ulde2Rrrifu5U9w=
97106
github.com/geoffgarside/ber v1.1.0/go.mod h1:jVPKeCbj6MvQZhwLYsGwaGI52oUorHoHKNecGT85ZCc=
98107
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
108+
github.com/github/smimesign v0.2.0 h1:Hho4YcX5N1I9XNqhq0fNx0Sts8MhLonHd+HRXVGNjvk=
109+
github.com/github/smimesign v0.2.0/go.mod h1:iZiiwNT4HbtGRVqCQu7uJPEZCuEE5sfSSttcnePkDl4=
99110
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
100111
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
101112
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
@@ -198,10 +209,12 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
198209
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
199210
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
200211
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
212+
github.com/pborman/getopt v0.0.0-20180811024354-2b5b3bfb099b/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
201213
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
202214
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
203215
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
204216
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
217+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
205218
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
206219
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
207220
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -293,11 +306,14 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/
293306
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
294307
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
295308
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
309+
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
296310
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
297311
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
298312
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
299313
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
300314
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
315+
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
316+
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
301317
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
302318
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
303319
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
@@ -350,6 +366,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
350366
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
351367
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
352368
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
369+
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
370+
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
353371
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
354372
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
355373
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@@ -363,6 +381,8 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
363381
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
364382
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
365383
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
384+
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
385+
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
366386
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
367387
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 h1:vVKdlvoWBphwdxWKrFZEuM0kGgGLxUOYcY4U/2Vjg44=
368388
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -381,6 +401,7 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
381401
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
382402
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
383403
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
404+
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
384405
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
385406
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
386407
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/config/process.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ func decryptConfig(spec api.EncryptedSpec, credentials []string) (api.Config, er
181181
hkdfReader := hkdf.New(sha256.New, sharedSecret[:], nil, []byte(api.SymmetricKey))
182182
var symmetricKeySize int
183183
switch *spec.Algorithm {
184-
case api.AesGcm256:
184+
case api.EncryptedSpecAlgorithmAes256Gcm:
185185
symmetricKeySize = 32
186-
case api.Chacha20Poly1305:
186+
case api.EncryptedSpecAlgorithmChacha20Poly1305:
187187
symmetricKeySize = 32
188188
default:
189189
return plainConfig, fmt.Errorf("unsupported algorithm: %s", *spec.Algorithm)
@@ -202,7 +202,7 @@ func decryptConfig(spec api.EncryptedSpec, credentials []string) (api.Config, er
202202
return plainConfig, fmt.Errorf("failed to decode encrypted data: %w", err)
203203
}
204204
switch *spec.Algorithm {
205-
case api.AesGcm256:
205+
case api.EncryptedSpecAlgorithmAes256Gcm:
206206
// Decrypt with AES-GCM
207207
block, err := aes.NewCipher(symmetricKey)
208208
if err != nil {
@@ -212,7 +212,7 @@ func decryptConfig(spec api.EncryptedSpec, credentials []string) (api.Config, er
212212
if err != nil {
213213
return plainConfig, fmt.Errorf("failed to initialize AES-GCM: %w", err)
214214
}
215-
case api.Chacha20Poly1305:
215+
case api.EncryptedSpecAlgorithmChacha20Poly1305:
216216
// Decrypt with ChaCha20Poly1305
217217
aead, err = chacha20poly1305.New(symmetricKey)
218218
if err != nil {

pkg/config/process_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func TestDecryptConfig(t *testing.T) {
169169
// Embed the nonce in the ciphertext
170170
fullCiphertext := append(nonce, ciphertext...)
171171

172-
algo := api.AesGcm256
172+
algo := api.EncryptedSpecAlgorithmAes256Gcm
173173
data := base64.StdEncoding.EncodeToString(fullCiphertext)
174174

175175
// this is a valid spec, we want to be able to change fields

pkg/core/dump.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (e *Executor) Dump(ctx context.Context, opts DumpOptions) (DumpResults, err
3434
dbnames := opts.DBNames
3535
dbconn := opts.DBConn
3636
compressor := opts.Compressor
37+
encryptor := opts.Encryptor
3738
compact := opts.Compact
3839
suppressUseDatabase := opts.SuppressUseDatabase
3940
maxAllowedPacket := opts.MaxAllowedPacket
@@ -132,6 +133,14 @@ func (e *Executor) Dump(ctx context.Context, opts DumpOptions) (DumpResults, err
132133
tarSpan.End()
133134
return results, fmt.Errorf("failed to create compressor: %v", err)
134135
}
136+
if encryptor != nil {
137+
cw, err = encryptor.Encrypt(cw)
138+
if err != nil {
139+
tarSpan.SetStatus(codes.Error, err.Error())
140+
tarSpan.End()
141+
return results, fmt.Errorf("failed to create encryptor: %v", err)
142+
}
143+
}
135144
if err := archive.Tar(workdir, cw); err != nil {
136145
tarSpan.SetStatus(codes.Error, err.Error())
137146
tarSpan.End()

pkg/core/dumpoptions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"github.com/databacker/mysql-backup/pkg/compression"
55
"github.com/databacker/mysql-backup/pkg/database"
6+
"github.com/databacker/mysql-backup/pkg/encrypt"
67
"github.com/databacker/mysql-backup/pkg/storage"
78
"github.com/google/uuid"
89
)
@@ -13,6 +14,7 @@ type DumpOptions struct {
1314
DBNames []string
1415
DBConn database.Connection
1516
Compressor compression.Compressor
17+
Encryptor encrypt.Encryptor
1618
Exclude []string
1719
PreBackupScripts string
1820
PostBackupScripts string

0 commit comments

Comments
 (0)