forked from signal18/replication-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
67 lines (59 loc) · 1.78 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
//go:build server
// +build server
// replication-manager - Replication Manager Monitoring and CLI for MariaDB and MySQL
// Copyright 2017-2021 SIGNAL18 CLOUD SAS
// Authors: Guillaume Lefranc <[email protected]>
// Stephane Varoqui <[email protected]>
// This source code is licensed under the GNU General Public License, version 3.
package main
import (
"fmt"
"log"
"strings"
"github.com/signal18/replication-manager/utils/crypto"
"github.com/spf13/cobra"
)
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 = crypto.WriteKey(p.Key, keyPath, overwrite)
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 = crypto.ReadKey(keyPath)
if err != nil {
log.Fatalln(err)
}
p.PlainText = strings.Join(args, " ")
p.Encrypt()
fmt.Println("Encrypted password hash:", p.CipherText)
},
}