forked from signal18/replication-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.go
96 lines (85 loc) · 2.28 KB
/
password.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// replication-manager - Replication Manager Monitoring and CLI for MariaDB and MySQL
// Authors: Guillaume Lefranc <guillaume@signal18.io>
// Stephane Varoqui <stephane@mariadb.com>
// This source code is licensed under the GNU General Public License, version 3.
package main
import (
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/tanji/replication-manager/crypto"
)
var (
keyPath string
overwrite bool
)
func init() {
rootCmd.AddCommand(keygenCmd)
rootCmd.AddCommand(passwordCmd)
keygenCmd.Flags().StringVar(&keyPath, "keypath", "/etc/replication-manager/.replication-manager.key", "Encryption key file path")
keygenCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite the previous key")
}
var keygenCmd = &cobra.Command{
Use: "keygen",
Short: "Generate a new encryption key",
Long: `Generates a new AES128 encryption key that can be used for encrypting cleartext passwords on
the CLI or in the replication-manager config file`,
Run: func(cmd *cobra.Command, args []string) {
p := crypto.Password{}
var err error
p.Key, err = crypto.Keygen()
if err != nil {
log.Fatalln(err)
}
err = writeKey(p.Key)
if err != nil {
log.Fatalln(err)
}
},
}
var passwordCmd = &cobra.Command{
Use: "password",
Short: "Encrypt a clear text password",
Long: "Encrypt a clear text password using the AES encryption key generated with the keygen command",
Run: func(cmd *cobra.Command, args []string) {
p := crypto.Password{}
var err error
p.Key, err = readKey()
if err != nil {
log.Fatalln(err)
}
p.PlainText = strings.Join(args, " ")
p.Encrypt()
fmt.Println("Encrypted password hash:", p.CipherText)
},
}
func writeKey(key []byte) error {
if _, err := os.Stat(keyPath); err == nil {
if !overwrite {
return errors.New("Key file already exists")
}
}
flag := os.O_WRONLY | os.O_CREATE
file, err := os.OpenFile(keyPath, flag, 0600)
if err != nil {
return err
}
_, err = file.Write(key)
return err
}
func readKey() ([]byte, error) {
flag := os.O_RDONLY
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
return nil, errors.New("Key file does not exist")
}
file, err := os.OpenFile(keyPath, flag, 0600)
if err != nil {
return nil, err
}
key := make([]byte, 16)
_, err = file.Read(key)
return key, err
}