provider.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 onepasswordsdk implements a provider for 1Password using the official SDK.
  14. // It allows fetching and managing secrets stored in 1Password using their official Go SDK.
  15. package onepasswordsdk
  16. import (
  17. "context"
  18. "errors"
  19. "fmt"
  20. "time"
  21. "github.com/1password/onepassword-sdk-go"
  22. "github.com/hashicorp/golang-lru/v2/expirable"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  25. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  26. "github.com/external-secrets/external-secrets/runtime/cache"
  27. "github.com/external-secrets/external-secrets/runtime/esutils"
  28. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  29. )
  30. const (
  31. errOnePasswordSdkStore = "received invalid 1PasswordSdk SecretStore resource: %w"
  32. errOnePasswordSdkStoreNilSpec = "nil spec"
  33. errOnePasswordSdkStoreNilSpecProvider = "nil spec.provider"
  34. errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk = "nil spec.provider.onepasswordsdk"
  35. errOnePasswordSdkStoreMissingRefName = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.name"
  36. errOnePasswordSdkStoreMissingRefKey = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.key"
  37. errOnePasswordSdkStoreMissingVaultKey = "missing: spec.provider.onepasswordsdk.vault"
  38. errVersionNotImplemented = "'remoteRef.version' is not implemented in the 1Password SDK provider"
  39. errNotImplemented = "not implemented"
  40. )
  41. // Provider contains the main cache for onepasswordsdk provider.
  42. type Provider struct {
  43. clientCache *cache.Cache[esv1.SecretsClient]
  44. }
  45. // SecretsClient wraps a 1Password SDK client for a specific vault.
  46. type SecretsClient struct {
  47. client *onepassword.Client
  48. vaultPrefix string
  49. vaultID string
  50. cache *expirable.LRU[string, []byte]
  51. }
  52. // NewClient will create a new client.
  53. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube client.Client, namespace string) (esv1.SecretsClient, error) {
  54. key := cache.Key{
  55. Name: store.GetObjectMeta().Name,
  56. Namespace: namespace,
  57. Kind: store.GetTypeMeta().Kind,
  58. }
  59. if cachedClient, ok := p.clientCache.Get(store.GetObjectMeta().ResourceVersion, key); ok {
  60. return cachedClient, nil
  61. }
  62. config := store.GetSpec().Provider.OnePasswordSDK
  63. serviceAccountToken, err := resolvers.SecretKeyRef(
  64. ctx,
  65. kube,
  66. store.GetKind(),
  67. namespace,
  68. &config.Auth.ServiceAccountSecretRef,
  69. )
  70. if err != nil {
  71. return nil, err
  72. }
  73. if config.IntegrationInfo == nil {
  74. config.IntegrationInfo = &esv1.IntegrationInfo{
  75. Name: "1Password SDK",
  76. Version: "v1.0.0",
  77. }
  78. }
  79. c, err := onepassword.NewClient(
  80. ctx,
  81. onepassword.WithServiceAccountToken(serviceAccountToken),
  82. onepassword.WithIntegrationInfo(config.IntegrationInfo.Name, config.IntegrationInfo.Version),
  83. )
  84. if err != nil {
  85. return nil, err
  86. }
  87. sc := &SecretsClient{
  88. client: c,
  89. vaultPrefix: "op://" + config.Vault + "/",
  90. }
  91. vaultID, err := sc.GetVault(ctx, config.Vault)
  92. if err != nil {
  93. return nil, fmt.Errorf("failed to get store ID: %w", err)
  94. }
  95. sc.vaultID = vaultID
  96. if config.Cache != nil {
  97. ttl := 5 * time.Minute
  98. if config.Cache.TTL.Duration > 0 {
  99. ttl = config.Cache.TTL.Duration
  100. }
  101. maxSize := 100
  102. if config.Cache.MaxSize > 0 {
  103. maxSize = config.Cache.MaxSize
  104. }
  105. sc.cache = expirable.NewLRU[string, []byte](maxSize, nil, ttl)
  106. }
  107. p.clientCache.Add(store.GetObjectMeta().ResourceVersion, key, sc)
  108. return sc, nil
  109. }
  110. // ValidateStore validates the 1Password SDK SecretStore resource configuration.
  111. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  112. storeSpec := store.GetSpec()
  113. if storeSpec == nil {
  114. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpec))
  115. }
  116. if storeSpec.Provider == nil {
  117. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProvider))
  118. }
  119. if storeSpec.Provider.OnePasswordSDK == nil {
  120. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk))
  121. }
  122. config := storeSpec.Provider.OnePasswordSDK
  123. if config.Auth.ServiceAccountSecretRef.Name == "" {
  124. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefName))
  125. }
  126. if config.Auth.ServiceAccountSecretRef.Key == "" {
  127. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefKey))
  128. }
  129. if config.Vault == "" {
  130. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingVaultKey))
  131. }
  132. // check namespace compared to kind
  133. if err := esutils.ValidateSecretSelector(store, config.Auth.ServiceAccountSecretRef); err != nil {
  134. return nil, fmt.Errorf(errOnePasswordSdkStore, err)
  135. }
  136. return nil, nil
  137. }
  138. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  139. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  140. return esv1.SecretStoreReadWrite
  141. }
  142. // NewProvider creates a new Provider instance.
  143. func NewProvider() esv1.Provider {
  144. return &Provider{
  145. clientCache: cache.Must[esv1.SecretsClient](100, nil),
  146. }
  147. }
  148. // ProviderSpec returns the provider specification for registration.
  149. func ProviderSpec() *esv1.SecretStoreProvider {
  150. return &esv1.SecretStoreProvider{
  151. OnePasswordSDK: &esv1.OnePasswordSDKProvider{},
  152. }
  153. }
  154. // MaintenanceStatus returns the maintenance status of the provider.
  155. func MaintenanceStatus() esv1.MaintenanceStatus {
  156. return esv1.MaintenanceStatusMaintained
  157. }