mirror of
https://github.com/wahyd4/cert-manager.git
synced 2026-08-21 11:06:29 +10:00
291 lines
8.6 KiB
Go
291 lines
8.6 KiB
Go
/*
|
|
Copyright 2020 The Jetstack cert-manager contributors.
|
|
|
|
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 issuing
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
apiutil "github.com/jetstack/cert-manager/pkg/api/util"
|
|
cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
|
|
cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
|
|
"github.com/jetstack/cert-manager/pkg/util/pki"
|
|
"github.com/jetstack/cert-manager/test/unit/gen"
|
|
)
|
|
|
|
type cryptoBundle struct {
|
|
// certificate is the Certificate resource used to create this bundle
|
|
certificate *cmapi.Certificate
|
|
// expectedRequestName is the name of the CertificateRequest that is
|
|
// expected to be created to issue this certificate
|
|
expectedRequestName string
|
|
|
|
// privateKey is the private key used as the complement to the certificates
|
|
// in this bundle
|
|
privateKey crypto.Signer
|
|
privateKeyBytes []byte
|
|
|
|
// csr is the CSR used to obtain the certificate in this bundle
|
|
csr *x509.CertificateRequest
|
|
csrBytes []byte
|
|
|
|
// certificateRequest is the request that is expected to be created to
|
|
// obtain a certificate when using this bundle
|
|
certificateRequest *cmapi.CertificateRequest
|
|
certificateRequestReady *cmapi.CertificateRequest
|
|
certificateRequestFailed *cmapi.CertificateRequest
|
|
certificateRequestFailedInvalidRequest *cmapi.CertificateRequest
|
|
|
|
// cert is a signed certificate
|
|
cert *x509.Certificate
|
|
certBytes []byte
|
|
}
|
|
|
|
func mustCreateCryptoBundle(t *testing.T, crt *cmapi.Certificate) cryptoBundle {
|
|
c, err := createCryptoBundle(crt)
|
|
if err != nil {
|
|
t.Fatalf("error generating crypto bundle: %v", err)
|
|
}
|
|
return *c
|
|
}
|
|
|
|
func createCryptoBundle(crt *cmapi.Certificate) (*cryptoBundle, error) {
|
|
reqName, err := apiutil.ComputeCertificateRequestName(crt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
privateKey, err := pki.GeneratePrivateKeyForCertificate(crt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
privateKeyBytes, err := pki.EncodePrivateKey(privateKey, crt.Spec.KeyEncoding)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
csrPEM, err := generateCSRImpl(crt, privateKeyBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
csr, err := pki.DecodeX509CertificateRequestBytes(csrPEM)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
annotations := make(map[string]string)
|
|
for k, v := range crt.Annotations {
|
|
annotations[k] = v
|
|
}
|
|
if crt.Status.Revision != nil {
|
|
annotations[cmapi.CertificateRequestRevisionAnnotationKey] = fmt.Sprintf("%d", crt.Status.Revision)
|
|
}
|
|
|
|
annotations[cmapi.CRPrivateKeyAnnotationKey] = crt.Spec.SecretName
|
|
annotations[cmapi.CertificateNameKey] = crt.Name
|
|
certificateRequest := &cmapi.CertificateRequest{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: reqName,
|
|
Namespace: crt.Namespace,
|
|
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(crt, certificateGvk)},
|
|
Annotations: annotations,
|
|
},
|
|
Spec: cmapi.CertificateRequestSpec{
|
|
CSRPEM: csrPEM,
|
|
Duration: crt.Spec.Duration,
|
|
IssuerRef: crt.Spec.IssuerRef,
|
|
IsCA: crt.Spec.IsCA,
|
|
},
|
|
}
|
|
|
|
unsignedCert, err := pki.GenerateTemplateFromCertificateRequest(certificateRequest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
certBytes, cert, err := pki.SignCertificate(unsignedCert, unsignedCert, privateKey.Public(), privateKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
certificateRequestReady := gen.CertificateRequestFrom(certificateRequest,
|
|
gen.SetCertificateRequestCertificate(certBytes),
|
|
gen.SetCertificateRequestStatusCondition(cmapi.CertificateRequestCondition{
|
|
Type: cmapi.CertificateRequestConditionReady,
|
|
Status: cmmeta.ConditionTrue,
|
|
Reason: cmapi.CertificateRequestReasonIssued,
|
|
}),
|
|
)
|
|
|
|
certificateRequestFailed := gen.CertificateRequestFrom(certificateRequest,
|
|
gen.SetCertificateRequestStatusCondition(cmapi.CertificateRequestCondition{
|
|
Type: cmapi.CertificateRequestConditionReady,
|
|
Status: cmmeta.ConditionFalse,
|
|
Reason: cmapi.CertificateRequestReasonFailed,
|
|
}),
|
|
)
|
|
|
|
certificateRequestFailedInvalidRequest := gen.CertificateRequestFrom(certificateRequestFailed,
|
|
gen.SetCertificateRequestStatusCondition(cmapi.CertificateRequestCondition{
|
|
Type: cmapi.CertificateRequestConditionInvalidRequest,
|
|
Status: cmmeta.ConditionTrue,
|
|
Reason: cmapi.CertificateRequestReasonFailed,
|
|
}),
|
|
)
|
|
|
|
return &cryptoBundle{
|
|
certificate: crt,
|
|
expectedRequestName: reqName,
|
|
privateKey: privateKey,
|
|
privateKeyBytes: privateKeyBytes,
|
|
csr: csr,
|
|
csrBytes: csrPEM,
|
|
certificateRequest: certificateRequest,
|
|
certificateRequestReady: certificateRequestReady,
|
|
certificateRequestFailed: certificateRequestFailed,
|
|
certificateRequestFailedInvalidRequest: certificateRequestFailedInvalidRequest,
|
|
cert: cert,
|
|
certBytes: certBytes,
|
|
}, nil
|
|
}
|
|
|
|
func (c *cryptoBundle) generateTestCSR(crt *cmapi.Certificate) []byte {
|
|
csrPEM, err := generateCSRImpl(crt, c.privateKeyBytes)
|
|
if err != nil {
|
|
panic("failed to generate test fixture: " + err.Error())
|
|
}
|
|
|
|
return csrPEM
|
|
}
|
|
|
|
func (c *cryptoBundle) generateTestCertificate(crt *cmapi.Certificate, notBefore *time.Time) []byte {
|
|
csr := c.generateTestCSR(crt)
|
|
certificateRequest := &cmapi.CertificateRequest{
|
|
Spec: cmapi.CertificateRequestSpec{
|
|
CSRPEM: csr,
|
|
Duration: crt.Spec.Duration,
|
|
IssuerRef: crt.Spec.IssuerRef,
|
|
IsCA: crt.Spec.IsCA,
|
|
},
|
|
}
|
|
|
|
unsignedCert, err := pki.GenerateTemplateFromCertificateRequest(certificateRequest)
|
|
if err != nil {
|
|
panic("failed to generate test fixture: " + err.Error())
|
|
}
|
|
|
|
if notBefore != nil {
|
|
unsignedCert.NotBefore = *notBefore
|
|
}
|
|
|
|
certBytes, _, err := pki.SignCertificate(unsignedCert, unsignedCert, c.privateKey.Public(), c.privateKey)
|
|
if err != nil {
|
|
panic("failed to generate test fixture: " + err.Error())
|
|
}
|
|
|
|
return certBytes
|
|
}
|
|
|
|
func (c *cryptoBundle) generateCertificateExpiring1H(crt *cmapi.Certificate) []byte {
|
|
csr := c.generateTestCSR(crt)
|
|
certificateRequest := &cmapi.CertificateRequest{
|
|
Spec: cmapi.CertificateRequestSpec{
|
|
CSRPEM: csr,
|
|
Duration: crt.Spec.Duration,
|
|
IssuerRef: crt.Spec.IssuerRef,
|
|
IsCA: crt.Spec.IsCA,
|
|
},
|
|
}
|
|
|
|
unsignedCert, err := pki.GenerateTemplateFromCertificateRequest(certificateRequest)
|
|
if err != nil {
|
|
panic("failed to generate test fixture: " + err.Error())
|
|
}
|
|
|
|
nowTime := fixedClock.Now()
|
|
duration := unsignedCert.NotAfter.Sub(unsignedCert.NotBefore)
|
|
unsignedCert.NotBefore = nowTime.Add(time.Hour).Add(-1 * duration)
|
|
unsignedCert.NotAfter = nowTime.Add(time.Hour)
|
|
|
|
certBytes, _, err := pki.SignCertificate(unsignedCert, unsignedCert, c.privateKey.Public(), c.privateKey)
|
|
if err != nil {
|
|
panic("failed to generate test fixture: " + err.Error())
|
|
}
|
|
|
|
return certBytes
|
|
}
|
|
|
|
func (c *cryptoBundle) generateCertificateExpired(crt *cmapi.Certificate) []byte {
|
|
csr := c.generateTestCSR(crt)
|
|
certificateRequest := &cmapi.CertificateRequest{
|
|
Spec: cmapi.CertificateRequestSpec{
|
|
CSRPEM: csr,
|
|
Duration: crt.Spec.Duration,
|
|
IssuerRef: crt.Spec.IssuerRef,
|
|
IsCA: crt.Spec.IsCA,
|
|
},
|
|
}
|
|
|
|
unsignedCert, err := pki.GenerateTemplateFromCertificateRequest(certificateRequest)
|
|
if err != nil {
|
|
panic("failed to generate test fixture: " + err.Error())
|
|
}
|
|
|
|
nowTime := fixedClock.Now()
|
|
duration := unsignedCert.NotAfter.Sub(unsignedCert.NotBefore)
|
|
unsignedCert.NotBefore = nowTime.Add(-1 * time.Hour).Add(-1 * duration)
|
|
unsignedCert.NotAfter = nowTime.Add(-1 * time.Hour)
|
|
|
|
certBytes, _, err := pki.SignCertificate(unsignedCert, unsignedCert, c.privateKey.Public(), c.privateKey)
|
|
if err != nil {
|
|
panic("failed to generate test fixture: " + err.Error())
|
|
}
|
|
|
|
return certBytes
|
|
}
|
|
|
|
func generateCSRImpl(crt *cmapi.Certificate, pk []byte) ([]byte, error) {
|
|
csr, err := pki.GenerateCSR(crt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
signer, err := pki.DecodePrivateKeyBytes(pk)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
csrDER, err := pki.EncodeCSR(csr, signer)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
csrPEM := pem.EncodeToMemory(&pem.Block{
|
|
Type: "CERTIFICATE REQUEST", Bytes: csrDER,
|
|
})
|
|
|
|
return csrPEM, nil
|
|
}
|