provider.go 3.9 KB

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