Skip to content

Commit f79791f

Browse files
author
Mengqi Yu
committed
implement SelfSignedCertProvisioner
1 parent fea9e3a commit f79791f

File tree

6 files changed

+125
-67
lines changed

6 files changed

+125
-67
lines changed

pkg/webhook/internal/certprovisioner/doc.go renamed to pkg/webhook/certprovisioner/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Package certprovisioner provides an interface and implementation to provision ce
2020
Create a implementation instance of certprovisioner.
2121
2222
cp := SelfSignedCertProvisioner{
23-
// your configuration
23+
CommonName: "foo.bar.com"
2424
}
2525
2626
Provision the certificates.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2018 The Kubernetes 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 certprovisioner
18+
19+
import (
20+
"crypto/x509"
21+
"fmt"
22+
23+
"k8s.io/client-go/util/cert"
24+
)
25+
26+
// SelfSignedCertProvisioner implements the CertProvisioner interface.
27+
// It provisions self-signed certificates.
28+
type SelfSignedCertProvisioner struct {
29+
// Required Common Name
30+
CommonName string
31+
}
32+
33+
var _ CertProvisioner = &SelfSignedCertProvisioner{}
34+
35+
// ProvisionServingCert creates and returns a CA certificate and certificate and
36+
// key for the server. serverKey and serverCert are used by the server
37+
// to establish trust for clients, CA certificate is used by the
38+
// client to verify the server authentication chain.
39+
// The cert will be valid for 365 days.
40+
func (cp *SelfSignedCertProvisioner) ProvisionServingCert() (serverKey, serverCert, caCert []byte, err error) {
41+
signingKey, err := cert.NewPrivateKey()
42+
if err != nil {
43+
return nil, nil, nil,
44+
fmt.Errorf("failed to create the CA private key: %v", err)
45+
}
46+
signingCert, err := cert.NewSelfSignedCACert(cert.Config{CommonName: "webhook-cert-ca"}, signingKey)
47+
if err != nil {
48+
return nil, nil, nil,
49+
fmt.Errorf("failed to create the CA cert: %v", err)
50+
}
51+
key, err := cert.NewPrivateKey()
52+
if err != nil {
53+
return nil, nil, nil,
54+
fmt.Errorf("failed to create the private key: %v", err)
55+
}
56+
signedCert, err := cert.NewSignedCert(
57+
cert.Config{
58+
CommonName: cp.CommonName,
59+
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
60+
},
61+
key, signingCert, signingKey,
62+
)
63+
if err != nil {
64+
return nil, nil, nil,
65+
fmt.Errorf("failed to create the cert: %v", err)
66+
}
67+
return cert.EncodePrivateKeyPEM(key), cert.EncodeCertPEM(signedCert), cert.EncodeCertPEM(signingCert), nil
68+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2018 The Kubernetes 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 certprovisioner
18+
19+
import (
20+
"crypto/x509"
21+
"encoding/pem"
22+
"testing"
23+
)
24+
25+
func TestProvisionServingCert(t *testing.T) {
26+
CN := "mysvc.myns.svc"
27+
cp := SelfSignedCertProvisioner{CommonName: CN}
28+
_, certPEM, caPEM, err := cp.ProvisionServingCert()
29+
30+
// First, create the set of root certificates. For this example we only
31+
// have one. It's also possible to omit this in order to use the
32+
// default root set of the current operating system.
33+
roots := x509.NewCertPool()
34+
ok := roots.AppendCertsFromPEM([]byte(caPEM))
35+
if !ok {
36+
t.Fatalf("failed to parse root certificate: %s", caPEM)
37+
}
38+
39+
block, _ := pem.Decode(certPEM)
40+
if block == nil {
41+
t.Fatalf("failed to parse certificate PEM: %s", certPEM)
42+
}
43+
cert, err := x509.ParseCertificate(block.Bytes)
44+
if err != nil {
45+
t.Fatalf("failed to parse certificate: %v", err)
46+
}
47+
48+
opts := x509.VerifyOptions{
49+
DNSName: CN,
50+
Roots: roots,
51+
}
52+
53+
if _, err := cert.Verify(opts); err != nil {
54+
t.Fatalf("failed to verify certificate: %v", err)
55+
}
56+
}

pkg/webhook/internal/certprovisioner/example_test.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

pkg/webhook/internal/certprovisioner/selfsignedcertprovisioner.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)