-
Notifications
You must be signed in to change notification settings - Fork 344
/
elasticsearch_secrets.go
216 lines (192 loc) · 6.31 KB
/
elasticsearch_secrets.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package storage
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)
const (
tmpWorkingDir = "/tmp/_certs"
)
type secret struct {
name string
keyFileNameMap map[string]string
}
func (s secret) instanceName(jaeger *v1.Jaeger) string {
// elasticsearch secret is hardcoded in es-operator https://jira.coreos.com/browse/LOG-326
if s.name == esSecret.name {
return esSecret.name
}
return fmt.Sprintf("%s-%s", jaeger.Name, s.name)
}
// master secret is used to generate other certs
var masterSecret = secret{
name: "master-certs",
keyFileNameMap: map[string]string{
"ca": "ca.crt",
"ca-key": "ca.key",
},
}
// es secret is used by Elasticsearch nodes
var esSecret = secret{
name: "elasticsearch",
keyFileNameMap: map[string]string{
"elasticsearch.key": "elasticsearch.key",
"elasticsearch.crt": "elasticsearch.crt",
"logging-es.key": "logging-es.key",
"logging-es.crt": "logging-es.crt",
"admin-key": "system.admin.key",
"admin-cert": "system.admin.crt",
"admin-ca": "ca.crt",
},
}
// jaeger secret is used by jaeger components to talk to Elasticsearch
var jaegerSecret = secret{
name: "jaeger-elasticsearch",
keyFileNameMap: map[string]string{
"ca": "ca.crt",
"key": "user.jaeger.key",
"cert": "user.jaeger.crt",
},
}
// curator secret is used for index cleaner and rollover
var curatorSecret = secret{
name: "curator",
keyFileNameMap: map[string]string{
"ca": "ca.crt",
"key": "system.logging.curator.key",
"cert": "system.logging.curator.crt",
},
}
// ExtractSecrets assembles a set of secrets related to Elasticsearch
func (ed *ElasticsearchDeployment) ExtractSecrets() []corev1.Secret {
return []corev1.Secret{
createSecret(ed.Jaeger, masterSecret.instanceName(ed.Jaeger), getWorkingDirContents(getWorkingDir(ed.Jaeger), masterSecret.keyFileNameMap)),
createSecret(ed.Jaeger, esSecret.instanceName(ed.Jaeger), getWorkingDirContents(getWorkingDir(ed.Jaeger), esSecret.keyFileNameMap)),
createSecret(ed.Jaeger, jaegerSecret.instanceName(ed.Jaeger), getWorkingDirContents(getWorkingDir(ed.Jaeger), jaegerSecret.keyFileNameMap)),
createSecret(ed.Jaeger, curatorSecret.instanceName(ed.Jaeger), getWorkingDirContents(getWorkingDir(ed.Jaeger), curatorSecret.keyFileNameMap)),
}
}
// CreateCerts creates certificates for elasticsearch, jaeger and curator
// The cert generation is done by shell script. If the certificates are not present
// on the filesystem the operator injects them from secrets - this allows operator restarts.
// The script also re-generates expired certificates.
func (ed *ElasticsearchDeployment) CreateCerts() error {
err := extractSecretsToFile(ed.Jaeger, ed.Secrets, masterSecret, esSecret, jaegerSecret, curatorSecret)
if err != nil {
return errors.Wrap(err, "failed to extract certificates from secrets to file")
}
return createESCerts(ed.CertScript, ed.Jaeger)
}
// CleanCerts removes certificates from local filesystem.
// Use this function in tests to clean resources
func (ed *ElasticsearchDeployment) CleanCerts() error {
return os.RemoveAll(getWorkingDir(ed.Jaeger))
}
func extractSecretsToFile(jaeger *v1.Jaeger, secrets []corev1.Secret, s ...secret) error {
secretMap := map[string]corev1.Secret{}
for _, sec := range secrets {
secretMap[sec.Name] = sec
}
for _, sec := range s {
if secret, ok := secretMap[sec.instanceName(jaeger)]; ok {
if err := extractSecretToFile(getWorkingDir(jaeger), secret.Data, sec); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to extract secret %s", secret.Name))
}
}
}
return nil
}
func extractSecretToFile(workingDir string, data map[string][]byte, secret secret) error {
for k, v := range secret.keyFileNameMap {
if err := writeToFile(workingDir, v, data[k]); err != nil {
return err
}
}
return nil
}
func getWorkingDir(jaeger *v1.Jaeger) string {
return filepath.Clean(fmt.Sprintf("%s/%s/%s/%s", tmpWorkingDir, jaeger.Namespace, jaeger.Name, jaeger.UID))
}
// createESCerts runs bash scripts which generates certificates
func createESCerts(script string, jaeger *v1.Jaeger) error {
// #nosec G204: Subprocess launching should be audited
cmd := exec.Command("bash", script)
cmd.Env = append(os.Environ(),
"NAMESPACE="+jaeger.Namespace,
"WORKING_DIR="+getWorkingDir(jaeger),
)
if out, err := cmd.CombinedOutput(); err != nil {
log.WithFields(log.Fields{
"script": script,
"out": string(out)}).
Error("Failed to create certificates")
return fmt.Errorf("error running script %s: %v", script, err)
}
return nil
}
func createSecret(jaeger *v1.Jaeger, secretName string, data map[string][]byte) corev1.Secret {
return corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: jaeger.Namespace,
Labels: util.Labels(secretName, "es-secret", *jaeger),
OwnerReferences: []metav1.OwnerReference{util.AsOwner(jaeger)},
},
Type: corev1.SecretTypeOpaque,
Data: data,
}
}
func getWorkingDirContents(dir string, content map[string]string) map[string][]byte {
c := map[string][]byte{}
for secretKey, certName := range content {
c[secretKey] = getDirFileContents(dir, certName)
}
return c
}
func getDirFileContents(dir, filePath string) []byte {
return getFileContents(getFilePath(dir, filePath))
}
func getFilePath(dir, toFile string) string {
return path.Join(dir, toFile)
}
func getFileContents(path string) []byte {
if path == "" {
return nil
}
contents, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return nil
}
return contents
}
func writeToFile(dir, file string, value []byte) error {
// first check if file exists - we prefer what is on FS to revert users editing secrets
path := getFilePath(dir, file)
if _, err := os.Stat(path); err == nil {
return nil
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(value)
if err != nil {
// remove the file on failure - it can be correctly created in the next iteration
os.RemoveAll(path)
return err
}
return nil
}