/
generate_key_pair.go
51 lines (45 loc) · 1.14 KB
/
generate_key_pair.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/encloud-tech/encloud/pkg/api"
"github.com/encloud-tech/encloud/pkg/types"
"github.com/spf13/cobra"
)
func GenerateKeyPairCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "keygen",
Short: "Generate new key pair",
Long: `Generate ECIES secp256k1 OR RSA 2048 key pair to encrypt & decrypt the AES-256 keys`,
Run: func(cmd *cobra.Command, args []string) {
cfg, err := api.Fetch()
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
keys, err := api.GenerateKeyPair(cfg.Stat.KekType)
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
response := types.GenerateKeyPairResponse{
Status: "success",
StatusCode: http.StatusCreated,
Message: "Keys generated successfully.",
Data: keys,
}
encoded, err := json.MarshalIndent(response, "", " ")
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), err.Error())
os.Exit(-1)
}
fmt.Fprintf(cmd.OutOrStdout(), string(encoded))
},
}
return cmd
}
func init() {
RootCmd.AddCommand(GenerateKeyPairCmd())
}