forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listenconfig_data.go
85 lines (72 loc) · 2.2 KB
/
listenconfig_data.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
package app
import (
"bytes"
"crypto/x509"
"encoding/pem"
"github.com/rancher/types/config"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/cert"
)
func addListenConfig(management *config.ManagementContext, cfg Config) error {
userCACerts := cfg.ListenConfig.CACerts
existing, err := management.Management.ListenConfigs("").Get(cfg.ListenConfig.Name, v1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if apierrors.IsNotFound(err) {
existing = nil
}
if existing != nil {
if cfg.ListenConfig.Cert == "" {
cfg.ListenConfig.Cert = existing.Cert
cfg.ListenConfig.CACerts = existing.CACerts
cfg.ListenConfig.Key = existing.Key
cfg.ListenConfig.CAKey = existing.CAKey
cfg.ListenConfig.CACert = existing.CACert
cfg.ListenConfig.KnownIPs = existing.KnownIPs
}
}
if (cfg.ListenConfig.Key == "" || cfg.ListenConfig.Cert == "") && cfg.ListenConfig.CACert == "" && cfg.ListenConfig.Mode != "acme" {
caKey, err := cert.NewPrivateKey()
if err != nil {
return err
}
caCert, err := cert.NewSelfSignedCACert(cert.Config{
CommonName: "cattle-ca",
Organization: []string{"the-ranch"},
}, caKey)
if err != nil {
return err
}
caCertBuffer := bytes.Buffer{}
if err := pem.Encode(&caCertBuffer, &pem.Block{
Type: cert.CertificateBlockType,
Bytes: caCert.Raw,
}); err != nil {
return err
}
caKeyBuffer := bytes.Buffer{}
if err := pem.Encode(&caKeyBuffer, &pem.Block{
Type: cert.RSAPrivateKeyBlockType,
Bytes: x509.MarshalPKCS1PrivateKey(caKey),
}); err != nil {
return err
}
cfg.ListenConfig.CACert = string(caCertBuffer.Bytes())
cfg.ListenConfig.CACerts = cfg.ListenConfig.CACert
cfg.ListenConfig.CAKey = string(caKeyBuffer.Bytes())
}
if cfg.NoCACerts || cfg.ListenConfig.Mode == "acme" {
cfg.ListenConfig.CACerts = ""
} else if userCACerts != "" {
cfg.ListenConfig.CACerts = userCACerts
}
if existing == nil {
_, err := management.Management.ListenConfigs("").Create(cfg.ListenConfig)
return err
}
cfg.ListenConfig.ResourceVersion = existing.ResourceVersion
_, err = management.Management.ListenConfigs("").Update(cfg.ListenConfig)
return err
}