credential.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2026 LocoStack.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package reconciler
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/LocoStack/loco-operator/api/v1alpha1"
  18. corev1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/types"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. logf "sigs.k8s.io/controller-runtime/pkg/log"
  25. )
  26. // reconcileCredentials checks that all referenced Secrets exist and contain the
  27. // required keys. Returns true when all credentials are resolved.
  28. func ReconcileCredentials(ctx context.Context, c client.Client, scheme *runtime.Scheme, authSpec *v1alpha1.AuthSpec, obj client.Object) (*metav1.Condition, error) {
  29. log := logf.FromContext(ctx)
  30. if authSpec == nil {
  31. return &metav1.Condition{
  32. Type: "CredentialsResolved",
  33. Status: metav1.ConditionTrue,
  34. Reason: "NoCredentialsRequired",
  35. Message: "No auth configured",
  36. ObservedGeneration: obj.GetGeneration(),
  37. }, nil
  38. }
  39. secretRefs := make([]*corev1.SecretKeySelector, 0)
  40. if authSpec.APIKey != nil {
  41. secretRefs = append(secretRefs, &authSpec.APIKey.SecretRef)
  42. }
  43. if authSpec.BearerToken != nil {
  44. secretRefs = append(secretRefs, authSpec.BearerToken)
  45. }
  46. for _, hdr := range authSpec.Headers {
  47. if hdr.ValueFrom.SecretKeyRef != nil {
  48. secretRefs = append(secretRefs, hdr.ValueFrom.SecretKeyRef)
  49. }
  50. }
  51. for _, ref := range secretRefs {
  52. if reason, msg, err := checkSecretKey(ctx, c, obj.GetNamespace(), ref.Name, ref.Key); err != nil {
  53. log.Error(err, "Failed to check API key secret", "namespace", obj.GetNamespace(), "name", obj.GetName(), "secretName", ref.Name, "secretKey", ref.Key)
  54. return nil, err
  55. } else {
  56. if reason != "" {
  57. return &metav1.Condition{
  58. Type: "CredentialsResolved",
  59. Status: metav1.ConditionTrue,
  60. Reason: reason,
  61. Message: msg,
  62. }, nil
  63. }
  64. }
  65. }
  66. return &metav1.Condition{
  67. Type: "CredentialsResolved",
  68. Status: metav1.ConditionTrue,
  69. Reason: "CredentialsFound",
  70. Message: "All referenced secrets exist and contain the required keys",
  71. }, nil
  72. }
  73. // checkSecretKey verifies that a Secret exists and contains the given key.
  74. func checkSecretKey(ctx context.Context, c client.Client, namespace, secretName, key string) (string, string, error) {
  75. secret := &corev1.Secret{}
  76. if err := c.Get(ctx, types.NamespacedName{Name: secretName, Namespace: namespace}, secret); err != nil {
  77. if apierrors.IsNotFound(err) {
  78. return "SecretNotFound", fmt.Sprintf("Secret %q not found", secretName), nil
  79. }
  80. return "", "", err
  81. }
  82. if _, ok := secret.Data[key]; !ok {
  83. return "SecretKeyMissing", fmt.Sprintf("Secret %q does not contain key %q", secretName, key), nil
  84. }
  85. return "", "", nil
  86. }