provider.go 4.3 KB

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