Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"sync/atomic"
"time"

"github.com/cert-manager/webhook-cert-lib/internal/pki"
Expand Down Expand Up @@ -100,30 +97,3 @@ func GenerateCA(
_, cert, err := pki.SignCertificate(template, template, pk.Public(), pk)
return cert, pk, err
}

var (
ErrCertNotAvailable = errors.New("no tls.Certificate available")
)

type Holder struct {
certP atomic.Pointer[tls.Certificate]
}

func (h *Holder) GetCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
cert := h.certP.Load()
if cert == nil {
return nil, ErrCertNotAvailable
}
return cert, nil
}

func (h *Holder) SetCertificate(cert *tls.Certificate) {
h.certP.Store(cert)
}

// RenewAfter returns the duration until the certificate should be renewed.
func RenewAfter(cert *x509.Certificate) time.Duration {
lifetime := cert.NotAfter.Sub(cert.NotBefore)
renewTime := cert.NotBefore.Add(lifetime * 2 / 3)
return time.Until(renewTime)
}
43 changes: 43 additions & 0 deletions internal/certificate/holder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2025 The cert-manager 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.
*/

package certificate

import (
"crypto/tls"
"errors"
"sync/atomic"
)

var (
ErrCertNotAvailable = errors.New("no tls.Certificate available")
)

type Holder struct {
certP atomic.Pointer[tls.Certificate]
}

func (h *Holder) GetCertificate(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
cert := h.certP.Load()
if cert == nil {
return nil, ErrCertNotAvailable
}
return cert, nil
}

func (h *Holder) SetCertificate(cert *tls.Certificate) {
h.certP.Store(cert)
}
29 changes: 29 additions & 0 deletions internal/certificate/renew.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2025 The cert-manager 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.
*/

package certificate

import (
"crypto/x509"
"time"
)

// RenewAfter returns the duration until the certificate should be renewed.
func RenewAfter(cert *x509.Certificate) time.Duration {
lifetime := cert.NotAfter.Sub(cert.NotBefore)
renewTime := cert.NotBefore.Add(lifetime * 2 / 3)
return time.Until(renewTime)
}
2 changes: 1 addition & 1 deletion pkg/authority/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/cert-manager/webhook-cert-lib/internal/certificate"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/api"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/certificate"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/injectable"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/authority/ca_secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/cert-manager/webhook-cert-lib/internal/certificate"
"github.com/cert-manager/webhook-cert-lib/internal/pki"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/api"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/certificate"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/internal/ssa"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/authority/leaf_cert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"

"github.com/cert-manager/webhook-cert-lib/internal/certificate"
"github.com/cert-manager/webhook-cert-lib/internal/pki"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/certificate"
)

// LeafCertReconciler reconciles the leaf/serving certificate
Expand Down
2 changes: 1 addition & 1 deletion test/leaf_cert_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"crypto/tls"
"time"

"github.com/cert-manager/webhook-cert-lib/internal/certificate"
"github.com/cert-manager/webhook-cert-lib/internal/pki"
"github.com/cert-manager/webhook-cert-lib/pkg/authority"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/api"
"github.com/cert-manager/webhook-cert-lib/pkg/authority/certificate"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
Expand Down