Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check data field for user provided tls #8200

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions pkg/controller/plan/tls_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ func CheckTLSSecretRef(ctx context.Context, cli client.Reader, namespace string,
if err := cli.Get(ctx, types.NamespacedName{Namespace: namespace, Name: secretRef.Name}, secret); err != nil {
return err
}
if secret.StringData == nil {
if secret.StringData == nil && secret.Data == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only needs to check the data field.

Copy link
Contributor Author

@loomts loomts Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return errors.New("tls secret's data field shouldn't be nil")
}
keys := []string{secretRef.CA, secretRef.Cert, secretRef.Key}
for _, key := range keys {
if _, ok := secret.StringData[key]; !ok {
if (secret.StringData != nil && secret.StringData[key] == "") || (secret.Data != nil && secret.Data[key] == nil) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check len(secret.Data[key]) == 0 instead, the value can be an empty string.

kubectl create secret generic test-empty-value \
    --from-literal=key=''

kubectl get secret test-empty-value -o yaml
>
apiVersion: v1
data:
  key: ""
kind: Secret
metadata:
  creationTimestamp: "2024-09-25T03:34:43Z"
  name: test-empty-value
  namespace: default
  resourceVersion: "2309582"
  uid: ff72e8d8-cbb2-4f7b-aac8-f5b6fd97b0b1
type: Opaque

return errors.Errorf("tls secret's data[%s] field shouldn't be empty", key)
}
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/controller/plan/tls_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var _ = Describe("TLSUtilsTest", func() {
Expect(err).ShouldNot(BeNil())
Expect(err.Error()).Should(ContainSubstring(secretRef.CA))

By("set everything ok")
By("stringData field is ok")
k8sMock.EXPECT().
Get(gomock.Any(), gomock.Any(), &corev1.Secret{}, gomock.Any()).
DoAndReturn(func(_ context.Context, objKey client.ObjectKey, obj *corev1.Secret, _ ...client.GetOption) error {
Expand All @@ -130,6 +130,22 @@ var _ = Describe("TLSUtilsTest", func() {
return nil
}).Times(1)
Expect(CheckTLSSecretRef(ctx, k8sMock, namespace, secretRef)).Should(Succeed())

By("data field is ok")
k8sMock.EXPECT().
Get(gomock.Any(), gomock.Any(), &corev1.Secret{}, gomock.Any()).
DoAndReturn(func(_ context.Context, objKey client.ObjectKey, obj *corev1.Secret, _ ...client.GetOption) error {
Expect(obj).ShouldNot(BeNil())
obj.Namespace = objKey.Namespace
obj.Name = objKey.Name
obj.Data = map[string][]byte{
secretRef.Cert: []byte("foo"),
secretRef.Key: []byte("bar"),
secretRef.CA: []byte("ca"),
}
return nil
}).Times(1)
Expect(CheckTLSSecretRef(ctx, k8sMock, namespace, secretRef)).Should(Succeed())
})

Context("GetTLSKeyWord function", func() {
Expand Down
Loading