provider.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package onepasswordsdk
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "github.com/1password/onepassword-sdk-go"
  18. "sigs.k8s.io/controller-runtime/pkg/client"
  19. "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
  20. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  21. "github.com/external-secrets/external-secrets/pkg/utils"
  22. "github.com/external-secrets/external-secrets/pkg/utils/resolvers"
  23. )
  24. const (
  25. errOnePasswordSdkStore = "received invalid 1PasswordSdk SecretStore resource: %w"
  26. errOnePasswordSdkStoreNilSpec = "nil spec"
  27. errOnePasswordSdkStoreNilSpecProvider = "nil spec.provider"
  28. errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk = "nil spec.provider.onepasswordsdk"
  29. errOnePasswordSdkStoreMissingRefName = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.name"
  30. errOnePasswordSdkStoreMissingRefKey = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.key"
  31. errOnePasswordSdkStoreMissingVaultKey = "missing: spec.provider.onepasswordsdk.vault"
  32. errVersionNotImplemented = "'remoteRef.version' is not implemented in the 1Password SDK provider"
  33. errNotImplemented = "not implemented"
  34. )
  35. type Provider struct {
  36. client *onepassword.Client
  37. vaultPrefix string
  38. vaultID string
  39. }
  40. func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube client.Client, namespace string) (esv1.SecretsClient, error) {
  41. config := store.GetSpec().Provider.OnePasswordSDK
  42. serviceAccountToken, err := resolvers.SecretKeyRef(
  43. ctx,
  44. kube,
  45. store.GetKind(),
  46. namespace,
  47. &config.Auth.ServiceAccountSecretRef,
  48. )
  49. if err != nil {
  50. return nil, err
  51. }
  52. if config.IntegrationInfo == nil {
  53. config.IntegrationInfo = &esv1.IntegrationInfo{
  54. Name: "1Password SDK",
  55. Version: "v1.0.0",
  56. }
  57. }
  58. c, err := onepassword.NewClient(
  59. ctx,
  60. onepassword.WithServiceAccountToken(serviceAccountToken),
  61. onepassword.WithIntegrationInfo(config.IntegrationInfo.Name, config.IntegrationInfo.Version),
  62. )
  63. if err != nil {
  64. return nil, err
  65. }
  66. p.client = c
  67. p.vaultPrefix = "op://" + config.Vault + "/"
  68. vaultID, err := p.GetVault(ctx, config.Vault)
  69. if err != nil {
  70. return nil, fmt.Errorf("failed to get store ID: %w", err)
  71. }
  72. p.vaultID = vaultID
  73. return p, nil
  74. }
  75. func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
  76. storeSpec := store.GetSpec()
  77. if storeSpec == nil {
  78. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpec))
  79. }
  80. if storeSpec.Provider == nil {
  81. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProvider))
  82. }
  83. if storeSpec.Provider.OnePasswordSDK == nil {
  84. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk))
  85. }
  86. config := storeSpec.Provider.OnePasswordSDK
  87. if config.Auth.ServiceAccountSecretRef.Name == "" {
  88. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefName))
  89. }
  90. if config.Auth.ServiceAccountSecretRef.Key == "" {
  91. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefKey))
  92. }
  93. if config.Vault == "" {
  94. return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingVaultKey))
  95. }
  96. // check namespace compared to kind
  97. if err := utils.ValidateSecretSelector(store, config.Auth.ServiceAccountSecretRef); err != nil {
  98. return nil, fmt.Errorf(errOnePasswordSdkStore, err)
  99. }
  100. return nil, nil
  101. }
  102. func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
  103. return esv1.SecretStoreReadWrite
  104. }
  105. func init() {
  106. esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
  107. OnePasswordSDK: &esv1.OnePasswordSDKProvider{},
  108. }, esv1.MaintenanceStatusMaintained)
  109. }