provider.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. p.client = c
  75. p.vaultPrefix = "op://" + config.Vault + "/"
  76. vaultID, err := p.GetVault(ctx, config.Vault)
  77. if err != nil {
  78. return nil, fmt.Errorf("failed to get store ID: %w", err)
  79. }
  80. p.vaultID = vaultID
  81. if config.Cache != nil {
  82. ttl := 5 * time.Minute
  83. if config.Cache.TTL.Duration > 0 {
  84. ttl = config.Cache.TTL.Duration
  85. }
  86. maxSize := 100
  87. if config.Cache.MaxSize > 0 {
  88. maxSize = config.Cache.MaxSize
  89. }
  90. p.cache = expirable.NewLRU[string, []byte](maxSize, nil, ttl)
  91. }
  92. return p, nil
  93. }
  94. // ValidateStore validates the 1Password SDK SecretStore resource configuration.
  95. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  96. storeSpec := store.GetSpec()
  97. if storeSpec == nil {
  98. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpec))
  99. }
  100. if storeSpec.Provider == nil {
  101. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProvider))
  102. }
  103. if storeSpec.Provider.OnePasswordSDK == nil {
  104. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk))
  105. }
  106. config := storeSpec.Provider.OnePasswordSDK
  107. if config.Auth.ServiceAccountSecretRef.Name == "" {
  108. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefName))
  109. }
  110. if config.Auth.ServiceAccountSecretRef.Key == "" {
  111. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefKey))
  112. }
  113. if config.Vault == "" {
  114. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingVaultKey))
  115. }
  116. // check namespace compared to kind
  117. if err := esutils.ValidateSecretSelector(store, config.Auth.ServiceAccountSecretRef); err != nil {
  118. return nil, fmt.Errorf(errOnePasswordSdkStore, err)
  119. }
  120. return nil, nil
  121. }
  122. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  123. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  124. return esv1.SecretStoreReadWrite
  125. }
  126. // NewProvider creates a new Provider instance.
  127. func NewProvider() esv1.Provider {
  128. return &Provider{}
  129. }
  130. // ProviderSpec returns the provider specification for registration.
  131. func ProviderSpec() *esv1.SecretStoreProvider {
  132. return &esv1.SecretStoreProvider{
  133. OnePasswordSDK: &esv1.OnePasswordSDKProvider{},
  134. }
  135. }
  136. // MaintenanceStatus returns the maintenance status of the provider.
  137. func MaintenanceStatus() esv1.MaintenanceStatus {
  138. return esv1.MaintenanceStatusMaintained
  139. }