From 432f2c6cf69423c42e441ba4c7449f606d977f36 Mon Sep 17 00:00:00 2001 From: Volodymyr Zotov Date: Thu, 29 May 2025 16:06:55 -0500 Subject: [PATCH] Add Client instance that utilizes either Connect or SDK --- pkg/onepassword/client/client.go | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pkg/onepassword/client/client.go diff --git a/pkg/onepassword/client/client.go b/pkg/onepassword/client/client.go new file mode 100644 index 0000000..f368f94 --- /dev/null +++ b/pkg/onepassword/client/client.go @@ -0,0 +1,44 @@ +package client + +import ( + "errors" + + "github.com/1Password/onepassword-operator/pkg/onepassword/client/connect" + "github.com/1Password/onepassword-operator/pkg/onepassword/client/sdk" + "github.com/1Password/onepassword-operator/pkg/onepassword/model" +) + +// Client is an interface for interacting with 1Password items and vaults. +type Client interface { + GetItemByID(vaultID, itemID string) (*model.Item, error) + GetItemsByTitle(vaultID, itemTitle string) ([]model.Item, error) + GetFileContent(vaultID, itemID, fileID string) ([]byte, error) + GetVaultsByTitle(title string) ([]model.Vault, error) +} + +// Config holds the configuration for creating a new 1Password client. +type Config struct { + ConnectHost string + ConnectToken string + UserAgent string + ServiceAccountToken string + IntegrationName string + IntegrationVersion string +} + +// NewClient creates a new 1Password client based on the provided configuration. +func NewClient(config Config) (Client, error) { + if config.ServiceAccountToken != "" { + return sdk.NewClient(sdk.Config{ + ServiceAccountToken: config.ServiceAccountToken, + IntegrationName: config.IntegrationName, + IntegrationVersion: config.IntegrationVersion, + }) + } else if config.ConnectHost != "" && config.ConnectToken != "" { + return connect.NewClient(connect.Config{ + ConnectHost: config.ConnectHost, + ConnectToken: config.ConnectToken, + }), nil + } + return nil, errors.New("invalid configuration. Either Connect or Service Account credentials should be set") +}