Pass logger to print what what type of client is used Connect or Service Account

This commit is contained in:
Volodymyr Zotov
2025-06-16 21:04:22 -05:00
parent 1aa27fdba0
commit 55b5781d7a
2 changed files with 15 additions and 6 deletions

View File

@@ -152,7 +152,10 @@ func main() {
} }
// Setup One Password Client // Setup One Password Client
opClient, err := opclient.NewFromEnvironment(version.OperatorVersion) opClient, err := opclient.NewFromEnvironment(opclient.Config{
Logger: setupLog,
Version: version.OperatorVersion,
})
if err != nil { if err != nil {
setupLog.Error(err, "unable to create 1Password client") setupLog.Error(err, "unable to create 1Password client")
os.Exit(1) os.Exit(1)

View File

@@ -2,9 +2,10 @@ package client
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"github.com/go-logr/logr"
"github.com/1Password/onepassword-operator/pkg/onepassword/client/connect" "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/client/sdk"
"github.com/1Password/onepassword-operator/pkg/onepassword/model" "github.com/1Password/onepassword-operator/pkg/onepassword/model"
@@ -18,8 +19,13 @@ type Client interface {
GetVaultsByTitle(title string) ([]model.Vault, error) GetVaultsByTitle(title string) ([]model.Vault, error)
} }
type Config struct {
Logger logr.Logger
Version string
}
// NewFromEnvironment creates a new 1Password client based on the provided configuration. // NewFromEnvironment creates a new 1Password client based on the provided configuration.
func NewFromEnvironment(integrationVersion string) (Client, error) { func NewFromEnvironment(cfg Config) (Client, error) {
connectHost, _ := os.LookupEnv("OP_CONNECT_HOST") connectHost, _ := os.LookupEnv("OP_CONNECT_HOST")
connectToken, _ := os.LookupEnv("OP_CONNECT_TOKEN") connectToken, _ := os.LookupEnv("OP_CONNECT_TOKEN")
serviceAccountToken, _ := os.LookupEnv("OP_SERVICE_ACCOUNT_TOKEN") serviceAccountToken, _ := os.LookupEnv("OP_SERVICE_ACCOUNT_TOKEN")
@@ -29,16 +35,16 @@ func NewFromEnvironment(integrationVersion string) (Client, error) {
} }
if serviceAccountToken != "" { if serviceAccountToken != "" {
fmt.Printf("Using Service Account Token") cfg.Logger.Info("Using Service Account Token")
return sdk.NewClient(sdk.Config{ return sdk.NewClient(sdk.Config{
ServiceAccountToken: serviceAccountToken, ServiceAccountToken: serviceAccountToken,
IntegrationName: "1password-operator", IntegrationName: "1password-operator",
IntegrationVersion: integrationVersion, IntegrationVersion: cfg.Version,
}) })
} }
if connectHost != "" && connectToken != "" { if connectHost != "" && connectToken != "" {
fmt.Printf("Using Connect") cfg.Logger.Info("Using 1Password Connect")
return connect.NewClient(connect.Config{ return connect.NewClient(connect.Config{
ConnectHost: connectHost, ConnectHost: connectHost,
ConnectToken: connectToken, ConnectToken: connectToken,