validate.go 3.0 KB

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