-
Notifications
You must be signed in to change notification settings - Fork 109
/
config.go
154 lines (131 loc) · 4.8 KB
/
config.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
/*
* Copyright 2020 The Knative Authors
*
* Licensed 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 security
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
"knative.dev/eventing-kafka-broker/control-plane/pkg/kafka"
)
const (
AuthSecretNameKey = "auth.secret.ref.name" /* #nosec G101 */ /* Potential hardcoded credentials (gosec) */
AuthSecretNamespaceKey = "auth.secret.ref.namespace" /* #nosec G101 */ /* Potential hardcoded credentials (gosec) */
)
// SecretLocator locates a secret in a cluster.
type SecretLocator interface {
// SecretName returns the secret name.
// It returns true if the name should be used and false if should be ignored.
SecretName() (string, bool)
// SecretNamespace returns the secret name.
// It returns true if the namespace should be used and false if should be ignored.
SecretNamespace() (string, bool)
}
// SecretProviderFunc provides a secret given a namespace/name pair.
type SecretProviderFunc func(ctx context.Context, namespace, name string) (*corev1.Secret, error)
func NewSaramaSecurityOptionFromSecret(secret *corev1.Secret) (kafka.ConfigOption, error) {
if secret == nil {
return kafka.NoOpConfigOption, nil
}
// if the secret does not have a protocol, try and resolve it from the legacy format
if _, ok := secret.Data[ProtocolKey]; !ok {
authContext, err := ResolveAuthContextFromLegacySecret(secret)
if err != nil {
return nil, err
}
secret = authContext.VirtualSecret
}
return secretData(secret.Data), nil
}
func Secret(ctx context.Context, config SecretLocator, secretProviderFunc SecretProviderFunc) (*corev1.Secret, error) {
name, ok := config.SecretName()
if !ok {
// No auth config, will later use a no-op config option.
return nil, nil
}
ns, ok := config.SecretNamespace()
if !ok {
// No auth config, will later use a no-op config option.
return nil, nil
}
secret, err := secretProviderFunc(ctx, ns, name)
if err != nil {
return nil, err
}
return secret, nil
}
// DefaultSecretProviderFunc is a secret provider that uses the local cache for getting the secret and when the secret
// is not found it uses the kube client to check if the secret doesn't actually exist.
func DefaultSecretProviderFunc(lister corelisters.SecretLister, kc kubernetes.Interface) SecretProviderFunc {
return func(ctx context.Context, namespace, name string) (*corev1.Secret, error) {
secret, err := lister.Secrets(namespace).Get(name)
if err != nil {
if apierrors.IsNotFound(err) {
// Check if the secret actually doesn't exist.
secret, err = kc.CoreV1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to get secret %s/%s: %w", namespace, name, err)
}
return secret, nil
}
return nil, fmt.Errorf("failed to get secret %s/%s: %w", namespace, name, err)
}
return secret, nil
}
}
// MTConfigMapSecretLocator is a SecretLocator that locates a secret using a reference in a ConfigMap.
//
// The name is taken from the data field using the key: AuthSecretNameKey.
// When UseNamespaceInConfigmap=true, the namespace is taken from the data field using the
// key: AuthSecretNamespaceKey. When false, namespace of the ConfigMap is returned.
type MTConfigMapSecretLocator struct {
*corev1.ConfigMap
// if false, secret namespace is NOT read from the configmap
UseNamespaceInConfigmap bool
}
func (cmp *MTConfigMapSecretLocator) SecretName() (string, bool) {
if cmp.ConfigMap == nil {
return "", false
}
v, ok := cmp.Data[AuthSecretNameKey]
return v, ok
}
func (cmp *MTConfigMapSecretLocator) SecretNamespace() (string, bool) {
if cmp.UseNamespaceInConfigmap {
if v, ok := cmp.Data[AuthSecretNamespaceKey]; ok && len(v) > 0 {
return v, ok
}
}
return cmp.Namespace, true
}
type AnnotationsSecretLocator struct {
Annotations map[string]string
Namespace string
}
func (a *AnnotationsSecretLocator) SecretName() (string, bool) {
name, ok := a.Annotations[AuthSecretNameKey]
if ok && name != "" {
return name, ok
}
return "", false
}
func (a *AnnotationsSecretLocator) SecretNamespace() (string, bool) {
name, ok := a.SecretName()
return a.Namespace, len(a.Namespace) > 0 && ok && len(name) > 0
}