2016-11-10 22:56:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes 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 authtls
|
|
|
|
|
|
|
|
import (
|
2022-01-10 00:29:12 +00:00
|
|
|
"fmt"
|
2022-04-15 19:59:10 +00:00
|
|
|
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
"regexp"
|
|
|
|
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
|
|
|
ing_errors "k8s.io/ingress-nginx/internal/ingress/errors"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
|
|
|
"k8s.io/ingress-nginx/internal/k8s"
|
2016-11-10 22:56:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-11-08 20:58:57 +00:00
|
|
|
defaultAuthTLSDepth = 1
|
|
|
|
defaultAuthVerifyClient = "on"
|
2017-10-05 11:26:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
authVerifyClientRegex = regexp.MustCompile(`on|off|optional|optional_no_ca`)
|
2022-04-15 19:59:10 +00:00
|
|
|
commonNameRegex = regexp.MustCompile(`CN=`)
|
2016-11-10 22:56:29 +00:00
|
|
|
)
|
|
|
|
|
2018-05-31 06:18:08 +00:00
|
|
|
// Config contains the AuthSSLCert used for mutual authentication
|
2017-02-06 18:16:36 +00:00
|
|
|
// and the configured ValidationDepth
|
2017-11-07 16:36:51 +00:00
|
|
|
type Config struct {
|
2017-08-22 20:16:59 +00:00
|
|
|
resolver.AuthSSLCert
|
2017-11-18 00:28:45 +00:00
|
|
|
VerifyClient string `json:"verify_client"`
|
|
|
|
ValidationDepth int `json:"validationDepth"`
|
|
|
|
ErrorPage string `json:"errorPage"`
|
|
|
|
PassCertToUpstream bool `json:"passCertToUpstream"`
|
2022-04-15 19:59:10 +00:00
|
|
|
MatchCN string `json:"matchCN"`
|
2018-02-25 20:20:14 +00:00
|
|
|
AuthTLSError string
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
// Equal tests for equality between two Config types
|
|
|
|
func (assl1 *Config) Equal(assl2 *Config) bool {
|
2017-06-14 21:33:12 +00:00
|
|
|
if assl1 == assl2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if assl1 == nil || assl2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-06-14 23:40:25 +00:00
|
|
|
if !(&assl1.AuthSSLCert).Equal(&assl2.AuthSSLCert) {
|
2017-06-14 21:33:12 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-10-05 11:26:07 +00:00
|
|
|
if assl1.VerifyClient != assl2.VerifyClient {
|
|
|
|
return false
|
|
|
|
}
|
2017-06-14 21:33:12 +00:00
|
|
|
if assl1.ValidationDepth != assl2.ValidationDepth {
|
|
|
|
return false
|
|
|
|
}
|
2017-09-03 20:12:03 +00:00
|
|
|
if assl1.ErrorPage != assl2.ErrorPage {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-18 00:28:45 +00:00
|
|
|
if assl1.PassCertToUpstream != assl2.PassCertToUpstream {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-06-14 21:33:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
// NewParser creates a new TLS authentication annotation parser
|
2017-11-08 20:58:57 +00:00
|
|
|
func NewParser(resolver resolver.Resolver) parser.IngressAnnotation {
|
2016-12-29 20:02:06 +00:00
|
|
|
return authTLS{resolver}
|
|
|
|
}
|
|
|
|
|
2017-02-06 18:16:36 +00:00
|
|
|
type authTLS struct {
|
2017-11-08 20:58:57 +00:00
|
|
|
r resolver.Resolver
|
2017-02-06 18:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the annotations contained in the ingress
|
|
|
|
// rule used to use a Certificate as authentication method
|
2019-06-09 22:49:59 +00:00
|
|
|
func (a authTLS) Parse(ing *networking.Ingress) (interface{}, error) {
|
2018-11-30 22:56:11 +00:00
|
|
|
var err error
|
|
|
|
config := &Config{}
|
2017-02-06 18:16:36 +00:00
|
|
|
|
2017-11-23 16:46:23 +00:00
|
|
|
tlsauthsecret, err := parser.GetStringAnnotation("auth-tls-secret", ing)
|
2016-11-10 22:56:29 +00:00
|
|
|
if err != nil {
|
2017-11-07 16:36:51 +00:00
|
|
|
return &Config{}, err
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 18:16:36 +00:00
|
|
|
_, _, err = k8s.ParseNameNS(tlsauthsecret)
|
2016-11-10 22:56:29 +00:00
|
|
|
if err != nil {
|
2017-11-07 16:36:51 +00:00
|
|
|
return &Config{}, ing_errors.NewLocationDenied(err.Error())
|
2017-02-06 18:16:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 20:58:57 +00:00
|
|
|
authCert, err := a.r.GetAuthCertificate(tlsauthsecret)
|
2017-01-12 22:00:42 +00:00
|
|
|
if err != nil {
|
2022-01-10 00:29:12 +00:00
|
|
|
e := fmt.Errorf("error obtaining certificate: %w", err)
|
2018-02-25 20:20:14 +00:00
|
|
|
return &Config{}, ing_errors.LocationDenied{Reason: e}
|
2017-01-12 22:00:42 +00:00
|
|
|
}
|
2018-11-30 22:56:11 +00:00
|
|
|
config.AuthSSLCert = *authCert
|
|
|
|
|
|
|
|
config.VerifyClient, err = parser.GetStringAnnotation("auth-tls-verify-client", ing)
|
|
|
|
if err != nil || !authVerifyClientRegex.MatchString(config.VerifyClient) {
|
|
|
|
config.VerifyClient = defaultAuthVerifyClient
|
|
|
|
}
|
|
|
|
|
|
|
|
config.ValidationDepth, err = parser.GetIntAnnotation("auth-tls-verify-depth", ing)
|
|
|
|
if err != nil || config.ValidationDepth == 0 {
|
|
|
|
config.ValidationDepth = defaultAuthTLSDepth
|
|
|
|
}
|
2017-01-12 22:00:42 +00:00
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
config.ErrorPage, err = parser.GetStringAnnotation("auth-tls-error-page", ing)
|
2018-12-02 18:35:12 +00:00
|
|
|
if err != nil {
|
2018-11-30 22:56:11 +00:00
|
|
|
config.ErrorPage = ""
|
2017-09-03 20:12:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
config.PassCertToUpstream, err = parser.GetBoolAnnotation("auth-tls-pass-certificate-to-upstream", ing)
|
2017-11-18 00:28:45 +00:00
|
|
|
if err != nil {
|
2018-11-30 22:56:11 +00:00
|
|
|
config.PassCertToUpstream = false
|
2017-11-18 00:28:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 19:59:10 +00:00
|
|
|
config.MatchCN, err = parser.GetStringAnnotation("auth-tls-match-cn", ing)
|
|
|
|
if err != nil || !commonNameRegex.MatchString(config.MatchCN) {
|
|
|
|
config.MatchCN = ""
|
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
return config, nil
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|