2016-06-01 18:47:37 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-06-01 18:47:37 +00:00
|
|
|
|
|
|
|
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 secureupstream
|
|
|
|
|
|
|
|
import (
|
2017-05-14 22:14:27 +00:00
|
|
|
"fmt"
|
2017-06-14 23:40:25 +00:00
|
|
|
|
2017-05-14 22:14:27 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-16 19:19:59 +00:00
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2016-06-01 18:47:37 +00:00
|
|
|
)
|
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
// Config describes SSL backend configuration
|
|
|
|
type Config struct {
|
2017-06-14 23:40:25 +00:00
|
|
|
CACert resolver.AuthSSLCert `json:"caCert"`
|
2017-05-14 22:14:27 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
type su struct {
|
2017-11-08 20:58:57 +00:00
|
|
|
r resolver.Resolver
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates a new secure upstream annotation parser
|
2017-11-08 20:58:57 +00:00
|
|
|
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
|
|
|
|
return su{r}
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the annotations contained in the ingress
|
2016-06-01 18:47:37 +00:00
|
|
|
// rule used to indicate if the upstream servers should use SSL
|
2016-12-29 20:02:06 +00:00
|
|
|
func (a su) Parse(ing *extensions.Ingress) (interface{}, error) {
|
2018-10-08 15:26:06 +00:00
|
|
|
bp, _ := parser.GetStringAnnotation("backend-protocol", ing)
|
2017-11-23 16:46:23 +00:00
|
|
|
ca, _ := parser.GetStringAnnotation("secure-verify-ca-secret", ing)
|
2017-11-07 16:36:51 +00:00
|
|
|
secure := &Config{
|
2017-05-14 22:14:27 +00:00
|
|
|
CACert: resolver.AuthSSLCert{},
|
|
|
|
}
|
2018-10-08 15:26:06 +00:00
|
|
|
|
|
|
|
if (bp != "HTTPS" && bp != "GRPCS") && ca != "" {
|
2017-05-14 22:14:27 +00:00
|
|
|
return secure,
|
|
|
|
errors.Errorf("trying to use CA from secret %v/%v on a non secure backend", ing.Namespace, ca)
|
|
|
|
}
|
|
|
|
if ca == "" {
|
|
|
|
return secure, nil
|
|
|
|
}
|
2017-11-08 20:58:57 +00:00
|
|
|
caCert, err := a.r.GetAuthCertificate(fmt.Sprintf("%v/%v", ing.Namespace, ca))
|
2017-05-14 22:14:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return secure, errors.Wrap(err, "error obtaining certificate")
|
|
|
|
}
|
|
|
|
if caCert == nil {
|
|
|
|
return secure, nil
|
|
|
|
}
|
2017-11-07 16:36:51 +00:00
|
|
|
return &Config{
|
2017-05-14 22:14:27 +00:00
|
|
|
CACert: *caCert,
|
|
|
|
}, nil
|
2016-06-01 18:47:37 +00:00
|
|
|
}
|