validate.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 conjur provides a Conjur provider for External Secrets.
  13. package conjur
  14. import (
  15. "fmt"
  16. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  17. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  18. "github.com/external-secrets/external-secrets/pkg/provider/conjur/util"
  19. "github.com/external-secrets/external-secrets/pkg/utils"
  20. )
  21. // ValidateStore validates the store.
  22. func (p *Provider) ValidateStore(store esv1beta1.GenericStore) (admission.Warnings, error) {
  23. prov, err := util.GetConjurProvider(store)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if prov.URL == "" {
  28. return nil, fmt.Errorf("conjur URL cannot be empty")
  29. }
  30. if prov.Auth.APIKey != nil {
  31. err := validateAPIKeyStore(store, *prov.Auth.APIKey)
  32. if err != nil {
  33. return nil, err
  34. }
  35. }
  36. if prov.Auth.Jwt != nil {
  37. err := validateJWTStore(store, *prov.Auth.Jwt)
  38. if err != nil {
  39. return nil, err
  40. }
  41. }
  42. // At least one auth must be configured
  43. if prov.Auth.APIKey == nil && prov.Auth.Jwt == nil {
  44. return nil, fmt.Errorf("missing Auth.* configuration")
  45. }
  46. return nil, nil
  47. }
  48. func validateAPIKeyStore(store esv1beta1.GenericStore, auth esv1beta1.ConjurAPIKey) error {
  49. if auth.Account == "" {
  50. return fmt.Errorf("missing Auth.ApiKey.Account")
  51. }
  52. if auth.UserRef == nil {
  53. return fmt.Errorf("missing Auth.Apikey.UserRef")
  54. }
  55. if auth.APIKeyRef == nil {
  56. return fmt.Errorf("missing Auth.Apikey.ApiKeyRef")
  57. }
  58. if err := utils.ValidateReferentSecretSelector(store, *auth.UserRef); err != nil {
  59. return fmt.Errorf("invalid Auth.Apikey.UserRef: %w", err)
  60. }
  61. if err := utils.ValidateReferentSecretSelector(store, *auth.APIKeyRef); err != nil {
  62. return fmt.Errorf("invalid Auth.Apikey.ApiKeyRef: %w", err)
  63. }
  64. return nil
  65. }
  66. func validateJWTStore(store esv1beta1.GenericStore, auth esv1beta1.ConjurJWT) error {
  67. if auth.Account == "" {
  68. return fmt.Errorf("missing Auth.Jwt.Account")
  69. }
  70. if auth.ServiceID == "" {
  71. return fmt.Errorf("missing Auth.Jwt.ServiceID")
  72. }
  73. if auth.ServiceAccountRef == nil && auth.SecretRef == nil {
  74. return fmt.Errorf("must specify Auth.Jwt.SecretRef or Auth.Jwt.ServiceAccountRef")
  75. }
  76. if auth.SecretRef != nil {
  77. if err := utils.ValidateReferentSecretSelector(store, *auth.SecretRef); err != nil {
  78. return fmt.Errorf("invalid Auth.Jwt.SecretRef: %w", err)
  79. }
  80. }
  81. if auth.ServiceAccountRef != nil {
  82. if err := utils.ValidateReferentServiceAccountSelector(store, *auth.ServiceAccountRef); err != nil {
  83. return fmt.Errorf("invalid Auth.Jwt.ServiceAccountRef: %w", err)
  84. }
  85. }
  86. return nil
  87. }