| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- /*
- Copyright © 2025 ESO Maintainer Team
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- https://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- // Package onepasswordsdk implements a provider for 1Password using the official SDK.
- // It allows fetching and managing secrets stored in 1Password using their official Go SDK.
- package onepasswordsdk
- import (
- "context"
- "errors"
- "fmt"
- "github.com/1password/onepassword-sdk-go"
- "sigs.k8s.io/controller-runtime/pkg/client"
- "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
- esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
- "github.com/external-secrets/external-secrets/pkg/esutils"
- "github.com/external-secrets/external-secrets/pkg/esutils/resolvers"
- )
- const (
- errOnePasswordSdkStore = "received invalid 1PasswordSdk SecretStore resource: %w"
- errOnePasswordSdkStoreNilSpec = "nil spec"
- errOnePasswordSdkStoreNilSpecProvider = "nil spec.provider"
- errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk = "nil spec.provider.onepasswordsdk"
- errOnePasswordSdkStoreMissingRefName = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.name"
- errOnePasswordSdkStoreMissingRefKey = "missing: spec.provider.onepasswordsdk.auth.secretRef.serviceAccountTokenSecretRef.key"
- errOnePasswordSdkStoreMissingVaultKey = "missing: spec.provider.onepasswordsdk.vault"
- errVersionNotImplemented = "'remoteRef.version' is not implemented in the 1Password SDK provider"
- errNotImplemented = "not implemented"
- )
- // Provider implements the External Secrets provider interface for 1Password SDK.
- type Provider struct {
- client *onepassword.Client
- vaultPrefix string
- vaultID string
- }
- // NewClient constructs a new secrets client based on the provided store.
- func (p *Provider) NewClient(ctx context.Context, store esv1.GenericStore, kube client.Client, namespace string) (esv1.SecretsClient, error) {
- config := store.GetSpec().Provider.OnePasswordSDK
- serviceAccountToken, err := resolvers.SecretKeyRef(
- ctx,
- kube,
- store.GetKind(),
- namespace,
- &config.Auth.ServiceAccountSecretRef,
- )
- if err != nil {
- return nil, err
- }
- if config.IntegrationInfo == nil {
- config.IntegrationInfo = &esv1.IntegrationInfo{
- Name: "1Password SDK",
- Version: "v1.0.0",
- }
- }
- c, err := onepassword.NewClient(
- ctx,
- onepassword.WithServiceAccountToken(serviceAccountToken),
- onepassword.WithIntegrationInfo(config.IntegrationInfo.Name, config.IntegrationInfo.Version),
- )
- if err != nil {
- return nil, err
- }
- p.client = c
- p.vaultPrefix = "op://" + config.Vault + "/"
- vaultID, err := p.GetVault(ctx, config.Vault)
- if err != nil {
- return nil, fmt.Errorf("failed to get store ID: %w", err)
- }
- p.vaultID = vaultID
- return p, nil
- }
- // ValidateStore validates the 1Password SDK SecretStore resource configuration.
- func (p *Provider) ValidateStore(store esv1.GenericStore) (admission.Warnings, error) {
- storeSpec := store.GetSpec()
- if storeSpec == nil {
- return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpec))
- }
- if storeSpec.Provider == nil {
- return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProvider))
- }
- if storeSpec.Provider.OnePasswordSDK == nil {
- return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreNilSpecProviderOnePasswordSdk))
- }
- config := storeSpec.Provider.OnePasswordSDK
- if config.Auth.ServiceAccountSecretRef.Name == "" {
- return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefName))
- }
- if config.Auth.ServiceAccountSecretRef.Key == "" {
- return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingRefKey))
- }
- if config.Vault == "" {
- return nil, fmt.Errorf(errOnePasswordSdkStore, errors.New(errOnePasswordSdkStoreMissingVaultKey))
- }
- // check namespace compared to kind
- if err := esutils.ValidateSecretSelector(store, config.Auth.ServiceAccountSecretRef); err != nil {
- return nil, fmt.Errorf(errOnePasswordSdkStore, err)
- }
- return nil, nil
- }
- // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
- func (p *Provider) Capabilities() esv1.SecretStoreCapabilities {
- return esv1.SecretStoreReadWrite
- }
- func init() {
- esv1.Register(&Provider{}, &esv1.SecretStoreProvider{
- OnePasswordSDK: &esv1.OnePasswordSDKProvider{},
- }, esv1.MaintenanceStatusMaintained)
- }
|