-
Notifications
You must be signed in to change notification settings - Fork 15
/
ca_set.go
67 lines (55 loc) · 1.26 KB
/
ca_set.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
package cmd
import (
"context"
"crypto/x509"
"encoding/pem"
"errors"
"os"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
)
var caData []byte
// caSetCmd represents the "ca set" command
var caSetCmd = &cobra.Command{
Use: "set NAME FILE",
Short: "store CA certificate in etcd",
Long: `Load PEM encoded x509 CA certificate from FILE, and stores it in etcd.
NAME is one of:
server
etcd-peer
etcd-client
kubernetes
In fact, these CA should be created in Vault.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("wrong number of arguments")
}
if !isValidCAName(args[0]) {
return errors.New("wrong CA name: " + args[0])
}
var err error
caData, err = os.ReadFile(args[1])
if err != nil {
return err
}
block, _ := pem.Decode(caData)
if block == nil {
return errors.New("invalid PEM data")
}
_, err = x509.ParseCertificate(block.Bytes)
if err != nil {
return errors.New("invalid certificate")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
well.Go(func(ctx context.Context) error {
return storage.PutCACertificate(ctx, args[0], string(caData))
})
well.Stop()
return well.Wait()
},
}
func init() {
caCmd.AddCommand(caSetCmd)
}