Files
kopia/cli/password.go
Jarek Kowalski cead806a3f blob: changed default shards from {3,3} to {1,3} (#1513)
* blob: changed default shards from {3,3} to {1,3}

Turns out for very large repository around 100TB (5M blobs),
we end up creating max ~16M directories which is way too much
and slows down listing. Currently each leaf directory only has a handful
of files.

Simple sharding of {3} should work much better and will end up creating
directories with meaningful shard sizes - 12 K files per directory
should not be too slow and will reduce the overhead of listing by
4096 times.

The change is done in a backwards-compatible way and will respect
custom sharding (.shards) file written by previous 0.9 builds
as well as older repositories that don't have the .shards file (which
we assume to be {3,3}).

* fixed compat tests
2021-11-16 06:02:04 -08:00

116 lines
2.6 KiB
Go

package cli
import (
"context"
"fmt"
"io"
"os"
"strings"
"github.com/pkg/errors"
"golang.org/x/term"
"github.com/kopia/kopia/internal/passwordpersist"
)
func askForNewRepositoryPassword(out io.Writer) (string, error) {
for {
p1, err := askPass(out, "Enter password to create new repository: ")
if err != nil {
return "", errors.Wrap(err, "password entry")
}
p2, err := askPass(out, "Re-enter password for verification: ")
if err != nil {
return "", errors.Wrap(err, "password verification")
}
if p1 != p2 {
fmt.Fprintln(out, "Passwords don't match!")
} else {
return p1, nil
}
}
}
func askForChangedRepositoryPassword(out io.Writer) (string, error) {
for {
p1, err := askPass(out, "Enter new password: ")
if err != nil {
return "", errors.Wrap(err, "password entry")
}
p2, err := askPass(out, "Re-enter password for verification: ")
if err != nil {
return "", errors.Wrap(err, "password verification")
}
if p1 != p2 {
fmt.Println("Passwords don't match!")
} else {
return p1, nil
}
}
}
func askForExistingRepositoryPassword(out io.Writer) (string, error) {
p1, err := askPass(out, "Enter password to open repository: ")
if err != nil {
return "", err
}
fmt.Fprintln(out)
return p1, nil
}
func (c *App) setPasswordFromToken(pwd string) {
c.password = pwd
}
func (c *App) getPasswordFromFlags(ctx context.Context, isCreate, allowPersistent bool) (string, error) {
switch {
case c.password != "":
// password provided via --password flag or KOPIA_PASSWORD environment variable
return strings.TrimSpace(c.password), nil
case isCreate:
// this is a new repository, ask for password
return askForNewRepositoryPassword(c.stdoutWriter)
case allowPersistent:
// try fetching the password from persistent storage specific to the configuration file.
pass, err := c.passwordPersistenceStrategy().GetPassword(ctx, c.repositoryConfigFileName())
if err == nil {
return pass, nil
}
if !errors.Is(err, passwordpersist.ErrPasswordNotFound) {
return "", errors.Wrap(err, "error getting persistent password")
}
}
// fall back to asking for existing password
return askForExistingRepositoryPassword(c.stdoutWriter)
}
// askPass presents a given prompt and asks the user for password.
func askPass(out io.Writer, prompt string) (string, error) {
for i := 0; i < 5; i++ {
fmt.Fprint(out, prompt)
passBytes, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", errors.Wrap(err, "password prompt error")
}
fmt.Fprintln(out)
if len(passBytes) == 0 {
continue
}
return string(passBytes), nil
}
return "", errors.New("can't get password")
}