provider.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 v1beta1
  13. import (
  14. "context"
  15. corev1 "k8s.io/api/core/v1"
  16. "sigs.k8s.io/controller-runtime/pkg/client"
  17. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  18. )
  19. const (
  20. // Ready indicates that the client is configured correctly
  21. // and can be used.
  22. ValidationResultReady ValidationResult = iota
  23. // Unknown indicates that the client can be used
  24. // but information is missing and it can not be validated.
  25. ValidationResultUnknown
  26. // Error indicates that there is a misconfiguration.
  27. ValidationResultError
  28. )
  29. type ValidationResult uint8
  30. func (v ValidationResult) String() string {
  31. return [...]string{"Ready", "Unknown", "Error"}[v]
  32. }
  33. // +kubebuilder:object:root=false
  34. // +kubebuilder:object:generate:false
  35. // +k8s:deepcopy-gen:interfaces=nil
  36. // +k8s:deepcopy-gen=nil
  37. // Provider is a common interface for interacting with secret backends.
  38. type Provider interface {
  39. // NewClient constructs a SecretsManager Provider
  40. NewClient(ctx context.Context, store GenericStore, kube client.Client, namespace string) (SecretsClient, error)
  41. // ValidateStore checks if the provided store is valid
  42. // The provider may return a warning and an error.
  43. // The intended use of the warning to indicate a deprecation of behavior
  44. // or other type of message that is NOT a validation failure but should be noticed by the user.
  45. ValidateStore(store GenericStore) (admission.Warnings, error)
  46. // Capabilities returns the provider Capabilities (Read, Write, ReadWrite)
  47. Capabilities() SecretStoreCapabilities
  48. }
  49. // +kubebuilder:object:root=false
  50. // +kubebuilder:object:generate:false
  51. // +k8s:deepcopy-gen:interfaces=nil
  52. // +k8s:deepcopy-gen=nil
  53. // SecretsClient provides access to secrets.
  54. type SecretsClient interface {
  55. // GetSecret returns a single secret from the provider
  56. // if GetSecret returns an error with type NoSecretError
  57. // then the secret entry will be deleted depending on the deletionPolicy.
  58. GetSecret(ctx context.Context, ref ExternalSecretDataRemoteRef) ([]byte, error)
  59. // PushSecret will write a single secret into the provider
  60. PushSecret(ctx context.Context, secret *corev1.Secret, data PushSecretData) error
  61. // DeleteSecret will delete the secret from a provider
  62. DeleteSecret(ctx context.Context, remoteRef PushSecretRemoteRef) error
  63. // SecretExists checks if a secret is already present in the provider at the given location.
  64. SecretExists(ctx context.Context, remoteRef PushSecretRemoteRef) (bool, error)
  65. // Validate checks if the client is configured correctly
  66. // and is able to retrieve secrets from the provider.
  67. // If the validation result is unknown it will be ignored.
  68. Validate() (ValidationResult, error)
  69. // GetSecretMap returns multiple k/v pairs from the provider
  70. GetSecretMap(ctx context.Context, ref ExternalSecretDataRemoteRef) (map[string][]byte, error)
  71. // GetAllSecrets returns multiple k/v pairs from the provider
  72. GetAllSecrets(ctx context.Context, ref ExternalSecretFind) (map[string][]byte, error)
  73. Close(ctx context.Context) error
  74. }
  75. var NoSecretErr = NoSecretError{}
  76. // NoSecretError shall be returned when a GetSecret can not find the
  77. // desired secret. This is used for deletionPolicy.
  78. type NoSecretError struct{}
  79. func (NoSecretError) Error() string {
  80. return "Secret does not exist"
  81. }
  82. var NotModifiedErr = NotModifiedError{}
  83. // NotModifiedError to signal that the webhook received no changes,
  84. // and it should just return without doing anything.
  85. type NotModifiedError struct{}
  86. func (NotModifiedError) Error() string {
  87. return "not modified"
  88. }