2016-11-10 19:56:29 -03: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-01-18 16:14:42 -03:00
|
|
|
package store
|
2016-11-10 19:56:29 -03:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2017-09-17 15:42:31 -03:00
|
|
|
apiv1 "k8s.io/api/core/v1"
|
2021-08-21 17:42:00 -03:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2018-01-19 15:44:31 -03:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2016-11-10 19:56:29 -03:00
|
|
|
|
2022-07-21 21:32:48 -03:00
|
|
|
"k8s.io/ingress-nginx/pkg/apis/ingress"
|
2022-07-20 15:53:44 -03:00
|
|
|
|
|
|
|
|
klog "k8s.io/klog/v2"
|
|
|
|
|
|
2017-11-07 19:02:12 -03:00
|
|
|
"k8s.io/ingress-nginx/internal/net/ssl"
|
2022-07-20 15:53:44 -03:00
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/pkg/util/file"
|
2016-11-10 19:56:29 -03:00
|
|
|
)
|
|
|
|
|
|
2018-07-02 22:59:54 +02:00
|
|
|
// syncSecret synchronizes the content of a TLS Secret (certificate(s), secret
|
|
|
|
|
// key) with the filesystem. The resulting files can be used by NGINX.
|
2018-12-06 21:41:21 +08:00
|
|
|
func (s *k8sStore) syncSecret(key string) {
|
2018-12-06 21:24:41 +08:00
|
|
|
s.syncSecretMu.Lock()
|
|
|
|
|
defer s.syncSecretMu.Unlock()
|
2018-01-18 16:14:42 -03:00
|
|
|
|
2020-09-27 17:32:40 -03:00
|
|
|
klog.V(3).InfoS("Syncing Secret", "name", key)
|
2017-04-09 13:52:10 -03:00
|
|
|
|
2018-01-18 16:14:42 -03:00
|
|
|
cert, err := s.getPemCertificate(key)
|
2017-07-18 14:21:38 -04:00
|
|
|
if err != nil {
|
2018-05-15 08:10:29 -04:00
|
|
|
if !isErrSecretForAuth(err) {
|
2018-12-05 13:27:55 -03:00
|
|
|
klog.Warningf("Error obtaining X.509 certificate: %v", err)
|
2018-05-15 08:10:29 -04:00
|
|
|
}
|
2017-07-18 14:21:38 -04:00
|
|
|
return
|
|
|
|
|
}
|
2016-11-10 19:56:29 -03:00
|
|
|
|
2017-07-18 14:21:38 -04:00
|
|
|
// create certificates and add or update the item in the store
|
2018-04-13 00:26:10 +02:00
|
|
|
cur, err := s.GetLocalSSLCert(key)
|
2018-01-18 16:14:42 -03:00
|
|
|
if err == nil {
|
|
|
|
|
if cur.Equal(cert) {
|
2017-07-18 14:21:38 -04:00
|
|
|
// no need to update
|
|
|
|
|
return
|
2017-03-10 12:34:13 -03:00
|
|
|
}
|
2020-09-27 17:32:40 -03:00
|
|
|
klog.InfoS("Updating secret in local store", "name", key)
|
2018-01-18 16:14:42 -03:00
|
|
|
s.sslStore.Update(key, cert)
|
2017-09-13 11:14:24 -03:00
|
|
|
// this update must trigger an update
|
|
|
|
|
// (like an update event from a change in Ingress)
|
2018-01-18 16:14:42 -03:00
|
|
|
s.sendDummyEvent()
|
2017-07-18 14:21:38 -04:00
|
|
|
return
|
2016-11-10 19:56:29 -03:00
|
|
|
}
|
2017-07-18 14:21:38 -04:00
|
|
|
|
2020-09-27 17:32:40 -03:00
|
|
|
klog.InfoS("Adding secret to local store", "name", key)
|
2018-01-18 16:14:42 -03:00
|
|
|
s.sslStore.Add(key, cert)
|
2017-09-30 21:48:14 -03:00
|
|
|
// this update must trigger an update
|
2017-09-30 20:42:42 -03:00
|
|
|
// (like an update event from a change in Ingress)
|
2018-01-18 16:14:42 -03:00
|
|
|
s.sendDummyEvent()
|
2016-11-10 19:56:29 -03:00
|
|
|
}
|
|
|
|
|
|
2017-02-06 16:16:36 -02:00
|
|
|
// getPemCertificate receives a secret, and creates a ingress.SSLCert as return.
|
|
|
|
|
// It parses the secret and verifies if it's a keypair, or a 'ca.crt' secret only.
|
2018-12-06 21:41:21 +08:00
|
|
|
func (s *k8sStore) getPemCertificate(secretName string) (*ingress.SSLCert, error) {
|
2018-01-18 16:14:42 -03:00
|
|
|
secret, err := s.listers.Secret.ByKey(secretName)
|
2016-11-10 19:56:29 -03:00
|
|
|
if err != nil {
|
2018-07-02 22:59:54 +02:00
|
|
|
return nil, err
|
2016-11-10 19:56:29 -03:00
|
|
|
}
|
|
|
|
|
|
2017-09-17 15:42:31 -03:00
|
|
|
cert, okcert := secret.Data[apiv1.TLSCertKey]
|
|
|
|
|
key, okkey := secret.Data[apiv1.TLSPrivateKeyKey]
|
2016-11-10 19:56:29 -03:00
|
|
|
ca := secret.Data["ca.crt"]
|
|
|
|
|
|
2019-09-03 17:47:28 -03:00
|
|
|
crl := secret.Data["ca.crl"]
|
|
|
|
|
|
2018-05-15 08:10:29 -04:00
|
|
|
auth := secret.Data["auth"]
|
|
|
|
|
|
2017-09-26 22:46:22 -05:00
|
|
|
// namespace/secretName -> namespace-secretName
|
2023-08-31 15:36:48 +08:00
|
|
|
nsSecName := strings.ReplaceAll(secretName, "/", "-")
|
2017-02-06 16:16:36 -02:00
|
|
|
|
2018-01-18 16:14:42 -03:00
|
|
|
var sslCert *ingress.SSLCert
|
2023-08-31 15:36:48 +08:00
|
|
|
switch {
|
|
|
|
|
case okcert && okkey:
|
2017-09-26 22:46:22 -05:00
|
|
|
if cert == nil {
|
2018-07-02 22:59:54 +02:00
|
|
|
return nil, fmt.Errorf("key 'tls.crt' missing from Secret %q", secretName)
|
2017-09-26 22:46:22 -05:00
|
|
|
}
|
2019-08-13 17:14:55 -04:00
|
|
|
|
2017-09-26 22:46:22 -05:00
|
|
|
if key == nil {
|
2018-07-02 22:59:54 +02:00
|
|
|
return nil, fmt.Errorf("key 'tls.key' missing from Secret %q", secretName)
|
2017-09-18 16:38:23 +02:00
|
|
|
}
|
2017-09-26 22:46:22 -05:00
|
|
|
|
2019-08-26 10:58:44 -04:00
|
|
|
sslCert, err = ssl.CreateSSLCert(cert, key, string(secret.UID))
|
2019-03-10 23:25:31 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("unexpected error creating SSL Cert: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 17:14:55 -04:00
|
|
|
if len(ca) > 0 {
|
2019-09-13 09:22:24 -03:00
|
|
|
caCert, err := ssl.CheckCACert(ca)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("parsing CA certificate: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 19:36:25 +02:00
|
|
|
path, err := ssl.StoreSSLCertOnDisk(nsSecName, sslCert)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error while storing certificate and key: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sslCert.PemFileName = path
|
2019-09-13 09:22:24 -03:00
|
|
|
sslCert.CACertificate = caCert
|
2019-08-13 17:14:55 -04:00
|
|
|
sslCert.CAFileName = path
|
|
|
|
|
sslCert.CASHA = file.SHA1(path)
|
|
|
|
|
|
|
|
|
|
err = ssl.ConfigureCACertWithCertAndKey(nsSecName, ca, sslCert)
|
2018-06-04 17:48:30 -04:00
|
|
|
if err != nil {
|
2019-03-10 23:25:31 -04:00
|
|
|
return nil, fmt.Errorf("error configuring CA certificate: %v", err)
|
2018-06-04 17:48:30 -04:00
|
|
|
}
|
2019-09-03 17:47:28 -03:00
|
|
|
|
|
|
|
|
if len(crl) > 0 {
|
|
|
|
|
err = ssl.ConfigureCRL(nsSecName, crl, sslCert)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error configuring CRL certificate: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-18 16:26:28 -04:00
|
|
|
}
|
2017-09-26 22:46:22 -05:00
|
|
|
|
2018-07-02 22:59:54 +02:00
|
|
|
msg := fmt.Sprintf("Configuring Secret %q for TLS encryption (CN: %v)", secretName, sslCert.CN)
|
2017-09-26 22:46:22 -05:00
|
|
|
if ca != nil {
|
2018-07-02 22:59:54 +02:00
|
|
|
msg += " and authentication"
|
2017-09-26 22:46:22 -05:00
|
|
|
}
|
2019-09-03 17:47:28 -03:00
|
|
|
|
|
|
|
|
if crl != nil {
|
|
|
|
|
msg += " and CRL"
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 17:32:40 -03:00
|
|
|
klog.V(3).InfoS(msg)
|
2023-08-31 15:36:48 +08:00
|
|
|
case len(ca) > 0:
|
2019-03-10 23:25:31 -04:00
|
|
|
sslCert, err = ssl.CreateCACert(ca)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("unexpected error creating SSL Cert: %v", err)
|
|
|
|
|
}
|
2017-09-26 22:46:22 -05:00
|
|
|
|
2019-08-13 17:14:55 -04:00
|
|
|
err = ssl.ConfigureCACert(nsSecName, ca, sslCert)
|
2017-07-18 16:26:28 -04:00
|
|
|
if err != nil {
|
2019-03-10 23:25:31 -04:00
|
|
|
return nil, fmt.Errorf("error configuring CA certificate: %v", err)
|
2017-07-18 16:26:28 -04:00
|
|
|
}
|
2017-09-26 22:46:22 -05:00
|
|
|
|
2022-01-09 18:06:00 +01:00
|
|
|
sslCert.CASHA = file.SHA1(sslCert.CAFileName)
|
|
|
|
|
|
2019-09-03 17:47:28 -03:00
|
|
|
if len(crl) > 0 {
|
|
|
|
|
err = ssl.ConfigureCRL(nsSecName, crl, sslCert)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-26 22:46:22 -05:00
|
|
|
// makes this secret in 'syncSecret' to be used for Certificate Authentication
|
|
|
|
|
// this does not enable Certificate Authentication
|
2020-09-27 17:32:40 -03:00
|
|
|
klog.V(3).InfoS("Configuring Secret for TLS authentication", "secret", secretName)
|
2023-08-31 15:36:48 +08:00
|
|
|
default:
|
2018-05-15 08:10:29 -04:00
|
|
|
if auth != nil {
|
|
|
|
|
return nil, ErrSecretForAuth
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 10:09:04 -05:00
|
|
|
return nil, fmt.Errorf("secret %q contains no keypair or CA certificate", secretName)
|
2017-02-06 16:16:36 -02:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 16:14:42 -03:00
|
|
|
sslCert.Name = secret.Name
|
|
|
|
|
sslCert.Namespace = secret.Namespace
|
|
|
|
|
|
2019-08-13 17:14:55 -04:00
|
|
|
// the default SSL certificate needs to be present on disk
|
|
|
|
|
if secretName == s.defaultSSLCertificate {
|
|
|
|
|
path, err := ssl.StoreSSLCertOnDisk(nsSecName, sslCert)
|
|
|
|
|
if err != nil {
|
2022-01-09 21:29:12 -03:00
|
|
|
return nil, fmt.Errorf("storing default SSL Certificate: %w", err)
|
2019-08-13 17:14:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sslCert.PemFileName = path
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 16:14:42 -03:00
|
|
|
return sslCert, nil
|
2016-11-10 19:56:29 -03:00
|
|
|
}
|
|
|
|
|
|
2018-01-19 15:44:31 -03:00
|
|
|
// sendDummyEvent sends a dummy event to trigger an update
|
|
|
|
|
// This is used in when a secret change
|
|
|
|
|
func (s *k8sStore) sendDummyEvent() {
|
2018-02-13 17:46:18 -08:00
|
|
|
s.updateCh.In() <- Event{
|
2018-01-19 15:44:31 -03:00
|
|
|
Type: UpdateEvent,
|
2019-06-09 18:49:59 -04:00
|
|
|
Obj: &networking.Ingress{
|
2018-01-19 15:44:31 -03:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: "dummy",
|
|
|
|
|
Namespace: "dummy",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-15 08:10:29 -04:00
|
|
|
|
|
|
|
|
// ErrSecretForAuth error to indicate a secret is used for authentication
|
2018-08-30 10:09:04 -05:00
|
|
|
var ErrSecretForAuth = fmt.Errorf("secret is used for authentication")
|
2018-05-15 08:10:29 -04:00
|
|
|
|
|
|
|
|
func isErrSecretForAuth(e error) bool {
|
|
|
|
|
return e == ErrSecretForAuth
|
|
|
|
|
}
|