provider.go 4.4 KB

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