7
7
"crypto/rsa"
8
8
"crypto/x509"
9
9
"encoding/pem"
10
+ "errors"
10
11
"fmt"
11
12
"log"
12
13
"net"
@@ -16,24 +17,24 @@ import (
16
17
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
17
18
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
18
19
corev1 "k8s.io/api/core/v1"
19
- "k8s.io/apimachinery/pkg/api/errors"
20
+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
20
21
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
22
"k8s.io/apimachinery/pkg/runtime"
22
23
"sigs.k8s.io/controller-runtime/pkg/client"
23
24
24
25
interfaces "go.etcd.io/etcd-operator/pkg/certificate/interfaces"
25
26
)
26
27
27
- type CertManagerProvider struct {
28
+ type Provider struct {
28
29
client.Client
29
30
Scheme * runtime.Scheme
30
31
}
31
32
32
- func New () * CertManagerProvider {
33
- return & CertManagerProvider {}
33
+ func New () * Provider {
34
+ return & Provider {}
34
35
}
35
36
36
- func (cm * CertManagerProvider ) createCertificate (
37
+ func (cm * Provider ) createCertificate (
37
38
ctx context.Context ,
38
39
secretName string ,
39
40
namespace string ,
@@ -56,20 +57,15 @@ func (cm *CertManagerProvider) createCertificate(
56
57
},
57
58
}
58
59
59
- createErr := cm .Create (ctx , certificateResource )
60
- if createErr != nil {
61
- return createErr
62
- }
63
-
64
- return nil
60
+ return cm .Create (ctx , certificateResource )
65
61
}
66
62
67
63
// parsePrivateKey parses the private key from the PEM-encoded data.
68
64
func parsePrivateKey (privateKeyData []byte ) (crypto.PrivateKey , error ) {
69
65
// Try to parse the private key in the format it might be provided in (e.g., PKCS#8, PEM)
70
66
block , _ := pem .Decode (privateKeyData )
71
67
if block == nil {
72
- return nil , fmt . Errorf ("failed to decode private key: invalid PEM" )
68
+ return nil , errors . New ("failed to decode private key: invalid PEM" )
73
69
}
74
70
75
71
// Parse the private key from the PEM block
@@ -78,7 +74,7 @@ func parsePrivateKey(privateKeyData []byte) (crypto.PrivateKey, error) {
78
74
// Try to parse the private key in another format (e.g., RSA)
79
75
privateKey , err = x509 .ParsePKCS1PrivateKey (block .Bytes )
80
76
if err != nil {
81
- return nil , fmt .Errorf ("failed to parse private key: %v " , err )
77
+ return nil , fmt .Errorf ("failed to parse private key: %w " , err )
82
78
}
83
79
}
84
80
@@ -92,13 +88,13 @@ func checkKeyPair(cert *x509.Certificate, privateKey crypto.PrivateKey) error {
92
88
// Check if the private key matches the certificate by validating the public key
93
89
pub := cert .PublicKey .(* rsa.PublicKey )
94
90
if ! key .PublicKey .Equal (pub ) {
95
- return fmt . Errorf ("private key does not match the public key in the certificate" )
91
+ return errors . New ("private key does not match the public key in the certificate" )
96
92
}
97
93
case * ecdsa.PrivateKey :
98
94
// Check if the private key matches the certificate by validating the public key
99
95
pub := cert .PublicKey .(* ecdsa.PublicKey )
100
96
if ! key .PublicKey .Equal (pub ) {
101
- return fmt . Errorf ("private key does not match the public key in the certificate" )
97
+ return errors . New ("private key does not match the public key in the certificate" )
102
98
}
103
99
default :
104
100
return fmt .Errorf ("unsupported private key type: %T" , key )
@@ -107,89 +103,94 @@ func checkKeyPair(cert *x509.Certificate, privateKey crypto.PrivateKey) error {
107
103
return nil
108
104
}
109
105
110
- func (cm * CertManagerProvider ) EnsureCertificateSecret (
106
+ func (cm * Provider ) EnsureCertificateSecret (
111
107
ctx context.Context ,
112
108
secretName string ,
113
109
namespace string ,
114
110
cfg * interfaces.Config ) error {
115
111
issuerName , found := cfg .ExtraConfig ["issuerName" ].(string )
116
112
if ! found || len (strings .TrimSpace (issuerName )) == 0 {
117
- return fmt . Errorf ("issuerName is missing in ExtraConfig" )
113
+ return errors . New ("issuerName is missing in ExtraConfig" )
118
114
}
119
115
issuerKind , found := cfg .ExtraConfig ["issuerKind" ].(string )
120
116
if ! found || len (strings .TrimSpace (issuerKind )) == 0 {
121
- return fmt . Errorf ("issuerKind is missing in ExtraConfig" )
117
+ return errors . New ("issuerKind is missing in ExtraConfig" )
122
118
}
123
119
124
120
checkCertSecret , valErr := cm .ValidateCertificateSecret (ctx , secretName , namespace , cfg )
121
+ if valErr != nil {
122
+ return fmt .Errorf ("invalid certificate secret: %s present in namespace: %s, please delete and try again" ,
123
+ secretName , namespace )
124
+ }
125
125
if checkCertSecret {
126
- return fmt .Errorf ("certificate secret: %s already present in namespace: %s, please delete and try again " ,
126
+ return fmt .Errorf ("valid certificate secret: %s already present in namespace: %s , skipping Certificate creation " ,
127
127
secretName , namespace )
128
128
}
129
- log .Println (valErr )
129
+ log .Printf ("certificate secret: %s not present in namespace: %s , creating new Certificate" ,
130
+ secretName , namespace )
130
131
131
132
return cm .createCertificate (ctx , secretName , namespace , cfg )
132
133
}
133
134
134
- func (cm * CertManagerProvider ) ValidateCertificateSecret (
135
+ func (cm * Provider ) ValidateCertificateSecret (
135
136
ctx context.Context ,
136
137
secretName string ,
137
138
namespace string ,
138
139
_ * interfaces.Config ) (bool , error ) {
139
140
secret := & corev1.Secret {}
140
141
err := cm .Get (ctx , client.ObjectKey {Name : secretName , Namespace : namespace }, secret )
141
142
if err != nil {
142
- return false , fmt . Errorf ( "failed to get secret: %v" , err )
143
+ return false , nil
143
144
}
144
145
145
146
certificateData , exists := secret .Data ["tls.crt" ]
146
147
if ! exists {
147
- return false , fmt . Errorf ("certificate not found in secret" )
148
+ return false , errors . New ("certificate not found in secret" )
148
149
}
149
150
150
151
privateKeyData , keyExists := secret .Data ["tls.key" ]
151
152
if ! keyExists {
152
- return false , fmt . Errorf ("private key not found in secret" )
153
+ return false , errors . New ("private key not found in secret" )
153
154
}
154
155
155
156
parseCert , err := x509 .ParseCertificate (certificateData )
156
157
if err != nil {
157
- return false , fmt .Errorf ("failed to parse certificate: %v " , err )
158
+ return false , fmt .Errorf ("failed to parse certificate: %w " , err )
158
159
}
159
160
160
161
if parseCert .NotAfter .Before (time .Now ()) {
161
- return false , fmt . Errorf ("certificate has expired" )
162
+ return false , errors . New ("certificate has expired" )
162
163
}
163
164
164
165
privateKey , err := parsePrivateKey (privateKeyData )
165
166
if err != nil {
166
- return false , fmt .Errorf ("failed to parse private key: %v " , err )
167
+ return false , fmt .Errorf ("failed to parse private key: %w " , err )
167
168
}
168
169
169
- if err := checkKeyPair (parseCert , privateKey ); err != nil {
170
- return false , fmt .Errorf ("private key does not match certificate: %v " , err )
170
+ if checkKeyPairErr := checkKeyPair (parseCert , privateKey ); checkKeyPairErr != nil {
171
+ return false , fmt .Errorf ("private key does not match certificate: %w " , checkKeyPairErr )
171
172
}
172
173
173
174
return true , nil
174
175
}
175
176
176
- func (cm * CertManagerProvider ) DeleteCertificateSecret (ctx context.Context , secretName string , namespace string ) error {
177
+ func (cm * Provider ) DeleteCertificateSecret (ctx context.Context , secretName string , namespace string ) error {
177
178
secret := & corev1.Secret {}
178
179
err := cm .Get (ctx , client.ObjectKey {Name : secretName , Namespace : namespace }, secret )
179
180
if err != nil {
180
- return fmt .Errorf ("failed to get secret: %v " , err )
181
+ return fmt .Errorf ("failed to get secret: %w " , err )
181
182
}
182
183
183
184
// Delete the Secret
184
185
err = cm .Delete (ctx , secret )
185
186
if err != nil {
186
- return fmt .Errorf ("failed to delete secret: %v " , err )
187
+ return fmt .Errorf ("failed to delete secret: %w " , err )
187
188
}
188
189
189
190
return nil
190
191
}
191
192
192
- func (cm * CertManagerProvider ) RevokeCertificate (ctx context.Context , secretName string , namespace string ) error {
193
+ func (cm * Provider ) RevokeCertificate (ctx context.Context , secretName string , namespace string ) error {
193
194
cmCertificate := & certmanagerv1.Certificate {}
194
195
getCertificateErr := cm .Get (ctx , client.ObjectKey {Name : secretName , Namespace : namespace }, cmCertificate )
195
196
if getCertificateErr != nil {
@@ -206,7 +207,7 @@ func (cm *CertManagerProvider) RevokeCertificate(ctx context.Context, secretName
206
207
// More info: https://cert-manager.io/docs/usage/certificate/#cleaning-up-secrets-when-certificates-are-deleted
207
208
deleteCertificateSecretErr := cm .DeleteCertificateSecret (ctx , secretName , namespace )
208
209
if deleteCertificateSecretErr != nil {
209
- if errors .IsNotFound (deleteCertificateSecretErr ) {
210
+ if k8serrors .IsNotFound (deleteCertificateSecretErr ) {
210
211
fmt .Println ("Certificate secret not found, maybe already deleted" )
211
212
} else {
212
213
return deleteCertificateSecretErr
@@ -217,14 +218,14 @@ func (cm *CertManagerProvider) RevokeCertificate(ctx context.Context, secretName
217
218
return nil
218
219
}
219
220
220
- func (cm * CertManagerProvider ) GetCertificateConfig (
221
+ func (cm * Provider ) GetCertificateConfig (
221
222
ctx context.Context ,
222
223
secretName string ,
223
224
namespace string ) (* interfaces.Config , error ) {
224
225
cmCertificate := & certmanagerv1.Certificate {}
225
226
err := cm .Get (ctx , client.ObjectKey {Name : secretName , Namespace : namespace }, cmCertificate )
226
227
if err != nil {
227
- return nil , fmt .Errorf ("failed to get certificate: %v " , err )
228
+ return nil , fmt .Errorf ("failed to get certificate: %w " , err )
228
229
}
229
230
230
231
cfg := & interfaces.Config {
0 commit comments