kubernetes_secret.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package auth
  13. import (
  14. "context"
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. "sigs.k8s.io/controller-runtime/pkg/client"
  18. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  19. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  20. )
  21. const (
  22. errRequiredNamespaceNotFound = "invalid ClusterSecretStore: missing namespace in %s"
  23. errCannotFetchKubernetesSecret = "could not fetch Kubernetes secret %s"
  24. )
  25. /*
  26. getKubernetesSecret get Kubernetes Secret based on object parameter in namespace where ESO is installed or another, if ClusterSecretStore is used
  27. */
  28. func getKubernetesSecret(ctx context.Context, object esmeta.SecretKeySelector, store esv1beta1.GenericStore, kube client.Client, namespace string) (string, error) {
  29. ke := client.ObjectKey{
  30. Name: object.Name,
  31. Namespace: namespace, // Default to ExternalSecret namespace
  32. }
  33. // Only ClusterStore is allowed to set namespace (and then it's required)
  34. if store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind {
  35. if object.Namespace == nil {
  36. return "", fmt.Errorf(errRequiredNamespaceNotFound, object.Key)
  37. }
  38. ke.Namespace = *object.Namespace
  39. }
  40. secret := v1.Secret{}
  41. err := kube.Get(ctx, ke, &secret)
  42. if err != nil {
  43. return "", fmt.Errorf(errCannotFetchKubernetesSecret, object.Name)
  44. }
  45. return string(secret.Data[object.Key]), nil
  46. }