-
Notifications
You must be signed in to change notification settings - Fork 138
/
encrypt.go
65 lines (54 loc) · 1.64 KB
/
encrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"os"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var encCmd = &cobra.Command{
Use: "enc FILENAME",
Short: "Encrypt config file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Help()
return
}
if conf.ConfigEncPassword == "" {
logrus.Fatalf("[enc] configuration file encryption password cannot be empty")
}
plaintext, err := os.ReadFile(args[0])
if err != nil {
logrus.Fatalf("[enc] failed to read config file: %v", err)
}
ciphertext := Encrypt(plaintext, conf.ConfigEncPassword)
if err = os.WriteFile(args[0]+".enc", ciphertext, 0644); err != nil {
logrus.Fatalf("[enc] failed to write encrypted config file: %v", err)
}
logrus.Infof("[enc] encrypted file storage location %s", args[0]+".enc")
},
}
var decCmd = &cobra.Command{
Use: "dec FILENAME",
Short: "Decrypt config file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Help()
return
}
if conf.ConfigEncPassword == "" {
logrus.Fatalf("[dec] configuration file encryption password cannot be empty")
}
ciphertext, err := os.ReadFile(args[0])
if err != nil {
logrus.Fatalf("[dec] failed to read encrypted config file: %v", err)
}
plaintext, err := Decrypt(ciphertext, conf.ConfigEncPassword)
if err != nil {
logrus.Fatalf("[dec] failed to decrypt config file: %v", err)
}
if err = os.WriteFile(strings.TrimSuffix(args[0], ".enc"), plaintext, 0644); err != nil {
logrus.Fatalf("[dec] failde to write config file: %v", err)
}
logrus.Infof("[enc] decrypted file storage location %s", strings.TrimSuffix(args[0], ".enc"))
},
}