diff --git a/examples/repository/main.go b/examples/repository/main.go index d3bb11578..964ec4a32 100644 --- a/examples/repository/main.go +++ b/examples/repository/main.go @@ -5,6 +5,7 @@ import ( "context" "os" + "github.com/kopia/kopia/auth" "github.com/kopia/kopia/repo" colorable "github.com/mattn/go-colorable" "github.com/rs/zerolog" @@ -15,12 +16,20 @@ func main() { ctx := context.Background() log.Logger = zerolog.New(zerolog.ConsoleWriter{Out: colorable.NewColorableStderr()}).With().Timestamp().Logger() - if err := setupRepositoryAndConnect(ctx); err != nil { + // set up credentials + creds, crederr := auth.Password(masterPassword) + if crederr != nil { + log.Fatal().Msgf("invalid password: %v", crederr) + } + + if err := setupRepositoryAndConnect(ctx, creds); err != nil { log.Error().Msgf("unable to set up repository: %v", err) os.Exit(1) } - r, err := repo.Open(ctx, configFile, &repo.Options{}) + r, err := repo.Open(ctx, configFile, &repo.Options{ + Credentials: creds, + }) if err != nil { log.Error().Msgf("unable to open repository: %v", err) os.Exit(1) @@ -29,6 +38,7 @@ func main() { uploadAndDownloadObjects(ctx, r) + // Now list blocks found in the repository. blks, err := r.Blocks.ListBlocks("") if err != nil { log.Fatal().Msgf("err: %v") diff --git a/examples/repository/setup_repository.go b/examples/repository/setup_repository.go index c89d4bba1..c5bd29d1d 100644 --- a/examples/repository/setup_repository.go +++ b/examples/repository/setup_repository.go @@ -20,13 +20,7 @@ const ( cacheDirectory = "/tmp/kopia-example/cache" ) -func setupRepositoryAndConnect(ctx context.Context) error { - // set up credentials - creds, err := auth.Password(masterPassword) - if err != nil { - return fmt.Errorf("invalid password: %v", err) - } - +func setupRepositoryAndConnect(ctx context.Context, creds auth.Credentials) error { st, err := gcs.New(ctx, &gcs.Options{ BucketName: bucketName, }) @@ -46,7 +40,7 @@ func setupRepositoryAndConnect(ctx context.Context) error { // now establish connection to repository and create configuration file. if err := repo.Connect(ctx, configFile, st, creds, repo.ConnectOptions{ - PersistCredentials: true, + PersistCredentials: false, CachingOptions: block.CachingOptions{ CacheDirectory: cacheDirectory, MaxCacheSizeBytes: 100000000, diff --git a/repo/connect.go b/repo/connect.go index d9fc84000..4ffd6adbf 100644 --- a/repo/connect.go +++ b/repo/connect.go @@ -77,9 +77,7 @@ func Connect(ctx context.Context, configFile string, st storage.Storage, creds a // now verify that the repository can be opened with the provided config file. r, err := Open(ctx, configFile, &Options{ - CredentialsCallback: func() (auth.Credentials, error) { - return creds, nil - }, + Credentials: creds, }) if err != nil { return err diff --git a/repo/open.go b/repo/open.go index f6c1c10c6..3054a31c9 100644 --- a/repo/open.go +++ b/repo/open.go @@ -22,7 +22,8 @@ import ( // Options provides configuration parameters for connection to a repository. type Options struct { - CredentialsCallback func() (auth.Credentials, error) // Provides credentials required to open the repository if not persisted. + Credentials auth.Credentials // Provides credentials required to open the repository if not persisted. + CredentialsCallback func() (auth.Credentials, error) // Callback that provides credentials required to open the repository if not persisted. TraceStorage func(f string, args ...interface{}) // Logs all storage access using provided Printf-style function DisableCache bool // disable caching @@ -90,6 +91,10 @@ func Open(ctx context.Context, configFile string, options *Options) (rep *Reposi } func getCredentials(lc *config.LocalConfig, options *Options) (auth.Credentials, error) { + if options.Credentials != nil { + return options.Credentials, nil + } + if len(lc.Connection.Key) > 0 { log.Debug().Msg("getting credentials from master key") return auth.MasterKey(lc.Connection.Key)