Permalink
Browse files

add proper key management command with varied subcommands

get, store, copy, check and clear
  • Loading branch information...
justone committed May 27, 2018
1 parent bd13a5d commit 6db018c23866054fb0386f147cccc6ff98b41bab
Showing with 3,413 additions and 1,743 deletions.
  1. +19 −0 api/crocker.go
  2. +4 −0 api/pmb.go
  3. +0 −30 copy_key.go
  4. +0 −41 get_key.go
  5. +146 −0 key.go
  6. +92 −86 vendor/github.com/jessevdk/go-flags/README.md
  7. +27 −0 vendor/github.com/jessevdk/go-flags/arg.go
  8. +25 −27 vendor/github.com/jessevdk/go-flags/closest.go
  9. +375 −0 vendor/github.com/jessevdk/go-flags/command.go
  10. +0 −211 vendor/github.com/jessevdk/go-flags/command_private.go
  11. +309 −0 vendor/github.com/jessevdk/go-flags/completion.go
  12. +40 −23 vendor/github.com/jessevdk/go-flags/convert.go
  13. +41 −2 vendor/github.com/jessevdk/go-flags/error.go
  14. +257 −152 vendor/github.com/jessevdk/go-flags/flags.go
  15. +327 −0 vendor/github.com/jessevdk/go-flags/group.go
  16. +0 −271 vendor/github.com/jessevdk/go-flags/group_private.go
  17. +219 −67 vendor/github.com/jessevdk/go-flags/help.go
  18. +463 −6 vendor/github.com/jessevdk/go-flags/ini.go
  19. +0 −353 vendor/github.com/jessevdk/go-flags/ini_private.go
  20. +82 −17 vendor/github.com/jessevdk/go-flags/man.go
  21. +420 −6 vendor/github.com/jessevdk/go-flags/option.go
  22. +0 −121 vendor/github.com/jessevdk/go-flags/option_private.go
  23. +17 −5 vendor/github.com/jessevdk/go-flags/optstyle_other.go
  24. +28 −7 vendor/github.com/jessevdk/go-flags/optstyle_windows.go
  25. +517 −60 vendor/github.com/jessevdk/go-flags/parser.go
  26. +0 −253 vendor/github.com/jessevdk/go-flags/parser_private.go
  27. +1 −1 vendor/github.com/jessevdk/go-flags/termsize.go
  28. +1 −1 vendor/github.com/jessevdk/go-flags/termsize_nosysioctl.go
  29. 0 vendor/github.com/jessevdk/go-flags/{termsize_unix.go → tiocgwinsz_bsdish.go}
  30. 0 vendor/github.com/jessevdk/go-flags/{termsize_linux.go → tiocgwinsz_linux.go}
  31. 0 vendor/github.com/jessevdk/go-flags/{termsize_other.go → tiocgwinsz_other.go}
  32. +3 −3 vendor/vendor.json
@@ -45,3 +45,22 @@ func StoreCredHelperKey(keys string) error {
logrus.Debugf("stored cred helper creds", creds)
return nil
}
// ClearCredHelperKey tries to clear the key using the docker-credential-* set
// of utilities, as discovered by crocker (https://github.com/justone/crocker).
func ClearCredHelperKey() error {
cr, err := crocker.NewWithStrategy(crocker.MemThenStockStrategy{})
if err != nil {
return err
}
logrus.Debugf("found cred helper instance", cr)
err = cr.Erase(url)
if err != nil {
return err
}
logrus.Debugf("cleared cred helper creds")
return nil
}
@@ -119,6 +119,10 @@ func (pmb *PMB) CopyKey(id string) (*Connection, error) {
return nil, errors.New("No URI found, use '-p' to specify one")
}
func (pmb *PMB) GetConfigKey() string {
return pmb.config["key"]
}
var charactersForRandom = []byte("1234567890abcdefghijklmnopqrstuvwxyz")
var randSeeded = false

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
146 key.go
@@ -0,0 +1,146 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/justone/pmb/api"
)
type GetKeyCommand struct {
LocalCheck bool `short:"l" long:"local-check" description:"Check if there is a local key set, but don't attempt to verify."`
}
type StoreKeyCommand struct {
Key string `short:"k" env:"PMB_KEY" long:"key" description:"Key to store"`
File string `short:"f" long:"file" description:"Read key from file"`
}
type ClearKeyCommand struct{}
type CheckKeyCommand struct {
Key string `short:"k" env:"PMB_KEY" long:"key" description:"Key to check"`
File string `short:"f" long:"file" description:"Read key from file"`
}
type CopyKeyCommand struct{}
type KeyCommand struct {
Get GetKeyCommand `command:"get" description:"Get the PMB key."`
Store StoreKeyCommand `command:"store" description:"Store the PMB key, if possible."`
Clear ClearKeyCommand `command:"clear" description:"Clear the locally cached key."`
Check CheckKeyCommand `command:"check" description:"Check the key."`
Copy CopyKeyCommand `command:"copy" description:"Cause the key to be copied into the paste buffer."`
}
func (x *GetKeyCommand) Execute(args []string) error {
bus := pmb.GetPMB(globalOptions.Primary)
if x.LocalCheck {
if key, _ := pmb.GetCredHelperKey(); len(key) > 0 {
fmt.Println(key)
os.Exit(0)
}
os.Exit(1)
}
id := pmb.GenerateRandomID("getKey")
conn, err := bus.ConnectClient(id, !globalOptions.TrustKey)
if err != nil {
return err
}
fmt.Println(strings.Join(conn.Keys, ","))
return nil
}
func getKey(fileOpt, keyOpt string) (string, error) {
var key string
if file := fileOpt; len(file) > 0 {
if file == "-" {
stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return "", err
}
key = strings.TrimSpace(string(stdin))
} else if keyData, err := ioutil.ReadFile(file); err == nil {
key = string(keyData)
}
} else {
key = keyOpt
}
return key, nil
}
func (x *StoreKeyCommand) Execute(args []string) error {
key, err := getKey(x.File, x.Key)
if err != nil {
return err
}
err = pmb.StoreCredHelperKey(key)
if err != nil {
return err
}
return nil
}
func (x *ClearKeyCommand) Execute(args []string) error {
err := pmb.ClearCredHelperKey()
if err != nil {
os.Exit(0)
}
os.Exit(1)
return nil
}
func (x *CheckKeyCommand) Execute(args []string) error {
key, err := getKey(x.File, x.Key)
if err != nil {
return err
}
os.Setenv("PMB_KEY", key)
bus := pmb.GetPMB(globalOptions.Primary)
id := pmb.GenerateRandomID("checkKey")
_, err = bus.ConnectClient(id, !globalOptions.TrustKey)
if err != nil {
return err
}
return nil
}
func (x *CopyKeyCommand) Execute(args []string) error {
bus := pmb.GetPMB(globalOptions.Primary)
id := pmb.GenerateRandomID("copyKey")
_, err := bus.CopyKey(id)
if err != nil {
return err
}
return nil
}
func init() {
var keyCommand KeyCommand
_, err := parser.AddCommand("key",
"Manage key (low level).",
"",
&keyCommand)
if err != nil {
fmt.Println(err)
}
}
Oops, something went wrong.

0 comments on commit 6db018c

Please sign in to comment.