validate.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 kubernetes
  13. import (
  14. "context"
  15. "fmt"
  16. authv1 "k8s.io/api/authorization/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  19. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  20. "github.com/external-secrets/external-secrets/pkg/constants"
  21. "github.com/external-secrets/external-secrets/pkg/metrics"
  22. "github.com/external-secrets/external-secrets/pkg/utils"
  23. )
  24. func (p *Provider) ValidateStore(store esv1beta1.GenericStore) (admission.Warnings, error) {
  25. storeSpec := store.GetSpec()
  26. k8sSpec := storeSpec.Provider.Kubernetes
  27. if k8sSpec.Server.CABundle == nil && k8sSpec.Server.CAProvider == nil {
  28. return nil, fmt.Errorf("a CABundle or CAProvider is required")
  29. }
  30. if store.GetObjectKind().GroupVersionKind().Kind == esv1beta1.ClusterSecretStoreKind &&
  31. k8sSpec.Server.CAProvider != nil &&
  32. k8sSpec.Server.CAProvider.Namespace == nil {
  33. return nil, fmt.Errorf("CAProvider.namespace must not be empty with ClusterSecretStore")
  34. }
  35. if k8sSpec.Auth.Cert != nil {
  36. if k8sSpec.Auth.Cert.ClientCert.Name == "" {
  37. return nil, fmt.Errorf("ClientCert.Name cannot be empty")
  38. }
  39. if k8sSpec.Auth.Cert.ClientCert.Key == "" {
  40. return nil, fmt.Errorf("ClientCert.Key cannot be empty")
  41. }
  42. if err := utils.ValidateSecretSelector(store, k8sSpec.Auth.Cert.ClientCert); err != nil {
  43. return nil, err
  44. }
  45. }
  46. if k8sSpec.Auth.Token != nil {
  47. if k8sSpec.Auth.Token.BearerToken.Name == "" {
  48. return nil, fmt.Errorf("BearerToken.Name cannot be empty")
  49. }
  50. if k8sSpec.Auth.Token.BearerToken.Key == "" {
  51. return nil, fmt.Errorf("BearerToken.Key cannot be empty")
  52. }
  53. if err := utils.ValidateSecretSelector(store, k8sSpec.Auth.Token.BearerToken); err != nil {
  54. return nil, err
  55. }
  56. }
  57. if k8sSpec.Auth.ServiceAccount != nil {
  58. if err := utils.ValidateReferentServiceAccountSelector(store, *k8sSpec.Auth.ServiceAccount); err != nil {
  59. return nil, err
  60. }
  61. }
  62. return nil, nil
  63. }
  64. func (c *Client) Validate() (esv1beta1.ValidationResult, error) {
  65. // when using referent namespace we can not validate the token
  66. // because the namespace is not known yet when Validate() is called
  67. // from the SecretStore controller.
  68. if c.storeKind == esv1beta1.ClusterSecretStoreKind && isReferentSpec(c.store) {
  69. return esv1beta1.ValidationResultUnknown, nil
  70. }
  71. ctx := context.Background()
  72. t := authv1.SelfSubjectRulesReview{
  73. Spec: authv1.SelfSubjectRulesReviewSpec{
  74. Namespace: c.store.RemoteNamespace,
  75. },
  76. }
  77. authReview, err := c.userReviewClient.Create(ctx, &t, metav1.CreateOptions{})
  78. metrics.ObserveAPICall(constants.ProviderKubernetes, constants.CallKubernetesCreateSelfSubjectRulesReview, err)
  79. if err != nil {
  80. return esv1beta1.ValidationResultUnknown, fmt.Errorf("could not verify if client is valid: %w", err)
  81. }
  82. for _, rev := range authReview.Status.ResourceRules {
  83. if (contains("secrets", rev.Resources) || contains("*", rev.Resources)) &&
  84. (contains("get", rev.Verbs) || contains("*", rev.Verbs)) &&
  85. (len(rev.APIGroups) == 0 || (contains("", rev.APIGroups) || contains("*", rev.APIGroups))) {
  86. return esv1beta1.ValidationResultReady, nil
  87. }
  88. }
  89. return esv1beta1.ValidationResultError, fmt.Errorf("client is not allowed to get secrets")
  90. }
  91. func contains(sub string, args []string) bool {
  92. for _, k := range args {
  93. if k == sub {
  94. return true
  95. }
  96. }
  97. return false
  98. }