-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
168 lines (148 loc) · 5.28 KB
/
secret.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
156
157
158
159
160
161
162
163
164
165
166
167
168
package utils
import (
"io/ioutil"
"os"
"reflect"
"strings"
"github.com/pkg/errors"
rv1 "github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
)
func UpdateSSLAuthentication(prefix string, esConfig *v3.ElasticsearchConfig, spConfig *v3.SplunkConfig, kfConfig *v3.KafkaConfig, syslogConfig *v3.SyslogConfig, fluentForwarder *v3.FluentForwarderConfig, secrets rv1.SecretInterface) error {
var certificate, clientCert, clientKey string
if esConfig != nil {
certificate = esConfig.Certificate
clientCert = esConfig.ClientCert
clientKey = esConfig.ClientKey
} else if spConfig != nil {
certificate = spConfig.Certificate
clientCert = spConfig.ClientCert
clientKey = spConfig.ClientKey
} else if kfConfig != nil {
certificate = kfConfig.Certificate
clientCert = kfConfig.ClientCert
clientKey = kfConfig.ClientKey
} else if syslogConfig != nil {
certificate = syslogConfig.Certificate
clientCert = syslogConfig.ClientCert
clientKey = syslogConfig.ClientKey
} else if fluentForwarder != nil {
certificate = fluentForwarder.Certificate
}
data := buildCertSecret(prefix, certificate, clientCert, clientKey)
return updateSecret(loggingconfig.SSLSecretName, data, secrets)
}
func UpdateConfig(configPath, name, level string, secrets rv1.SecretInterface) error {
data, err := buildConfigSecret(configPath, loggingconfig.LoggingNamespace, name, level)
if err != nil {
return err
}
return updateSecret(name, data, secrets)
}
func InitSecret(secrets rv1.SecretInterface) (err error) {
if _, err = secrets.Controller().Lister().Get(loggingconfig.LoggingNamespace, loggingconfig.SSLSecretName); err != nil && !apierrors.IsNotFound(err) {
return err
}
if _, err := secrets.Create(newSecret(loggingconfig.LoggingNamespace, loggingconfig.SSLSecretName, make(map[string][]byte))); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
if _, err := secrets.Controller().Lister().Get(loggingconfig.LoggingNamespace, loggingconfig.ClusterLoggingName); err != nil && !apierrors.IsNotFound(err) {
return err
}
if _, err = secrets.Create(newSecret(loggingconfig.LoggingNamespace, loggingconfig.ClusterLoggingName, map[string][]byte{
"cluster.conf": []byte{},
})); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
if _, err = secrets.Controller().Lister().Get(loggingconfig.LoggingNamespace, loggingconfig.ProjectLoggingName); err != nil && !apierrors.IsNotFound(err) {
return err
}
if _, err = secrets.Create(newSecret(loggingconfig.LoggingNamespace, loggingconfig.ProjectLoggingName, map[string][]byte{
"project.conf": []byte{},
})); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
func UnsetSecret(secrets rv1.SecretInterface, name, prefix string) error {
ncm, err := secrets.Get(name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
return nil
}
us := ncm.DeepCopy()
newData := make(map[string][]byte)
for k, v := range us.Data {
if !strings.HasPrefix(k, prefix) {
newData[k] = v
break
}
newData[k] = []byte{}
}
us.Data = newData
if _, err := secrets.Update(us); err != nil && !apierrors.IsNotFound(err) {
return err
}
return nil
}
func updateSecret(name string, data map[string][]byte, secrets rv1.SecretInterface) error {
existSecret, err := secrets.Get(name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "get secret %s:%s failed", loggingconfig.LoggingNamespace, name)
}
if existSecret, err = secrets.Create(newSecret(loggingconfig.LoggingNamespace, name, data)); err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "create secret %s:%s failed", loggingconfig.LoggingNamespace, name)
}
}
for k, v := range existSecret.Data {
if _, ok := data[k]; !ok {
data[k] = v
}
}
newSecret := existSecret.DeepCopy()
newSecret.Data = data
if reflect.DeepEqual(existSecret.Data, newSecret.Data) {
return nil
}
if _, err = secrets.Update(newSecret); err != nil {
return errors.Wrapf(err, "update secret %s:%s failed", loggingconfig.LoggingNamespace, name)
}
return nil
}
func newSecret(namespace, name string, data map[string][]byte) *v1.Secret {
return &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: data,
}
}
func buildCertSecret(prefix string, certificate, clientCert, clientKey string) map[string][]byte {
return map[string][]byte{
prefix + "_" + loggingconfig.CaFileName: []byte(certificate),
prefix + "_" + loggingconfig.ClientCertName: []byte(clientCert),
prefix + "_" + loggingconfig.ClientKeyName: []byte(clientKey),
}
}
func buildConfigSecret(configPath, namespace, name, level string) (map[string][]byte, error) {
file, err := os.Open(configPath)
if err != nil {
return nil, errors.Wrapf(err, "find %s logging configuration file %s failed", level, configPath)
}
defer file.Close()
buf, err := ioutil.ReadAll(file)
if err != nil {
return nil, errors.Wrapf(err, "read %s logging configuration file %s failed", level, configPath)
}
return map[string][]byte{
level + ".conf": buf,
}, nil
}