forked from cert-manager/cert-manager
/
ingress.go
290 lines (254 loc) · 9.74 KB
/
ingress.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Copyright 2018 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 http
import (
"fmt"
extv1beta1 "k8s.io/api/extensions/v1beta1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/ingress/core/pkg/ingress/annotations/class"
"github.com/golang/glog"
"github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
"github.com/jetstack/cert-manager/pkg/issuer/acme/http/solver"
)
// getIngressesForChallenge returns a list of Ingresses that were created to solve
// http challenges for the given domain
func (s *Solver) getIngressesForChallenge(crt *v1alpha1.Certificate, ch v1alpha1.ACMEOrderChallenge) ([]*extv1beta1.Ingress, error) {
podLabels := podLabels(ch)
selector := labels.NewSelector()
for key, val := range podLabels {
req, err := labels.NewRequirement(key, selection.Equals, []string{val})
if err != nil {
return nil, err
}
selector = selector.Add(*req)
}
glog.Infof("Looking up Ingresses for selector %v", selector)
ingressList, err := s.ingressLister.Ingresses(crt.Namespace).List(selector)
if err != nil {
return nil, err
}
var relevantIngresses []*extv1beta1.Ingress
for _, ingress := range ingressList {
if !metav1.IsControlledBy(ingress, crt) {
glog.Infof("Found ingress %q with acme-order-url annotation set to that of Certificate %q "+
"but it is not owned by the Certificate resource, so skipping it.", ingress.Namespace+"/"+ingress.Name, crt.Namespace+"/"+crt.Name)
continue
}
relevantIngresses = append(relevantIngresses, ingress)
}
return relevantIngresses, nil
}
// ensureIngress will ensure the ingress required to solve this challenge
// exists, or if an existing ingress is specified on the secret will ensure
// that the ingress has an appropriate challenge path configured
func (s *Solver) ensureIngress(crt *v1alpha1.Certificate, svcName string, ch v1alpha1.ACMEOrderChallenge) (ing *extv1beta1.Ingress, err error) {
domainCfg := v1alpha1.ConfigForDomain(crt.Spec.ACME.Config, ch.Domain)
if domainCfg == nil {
return nil, fmt.Errorf("no ACME challenge configuration found for domain %q", ch.Domain)
}
httpDomainCfg := domainCfg.HTTP01
if httpDomainCfg == nil {
httpDomainCfg = &v1alpha1.HTTP01SolverConfig{}
}
if httpDomainCfg != nil &&
httpDomainCfg.Ingress != "" {
return s.addChallengePathToIngress(crt, svcName, ch)
}
existingIngresses, err := s.getIngressesForChallenge(crt, ch)
if err != nil {
return nil, err
}
if len(existingIngresses) == 1 {
return existingIngresses[0], nil
}
if len(existingIngresses) > 1 {
errMsg := fmt.Sprintf("multiple challenge solver ingresses found for certificate '%s/%s'. Cleaning up existing pods.", crt.Namespace, crt.Name)
glog.Infof(errMsg)
err := s.cleanupIngresses(crt, ch)
if err != nil {
return nil, err
}
return nil, fmt.Errorf(errMsg)
}
glog.Infof("No existing HTTP01 challenge solver ingress found for Certificate %q. One will be created.", crt.Namespace+"/"+crt.Name)
return s.createIngress(crt, svcName, ch)
}
// createIngress will create a challenge solving pod for the given certificate,
// domain, token and key.
func (s *Solver) createIngress(crt *v1alpha1.Certificate, svcName string, ch v1alpha1.ACMEOrderChallenge) (*extv1beta1.Ingress, error) {
return s.Client.ExtensionsV1beta1().Ingresses(crt.Namespace).Create(buildIngressResource(crt, svcName, ch))
}
func buildIngressResource(crt *v1alpha1.Certificate, svcName string, ch v1alpha1.ACMEOrderChallenge) *extv1beta1.Ingress {
var ingClass *string
if ch.SolverConfig.HTTP01 != nil {
ingClass = ch.SolverConfig.HTTP01.IngressClass
}
podLabels := podLabels(ch)
// TODO: add additional annotations to help workaround problematic ingress controller behaviours
ingAnnotaions := make(map[string]string)
if ingClass != nil {
ingAnnotaions[class.IngressKey] = *ingClass
}
ingPathToAdd := ingressPath(ch.Token, svcName)
return &extv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "cm-acme-http-solver-",
Namespace: crt.Namespace,
Labels: podLabels,
Annotations: ingAnnotaions,
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(crt, certificateGvk)},
},
Spec: extv1beta1.IngressSpec{
Rules: []extv1beta1.IngressRule{
{
Host: ch.Domain,
IngressRuleValue: extv1beta1.IngressRuleValue{
HTTP: &extv1beta1.HTTPIngressRuleValue{
Paths: []extv1beta1.HTTPIngressPath{ingPathToAdd},
},
},
},
},
},
}
}
func (s *Solver) addChallengePathToIngress(crt *v1alpha1.Certificate, svcName string, ch v1alpha1.ACMEOrderChallenge) (*extv1beta1.Ingress, error) {
ingressName := ch.SolverConfig.HTTP01.Ingress
ing, err := s.ingressLister.Ingresses(crt.Namespace).Get(ingressName)
if err != nil {
return nil, err
}
ingPathToAdd := ingressPath(ch.Token, svcName)
// check for an existing Rule for the given domain on the ingress resource
for _, rule := range ing.Spec.Rules {
if rule.Host == ch.Domain {
if rule.HTTP == nil {
rule.HTTP = &extv1beta1.HTTPIngressRuleValue{}
}
for i, p := range rule.HTTP.Paths {
// if an existing path exists on this rule for the challenge path,
// we overwrite it else we'll confuse ingress controllers
if p.Path == ingPathToAdd.Path {
// ingress resource is already up to date
if p.Backend.ServiceName == ingPathToAdd.Backend.ServiceName &&
p.Backend.ServicePort == ingPathToAdd.Backend.ServicePort {
return ing, nil
}
rule.HTTP.Paths[i] = ingPathToAdd
return s.Client.ExtensionsV1beta1().Ingresses(ing.Namespace).Update(ing)
}
}
rule.HTTP.Paths = append(rule.HTTP.Paths, ingPathToAdd)
return s.Client.ExtensionsV1beta1().Ingresses(ing.Namespace).Update(ing)
}
}
// if one doesn't exist, create a new IngressRule
ing.Spec.Rules = append(ing.Spec.Rules, extv1beta1.IngressRule{
Host: ch.Domain,
IngressRuleValue: extv1beta1.IngressRuleValue{
HTTP: &extv1beta1.HTTPIngressRuleValue{
Paths: []extv1beta1.HTTPIngressPath{ingPathToAdd},
},
},
})
return s.Client.ExtensionsV1beta1().Ingresses(ing.Namespace).Update(ing)
}
// cleanupIngresses will remove the rules added by cert-manager to an existing
// ingress, or delete the ingress if an existing ingress name is not specified
// on the certificate.
func (s *Solver) cleanupIngresses(crt *v1alpha1.Certificate, ch v1alpha1.ACMEOrderChallenge) error {
httpDomainCfg := ch.SolverConfig.HTTP01
if httpDomainCfg == nil {
httpDomainCfg = &v1alpha1.HTTP01SolverConfig{}
}
existingIngressName := httpDomainCfg.Ingress
// if the 'ingress' field on the domain config is not set, we need to delete
// the ingress resources that cert-manager has created to solve the challenge
if existingIngressName == "" {
ingresses, err := s.getIngressesForChallenge(crt, ch)
if err != nil {
return err
}
glog.V(4).Infof("Found %d ingresses to clean up for certificate %q", len(ingresses), crt.Namespace+"/"+crt.Name)
var errs []error
for _, ingress := range ingresses {
// TODO: should we call DeleteCollection here? We'd need to somehow
// also ensure ownership as part of that request using a FieldSelector.
err := s.Client.ExtensionsV1beta1().Ingresses(ingress.Namespace).Delete(ingress.Name, nil)
if err != nil {
errs = append(errs, err)
}
}
return utilerrors.NewAggregate(errs)
}
// otherwise, we need to remove any cert-manager added rules from the ingress resource
// TODO: switch to using ingress lister?
ing, err := s.Client.ExtensionsV1beta1().Ingresses(crt.Namespace).Get(existingIngressName, metav1.GetOptions{})
if k8sErrors.IsNotFound(err) {
glog.Infof("attempt to cleanup Ingress %q of ACME challenge path failed: %v", crt.Namespace+"/"+existingIngressName, err)
return nil
}
if err != nil {
return err
}
ingPathToDel := solverPathFn(ch.Token)
var ingRules []extv1beta1.IngressRule
for _, rule := range ing.Spec.Rules {
// always retain rules that are not for the same DNSName
if rule.Host != ch.Domain {
ingRules = append(ingRules, rule)
continue
}
// always retain rules that don't specify `HTTP`
if rule.HTTP == nil {
ingRules = append(ingRules, rule)
continue
}
// check the rule for paths. If we find the ingress path we need to
// delete here, delete it
for i, path := range rule.HTTP.Paths {
if path.Path == ingPathToDel {
rule.HTTP.Paths = append(rule.HTTP.Paths[:i], rule.HTTP.Paths[i+1:]...)
}
}
// if there are still paths level on this rule, we should retain it
if len(rule.HTTP.Paths) > 0 {
ingRules = append(ingRules, rule)
}
}
ing.Spec.Rules = ingRules
_, err = s.Client.ExtensionsV1beta1().Ingresses(ing.Namespace).Update(ing)
if err != nil {
return err
}
return nil
}
// ingressPath returns the ingress HTTPIngressPath object needed to solve this
// challenge.
func ingressPath(token, serviceName string) extv1beta1.HTTPIngressPath {
return extv1beta1.HTTPIngressPath{
Path: solverPathFn(token),
Backend: extv1beta1.IngressBackend{
ServiceName: serviceName,
ServicePort: intstr.FromInt(acmeSolverListenPort),
},
}
}
var solverPathFn = func(token string) string {
return fmt.Sprintf("%s/%s", solver.HTTPChallengePath, token)
}