lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 2cd5e350b6210a1a76910209a7d9fcc1efdd0c25
parent ac7ae8065c02038d9d131e107aaea0e06e230c0f
Author: Sean Enck <sean@ttypty.com>
Date:   Wed, 21 Jul 2021 18:45:52 -0400

linting

Diffstat:
Mcmd/lb/main.go | 20++++++++++----------
Mcmd/pwgen/main.go | 14+++++++-------
Mcmd/rw/main.go | 2+-
Mcmd/stats/main.go | 15+++++----------
Mcmd/totp/main.go | 6+++---
Minternal/encdec.go | 20+++++++++-----------
Minternal/utils.go | 14+++++++-------
Mversion.sh | 2+-
8 files changed, 43 insertions(+), 50 deletions(-)

diff --git a/cmd/lb/main.go b/cmd/lb/main.go @@ -14,7 +14,7 @@ import ( func getEntry(store string, args []string, idx int) string { if len(args) != idx+1 { - internal.Die("invalid entry given", fmt.Errorf("specific entry required")) + internal.Die("invalid entry given", internal.NewLockboxError("specific entry required")) } return filepath.Join(store, args[idx]) + internal.Extension } @@ -64,7 +64,7 @@ func readInput() (string, error) { return "", err } if first != second { - return "", fmt.Errorf("passwords do NOT match") + return "", internal.NewLockboxError("passwords do NOT match") } return first, nil } @@ -100,7 +100,7 @@ func clipboard(value string) { func main() { args := os.Args if len(args) < 2 { - internal.Die("missing arguments", fmt.Errorf("requires subcommand")) + internal.Die("missing arguments", internal.NewLockboxError("requires subcommand")) } command := args[1] store := internal.GetStore() @@ -120,16 +120,16 @@ func main() { idx := 2 switch len(args) { case 2: - internal.Die("insert missing required arguments", fmt.Errorf("entry required")) + internal.Die("insert missing required arguments", internal.NewLockboxError("entry required")) case 3: case 4: multi = args[2] == "-m" if !multi { - internal.Die("multi-line insert must be after 'insert'", fmt.Errorf("invalid command")) + internal.Die("multi-line insert must be after 'insert'", internal.NewLockboxError("invalid command")) } idx = 3 default: - internal.Die("too many arguments", fmt.Errorf("insert can only perform one operation")) + internal.Die("too many arguments", internal.NewLockboxError("insert can only perform one operation")) } isPipe := isInputFromPipe() entry := getEntry(store, args, idx) @@ -147,7 +147,7 @@ func main() { } } } - password := "" + var password string if !multi && !isPipe { input, err := readInput() if err != nil { @@ -162,7 +162,7 @@ func main() { password = input } if password == "" { - internal.Die("empty password provided", fmt.Errorf("password can NOT be empty")) + internal.Die("empty password provided", internal.NewLockboxError("password can NOT be empty")) } l, err := internal.NewLockbox("", "", entry) if err != nil { @@ -175,7 +175,7 @@ func main() { case "rm": entry := getEntry(store, args, 2) if !internal.PathExists(entry) { - internal.Die("does not exists", fmt.Errorf("can not delete unknown entry")) + internal.Die("does not exists", internal.NewLockboxError("can not delete unknown entry")) } if confirm("remove entry") { os.Remove(entry) @@ -183,7 +183,7 @@ func main() { case "show", "-c", "clip": entry := getEntry(store, args, 2) if !internal.PathExists(entry) { - internal.Die("invalid entry", fmt.Errorf("entry not found")) + internal.Die("invalid entry", internal.NewLockboxError("entry not found")) } l, err := internal.NewLockbox("", "", entry) if err != nil { diff --git a/cmd/pwgen/main.go b/cmd/pwgen/main.go @@ -40,7 +40,7 @@ func main() { special := strings.TrimSpace(os.Getenv("PWGEN_SPECIAL")) transform := *rawTokens if len(allowed) == 0 { - internal.Die("no allowed characters found", fmt.Errorf("allowed characters required")) + internal.Die("no allowed characters found", internal.NewLockboxError("allowed characters required")) } var paths []string parts := strings.Split(src, ":") @@ -66,7 +66,7 @@ func main() { } } if len(paths) == 0 { - internal.Die("no paths found for generation", fmt.Errorf("unable to read paths")) + internal.Die("no paths found for generation", internal.NewLockboxError("unable to read paths")) } result := "" l := *length @@ -79,7 +79,7 @@ func main() { for len(result) < l { if specialChars > 0 && makeChoice() { subChar := rand.Intn(specialChars) - result = result + string(specials[subChar]) + result += string(specials[subChar]) } sub := rand.Intn(pathOptions) name := paths[sub] @@ -96,7 +96,7 @@ func main() { name = newValue case transformModeSed: if len(sedPattern) == 0 { - internal.Die("unable to use sed transform without pattern", fmt.Errorf("set PWGEN_SED")) + internal.Die("unable to use sed transform without pattern", internal.NewLockboxError("set PWGEN_SED")) } cmd := exec.Command("sed", "-e", sedPattern) stdin, err := cmd.StdinPipe() @@ -120,15 +120,15 @@ func main() { } errors := strings.TrimSpace(stderr.String()) if len(errors) > 0 { - internal.Die("sed stderr failure", fmt.Errorf(errors)) + internal.Die("sed stderr failure", internal.NewLockboxError(errors)) } name = strings.TrimSpace(stdout.String()) case transformModeNone: break default: - internal.Die("unknown transform mode", fmt.Errorf(transform)) + internal.Die("unknown transform mode", internal.NewLockboxError(transform)) } - result = result + name + result += name } fmt.Println(result[0:l]) } diff --git a/cmd/rw/main.go b/cmd/rw/main.go @@ -29,6 +29,6 @@ func main() { } fmt.Println(string(results)) default: - internal.Die("invalid mode", fmt.Errorf("bad mode")) + internal.Die("invalid mode", internal.NewLockboxError("bad mode")) } } diff --git a/cmd/stats/main.go b/cmd/stats/main.go @@ -18,7 +18,7 @@ type ( Date string `json:"date"` } - // Stats are general entry stats + // Stats are general entry stats. Stats struct { Entry string `json:"entry"` Name string `json:"name"` @@ -29,11 +29,6 @@ type ( func main() { args := os.Args - filtering := len(args) > 1 - filter := "" - if filtering { - filter = args[1] - } store := internal.GetStore() items, err := internal.Find(store, true) if err != nil { @@ -41,8 +36,8 @@ func main() { } results := []Stats{} for _, item := range items { - if filtering { - if !strings.HasPrefix(item, filter) { + if len(args) > 1 { + if !strings.HasPrefix(item, args[1]) { continue } } @@ -63,7 +58,7 @@ func main() { } parts := strings.Split(cleaned, " ") if len(parts) != 2 { - internal.Die("invalid format entry", fmt.Errorf("mismatch between format string and struct?")) + internal.Die("invalid format entry", internal.NewLockboxError("mismatch between format string and struct?")) } history = append(history, History{Hash: parts[0], Date: parts[1]}) } @@ -71,7 +66,7 @@ func main() { results = append(results, stat) } if len(results) == 0 { - internal.Die("found no entries", fmt.Errorf("no entries")) + internal.Die("found no entries", internal.NewLockboxError("no entries")) } j, err := json.MarshalIndent(results, "", " ") if err != nil { diff --git a/cmd/totp/main.go b/cmd/totp/main.go @@ -31,7 +31,7 @@ func list() ([]string, error) { } } if len(results) == 0 { - return nil, fmt.Errorf("no objects found") + return nil, internal.NewLockboxError("no objects found") } return results, nil } @@ -48,7 +48,7 @@ func display(token string) error { tok := strings.TrimSpace(token) store := filepath.Join(getEnv(), tok+internal.Extension) if !internal.PathExists(store) { - return fmt.Errorf("object does not exist") + return internal.NewLockboxError("object does not exist") } l, err := internal.NewLockbox("", "", store) if err != nil { @@ -102,7 +102,7 @@ func display(token string) error { func main() { args := os.Args if len(args) != 2 { - internal.Die("subkey required", fmt.Errorf("invalid arguments")) + internal.Die("subkey required", internal.NewLockboxError("invalid arguments")) } cmd := args[1] if cmd == "list" || cmd == "ls" { diff --git a/internal/encdec.go b/internal/encdec.go @@ -2,7 +2,6 @@ package internal import ( "crypto/rand" - "fmt" "io" random "math/rand" "os" @@ -17,21 +16,21 @@ const ( keyLength = 32 nonceLength = 24 padLength = 256 - // MacOSKeyMode is macOS based key resolution + // MacOSKeyMode is macOS based key resolution. MacOSKeyMode = "macos" - // PlainKeyMode is plaintext based key resolution + // PlainKeyMode is plaintext based key resolution. PlainKeyMode = "plaintext" ) type ( - // Lockbox represents a method to encrypt/decrypt locked files + // Lockbox represents a method to encrypt/decrypt locked files. Lockbox struct { secret [keyLength]byte file string } ) -// NewLockbox creates a new lockbox for encryption/decryption +// NewLockbox creates a new lockbox for encryption/decryption. func NewLockbox(key, keyMode, file string) (Lockbox, error) { useKeyMode := keyMode if useKeyMode == "" { @@ -47,7 +46,7 @@ func NewLockbox(key, keyMode, file string) (Lockbox, error) { } if len(b) > keyLength { - return Lockbox{}, fmt.Errorf("key is too large for use") + return Lockbox{}, NewLockboxError("key is too large for use") } for len(b) < keyLength { @@ -73,8 +72,7 @@ func getKey(keyMode, name string) ([]byte, error) { case PlainKeyMode: data = []byte(name) default: - return nil, fmt.Errorf("unknown keymode") - + return nil, NewLockboxError("unknown keymode") } return []byte(strings.TrimSpace(string(data))), nil } @@ -83,7 +81,7 @@ func init() { random.Seed(time.Now().UnixNano()) } -// Encrypt will encrypt contents to file +// Encrypt will encrypt contents to file. func (l Lockbox) Encrypt(datum []byte) error { var nonce [nonceLength]byte padTo := random.Intn(padLength) @@ -110,7 +108,7 @@ func (l Lockbox) Encrypt(datum []byte) error { return os.WriteFile(l.file, encrypted, 0600) } -// Decrypt will decrypt an object from file +// Decrypt will decrypt an object from file. func (l Lockbox) Decrypt() ([]byte, error) { var nonce [nonceLength]byte encrypted, err := os.ReadFile(l.file) @@ -120,7 +118,7 @@ func (l Lockbox) Decrypt() ([]byte, error) { copy(nonce[:], encrypted[:nonceLength]) decrypted, ok := secretbox.Open(nil, encrypted[nonceLength:], &nonce, &l.secret) if !ok { - return nil, fmt.Errorf("decrypt not ok") + return nil, NewLockboxError("decrypt not ok") } padding := int(decrypted[0]) diff --git a/internal/utils.go b/internal/utils.go @@ -12,20 +12,20 @@ import ( ) const ( - // Extension is the lockbox file extension + // Extension is the lockbox file extension. Extension = ".lb" ) -// GetStore gets the lockbox directory +// GetStore gets the lockbox directory. func GetStore() string { return os.Getenv("LOCKBOX_STORE") } -// Find will find all lockbox files in a directory store +// Find will find all lockbox files in a directory store. func Find(store string, display bool) ([]string, error) { var results []string if !PathExists(store) { - return nil, fmt.Errorf("store does not exists") + return nil, NewLockboxError("store does not exists") } err := filepath.Walk(store, func(path string, info fs.FileInfo, err error) error { if err != nil { @@ -52,14 +52,14 @@ func Find(store string, display bool) ([]string, error) { return results, nil } -// Die will print messages and exit +// Die will print messages and exit. func Die(message string, err error) { msg := fmt.Sprintf("%s (%v)", message, err) fmt.Fprintln(os.Stderr, msg) os.Exit(1) } -// Stdin reads one (or more) lines from stdin +// Stdin reads one (or more) lines from stdin. func Stdin(one bool) ([]byte, error) { scanner := bufio.NewScanner(os.Stdin) var b bytes.Buffer @@ -76,7 +76,7 @@ func Stdin(one bool) ([]byte, error) { return b.Bytes(), nil } -// PathExists indicates if a path exists +// PathExists indicates if a path exists. func PathExists(path string) bool { if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { diff --git a/version.sh b/version.sh @@ -3,7 +3,7 @@ GEN=$1 VERS=$2 VERS_NAME="Version" _version_info() { - echo "// Version is the hash/version info for lb" + echo "// Version is the hash/version info for lb." git log -n 1 --format=%h | sed 's/^/'$VERS_NAME' = "/g;s/$/"/g' }