provider.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "github.com/1password/onepassword-sdk-go"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  23. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  24. "github.com/external-secrets/external-secrets/pkg/esutils"
  25. "github.com/external-secrets/external-secrets/pkg/esutils/resolvers"
  26. )
  27. const (
  28. errOnePasswordSdkStore = "received invalid 1PasswordSdk SecretStore resource: %w"
  29. errOnePasswordSdkStoreNilSpec = "nil spec"
  30. errOnePasswordSdkStoreNilSpecProvider = "nil spec.provider"
  31. errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk = "nil spec.provider.onepasswordsdk"
  32. errOnePasswordSdkStoreMissingRefName = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.name"
  33. errOnePasswordSdkStoreMissingRefKey = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.key"
  34. errOnePasswordSdkStoreMissingVaultKey = "missing: spec.provider.onepasswordsdk.vault"
  35. errVersionNotImplemented = "'remoteRef.version' is not implemented in the 1Password SDK provider"
  36. errNotImplemented = "not implemented"
  37. )
  38. // Provider implements the External Secrets provider interface for 1Password SDK.
  39. type Provider struct {
  40. client *onepassword.Client
  41. vaultPrefix string
  42. vaultID string
  43. }
  44. // NewClient constructs a new secrets client based on the provided store.
  45. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube client.Client, namespace string) (esv1.SecretsClient, error) {
  46. config := store.GetSpec().Provider.OnePasswordSDK
  47. serviceAccountToken, err := resolvers.SecretKeyRef(
  48. ctx,
  49. kube,
  50. store.GetKind(),
  51. namespace,
  52. &config.Auth.ServiceAccountSecretRef,
  53. )
  54. if err != nil {
  55. return nil, err
  56. }
  57. if config.IntegrationInfo == nil {
  58. config.IntegrationInfo = &esv1.IntegrationInfo{
  59. Name: "1Password SDK",
  60. Version: "v1.0.0",
  61. }
  62. }
  63. c, err := onepassword.NewClient(
  64. ctx,
  65. onepassword.WithServiceAccountToken(serviceAccountToken),
  66. onepassword.WithIntegrationInfo(config.IntegrationInfo.Name, config.IntegrationInfo.Version),
  67. )
  68. if err != nil {
  69. return nil, err
  70. }
  71. p.client = c
  72. p.vaultPrefix = "op://" + config.Vault + "/"
  73. vaultID, err := p.GetVault(ctx, config.Vault)
  74. if err != nil {
  75. return nil, fmt.Errorf("failed to get store ID: %w", err)
  76. }
  77. p.vaultID = vaultID
  78. return p, nil
  79. }
  80. // ValidateStore validates the 1Password SDK SecretStore resource configuration.
  81. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  82. storeSpec := store.GetSpec()
  83. if storeSpec == nil {
  84. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpec))
  85. }
  86. if storeSpec.Provider == nil {
  87. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProvider))
  88. }
  89. if storeSpec.Provider.OnePasswordSDK == nil {
  90. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk))
  91. }
  92. config := storeSpec.Provider.OnePasswordSDK
  93. if config.Auth.ServiceAccountSecretRef.Name == "" {
  94. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefName))
  95. }
  96. if config.Auth.ServiceAccountSecretRef.Key == "" {
  97. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefKey))
  98. }
  99. if config.Vault == "" {
  100. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingVaultKey))
  101. }
  102. // check namespace compared to kind
  103. if err := esutils.ValidateSecretSelector(store, config.Auth.ServiceAccountSecretRef); err != nil {
  104. return nil, fmt.Errorf(errOnePasswordSdkStore, err)
  105. }
  106. return nil, nil
  107. }
  108. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  109. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  110. return esv1.SecretStoreReadWrite
  111. }
  112. func init() {
  113. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  114. OnePasswordSDK: &esv1.OnePasswordSDKProvider{},
  115. }, esv1.MaintenanceStatusMaintained)
  116. }