-
Notifications
You must be signed in to change notification settings - Fork 81
/
initserver.go
101 lines (88 loc) · 2.72 KB
/
initserver.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
/*
Copyright The Guard Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"crypto/x509"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"go.kubeguard.dev/guard/auth"
"github.com/spf13/cobra"
"gomodules.xyz/blobfs"
"gomodules.xyz/cert"
"gomodules.xyz/cert/certstore"
"gomodules.xyz/x/term"
"k8s.io/klog/v2"
)
func NewCmdInitServer() *cobra.Command {
var (
rootDir = auth.DefaultDataDir
sans = cert.AltNames{
IPs: []net.IP{net.ParseIP("127.0.0.1")},
}
)
cmd := &cobra.Command{
Use: "server",
Short: "Generate server certificate pair",
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
cfg := cert.Config{
AltNames: cert.AltNames{
DNSNames: merge("server", sans.DNSNames),
IPs: sans.IPs,
},
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
store, err := certstore.New(blobfs.NewOsFs(), filepath.Join(rootDir, "pki"), cfg.Organization...)
if err != nil {
klog.Fatalf("Failed to create certificate store. Reason: %v.", err)
}
if store.IsExists(filename(cfg)) {
if !term.Ask(fmt.Sprintf("Server certificate found at %s. Do you want to overwrite?", store.Location()), false) {
os.Exit(1)
}
}
if err = store.LoadCA(); err != nil {
klog.Fatalf("Failed to load ca certificate. Reason: %v.", err)
}
crt, key, err := store.NewServerCertPairBytes(cfg.AltNames)
if err != nil {
klog.Fatalf("Failed to generate certificate pair. Reason: %v.", err)
}
err = store.WriteBytes(filename(cfg), crt, key)
if err != nil {
klog.Fatalf("Failed to init server certificate pair. Reason: %v.", err)
}
term.Successln("Wrote server certificates in ", store.Location())
},
}
cmd.Flags().StringVar(&rootDir, "pki-dir", rootDir, "Path to directory where pki files are stored.")
cmd.Flags().IPSliceVar(&sans.IPs, "ips", sans.IPs, "Alternative IP addresses")
cmd.Flags().StringSliceVar(&sans.DNSNames, "domains", sans.DNSNames, "Alternative Domain names")
return cmd
}
func merge(cn string, sans []string) []string {
var found bool
for _, name := range sans {
if strings.EqualFold(name, cn) {
found = true
break
}
}
if found {
return sans
}
return append([]string{cn}, sans...)
}