validate.go 3.7 KB

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