|
| 1 | +/* |
| 2 | +Copyright 2023 The cert-manager 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 controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/ecdsa" |
| 22 | + "crypto/elliptic" |
| 23 | + "crypto/rand" |
| 24 | + "crypto/x509" |
| 25 | + "crypto/x509/pkix" |
| 26 | + "encoding/pem" |
| 27 | + "fmt" |
| 28 | + "math/big" |
| 29 | + "time" |
| 30 | + |
| 31 | + ctrl "sigs.k8s.io/controller-runtime" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 33 | + |
| 34 | + "github.com/cert-manager/issuer-lib/api/v1alpha1" |
| 35 | + "github.com/cert-manager/issuer-lib/controllers" |
| 36 | + "github.com/cert-manager/issuer-lib/controllers/signer" |
| 37 | + "github.com/cert-manager/issuer-lib/intree" |
| 38 | +) |
| 39 | + |
| 40 | +// +kubebuilder:rbac:groups=cert-manager.io,resources=certificaterequests,verbs=get;list;watch |
| 41 | +// +kubebuilder:rbac:groups=cert-manager.io,resources=certificaterequests/status,verbs=patch |
| 42 | + |
| 43 | +// +kubebuilder:rbac:groups=certificates.k8s.io,resources=certificatesigningrequests,verbs=get;list;watch |
| 44 | +// +kubebuilder:rbac:groups=certificates.k8s.io,resources=certificatesigningrequests/status,verbs=patch |
| 45 | +// +kubebuilder:rbac:groups=certificates.k8s.io,resources=signers,verbs=sign,resourceNames=issuers.cert-manager.io/*;clusterissuers.cert-manager.io/* |
| 46 | + |
| 47 | +// +kubebuilder:rbac:groups=cert-manager.io,resources=issuers;clusterissuers,verbs=get;list;watch |
| 48 | +// +kubebuilder:rbac:groups=cert-manager.io,resources=issuers/status;clusterissuers/status,verbs=patch |
| 49 | + |
| 50 | +// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch |
| 51 | + |
| 52 | +type Signer struct { |
| 53 | + client client.Client |
| 54 | +} |
| 55 | + |
| 56 | +func (s *Signer) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { |
| 57 | + s.client = mgr.GetClient() |
| 58 | + |
| 59 | + return (&controllers.CombinedController{ |
| 60 | + IssuerTypes: intree.Issuers, |
| 61 | + ClusterIssuerTypes: intree.ClusterIssuers, |
| 62 | + |
| 63 | + FieldOwner: "selfsigned.cert-manager.io", |
| 64 | + MaxRetryDuration: 1 * time.Minute, |
| 65 | + |
| 66 | + // Ignore non self-signing certificate requests |
| 67 | + IgnoreIssuer: s.IgnoreIssuer, |
| 68 | + |
| 69 | + Sign: s.Sign, |
| 70 | + Check: s.Check, |
| 71 | + EventRecorder: mgr.GetEventRecorderFor("selfsigned.cert-manager.io"), |
| 72 | + }).SetupWithManager(ctx, mgr) |
| 73 | +} |
| 74 | + |
| 75 | +func (Signer) IgnoreIssuer(ctx context.Context, issuerObject v1alpha1.Issuer) (bool, error) { |
| 76 | + iss, ok := issuerObject.(intree.CMGenericIssuer) |
| 77 | + if !ok { |
| 78 | + return false, fmt.Errorf("issuer does not implement CMGenericIssuer") |
| 79 | + } |
| 80 | + |
| 81 | + return iss.IssuerSpec().SelfSigned == nil, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (Signer) Check(ctx context.Context, issuerObject v1alpha1.Issuer) error { |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (Signer) Sign(ctx context.Context, cr signer.CertificateRequestObject, issuerObject v1alpha1.Issuer) (signer.PEMBundle, error) { |
| 89 | + // generate random ca private key |
| 90 | + caPrivateKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) |
| 91 | + if err != nil { |
| 92 | + return signer.PEMBundle{}, err |
| 93 | + } |
| 94 | + |
| 95 | + caCRT := &x509.Certificate{ |
| 96 | + SerialNumber: big.NewInt(1), |
| 97 | + Subject: pkix.Name{ |
| 98 | + Organization: []string{"Acme Co"}, |
| 99 | + }, |
| 100 | + NotBefore: time.Now(), |
| 101 | + NotAfter: time.Now().Add(time.Hour * 24 * 180), |
| 102 | + |
| 103 | + KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
| 104 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 105 | + BasicConstraintsValid: true, |
| 106 | + } |
| 107 | + |
| 108 | + // load client certificate request |
| 109 | + certDetails, err := cr.GetCertificateDetails() |
| 110 | + if err != nil { |
| 111 | + return signer.PEMBundle{}, err |
| 112 | + } |
| 113 | + |
| 114 | + clientCRTTemplate, err := certDetails.CertificateTemplate() |
| 115 | + if err != nil { |
| 116 | + return signer.PEMBundle{}, err |
| 117 | + } |
| 118 | + |
| 119 | + // create client certificate from template and CA public key |
| 120 | + clientCRTRaw, err := x509.CreateCertificate(rand.Reader, clientCRTTemplate, caCRT, clientCRTTemplate.PublicKey, caPrivateKey) |
| 121 | + if err != nil { |
| 122 | + panic(err) |
| 123 | + } |
| 124 | + |
| 125 | + clientCrt := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: clientCRTRaw}) |
| 126 | + return signer.PEMBundle{ |
| 127 | + ChainPEM: clientCrt, |
| 128 | + }, nil |
| 129 | +} |
0 commit comments