provider.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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/esutils"
  27. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  28. )
  29. const (
  30. errOnePasswordSdkStore = "received invalid 1PasswordSdk SecretStore resource: %w"
  31. errOnePasswordSdkStoreNilSpec = "nil spec"
  32. errOnePasswordSdkStoreNilSpecProvider = "nil spec.provider"
  33. errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk = "nil spec.provider.onepasswordsdk"
  34. errOnePasswordSdkStoreMissingRefName = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.name"
  35. errOnePasswordSdkStoreMissingRefKey = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.key"
  36. errOnePasswordSdkStoreMissingVaultKey = "missing: spec.provider.onepasswordsdk.vault"
  37. errVersionNotImplemented = "'remoteRef.version' is not implemented in the 1Password SDK provider"
  38. errNotImplemented = "not implemented"
  39. )
  40. // Provider implements the External Secrets provider interface for 1Password SDK.
  41. type Provider struct {
  42. client *onepassword.Client
  43. vaultPrefix string
  44. vaultID string
  45. cache *expirable.LRU[string, []byte] // nil if caching is disabled
  46. }
  47. // NewClient constructs a new secrets client based on the provided store.
  48. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube client.Client, namespace string) (esv1.SecretsClient, error) {
  49. config := store.GetSpec().Provider.OnePasswordSDK
  50. serviceAccountToken, err := resolvers.SecretKeyRef(
  51. ctx,
  52. kube,
  53. store.GetKind(),
  54. namespace,
  55. &config.Auth.ServiceAccountSecretRef,
  56. )
  57. if err != nil {
  58. return nil, err
  59. }
  60. if config.IntegrationInfo == nil {
  61. config.IntegrationInfo = &esv1.IntegrationInfo{
  62. Name: "1Password SDK",
  63. Version: "v1.0.0",
  64. }
  65. }
  66. c, err := onepassword.NewClient(
  67. ctx,
  68. onepassword.WithServiceAccountToken(serviceAccountToken),
  69. onepassword.WithIntegrationInfo(config.IntegrationInfo.Name, config.IntegrationInfo.Version),
  70. )
  71. if err != nil {
  72. return nil, err
  73. }
  74. provider := &Provider{
  75. client: c,
  76. vaultPrefix: "op://" + config.Vault + "/",
  77. }
  78. vaultID, err := provider.GetVault(ctx, config.Vault)
  79. if err != nil {
  80. return nil, fmt.Errorf("failed to get store ID: %w", err)
  81. }
  82. provider.vaultID = vaultID
  83. if config.Cache != nil {
  84. ttl := 5 * time.Minute
  85. if config.Cache.TTL.Duration > 0 {
  86. ttl = config.Cache.TTL.Duration
  87. }
  88. maxSize := 100
  89. if config.Cache.MaxSize > 0 {
  90. maxSize = config.Cache.MaxSize
  91. }
  92. provider.cache = expirable.NewLRU[string, []byte](maxSize, nil, ttl)
  93. }
  94. return provider, nil
  95. }
  96. // ValidateStore validates the 1Password SDK SecretStore resource configuration.
  97. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  98. storeSpec := store.GetSpec()
  99. if storeSpec == nil {
  100. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpec))
  101. }
  102. if storeSpec.Provider == nil {
  103. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProvider))
  104. }
  105. if storeSpec.Provider.OnePasswordSDK == nil {
  106. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk))
  107. }
  108. config := storeSpec.Provider.OnePasswordSDK
  109. if config.Auth.ServiceAccountSecretRef.Name == "" {
  110. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefName))
  111. }
  112. if config.Auth.ServiceAccountSecretRef.Key == "" {
  113. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefKey))
  114. }
  115. if config.Vault == "" {
  116. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingVaultKey))
  117. }
  118. // check namespace compared to kind
  119. if err := esutils.ValidateSecretSelector(store, config.Auth.ServiceAccountSecretRef); err != nil {
  120. return nil, fmt.Errorf(errOnePasswordSdkStore, err)
  121. }
  122. return nil, nil
  123. }
  124. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  125. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  126. return esv1.SecretStoreReadWrite
  127. }
  128. // NewProvider creates a new Provider instance.
  129. func NewProvider() esv1.Provider {
  130. return &Provider{}
  131. }
  132. // ProviderSpec returns the provider specification for registration.
  133. func ProviderSpec() *esv1.SecretStoreProvider {
  134. return &esv1.SecretStoreProvider{
  135. OnePasswordSDK: &esv1.OnePasswordSDKProvider{},
  136. }
  137. }
  138. // MaintenanceStatus returns the maintenance status of the provider.
  139. func MaintenanceStatus() esv1.MaintenanceStatus {
  140. return esv1.MaintenanceStatusMaintained
  141. }