|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package certwriter |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "net/url" |
| 23 | + |
| 24 | + admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + |
| 28 | + "github.com/kubernetes-sigs/controller-runtime/pkg/admission/certgenerator" |
| 29 | + "github.com/kubernetes-sigs/controller-runtime/pkg/client" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + // CACertName is the name of the CA certificate |
| 34 | + CACertName = "ca-cert.pem" |
| 35 | + // ServerKeyName is the name of the server private key |
| 36 | + ServerKeyName = "key.pem" |
| 37 | + // ServerCertName is the name of the serving certificate |
| 38 | + ServerCertName = "cert.pem" |
| 39 | +) |
| 40 | + |
| 41 | +// Options are options for configuring a CertOutput. |
| 42 | +type Options struct { |
| 43 | + Client client.Client |
| 44 | + CertGenerator certgenerator.CertGenerator |
| 45 | +} |
| 46 | + |
| 47 | +// CertWriter provides method to handle webhooks. |
| 48 | +type CertWriter interface { |
| 49 | + // Create a new CertWriter with the given runtime.Object |
| 50 | + New(runtime.Object) (CertWriter, error) |
| 51 | + // Handle ensures that the webhooks it manages have the right certificates. |
| 52 | + Handle() error |
| 53 | +} |
| 54 | + |
| 55 | +// New builds a new CertWriter for the provided webhook configuration. |
| 56 | +func New(webhookConfig runtime.Object, ops Options) (CertWriter, error) { |
| 57 | + if ops.CertGenerator == nil { |
| 58 | + ops.CertGenerator = &certgenerator.SelfSignedCertGenerator{} |
| 59 | + } |
| 60 | + if ops.Client == nil { |
| 61 | + // TODO: default the client |
| 62 | + } |
| 63 | + s := &SecretCertWriter{ |
| 64 | + Client: ops.Client, |
| 65 | + CertGenerator: ops.CertGenerator, |
| 66 | + } |
| 67 | + sWriter, err := s.New(webhookConfig) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + f := &FSCertWriter{ |
| 73 | + CertGenerator: ops.CertGenerator, |
| 74 | + } |
| 75 | + fWriter, err := f.New(webhookConfig) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + return &MultiCertWriter{ |
| 81 | + CertWriters: []CertWriter{sWriter, fWriter}, |
| 82 | + }, nil |
| 83 | +} |
| 84 | + |
| 85 | +func handleCommon(webhook *admissionregistrationv1beta1.Webhook, ch certio) error { |
| 86 | + // This happens when a webhook name is specified in the annotation, but this webhook doesn't exist. |
| 87 | + if webhook == nil { |
| 88 | + // TODO: add a log here if useful. |
| 89 | + return nil |
| 90 | + } |
| 91 | + webhookName := webhook.Name |
| 92 | + |
| 93 | + certs, err := ch.read(webhookName) |
| 94 | + if apierrors.IsNotFound(err) { |
| 95 | + certs, err = ch.write(webhookName) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + } else if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + // Recreate the cert if it's invalid. |
| 104 | + if !validCert(certs) { |
| 105 | + certs, err = ch.write(webhookName) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Ensure the CA bundle in the webhook configuration has the signing CA. |
| 115 | + caBundle := webhook.ClientConfig.CABundle |
| 116 | + caCert := certs.CACert |
| 117 | + if !bytes.Contains(caBundle, caCert) { |
| 118 | + webhook.ClientConfig.CABundle = append(caBundle, caCert...) |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// certio provides methods for handling certificates for webhook. |
| 124 | +type certio interface { |
| 125 | + // read reads a wehbook name and returns the certs for it. |
| 126 | + read(webhookName string) (*certgenerator.Certs, error) |
| 127 | + // write writes the certs and return the certs it wrote. |
| 128 | + write(webhookName string) (*certgenerator.Certs, error) |
| 129 | +} |
| 130 | + |
| 131 | +func validCert(certs *certgenerator.Certs) bool { |
| 132 | + // TODO: 1) validate the key and the cert are valid pair e.g. call crypto/tls.X509KeyPair() |
| 133 | + // 2) validate the cert with the CA cert |
| 134 | + // 3) validate the cert is for a certain DNSName |
| 135 | + // e.g. |
| 136 | + // c, err := tls.X509KeyPair(cert, key) |
| 137 | + // err := c.Verify(options) |
| 138 | + |
| 139 | + return true |
| 140 | +} |
| 141 | + |
| 142 | +func getWebhooksFromObject(obj runtime.Object) ([]admissionregistrationv1beta1.Webhook, error) { |
| 143 | + switch typed := obj.(type) { |
| 144 | + case *admissionregistrationv1beta1.MutatingWebhookConfiguration: |
| 145 | + return typed.Webhooks, nil |
| 146 | + case *admissionregistrationv1beta1.ValidatingWebhookConfiguration: |
| 147 | + return typed.Webhooks, nil |
| 148 | + //case *unstructured.Unstructured: |
| 149 | + // TODO: implement this if needed |
| 150 | + default: |
| 151 | + return nil, fmt.Errorf("unsupported type: %T, only support v1beta1.MutatingWebhookConfiguration and v1beta1.ValidatingWebhookConfiguration", typed) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func webhookClientConfigToCommonName(config *admissionregistrationv1beta1.WebhookClientConfig) (string, error) { |
| 156 | + if config.Service != nil && config.URL != nil { |
| 157 | + return "", fmt.Errorf("service and URL can't be set at the same time in a webhook: %#v", config) |
| 158 | + } |
| 159 | + if config.Service == nil && config.URL == nil { |
| 160 | + return "", fmt.Errorf("one of service and URL need to be set in a webhook: %#v", config) |
| 161 | + } |
| 162 | + if config.Service != nil { |
| 163 | + return certgenerator.ServiceToCommonName(config.Service.Namespace, config.Service.Name), nil |
| 164 | + } |
| 165 | + if config.URL != nil { |
| 166 | + u, err := url.Parse(*config.URL) |
| 167 | + return u.Host, err |
| 168 | + } |
| 169 | + return "", nil |
| 170 | +} |
0 commit comments