-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
91 lines (86 loc) · 2.75 KB
/
update.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
package webhook
import (
"strings"
"github.com/aescanero/micropki/pki"
"github.com/aescanero/micropki/vars"
"github.com/aescanero/openldap-controller/utils"
"github.com/spf13/cobra"
)
var (
caname string
certname string
namespace string
client bool
caNamespace string
fqdns string
commonname string
webhook string
)
func init() {
UpdateCmd.Flags().StringVarP(&caname, "caname", "", "", "Name of the secret where the CA is saved (Default: micropki-ca)")
UpdateCmd.Flags().StringVarP(&certname, "certname", "", "", "Name of the secret where the CERT is saved (Default: micropki-cert)")
UpdateCmd.Flags().StringVarP(&namespace, "certnamespace", "", "", "Name of the namespace where the secret of the CERT is saved (Default: where is running micropki)")
UpdateCmd.Flags().BoolVarP(&client, "client", "", false, "The cert is for a server or a cliente (default: server)")
UpdateCmd.Flags().StringVarP(&caNamespace, "canamespace", "", "", "Name of the namespace where the secret of the CA is saved (Default: where is running micropki)")
UpdateCmd.Flags().StringVarP(&fqdns, "hosts", "", "", "FQDN Host list separated by ','")
UpdateCmd.Flags().StringVarP(&webhook, "webhook", "", "", "Name of the webhook")
UpdateCmd.Flags().StringVarP(&commonname, "commonname", "", "", "Common Name of the CERT','")
}
var UpdateCmd = &cobra.Command{
Use: "update",
Short: "Prepare all the pki stuff",
Long: `Prepare all the pki stuff`,
Run: func(cmd *cobra.Command, args []string) {
myca := new(pki.CA)
myca.SetupCA()
if caname == "" {
caname = utils.GetEnv("CA_SECRET_NAME", "micropki-ca")
}
if certname == "" {
certname = utils.GetEnv("CERT_SECRET_NAME", "micropki-cert")
}
caNamespace, err := vars.ValidateNamespace(caNamespace)
if err != nil {
panic(err.Error())
}
namespace, err := vars.ValidateNamespace(namespace)
if err != nil {
panic(err.Error())
}
err = myca.NeedInitialization(caname, caNamespace)
if err != nil {
if err.Error() != "need update" {
panic(err.Error())
} else {
err = myca.NewCA()
if err != nil {
panic(err.Error())
}
err = myca.UpdateSecret(caname, caNamespace)
if err != nil {
panic(err.Error())
}
}
}
mycert := new(pki.CERT)
mycert.SetupCERT(client, strings.Split(fqdns, ","), commonname)
err = mycert.NeedInitialization(certname, namespace)
if err != nil {
if err.Error() != "need update" {
panic(err.Error())
} else {
err = mycert.NewCERT(caname, caNamespace)
if err != nil {
panic(err.Error())
}
err = mycert.UpdateSecret(certname, namespace)
if err != nil {
panic(err.Error())
}
err = mycert.UpdateValidatingWebhookConfiguration(webhook)
if err != nil {
panic(err.Error())
}
}
}
}}