forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
73 lines (58 loc) · 1.27 KB
/
tls.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
package app
import (
"fmt"
"io/ioutil"
"github.com/rancher/types/apis/management.cattle.io/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func ReadTLSConfig(config *Config) error {
var err error
if config.ListenConfig != nil {
return nil
}
lc := &v3.ListenConfig{
TypeMeta: metav1.TypeMeta{
Kind: "ListenConfig",
APIVersion: "management.cattle.io/v3",
},
ObjectMeta: metav1.ObjectMeta{
Name: "cli-config",
},
Enabled: true,
}
lc.CACerts, err = readPEM("/etc/rancher/ssl/cacerts.pem")
if err != nil {
return err
}
lc.Key, err = readPEM("/etc/rancher/ssl/key.pem")
if err != nil {
return err
}
lc.Cert, err = readPEM("/etc/rancher/ssl/cert.pem")
if err != nil {
return err
}
lc.Mode = "https"
if len(config.ACMEDomains) > 0 {
lc.Mode = "acme"
lc.Domains = config.ACMEDomains
}
valid := false
if lc.Key != "" && lc.Cert != "" {
valid = true
} else if lc.Key == "" && lc.Cert == "" {
valid = true
}
if !valid {
return fmt.Errorf("invalid SSL configuration found, please set cert/key, cert/key/cacerts, cacerts only, or none")
}
config.ListenConfig = lc
return nil
}
func readPEM(path string) (string, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return "", nil
}
return string(content), nil
}