forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certs.go
155 lines (125 loc) · 3.04 KB
/
certs.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package rkecerts
import (
"bytes"
"crypto/rsa"
"crypto/x509"
"io/ioutil"
"os"
"path/filepath"
"context"
"github.com/rancher/kontainer-engine/drivers/rke/rkecerts"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/librke"
"github.com/rancher/rke/pki"
"github.com/rancher/types/apis/management.cattle.io/v3"
"k8s.io/client-go/util/cert"
)
const (
bundleFile = "./management-state/certs/bundle.json"
)
type Bundle struct {
certs map[string]pki.CertificatePKI
}
func newBundle(certs map[string]pki.CertificatePKI) *Bundle {
return &Bundle{
certs: certs,
}
}
func Unmarshal(input string) (*Bundle, error) {
certs, err := rkecerts.LoadString(input)
return newBundle(certs), err
}
func (b *Bundle) Certs() map[string]pki.CertificatePKI {
return b.certs
}
func LoadLocal() (*Bundle, error) {
f, err := os.Open(bundleFile)
if err != nil {
return nil, err
}
defer f.Close()
certMap, err := rkecerts.Load(f)
if err != nil {
return nil, err
}
return newBundle(certMap), nil
}
func Generate(config *v3.RancherKubernetesEngineConfig) (*Bundle, error) {
certs, err := librke.New().GenerateCerts(config)
if err != nil {
return nil, err
}
return &Bundle{
certs: certs,
}, nil
}
func (b *Bundle) Marshal() (string, error) {
output := &bytes.Buffer{}
err := rkecerts.Save(b.certs, output)
return output.String(), err
}
func (b *Bundle) ForNode(config *v3.RancherKubernetesEngineConfig, nodeAddress string) *Bundle {
certs := librke.New().GenerateRKENodeCerts(context.Background(), *config, nodeAddress, b.certs)
return &Bundle{
certs: certs,
}
}
func (b *Bundle) SaveLocal() error {
bundlePath := filepath.Dir(bundleFile)
if err := os.MkdirAll(bundlePath, 0700); err != nil {
return err
}
f, err := ioutil.TempFile(bundlePath, "bundle-")
if err != nil {
return err
}
defer f.Close()
defer os.Remove(f.Name())
if err := rkecerts.Save(b.certs, f); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(f.Name(), bundleFile)
}
func (b *Bundle) KubeConfig() string {
return b.certs["kube-admin"].ConfigPath
}
func (b *Bundle) Explode() error {
f := &fileWriter{}
for _, item := range b.certs {
f.write(item.Path, nil, item.Certificate, nil)
f.write(item.ConfigPath, []byte(item.Config), nil, nil)
f.write(item.KeyPath, nil, nil, item.Key)
}
return f.err()
}
type fileWriter struct {
errs []error
}
func (f *fileWriter) write(path string, content []byte, x509cert *x509.Certificate, key *rsa.PrivateKey) {
if x509cert != nil {
content = cert.EncodeCertPEM(x509cert)
}
if key != nil {
content = cert.EncodePrivateKeyPEM(key)
}
if path == "" || len(content) == 0 {
return
}
existing, err := ioutil.ReadFile(path)
if err == nil && bytes.Equal(existing, content) {
return
}
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0700); err != nil {
f.errs = append(f.errs, err)
}
if err := ioutil.WriteFile(path, content, 0600); err != nil {
f.errs = append(f.errs, err)
}
}
func (f *fileWriter) err() error {
return types.NewErrors(f.errs...)
}