validate.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // ValidateStore validates the Kubernetes SecretStore configuration.
  28. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  29. k8sSpec := store.GetSpec().Provider.Kubernetes
  30. // server/auth/authRef validation is shared with the CRD provider, which
  31. // reuses the same connection types.
  32. warnings, err := esutils.ValidateKubernetesConnection(store, k8sSpec.Server, k8sSpec.Auth, k8sSpec.AuthRef)
  33. return warnings, err
  34. }
  35. // Validate checks if the client has the necessary permissions to access secrets in the target namespace.
  36. func (c *Client) Validate() (esv1.ValidationResult, error) {
  37. // when using referent namespace we can not validate the token
  38. // because the namespace is not known yet when Validate() is called
  39. // from the SecretStore controller.
  40. if c.storeKind == esv1.ClusterSecretStoreKind && esutils.IsReferentKubernetesAuth(c.store.Auth) {
  41. return esv1.ValidationResultUnknown, nil
  42. }
  43. ctx := context.Background()
  44. t := authv1.SelfSubjectRulesReview{
  45. Spec: authv1.SelfSubjectRulesReviewSpec{
  46. Namespace: c.store.RemoteNamespace,
  47. },
  48. }
  49. authReview, err := c.userReviewClient.Create(ctx, &t, metav1.CreateOptions{})
  50. metrics.ObserveAPICall(constants.ProviderKubernetes, constants.CallKubernetesCreateSelfSubjectRulesReview, err)
  51. if err != nil {
  52. return esv1.ValidationResultUnknown, fmt.Errorf("could not verify if client is valid: %w", err)
  53. }
  54. for _, rev := range authReview.Status.ResourceRules {
  55. if (slices.Contains(rev.Resources, "secrets") || slices.Contains(rev.Resources, "*")) &&
  56. (slices.Contains(rev.Verbs, "get") || slices.Contains(rev.Verbs, "*")) &&
  57. (len(rev.APIGroups) == 0 || (slices.Contains(rev.APIGroups, "") || slices.Contains(rev.APIGroups, "*"))) {
  58. return esv1.ValidationResultReady, nil
  59. }
  60. }
  61. a := authv1.SelfSubjectAccessReview{
  62. Spec: authv1.SelfSubjectAccessReviewSpec{
  63. ResourceAttributes: &authv1.ResourceAttributes{
  64. Resource: "secrets",
  65. Namespace: c.store.RemoteNamespace,
  66. Verb: "get",
  67. },
  68. },
  69. }
  70. accessReview, err := c.userAccessReviewClient.Create(ctx, &a, metav1.CreateOptions{})
  71. if err != nil {
  72. return esv1.ValidationResultUnknown, fmt.Errorf("could not verify if client is valid: %w", err)
  73. }
  74. if accessReview.Status.Allowed {
  75. return esv1.ValidationResultReady, nil
  76. }
  77. return esv1.ValidationResultError, errors.New("client is not allowed to get secrets")
  78. }