-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathkubernetes_keystore.go
155 lines (141 loc) · 5.56 KB
/
kubernetes_keystore.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package k8skeystore
import (
"context"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/bus"
"github.com/elastic/beats/v7/libbeat/keystore"
"github.com/elastic/beats/v7/libbeat/logp"
)
type KubernetesKeystores map[string]keystore.Keystore
// KubernetesKeystoresRegistry holds KubernetesKeystores for known namespaces. Once a Keystore for one k8s namespace
// is initialized it will be reused every time it is needed.
type KubernetesKeystoresRegistry struct {
kubernetesKeystores KubernetesKeystores
logger *logp.Logger
client k8s.Interface
}
// KubernetesSecretsKeystore allows to retrieve passwords from Kubernetes secrets for a given namespace
type KubernetesSecretsKeystore struct {
namespace string
client k8s.Interface
logger *logp.Logger
}
// Factoryk8s Create the right keystore with the configured options
func Factoryk8s(keystoreNamespace string, ks8client k8s.Interface, logger *logp.Logger) (keystore.Keystore, error) {
keystore, err := NewKubernetesSecretsKeystore(keystoreNamespace, ks8client, logger)
return keystore, err
}
// NewKubernetesKeystoresRegistry initializes a KubernetesKeystoresRegistry
func NewKubernetesKeystoresRegistry(logger *logp.Logger, client k8s.Interface) keystore.Provider {
return &KubernetesKeystoresRegistry{
kubernetesKeystores: KubernetesKeystores{},
logger: logger,
client: client,
}
}
// GetKeystore return a KubernetesSecretsKeystore if it already exists for a given namespace or creates a new one.
func (kr *KubernetesKeystoresRegistry) GetKeystore(event bus.Event) keystore.Keystore {
namespace := ""
if val, ok := event["kubernetes"]; ok {
kubernetesMeta := val.(common.MapStr)
ns, err := kubernetesMeta.GetValue("namespace")
if err != nil {
kr.logger.Debugf("Cannot retrieve kubernetes namespace from event: %s", event)
return nil
}
namespace = ns.(string)
}
if namespace != "" {
// either retrieve already stored keystore or create a new one for the namespace
if storedKeystore, ok := kr.kubernetesKeystores[namespace]; ok {
return storedKeystore
}
k8sKeystore, _ := Factoryk8s(namespace, kr.client, kr.logger)
kr.kubernetesKeystores["namespace"] = k8sKeystore
return k8sKeystore
}
kr.logger.Debugf("Cannot retrieve kubernetes namespace from event: %s", event)
return nil
}
// NewKubernetesSecretsKeystore returns an new k8s Keystore
func NewKubernetesSecretsKeystore(keystoreNamespace string, ks8client k8s.Interface, logger *logp.Logger) (keystore.Keystore, error) {
keystore := KubernetesSecretsKeystore{
namespace: keystoreNamespace,
client: ks8client,
logger: logger,
}
return &keystore, nil
}
// Retrieve return a SecureString instance that will contains both the key and the secret.
func (k *KubernetesSecretsKeystore) Retrieve(key string) (*keystore.SecureString, error) {
// key = "kubernetes.somenamespace.somesecret.value"
tokens := strings.Split(key, ".")
if len(tokens) > 0 && tokens[0] != "kubernetes" {
return nil, keystore.ErrKeyDoesntExists
}
if len(tokens) != 4 {
k.logger.Debugf(
"not valid secret key: %v. Secrets should be of the following format %v",
key,
"kubernetes.somenamespace.somesecret.value",
)
return nil, keystore.ErrKeyDoesntExists
}
ns := tokens[1]
secretName := tokens[2]
secretVar := tokens[3]
if ns != k.namespace {
k.logger.Debugf("cannot access Kubernetes secrets from a different namespace (%v) than: %v", ns, k.namespace)
return nil, keystore.ErrKeyDoesntExists
}
secretIntefrace := k.client.CoreV1().Secrets(ns)
ctx := context.TODO()
secrets, err := secretIntefrace.List(ctx, metav1.ListOptions{})
if err != nil {
k.logger.Errorf("Could not retrieve secrets from k8s API: %v", err)
return nil, keystore.ErrKeyDoesntExists
}
if len(secrets.Items) == 0 {
k.logger.Debugf("no secrets found for namespace: %v", ns)
return nil, keystore.ErrKeyDoesntExists
}
secret, err := secretIntefrace.Get(ctx, secretName, metav1.GetOptions{})
if err != nil {
k.logger.Errorf("Could not retrieve secret from k8s API: %v", err)
return nil, keystore.ErrKeyDoesntExists
}
if _, ok := secret.Data[secretVar]; !ok {
k.logger.Errorf("Could not retrieve value %v for secret %v", secretVar, secretName)
return nil, keystore.ErrKeyDoesntExists
}
secretString := secret.Data[secretVar]
return keystore.NewSecureString(secretString), nil
}
// GetConfig returns common.Config representation of the key / secret pair to be merged with other
// loaded configuration.
func (k *KubernetesSecretsKeystore) GetConfig() (*common.Config, error) {
return nil, nil
}
// IsPersisted return if the keystore is physically persisted on disk.
func (k *KubernetesSecretsKeystore) IsPersisted() bool {
return true
}