mirror of
https://github.com/kopia/kopia.git
synced 2026-10-27 16:48:59 -04:00
example: stopped persisting credentials
This commit is contained in:
1 parent
8763c31db9
commit
49dfc43ad3
4 files changed
+21
-14
No files matched your search
@@ -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")
|
||||
|
||||
@@ -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,
|
||||
|
||||
+1
-3
@@ -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
|
||||
|
||||
+6
-1
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user