Skip to content

Commit 56b6e58

Browse files
authored
Merge pull request #1 from cert-manager/initial-version
WIP: Initial MVP
2 parents d3beef0 + 0263c53 commit 56b6e58

26 files changed

+2579
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# webhook-cert-lib
1+
# cert-manager webhook-cert-lib
2+
3+
Note: This project is under development and will be changed without notice.
4+
Do NOT use in production!

TODO

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MUST:
2+
- make sure to re-reconcile certificates before they expire
3+
- Scope down controller RBAC to single CA Secret resource.
4+
- Use cli flags in the program to list the injectables, allowing use to scope down the injectable RBAC.
5+
6+
CONSIDER:
7+
- maybe remove dependency on controller-runtime (use client-go directly instead)
8+
- maybe support all controller-runtime Server types: webhook and metrics (might require rebranding)
9+
- Can we make the solution leader-election-less? Does it make sense that we use the existing cr leader election or should we create a separate leader-election just for the logic in this library?

go.mod

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module github.com/cert-manager/webhook-cert-lib
2+
3+
go 1.23.0
4+
5+
toolchain go1.24.2
6+
7+
require (
8+
github.com/go-logr/logr v1.4.2
9+
k8s.io/api v0.32.3
10+
k8s.io/apimachinery v0.32.3
11+
k8s.io/client-go v0.32.3
12+
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e
13+
sigs.k8s.io/controller-runtime v0.20.4
14+
)
15+
16+
require (
17+
github.com/beorn7/perks v1.0.1 // indirect
18+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
19+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
20+
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
21+
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
22+
github.com/fsnotify/fsnotify v1.9.0 // indirect
23+
github.com/fxamacker/cbor/v2 v2.8.0 // indirect
24+
github.com/go-openapi/jsonpointer v0.21.1 // indirect
25+
github.com/go-openapi/jsonreference v0.21.0 // indirect
26+
github.com/go-openapi/swag v0.23.1 // indirect
27+
github.com/gogo/protobuf v1.3.2 // indirect
28+
github.com/golang/protobuf v1.5.4 // indirect
29+
github.com/google/btree v1.1.3 // indirect
30+
github.com/google/gnostic-models v0.6.9 // indirect
31+
github.com/google/go-cmp v0.7.0 // indirect
32+
github.com/google/gofuzz v1.2.0 // indirect
33+
github.com/google/uuid v1.6.0 // indirect
34+
github.com/josharian/intern v1.0.0 // indirect
35+
github.com/json-iterator/go v1.1.12 // indirect
36+
github.com/mailru/easyjson v0.9.0 // indirect
37+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
38+
github.com/modern-go/reflect2 v1.0.2 // indirect
39+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
40+
github.com/pkg/errors v0.9.1 // indirect
41+
github.com/prometheus/client_golang v1.22.0 // indirect
42+
github.com/prometheus/client_model v0.6.2 // indirect
43+
github.com/prometheus/common v0.63.0 // indirect
44+
github.com/prometheus/procfs v0.16.0 // indirect
45+
github.com/spf13/pflag v1.0.6 // indirect
46+
github.com/x448/float16 v0.8.4 // indirect
47+
golang.org/x/net v0.39.0 // indirect
48+
golang.org/x/oauth2 v0.29.0 // indirect
49+
golang.org/x/sync v0.13.0 // indirect
50+
golang.org/x/sys v0.32.0 // indirect
51+
golang.org/x/term v0.31.0 // indirect
52+
golang.org/x/text v0.24.0 // indirect
53+
golang.org/x/time v0.11.0 // indirect
54+
gomodules.xyz/jsonpatch/v2 v2.5.0 // indirect
55+
google.golang.org/protobuf v1.36.6 // indirect
56+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
57+
gopkg.in/inf.v0 v0.9.1 // indirect
58+
gopkg.in/yaml.v3 v3.0.1 // indirect
59+
k8s.io/apiextensions-apiserver v0.32.3 // indirect
60+
k8s.io/klog/v2 v2.130.1 // indirect
61+
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
62+
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
63+
sigs.k8s.io/randfill v1.0.0 // indirect
64+
sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect
65+
sigs.k8s.io/yaml v1.4.0 // indirect
66+
)

go.sum

Lines changed: 185 additions & 0 deletions
Large diffs are not rendered by default.

internal/pki/cert_pool.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
Copyright 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 pki
18+
19+
import (
20+
"bytes"
21+
"crypto/sha256"
22+
"crypto/x509"
23+
"encoding/pem"
24+
"fmt"
25+
"slices"
26+
"time"
27+
)
28+
29+
// CertPool is a set of certificates.
30+
type CertPool struct {
31+
certificates map[[32]byte]*x509.Certificate
32+
33+
filterExpired bool
34+
}
35+
36+
type Option func(*CertPool)
37+
38+
func WithFilteredExpiredCerts(filterExpired bool) Option {
39+
return func(cp *CertPool) {
40+
cp.filterExpired = filterExpired
41+
}
42+
}
43+
44+
// NewCertPool returns a new, empty CertPool.
45+
// It will deduplicate certificates based on their SHA256 hash.
46+
// Optionally, it can filter out expired certificates.
47+
func NewCertPool(options ...Option) *CertPool {
48+
certPool := &CertPool{
49+
certificates: make(map[[32]byte]*x509.Certificate),
50+
}
51+
52+
for _, option := range options {
53+
option(certPool)
54+
}
55+
56+
return certPool
57+
}
58+
59+
func (cp *CertPool) AddCert(cert *x509.Certificate) bool {
60+
if cert == nil {
61+
panic("adding nil Certificate to CertPool")
62+
}
63+
if cp.filterExpired && time.Now().After(cert.NotAfter) {
64+
return false
65+
}
66+
67+
hash := sha256.Sum256(cert.Raw)
68+
cp.certificates[hash] = cert
69+
return true
70+
}
71+
72+
// AddCertsFromPEM strictly validates a given input PEM bundle to confirm it contains
73+
// only valid CERTIFICATE PEM blocks. If successful, returns the validated PEM blocks with any
74+
// comments or extra data stripped.
75+
//
76+
// This validation is broadly similar to the standard library function
77+
// crypto/x509.CertPool.AppendCertsFromPEM - that is, we decode each PEM block at a time and parse
78+
// it as a certificate.
79+
//
80+
// The difference here is that we want to ensure that the bundle _only_ contains certificates, and
81+
// not just skip over things which aren't certificates.
82+
//
83+
// If, for example, someone accidentally used a combined cert + private key as an input to a trust
84+
// bundle, we wouldn't want to then distribute the private key in the target.
85+
//
86+
// In addition, the standard library AppendCertsFromPEM also silently skips PEM blocks with
87+
// non-empty Headers. We error on such PEM blocks, for the same reason as above; headers could
88+
// contain (accidental) private information. They're also non-standard according to
89+
// https://www.rfc-editor.org/rfc/rfc7468
90+
//
91+
// Additionally, if the input PEM bundle contains no non-expired certificates, an error is returned.
92+
// TODO: Reconsider what should happen if the input only contains expired certificates.
93+
func (cp *CertPool) AddCertsFromPEM(pemData []byte) error {
94+
if pemData == nil {
95+
return fmt.Errorf("certificate data can't be nil")
96+
}
97+
98+
ok := false
99+
for {
100+
var block *pem.Block
101+
block, pemData = pem.Decode(pemData)
102+
103+
if block == nil {
104+
break
105+
}
106+
107+
if block.Type != "CERTIFICATE" {
108+
// only certificates are allowed in a bundle
109+
return fmt.Errorf("invalid PEM block in bundle: only CERTIFICATE blocks are permitted but found '%s'", block.Type)
110+
}
111+
112+
if len(block.Headers) != 0 {
113+
return fmt.Errorf("invalid PEM block in bundle; blocks are not permitted to have PEM headers")
114+
}
115+
116+
certificate, err := x509.ParseCertificate(block.Bytes)
117+
if err != nil {
118+
// the presence of an invalid cert (including things which aren't certs)
119+
// should cause the bundle to be rejected
120+
return fmt.Errorf("invalid PEM block in bundle; invalid PEM certificate: %w", err)
121+
}
122+
123+
if certificate == nil {
124+
return fmt.Errorf("failed appending a certificate: certificate is nil")
125+
}
126+
127+
if cp.AddCert(certificate) {
128+
ok = true // at least one non-expired certificate was found in the input
129+
}
130+
}
131+
132+
if !ok {
133+
return fmt.Errorf("no non-expired certificates found in input bundle")
134+
}
135+
136+
return nil
137+
}
138+
139+
// Get certificates quantity in the certificates pool
140+
func (cp *CertPool) Size() int {
141+
return len(cp.certificates)
142+
}
143+
144+
func (cp *CertPool) PEM() string {
145+
if cp == nil || len(cp.certificates) == 0 {
146+
return ""
147+
}
148+
149+
buffer := bytes.Buffer{}
150+
151+
for _, cert := range cp.Certificates() {
152+
if err := pem.Encode(&buffer, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
153+
return ""
154+
}
155+
}
156+
157+
return string(bytes.TrimSpace(buffer.Bytes()))
158+
}
159+
160+
func (cp *CertPool) PEMSplit() []string {
161+
if cp == nil || len(cp.certificates) == 0 {
162+
return nil
163+
}
164+
165+
pems := make([]string, 0, len(cp.certificates))
166+
for _, cert := range cp.Certificates() {
167+
pems = append(pems, string(bytes.TrimSpace(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}))))
168+
}
169+
170+
return pems
171+
}
172+
173+
// Get the list of all x509 Certificates in the certificates pool
174+
func (cp *CertPool) Certificates() []*x509.Certificate {
175+
hashes := make([][32]byte, 0, len(cp.certificates))
176+
for hash := range cp.certificates {
177+
hashes = append(hashes, hash)
178+
}
179+
180+
slices.SortFunc(hashes, func(i, j [32]byte) int {
181+
return bytes.Compare(i[:], j[:])
182+
})
183+
184+
orderedCertificates := make([]*x509.Certificate, 0, len(cp.certificates))
185+
for _, hash := range hashes {
186+
orderedCertificates = append(orderedCertificates, cp.certificates[hash])
187+
}
188+
189+
return orderedCertificates
190+
}

0 commit comments

Comments
 (0)