secret_ref.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 utils
  13. import (
  14. "context"
  15. "fmt"
  16. authv1 "k8s.io/api/authentication/v1"
  17. corev1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. "k8s.io/client-go/kubernetes"
  21. typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
  22. "sigs.k8s.io/controller-runtime/pkg/client"
  23. ctrlcfg "sigs.k8s.io/controller-runtime/pkg/client/config"
  24. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  25. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  26. )
  27. const (
  28. errGetKubeSecret = "cannot get Kubernetes secret %q: %w"
  29. errSecretKeyFmt = "cannot find secret data for key: %q"
  30. errGetKubeSATokenRequest = "cannot request Kubernetes service account token for service account %q: %w"
  31. )
  32. // Resolves a metav1.SecretKeySelector and returns the value of the secret it points to.
  33. // A user must pass the namespace of the originating ExternalSecret, as this may differ
  34. // from the namespace defined in the SecretKeySelector.
  35. // This func ensures that only a ClusterSecretStore is able to request secrets across namespaces.
  36. func ResolveSecretKeyRef(
  37. ctx context.Context,
  38. c client.Client,
  39. storeKind string,
  40. esNamespace string,
  41. ref *esmeta.SecretKeySelector) (string, error) {
  42. key := types.NamespacedName{
  43. Namespace: esNamespace,
  44. Name: ref.Name,
  45. }
  46. if (storeKind == esv1beta1.ClusterSecretStoreKind) &&
  47. (ref.Namespace != nil) {
  48. key.Namespace = *ref.Namespace
  49. }
  50. secret := &corev1.Secret{}
  51. err := c.Get(ctx, key, secret)
  52. if err != nil {
  53. return "", fmt.Errorf(errGetKubeSecret, ref.Name, err)
  54. }
  55. val, ok := secret.Data[ref.Key]
  56. if !ok {
  57. return "", fmt.Errorf(errSecretKeyFmt, ref.Key)
  58. }
  59. return string(val), nil
  60. }
  61. func CreateServiceAccountToken(
  62. ctx context.Context,
  63. corev1Client typedcorev1.CoreV1Interface,
  64. storeKind string,
  65. namespace string,
  66. serviceAccountRef esmeta.ServiceAccountSelector,
  67. additionalAud []string,
  68. expirationSeconds int64) (string, error) {
  69. audiences := serviceAccountRef.Audiences
  70. if len(additionalAud) > 0 {
  71. audiences = append(audiences, additionalAud...)
  72. }
  73. tokenRequest := &authv1.TokenRequest{
  74. ObjectMeta: metav1.ObjectMeta{
  75. Namespace: namespace,
  76. },
  77. Spec: authv1.TokenRequestSpec{
  78. Audiences: audiences,
  79. ExpirationSeconds: &expirationSeconds,
  80. },
  81. }
  82. if (storeKind == esv1beta1.ClusterSecretStoreKind) &&
  83. (serviceAccountRef.Namespace != nil) {
  84. tokenRequest.Namespace = *serviceAccountRef.Namespace
  85. }
  86. tokenResponse, err := corev1Client.ServiceAccounts(tokenRequest.Namespace).
  87. CreateToken(ctx, serviceAccountRef.Name, tokenRequest, metav1.CreateOptions{})
  88. if err != nil {
  89. return "", fmt.Errorf(errGetKubeSATokenRequest, serviceAccountRef.Name, err)
  90. }
  91. return tokenResponse.Status.Token, nil
  92. }
  93. func NewKubeClient() (*kubernetes.Clientset, error) {
  94. restCfg, err := ctrlcfg.GetConfig()
  95. if err != nil {
  96. return nil, err
  97. }
  98. c, err := kubernetes.NewForConfig(restCfg)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return c, err
  103. }