provider.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 senhasegura
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "net/url"
  18. "sigs.k8s.io/controller-runtime/pkg/client"
  19. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  20. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  21. senhaseguraAuth "github.com/external-secrets/external-secrets/pkg/provider/senhasegura/auth"
  22. "github.com/external-secrets/external-secrets/pkg/provider/senhasegura/dsm"
  23. )
  24. // https://github.com/external-secrets/external-secrets/issues/644
  25. var _ esv1beta1.Provider = &Provider{}
  26. // Provider struct that satisfier ESO interface.
  27. type Provider struct{}
  28. const (
  29. errUnknownProviderService = "unknown senhasegura Provider Service: %s"
  30. errNilStore = "nil store found"
  31. errMissingStoreSpec = "store is missing spec"
  32. errMissingProvider = "storeSpec is missing provider"
  33. errInvalidProvider = "invalid provider spec. Missing senhasegura field in store %s"
  34. errInvalidSenhaseguraURL = "invalid senhasegura URL"
  35. errInvalidSenhaseguraURLHTTPS = "invalid senhasegura URL, must be HTTPS for security reasons"
  36. errMissingClientID = "missing senhasegura authentication Client ID"
  37. )
  38. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  39. func (p *Provider) Capabilities() esv1beta1.SecretStoreCapabilities {
  40. return esv1beta1.SecretStoreReadOnly
  41. }
  42. /*
  43. Construct a new secrets client based on provided store.
  44. */
  45. func (p *Provider) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube client.Client, namespace string) (esv1beta1.SecretsClient, error) {
  46. spec := store.GetSpec()
  47. provider := spec.Provider.Senhasegura
  48. isoSession, err := senhaseguraAuth.Authenticate(ctx, store, provider, kube, namespace)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if provider.Module == esv1beta1.SenhaseguraModuleDSM {
  53. return dsm.New(isoSession)
  54. }
  55. return nil, fmt.Errorf(errUnknownProviderService, provider.Module)
  56. }
  57. // Validate store using Validating webhook during secret store creating
  58. // Checks here are usually the best experience for the user, as the SecretStore will not be created until it is a 'valid' one.
  59. // https://github.com/external-secrets/external-secrets/pull/830#discussion_r833278518
  60. func (p *Provider) ValidateStore(store esv1beta1.GenericStore) (admission.Warnings, error) {
  61. return nil, validateStore(store)
  62. }
  63. func validateStore(store esv1beta1.GenericStore) error {
  64. if store == nil {
  65. return errors.New(errNilStore)
  66. }
  67. spec := store.GetSpec()
  68. if spec == nil {
  69. return errors.New(errMissingStoreSpec)
  70. }
  71. if spec.Provider == nil {
  72. return errors.New(errMissingProvider)
  73. }
  74. provider := spec.Provider.Senhasegura
  75. if provider == nil {
  76. return fmt.Errorf(errInvalidProvider, store.GetObjectMeta().String())
  77. }
  78. url, err := url.Parse(provider.URL)
  79. if err != nil {
  80. return errors.New(errInvalidSenhaseguraURL)
  81. }
  82. // senhasegura doesn't accept requests without SSL/TLS layer for security reasons
  83. // DSM doesn't provides gRPC schema, only HTTPS
  84. if url.Scheme != "https" {
  85. return errors.New(errInvalidSenhaseguraURLHTTPS)
  86. }
  87. if url.Host == "" {
  88. return errors.New(errInvalidSenhaseguraURL)
  89. }
  90. if provider.Auth.ClientID == "" {
  91. return errors.New(errMissingClientID)
  92. }
  93. return nil
  94. }
  95. /*
  96. Register SenhaseguraProvider in ESO init.
  97. */
  98. func init() {
  99. esv1beta1.Register(&Provider{}, &esv1beta1.SecretStoreProvider{
  100. Senhasegura: &esv1beta1.SenhaseguraProvider{},
  101. })
  102. }