-
Notifications
You must be signed in to change notification settings - Fork 157
/
keyset.go
160 lines (124 loc) · 3.45 KB
/
keyset.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cli
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/hatchet-dev/hatchet/pkg/encryption"
)
var (
encryptionKeyDir string
cloudKMSCredentialsPath string
cloudKMSKeyURI string
)
var keysetCmd = &cobra.Command{
Use: "keyset",
Short: "command for managing keysets.",
}
var keysetCreateLocalKeysetsCmd = &cobra.Command{
Use: "create-local-keys",
Short: "create a new local master keyset and JWT public/private keyset.",
Run: func(cmd *cobra.Command, args []string) {
err := runCreateLocalKeysets()
if err != nil {
log.Printf("Fatal: could not run [keyset create-local-keys] command: %v", err)
os.Exit(1)
}
},
}
var keysetCreateCloudKMSJWTCmd = &cobra.Command{
Use: "create-cloudkms-jwt",
Short: "create a new JWT keyset encrypted by a remote CloudKMS repository.",
Run: func(cmd *cobra.Command, args []string) {
err := runCreateCloudKMSJWTKeyset()
if err != nil {
log.Printf("Fatal: could not run [keyset create-cloudkms-jwt] command: %v", err)
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(keysetCmd)
keysetCmd.AddCommand(keysetCreateLocalKeysetsCmd)
keysetCmd.AddCommand(keysetCreateCloudKMSJWTCmd)
keysetCmd.PersistentFlags().StringVar(
&encryptionKeyDir,
"key-dir",
"",
"if storing keys on disk, path to the directory where encryption keys should be stored",
)
keysetCreateCloudKMSJWTCmd.PersistentFlags().StringVar(
&cloudKMSCredentialsPath,
"credentials",
"",
"path to the JSON credentials file for the CloudKMS repository",
)
keysetCreateCloudKMSJWTCmd.PersistentFlags().StringVar(
&cloudKMSKeyURI,
"key-uri",
"",
"URI of the key in the CloudKMS repository",
)
}
func runCreateLocalKeysets() error {
masterKeyBytes, privateEc256, publicEc256, err := encryption.GenerateLocalKeys()
if err != nil {
return err
}
if encryptionKeyDir != "" {
// we write these as .key files so that they're gitignored by default
err = os.WriteFile(encryptionKeyDir+"/master.key", masterKeyBytes, 0600)
if err != nil {
return err
}
err = os.WriteFile(encryptionKeyDir+"/private_ec256.key", privateEc256, 0600)
if err != nil {
return err
}
err = os.WriteFile(encryptionKeyDir+"/public_ec256.key", publicEc256, 0600)
if err != nil {
return err
}
} else {
fmt.Println("Master Key Bytes:")
fmt.Println(string(masterKeyBytes))
fmt.Println("Private EC256 Keyset:")
fmt.Println(string(privateEc256))
fmt.Println("Public EC256 Keyset:")
fmt.Println(string(publicEc256))
}
return nil
}
func runCreateCloudKMSJWTKeyset() error {
if cloudKMSCredentialsPath == "" {
return fmt.Errorf("missing required flag --credentials")
}
if cloudKMSKeyURI == "" {
return fmt.Errorf("missing required flag --key-uri")
}
credentials, err := os.ReadFile(cloudKMSCredentialsPath)
if err != nil {
return err
}
privateEc256, publicEc256, err := encryption.GenerateJWTKeysetsFromCloudKMS(cloudKMSKeyURI, credentials)
if err != nil {
return err
}
if encryptionKeyDir != "" {
// we write these as .key files so that they're gitignored by default
err = os.WriteFile(encryptionKeyDir+"/private_ec256.key", privateEc256, 0600)
if err != nil {
return err
}
err = os.WriteFile(encryptionKeyDir+"/public_ec256.key", publicEc256, 0600)
if err != nil {
return err
}
} else {
fmt.Println("Private EC256 Keyset:")
fmt.Println(string(privateEc256))
fmt.Println("Public EC256 Keyset:")
fmt.Println(string(publicEc256))
}
return nil
}