validate.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright © The ESO Authors
  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. https://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 kubernetes
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "slices"
  19. authv1 "k8s.io/api/authorization/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  22. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  23. "github.com/external-secrets/external-secrets/runtime/constants"
  24. "github.com/external-secrets/external-secrets/runtime/esutils"
  25. "github.com/external-secrets/external-secrets/runtime/metrics"
  26. )
  27. const (
  28. warnNoCAConfigured = "No caBundle or caProvider specified; TLS connections will use system certificate roots."
  29. )
  30. // ValidateStore validates the Kubernetes SecretStore configuration.
  31. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  32. storeSpec := store.GetSpec()
  33. k8sSpec := storeSpec.Provider.Kubernetes
  34. var warnings admission.Warnings
  35. if k8sSpec.AuthRef == nil && k8sSpec.Server.CABundle == nil && k8sSpec.Server.CAProvider == nil {
  36. warnings = append(warnings, warnNoCAConfigured)
  37. }
  38. if store.GetObjectKind().GroupVersionKind().Kind == esv1.ClusterSecretStoreKind &&
  39. k8sSpec.Server.CAProvider != nil &&
  40. k8sSpec.Server.CAProvider.Namespace == nil {
  41. return warnings, errors.New("CAProvider.namespace must not be empty with ClusterSecretStore")
  42. }
  43. if store.GetObjectKind().GroupVersionKind().Kind == esv1.SecretStoreKind &&
  44. k8sSpec.Server.CAProvider != nil &&
  45. k8sSpec.Server.CAProvider.Namespace != nil {
  46. return warnings, errors.New("CAProvider.namespace must be empty with SecretStore")
  47. }
  48. if k8sSpec.Auth != nil && k8sSpec.Auth.Cert != nil {
  49. if k8sSpec.Auth.Cert.ClientCert.Name == "" {
  50. return warnings, errors.New("ClientCert.Name cannot be empty")
  51. }
  52. if k8sSpec.Auth.Cert.ClientCert.Key == "" {
  53. return warnings, errors.New("ClientCert.Key cannot be empty")
  54. }
  55. if err := esutils.ValidateSecretSelector(store, k8sSpec.Auth.Cert.ClientCert); err != nil {
  56. return warnings, err
  57. }
  58. }
  59. if k8sSpec.Auth != nil && k8sSpec.Auth.Token != nil {
  60. if k8sSpec.Auth.Token.BearerToken.Name == "" {
  61. return warnings, errors.New("BearerToken.Name cannot be empty")
  62. }
  63. if k8sSpec.Auth.Token.BearerToken.Key == "" {
  64. return warnings, errors.New("BearerToken.Key cannot be empty")
  65. }
  66. if err := esutils.ValidateSecretSelector(store, k8sSpec.Auth.Token.BearerToken); err != nil {
  67. return warnings, err
  68. }
  69. }
  70. if k8sSpec.Auth != nil && k8sSpec.Auth.ServiceAccount != nil {
  71. if err := esutils.ValidateReferentServiceAccountSelector(store, *k8sSpec.Auth.ServiceAccount); err != nil {
  72. return warnings, err
  73. }
  74. }
  75. return warnings, nil
  76. }
  77. // Validate checks if the client has the necessary permissions to access secrets in the target namespace.
  78. func (c *Client) Validate() (esv1.ValidationResult, error) {
  79. // when using referent namespace we can not validate the token
  80. // because the namespace is not known yet when Validate() is called
  81. // from the SecretStore controller.
  82. if c.storeKind == esv1.ClusterSecretStoreKind && isReferentSpec(c.store) {
  83. return esv1.ValidationResultUnknown, nil
  84. }
  85. ctx := context.Background()
  86. t := authv1.SelfSubjectRulesReview{
  87. Spec: authv1.SelfSubjectRulesReviewSpec{
  88. Namespace: c.store.RemoteNamespace,
  89. },
  90. }
  91. authReview, err := c.userReviewClient.Create(ctx, &t, metav1.CreateOptions{})
  92. metrics.ObserveAPICall(constants.ProviderKubernetes, constants.CallKubernetesCreateSelfSubjectRulesReview, err)
  93. if err != nil {
  94. return esv1.ValidationResultUnknown, fmt.Errorf("could not verify if client is valid: %w", err)
  95. }
  96. for _, rev := range authReview.Status.ResourceRules {
  97. if (slices.Contains(rev.Resources, "secrets") || slices.Contains(rev.Resources, "*")) &&
  98. (slices.Contains(rev.Verbs, "get") || slices.Contains(rev.Verbs, "*")) &&
  99. (len(rev.APIGroups) == 0 || (slices.Contains(rev.APIGroups, "") || slices.Contains(rev.APIGroups, "*"))) {
  100. return esv1.ValidationResultReady, nil
  101. }
  102. }
  103. a := authv1.SelfSubjectAccessReview{
  104. Spec: authv1.SelfSubjectAccessReviewSpec{
  105. ResourceAttributes: &authv1.ResourceAttributes{
  106. Resource: "secrets",
  107. Namespace: c.store.RemoteNamespace,
  108. Verb: "get",
  109. },
  110. },
  111. }
  112. accessReview, err := c.userAccessReviewClient.Create(ctx, &a, metav1.CreateOptions{})
  113. if err != nil {
  114. return esv1.ValidationResultUnknown, fmt.Errorf("could not verify if client is valid: %w", err)
  115. }
  116. if accessReview.Status.Allowed {
  117. return esv1.ValidationResultReady, nil
  118. }
  119. return esv1.ValidationResultError, errors.New("client is not allowed to get secrets")
  120. }