/
sources.go
230 lines (196 loc) · 8.4 KB
/
sources.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Copyright 2019 The Jetstack cert-manager contributors.
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 cainjector
import (
"context"
logf "github.com/jetstack/cert-manager/pkg/logs"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
)
// caDataSource knows how to extract CA data given a provided InjectTarget.
// This allows adaptable implementations of fetching CA data based on
// configuration given on the injection target (e.g. annotations).
type caDataSource interface {
// Configured returns true if this data source should be used for the given
// InjectTarget, i.e. if it has appropriate annotations enabled to use the
// annotations.
Configured(log logr.Logger, metaObj metav1.Object) bool
// ReadCA reads the CA that should be injected into the InjectTarget based
// on the configuration provided in the InjectTarget.
// ReadCA may return nil, nil if the CA data cannot be read.
// In this case, the caller should not retry the operation.
// It is up to the ReadCA implementation to inform the user why the CA
// failed to read.
ReadCA(ctx context.Context, log logr.Logger, metaObj metav1.Object) (ca []byte, err error)
// ApplyTo applies any required watchers to the given controller builder.
ApplyTo(mgr ctrl.Manager, setup injectorSetup, builder *ctrl.Builder) error
}
// kubeconfigDataSource reads the ca bundle provided as part of the struct
// instantiation if it has the 'cert-manager.io/inject-apiserver-ca'
// annotation.
type kubeconfigDataSource struct {
apiserverCABundle []byte
}
func (c *kubeconfigDataSource) Configured(log logr.Logger, metaObj metav1.Object) bool {
return metaObj.GetAnnotations()[cmapi.WantInjectAPIServerCAAnnotation] == "true"
}
func (c *kubeconfigDataSource) ReadCA(ctx context.Context, log logr.Logger, metaObj metav1.Object) (ca []byte, err error) {
return c.apiserverCABundle, nil
}
func (c *kubeconfigDataSource) ApplyTo(mgr ctrl.Manager, setup injectorSetup, builder *ctrl.Builder) error {
cfg := mgr.GetConfig()
caBundle, err := dataFromSliceOrFile(cfg.CAData, cfg.CAFile)
if err != nil {
return err
}
c.apiserverCABundle = caBundle
return nil
}
// certificateDataSource reads a CA bundle by fetching the Certificate named in
// the 'cert-manager.io/inject-ca-from' annotation in the form
// 'namespace/name'.
type certificateDataSource struct {
client client.Client
}
func (c *certificateDataSource) Configured(log logr.Logger, metaObj metav1.Object) bool {
certNameRaw, ok := metaObj.GetAnnotations()[cmapi.WantInjectAnnotation]
if !ok {
return false
}
log.V(logf.DebugLevel).Info("Extracting CA from Certificate resource", "certificate", certNameRaw)
return true
}
func (c *certificateDataSource) ReadCA(ctx context.Context, log logr.Logger, metaObj metav1.Object) (ca []byte, err error) {
certNameRaw := metaObj.GetAnnotations()[cmapi.WantInjectAnnotation]
certName := splitNamespacedName(certNameRaw)
log = log.WithValues("certificate", certName)
if certName.Namespace == "" {
log.Error(nil, "invalid certificate name")
// don't return an error, requeuing won't help till this is changed
return nil, nil
}
var cert cmapi.Certificate
if err := c.client.Get(ctx, certName, &cert); err != nil {
log.Error(err, "unable to fetch associated certificate")
// don't requeue if we're just not found, we'll get called when the secret gets created
return nil, dropNotFound(err)
}
secretName := &types.NamespacedName{Namespace: cert.Namespace, Name: cert.Spec.SecretName}
// grab the associated secret, and ensure it's owned by the cert
log = log.WithValues("secret", secretName)
var secret corev1.Secret
if err := c.client.Get(ctx, *secretName, &secret); err != nil {
log.Error(err, "unable to fetch associated secret")
// don't requeue if we're just not found, we'll get called when the secret gets created
return nil, dropNotFound(err)
}
owner := OwningCertForSecret(&secret)
if owner == nil || *owner != certName {
log.V(logf.WarnLevel).Info("refusing to target secret not owned by certificate", "owner", metav1.GetControllerOf(&secret))
return nil, nil
}
// inject the CA data
caData, hasCAData := secret.Data[cmmeta.TLSCAKey]
if !hasCAData {
log.Error(nil, "certificate has no CA data")
// don't requeue, we'll get called when the secret gets updated
return nil, nil
}
return caData, nil
}
func (c *certificateDataSource) ApplyTo(mgr ctrl.Manager, setup injectorSetup, builder *ctrl.Builder) error {
typ := setup.injector.NewTarget().AsObject()
if err := mgr.GetFieldIndexer().IndexField(context.TODO(), typ, injectFromPath, injectableCAFromIndexer); err != nil {
return err
}
builder.Watches(&source.Kind{Type: &cmapi.Certificate{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: &certMapper{
Client: mgr.GetClient(),
log: ctrl.Log.WithName("cert-mapper"),
toInjectable: buildCertToInjectableFunc(setup.listType, setup.resourceName),
}},
).
Watches(&source.Kind{Type: &corev1.Secret{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: &secretForCertificateMapper{
Client: mgr.GetClient(),
log: ctrl.Log.WithName("secret-for-certificate-mapper"),
certificateToInjectable: buildCertToInjectableFunc(setup.listType, setup.resourceName),
}},
)
return nil
}
// secretDataSource reads a CA bundle from a Secret resource named using the
// 'cert-manager.io/inject-ca-from-secret' annotation in the form
// 'namespace/name'.
type secretDataSource struct {
client client.Client
}
func (c *secretDataSource) Configured(log logr.Logger, metaObj metav1.Object) bool {
secretNameRaw, ok := metaObj.GetAnnotations()[cmapi.WantInjectFromSecretAnnotation]
if !ok {
return false
}
log.V(logf.DebugLevel).Info("Extracting CA from Secret resource", "secret", secretNameRaw)
return true
}
func (c *secretDataSource) ReadCA(ctx context.Context, log logr.Logger, metaObj metav1.Object) ([]byte, error) {
secretNameRaw := metaObj.GetAnnotations()[cmapi.WantInjectFromSecretAnnotation]
secretName := splitNamespacedName(secretNameRaw)
log = log.WithValues("secret", secretName)
if secretName.Namespace == "" {
log.Error(nil, "invalid certificate name")
// don't return an error, requeuing won't help till this is changed
return nil, nil
}
// grab the associated secret
var secret corev1.Secret
if err := c.client.Get(ctx, secretName, &secret); err != nil {
log.Error(err, "unable to fetch associated secret")
// don't requeue if we're just not found, we'll get called when the secret gets created
return nil, dropNotFound(err)
}
if secret.Annotations == nil || secret.Annotations[cmapi.AllowsInjectionFromSecretAnnotation] != "true" {
log.V(logf.WarnLevel).Info("Secret resource does not allow direct injection - refusing to inject CA")
return nil, nil
}
// inject the CA data
caData, hasCAData := secret.Data[cmmeta.TLSCAKey]
if !hasCAData {
log.Error(nil, "certificate has no CA data")
// don't requeue, we'll get called when the secret gets updated
return nil, nil
}
return caData, nil
}
func (c *secretDataSource) ApplyTo(mgr ctrl.Manager, setup injectorSetup, builder *ctrl.Builder) error {
typ := setup.injector.NewTarget().AsObject()
if err := mgr.GetFieldIndexer().IndexField(context.TODO(), typ, injectFromSecretPath, injectableCAFromSecretIndexer); err != nil {
return err
}
builder.Watches(&source.Kind{Type: &corev1.Secret{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: &secretForInjectableMapper{
Client: mgr.GetClient(),
log: ctrl.Log.WithName("secret-mapper"),
secretToInjectable: buildSecretToInjectableFunc(setup.listType, setup.resourceName),
}},
)
return nil
}