Skip to content

Commit

Permalink
Properly initialize the client with a secret
Browse files Browse the repository at this point in the history
Previouly, the client failed to initialize because the token was
not in the correct format.
  • Loading branch information
oz123 committed Feb 21, 2024
1 parent e5c5cb6 commit a40890d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 6 additions & 1 deletion internal/controller/maasvalidator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"context"
"encoding/base64"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -161,7 +162,11 @@ func (r *MaasValidatorReconciler) tokenFromSecret(name, namespace string) (strin
if key, found := secret.Data["MAAS_API_KEY"]; found {
token := string(key)
token = strings.TrimSuffix(token, "\n")
return token, nil
decodedBytes, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return "", fmt.Errorf("failed to decode secret data")

Check warning on line 167 in internal/controller/maasvalidator_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/maasvalidator_controller.go#L167

Added line #L167 was not covered by tests
}
return string(decodedBytes), nil
}
return "", fmt.Errorf("secret does not contain MAAS_API_KEY")
}
2 changes: 0 additions & 2 deletions internal/controller/maasvalidator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ var _ = Describe("MaaSValidator controller", Ordered, func() {
},
}

//secret := &corev1.Secret{}

vr := &vapi.ValidationResult{}
vrKey := types.NamespacedName{Name: validationResultName(val), Namespace: validatorNamespace}

Expand Down
15 changes: 15 additions & 0 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ var _ = BeforeSuite(func() {
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred(), "failed to start MaasValidator controller")

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "maas-api-token",
Namespace: validatorNamespace,
},
StringData: map[string]string{
// there is no such token, this is just a dummy
"MAAS_API_KEY": "LEV2Ulo4THd1eGtVMjJ4dkRjOmpXeXRqS2d5ZFlOVTlxdmZEaDo2VmFzTEhKU3hVaFBOeDdTNVd3N1ZzdVNQakJGc2FVUQo=", //gitleaks:allow
},
Type: corev1.SecretTypeOpaque,
}

err = k8sClient.Create(ctx, secret)
Expect(err).ToNot(HaveOccurred(), "failed to create maas secret")

go func() {
defer GinkgoRecover()
err = k8sManager.Start(ctx)
Expand Down

0 comments on commit a40890d

Please sign in to comment.