credential.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. for _, ref := range AuthSecretRefs(authSpec) {
  40. if reason, msg, err := checkSecretKey(ctx, c, obj.GetNamespace(), ref.Name, ref.Key); err != nil {
  41. log.Error(err, "Failed to check API key secret", "namespace", obj.GetNamespace(), "name", obj.GetName(), "secretName", ref.Name, "secretKey", ref.Key)
  42. return nil, err
  43. } else {
  44. if reason != "" {
  45. return &metav1.Condition{
  46. Type: "CredentialsResolved",
  47. Status: metav1.ConditionTrue,
  48. Reason: reason,
  49. Message: msg,
  50. }, nil
  51. }
  52. }
  53. }
  54. return &metav1.Condition{
  55. Type: "CredentialsResolved",
  56. Status: metav1.ConditionTrue,
  57. Reason: "CredentialsFound",
  58. Message: "All referenced secrets exist and contain the required keys",
  59. }, nil
  60. }
  61. // checkSecretKey verifies that a Secret exists and contains the given key.
  62. func checkSecretKey(ctx context.Context, c client.Client, namespace, secretName, key string) (string, string, error) {
  63. secret := &corev1.Secret{}
  64. if err := c.Get(ctx, types.NamespacedName{Name: secretName, Namespace: namespace}, secret); err != nil {
  65. if apierrors.IsNotFound(err) {
  66. return "SecretNotFound", fmt.Sprintf("Secret %q not found", secretName), nil
  67. }
  68. return "", "", err
  69. }
  70. if _, ok := secret.Data[key]; !ok {
  71. return "SecretKeyMissing", fmt.Sprintf("Secret %q does not contain key %q", secretName, key), nil
  72. }
  73. return "", "", nil
  74. }
  75. // AuthSecretRefs lists, in a deterministic order, the Secret keys that authSpec sources
  76. // its credentials from.
  77. func AuthSecretRefs(authSpec *v1alpha1.AuthSpec) []corev1.SecretKeySelector {
  78. if authSpec == nil {
  79. return nil
  80. }
  81. refs := make([]corev1.SecretKeySelector, 0, 2+len(authSpec.Headers))
  82. if authSpec.APIKey != nil {
  83. refs = append(refs, authSpec.APIKey.SecretRef)
  84. }
  85. if authSpec.BearerToken != nil {
  86. refs = append(refs, *authSpec.BearerToken)
  87. }
  88. for _, hdr := range authSpec.Headers {
  89. if hdr.ValueFrom.SecretKeyRef != nil {
  90. refs = append(refs, *hdr.ValueFrom.SecretKeyRef)
  91. }
  92. }
  93. return refs
  94. }