2021-03-10 18:30:06 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-09-23 13:53:21 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package dynamiccertauthority implements a x509 certificate authority capable of issuing
|
|
|
|
// certificates from a dynamically updating CA keypair.
|
|
|
|
package dynamiccertauthority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"time"
|
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
"k8s.io/apiserver/pkg/server/dynamiccertificates"
|
|
|
|
|
2020-09-23 13:53:21 +00:00
|
|
|
"go.pinniped.dev/internal/certauthority"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CA is a type capable of issuing certificates.
|
|
|
|
type CA struct {
|
2021-03-10 18:30:06 +00:00
|
|
|
provider dynamiccertificates.CertKeyContentProvider
|
2020-09-23 13:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new CA, ready to issue certs whenever the provided provider has a keypair to
|
|
|
|
// provide.
|
2021-03-10 18:30:06 +00:00
|
|
|
func New(provider dynamiccertificates.CertKeyContentProvider) *CA {
|
2020-09-23 13:53:21 +00:00
|
|
|
return &CA{
|
|
|
|
provider: provider,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IssuePEM issues a new server certificate for the given identity and duration, returning it as a
|
2020-10-23 19:34:25 +00:00
|
|
|
// pair of PEM-formatted byte slices for the certificate and private key.
|
2020-09-23 13:53:21 +00:00
|
|
|
func (c *CA) IssuePEM(subject pkix.Name, dnsNames []string, ttl time.Duration) ([]byte, []byte, error) {
|
|
|
|
caCrtPEM, caKeyPEM := c.provider.CurrentCertKeyContent()
|
|
|
|
ca, err := certauthority.Load(string(caCrtPEM), string(caKeyPEM))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ca.IssuePEM(subject, dnsNames, ttl)
|
|
|
|
}
|