Skip to content

Commit 94fce87

Browse files
committed
fixup! fixup! fixup! runtime/secrets: add package for consolidated secret handling
improve TLS validation error messages Signed-off-by: cappyzawa <[email protected]>
1 parent 27e72a9 commit 94fce87

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

runtime/secrets/error.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package secrets
1919
import (
2020
"errors"
2121
"fmt"
22+
23+
corev1 "k8s.io/api/core/v1"
2224
)
2325

2426
var (
@@ -38,3 +40,54 @@ func (e *KeyNotFoundError) Error() string {
3840
func (e *KeyNotFoundError) Is(target error) bool {
3941
return errors.Is(target, ErrKeyNotFound)
4042
}
43+
44+
// TLSValidationError represents TLS certificate validation errors.
45+
type TLSValidationError struct {
46+
Type TLSValidationErrorType
47+
}
48+
49+
// TLSValidationErrorType defines the type of TLS validation error.
50+
type TLSValidationErrorType int
51+
52+
const (
53+
// ErrMissingPrivateKey indicates that a certificate exists but the private key is missing.
54+
ErrMissingPrivateKey TLSValidationErrorType = iota
55+
// ErrMissingCertificate indicates that a private key exists but the certificate is missing.
56+
ErrMissingCertificate
57+
// ErrNoCertificatePairOrCA indicates that neither a certificate pair nor a CA certificate is present.
58+
ErrNoCertificatePairOrCA
59+
)
60+
61+
func (e *TLSValidationError) Error() string {
62+
switch e.Type {
63+
case ErrMissingPrivateKey:
64+
return "found certificate but missing private key"
65+
case ErrMissingCertificate:
66+
return "found private key but missing certificate"
67+
case ErrNoCertificatePairOrCA:
68+
return "no CA certificate or client certificate pair found"
69+
default:
70+
return "TLS validation error"
71+
}
72+
}
73+
74+
// enhanceSecretValidationError enhances TLS validation errors with secret reference information.
75+
func enhanceSecretValidationError(err error, secret *corev1.Secret) error {
76+
var tlsErr *TLSValidationError
77+
if !errors.As(err, &tlsErr) {
78+
return err
79+
}
80+
81+
secretRef := fmt.Sprintf("'%s/%s'", secret.Namespace, secret.Name)
82+
83+
switch tlsErr.Type {
84+
case ErrMissingPrivateKey:
85+
return fmt.Errorf("secret %s contains '%s' but missing '%s'", secretRef, TLSCertKey, TLSKeyKey)
86+
case ErrMissingCertificate:
87+
return fmt.Errorf("secret %s contains '%s' but missing '%s'", secretRef, TLSKeyKey, TLSCertKey)
88+
case ErrNoCertificatePairOrCA:
89+
return fmt.Errorf("secret %s must contain either '%s' or both '%s' and '%s'", secretRef, CACertKey, TLSCertKey, TLSKeyKey)
90+
default:
91+
return err
92+
}
93+
}

runtime/secrets/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TLSConfigFromSecret(ctx context.Context, c client.Client, name, namespace s
4747

4848
certData, err := getTLSCertificateData(secret, options.supportDeprecatedFields)
4949
if err != nil {
50-
return nil, err
50+
return nil, enhanceSecretValidationError(err, secret)
5151
}
5252

5353
return buildTLSConfig(certData)

runtime/secrets/reader_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func TestTLSConfigFromSecret(t *testing.T) {
228228
secrets.TLSKeyFileKey: tlsKey,
229229
},
230230
},
231-
errMsg: "no CA certificate or client certificate pair found",
231+
errMsg: "secret 'default/tls-secret' must contain either 'ca.crt' or both 'tls.crt' and 'tls.key'",
232232
},
233233
{
234234
name: "invalid certificate data",
@@ -282,7 +282,7 @@ func TestTLSConfigFromSecret(t *testing.T) {
282282
secrets.TLSCertKey: tlsCert,
283283
},
284284
},
285-
errMsg: "found certificate but missing private key",
285+
errMsg: "secret 'default/tls-secret' contains 'tls.crt' but missing 'tls.key'",
286286
},
287287
{
288288
name: "key without certificate",
@@ -295,7 +295,7 @@ func TestTLSConfigFromSecret(t *testing.T) {
295295
secrets.TLSKeyKey: tlsKey,
296296
},
297297
},
298-
errMsg: "found private key but missing certificate",
298+
errMsg: "secret 'default/tls-secret' contains 'tls.key' but missing 'tls.crt'",
299299
},
300300
{
301301
name: "no certificates at all",
@@ -306,7 +306,7 @@ func TestTLSConfigFromSecret(t *testing.T) {
306306
},
307307
Data: map[string][]byte{},
308308
},
309-
errMsg: "no CA certificate or client certificate pair found",
309+
errMsg: "secret 'default/tls-secret' must contain either 'ca.crt' or both 'tls.crt' and 'tls.key'",
310310
},
311311
}
312312

runtime/secrets/secrets.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ func (t *tlsCertificateData) validate() error {
8282

8383
if hasCert != hasKey {
8484
if hasCert {
85-
return fmt.Errorf("found certificate but missing private key")
85+
return &TLSValidationError{Type: ErrMissingPrivateKey}
8686
}
87-
return fmt.Errorf("found private key but missing certificate")
87+
return &TLSValidationError{Type: ErrMissingCertificate}
8888
}
8989

9090
if !hasCert && !hasCA {
91-
return fmt.Errorf("no CA certificate or client certificate pair found")
91+
return &TLSValidationError{Type: ErrNoCertificatePairOrCA}
9292
}
9393

9494
return nil

0 commit comments

Comments
 (0)