forked from mothership/rds-auth-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_certs.go
61 lines (52 loc) · 1.6 KB
/
gen_certs.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
package cmd
import (
"fmt"
"github.com/JacobJohansen/rds-auth-proxy/pkg/cert"
"github.com/JacobJohansen/rds-auth-proxy/pkg/file"
"github.com/spf13/cobra"
)
var genCertsCommand = &cobra.Command{
Use: "gen-cert",
Short: "Generates a self-signed certificate",
Long: `Generates a self-signed certificate`,
RunE: func(cmd *cobra.Command, args []string) error {
keyPath, err := cmd.Flags().GetString("key")
if err != nil {
return err
}
if keyPath == "" {
return fmt.Errorf("Key path must not be empty")
}
certPath, err := cmd.Flags().GetString("certificate")
if err != nil {
return err
}
if certPath == "" {
return fmt.Errorf("Certificate path must not be empty")
}
if file.Exists(certPath) || file.Exists(keyPath) {
return fmt.Errorf("certificate/key already exists at this location")
}
hosts, err := cmd.Flags().GetString("hosts")
if err != nil {
return err
}
certBytes, keyBytes, err := cert.GenerateSelfSignedCert(hosts, false)
if err != nil {
return err
}
err = cert.Save(certPath, certBytes)
if err != nil {
return err
}
return cert.Save(keyPath, keyBytes)
},
}
func init() {
rootCmd.AddCommand(genCertsCommand)
genCertsCommand.PersistentFlags().String("certificate", "", "Path to generate the certificate")
_ = genCertsCommand.MarkPersistentFlagRequired("certificate")
genCertsCommand.PersistentFlags().String("key", "", "Path to generate the private key")
_ = genCertsCommand.MarkPersistentFlagRequired("key")
genCertsCommand.PersistentFlags().String("hosts", "rds-auth-proxy", "Comma separated list of hosts to add to the certificate")
}