validate.go 3.0 KB

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