mirror of https://github.com/mitchell/selfpass.git
Modify cmds that list metadata to not sort; refactor to credentials flags;
fix issue with init command
This commit is contained in:
parent
d42af8e859
commit
2969189d98
|
@ -2,11 +2,12 @@ package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/AlecAivazis/survey.v1"
|
"gopkg.in/AlecAivazis/survey.v1"
|
||||||
|
|
||||||
"github.com/mitchell/selfpass/services/credentials/types"
|
"github.com/mitchell/selfpass/services/credentials/types"
|
||||||
|
@ -14,6 +15,40 @@ import (
|
||||||
|
|
||||||
type CredentialsClientInit func(ctx context.Context) (c types.CredentialsClient)
|
type CredentialsClientInit func(ctx context.Context) (c types.CredentialsClient)
|
||||||
|
|
||||||
|
var errSourceNotFound = errors.New("source host not found")
|
||||||
|
|
||||||
|
type credentialFlagSet struct {
|
||||||
|
includePasswordFlags bool
|
||||||
|
includeHostFlag bool
|
||||||
|
|
||||||
|
sourceHost string
|
||||||
|
noNumbers bool
|
||||||
|
noSpecials bool
|
||||||
|
length uint
|
||||||
|
}
|
||||||
|
|
||||||
|
func (set credentialFlagSet) withPasswordFlags() credentialFlagSet {
|
||||||
|
set.includePasswordFlags = true
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
func (set credentialFlagSet) withHostFlag() credentialFlagSet {
|
||||||
|
set.includeHostFlag = true
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
func (set *credentialFlagSet) register(cmd *cobra.Command) {
|
||||||
|
if set.includeHostFlag {
|
||||||
|
cmd.Flags().StringVarP(&set.sourceHost, "source-host", "s", "", "filter results to this source host")
|
||||||
|
}
|
||||||
|
|
||||||
|
if set.includePasswordFlags {
|
||||||
|
cmd.Flags().BoolVarP(&set.noNumbers, "no-numbers", "n", false, "do not use numbers in the generated password")
|
||||||
|
cmd.Flags().BoolVarP(&set.noSpecials, "no-specials", "p", false, "do not use special characters in the generated password")
|
||||||
|
cmd.Flags().UintVarP(&set.length, "length", "l", 32, "length of the generated password")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func check(err error) {
|
func check(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
@ -21,18 +56,17 @@ func check(err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func selectCredential(client types.CredentialsClient) types.Credential {
|
func selectCredential(client types.CredentialsClient, sourceHost string) types.Credential {
|
||||||
var (
|
var (
|
||||||
idKey string
|
idKey string
|
||||||
source string
|
|
||||||
prompt survey.Prompt
|
prompt survey.Prompt
|
||||||
)
|
)
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
mdch, errch := client.GetAllMetadata(ctx, "")
|
mdch, errch := client.GetAllMetadata(ctx, sourceHost)
|
||||||
mds := map[string][]types.Metadata{}
|
var mds []types.Metadata
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
|
@ -50,29 +84,42 @@ receive:
|
||||||
break receive
|
break receive
|
||||||
}
|
}
|
||||||
|
|
||||||
mds[md.SourceHost] = append(mds[md.SourceHost], md)
|
mds = append(mds, md)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sources := []string{}
|
var sources []string
|
||||||
for source := range mds {
|
mdmap := map[string][]types.Metadata{}
|
||||||
sources = append(sources, source)
|
for _, md := range mds {
|
||||||
|
tmds := mdmap[md.SourceHost]
|
||||||
|
|
||||||
|
if tmds == nil {
|
||||||
|
mdmap[md.SourceHost] = []types.Metadata{md}
|
||||||
|
sources = append(sources, md.SourceHost)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
mdmap[md.SourceHost] = append(mdmap[md.SourceHost], md)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(sources)
|
if sourceHost == "" {
|
||||||
|
prompt = &survey.Select{
|
||||||
|
Message: "Source host:",
|
||||||
|
Options: sources,
|
||||||
|
PageSize: 20,
|
||||||
|
VimMode: true,
|
||||||
|
}
|
||||||
|
|
||||||
prompt = &survey.Select{
|
check(survey.AskOne(prompt, &sourceHost, nil))
|
||||||
Message: "Source host:",
|
|
||||||
Options: sources,
|
|
||||||
PageSize: 20,
|
|
||||||
VimMode: true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
check(survey.AskOne(prompt, &source, nil))
|
if len(mdmap[sourceHost]) == 0 {
|
||||||
|
check(errSourceNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
keys := []string{}
|
keys := []string{}
|
||||||
keyIDMap := map[string]string{}
|
keyIDMap := map[string]string{}
|
||||||
for _, md := range mds[source] {
|
for _, md := range mdmap[sourceHost] {
|
||||||
key := md.Primary
|
key := md.Primary
|
||||||
if md.Tag != "" {
|
if md.Tag != "" {
|
||||||
key += "-" + md.Tag
|
key += "-" + md.Tag
|
||||||
|
@ -81,8 +128,6 @@ receive:
|
||||||
keyIDMap[key] = md.ID
|
keyIDMap[key] = md.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(keys)
|
|
||||||
|
|
||||||
prompt = &survey.Select{
|
prompt = &survey.Select{
|
||||||
Message: "Primary user key (and tag):",
|
Message: "Primary user key (and tag):",
|
||||||
Options: keys,
|
Options: keys,
|
||||||
|
|
|
@ -18,9 +18,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeCreate(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
func makeCreate(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
||||||
var length uint
|
flags := credentialFlagSet{}.withPasswordFlags()
|
||||||
var noNumbers bool
|
|
||||||
var noSpecials bool
|
|
||||||
|
|
||||||
createCmd := &cobra.Command{
|
createCmd := &cobra.Command{
|
||||||
Use: "create",
|
Use: "create",
|
||||||
|
@ -77,7 +75,7 @@ password.`,
|
||||||
check(survey.AskOne(prompt, &newpass, nil))
|
check(survey.AskOne(prompt, &newpass, nil))
|
||||||
|
|
||||||
if newpass {
|
if newpass {
|
||||||
ci.Password = crypto.GeneratePassword(int(length), !noNumbers, !noSpecials)
|
ci.Password = crypto.GeneratePassword(int(flags.length), !flags.noNumbers, !flags.noSpecials)
|
||||||
|
|
||||||
var copypass bool
|
var copypass bool
|
||||||
prompt = &survey.Confirm{Message: "Copy new pass to clipboard?", Default: true}
|
prompt = &survey.Confirm{Message: "Copy new pass to clipboard?", Default: true}
|
||||||
|
@ -130,10 +128,15 @@ password.`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*25)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
c, err := initClient(ctx).Create(ctx, ci)
|
client := initClient(ctx)
|
||||||
|
|
||||||
|
ctx, cancel = context.WithTimeout(context.Background(), time.Second*25)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c, err := client.Create(ctx, ci)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
fmt.Println(c)
|
fmt.Println(c)
|
||||||
|
@ -147,9 +150,7 @@ password.`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
createCmd.Flags().BoolVarP(&noNumbers, "no-numbers", "n", false, "do not use numbers in the generated password")
|
flags.register(createCmd)
|
||||||
createCmd.Flags().BoolVarP(&noSpecials, "no-specials", "s", false, "do not use special characters in the generated password")
|
|
||||||
createCmd.Flags().UintVarP(&length, "length", "l", 32, "length of the generated password")
|
|
||||||
|
|
||||||
return createCmd
|
return createCmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,18 +10,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeDelete(initClient CredentialsClientInit) *cobra.Command {
|
func makeDelete(initClient CredentialsClientInit) *cobra.Command {
|
||||||
|
flags := credentialFlagSet{}.withHostFlag()
|
||||||
|
|
||||||
deleteCmd := &cobra.Command{
|
deleteCmd := &cobra.Command{
|
||||||
Use: "delete",
|
Use: "delete",
|
||||||
Short: "Delete a credential using the given ID",
|
Short: "Delete a credential using the given ID",
|
||||||
Long: `Delete a credential using the given ID, permanently. THERE IS NO UNDOING THIS ACTION.`,
|
Long: `Delete a credential using the given ID, permanently. THERE IS NO UNDOING THIS ACTION.`,
|
||||||
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
client := initClient(ctx)
|
client := initClient(ctx)
|
||||||
|
|
||||||
cred := selectCredential(client)
|
cred := selectCredential(client, flags.sourceHost)
|
||||||
|
|
||||||
fmt.Println(cred)
|
fmt.Println(cred)
|
||||||
|
|
||||||
|
@ -38,5 +40,7 @@ func makeDelete(initClient CredentialsClientInit) *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flags.register(deleteCmd)
|
||||||
|
|
||||||
return deleteCmd
|
return deleteCmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeGet(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
func makeGet(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
||||||
|
flags := credentialFlagSet{}.withHostFlag()
|
||||||
|
|
||||||
getCmd := &cobra.Command{
|
getCmd := &cobra.Command{
|
||||||
Use: "get",
|
Use: "get",
|
||||||
Short: "Get a credential info and copy password to clipboard",
|
Short: "Get a credential info and copy password to clipboard",
|
||||||
|
@ -29,14 +31,14 @@ decrypting password.`,
|
||||||
prompt survey.Prompt
|
prompt survey.Prompt
|
||||||
)
|
)
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
client := initClient(ctx)
|
client := initClient(ctx)
|
||||||
masterpass, cfg, err := repo.OpenConfig()
|
masterpass, cfg, err := repo.OpenConfig()
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
cred := selectCredential(client)
|
cred := selectCredential(client, flags.sourceHost)
|
||||||
|
|
||||||
fmt.Println(cred)
|
fmt.Println(cred)
|
||||||
|
|
||||||
|
@ -90,5 +92,7 @@ decrypting password.`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flags.register(getCmd)
|
||||||
|
|
||||||
return getCmd
|
return getCmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/AlecAivazis/survey.v1"
|
"gopkg.in/AlecAivazis/survey.v1"
|
||||||
|
|
||||||
|
"github.com/mitchell/selfpass/sp/repositories"
|
||||||
"github.com/mitchell/selfpass/sp/types"
|
"github.com/mitchell/selfpass/sp/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,6 +32,9 @@ the users private key, and server certificates. (All of which will be encrypted)
|
||||||
privateKey = strings.Replace(uuid.New().String(), "-", "", -1)
|
privateKey = strings.Replace(uuid.New().String(), "-", "", -1)
|
||||||
)
|
)
|
||||||
_, cfg, err := repo.OpenConfig()
|
_, cfg, err := repo.OpenConfig()
|
||||||
|
if err == repositories.ErrNoConfigFound {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
prompt = &survey.Password{Message: "New master password:"}
|
prompt = &survey.Password{Message: "New master password:"}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package commands
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -13,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeList(initClient CredentialsClientInit) *cobra.Command {
|
func makeList(initClient CredentialsClientInit) *cobra.Command {
|
||||||
var sourceHost string
|
flags := credentialFlagSet{}.withHostFlag()
|
||||||
|
|
||||||
listCmd := &cobra.Command{
|
listCmd := &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
|
@ -22,11 +21,16 @@ func makeList(initClient CredentialsClientInit) *cobra.Command {
|
||||||
includes almost all the information but the most sensitive.`,
|
includes almost all the information but the most sensitive.`,
|
||||||
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
mdch, errch := initClient(ctx).GetAllMetadata(ctx, sourceHost)
|
client := initClient(ctx)
|
||||||
mds := map[string][]types.Metadata{}
|
|
||||||
|
ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
mdch, errch := client.GetAllMetadata(ctx, flags.sourceHost)
|
||||||
|
var mds []types.Metadata
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
|
@ -44,32 +48,40 @@ includes almost all the information but the most sensitive.`,
|
||||||
break receive
|
break receive
|
||||||
}
|
}
|
||||||
|
|
||||||
mds[md.SourceHost] = append(mds[md.SourceHost], md)
|
mds = append(mds, md)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sources := []string{}
|
var sources []string
|
||||||
for source := range mds {
|
mdmap := map[string][]types.Metadata{}
|
||||||
sources = append(sources, source)
|
for _, md := range mds {
|
||||||
|
tmds := mdmap[md.SourceHost]
|
||||||
|
|
||||||
|
if tmds == nil {
|
||||||
|
mdmap[md.SourceHost] = []types.Metadata{md}
|
||||||
|
sources = append(sources, md.SourceHost)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
mdmap[md.SourceHost] = append(mdmap[md.SourceHost], md)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(sources)
|
if flags.sourceHost == "" {
|
||||||
|
prompt := &survey.Select{
|
||||||
|
Message: "Source host:",
|
||||||
|
Options: sources,
|
||||||
|
PageSize: 20,
|
||||||
|
VimMode: true,
|
||||||
|
}
|
||||||
|
|
||||||
prompt := &survey.Select{
|
check(survey.AskOne(prompt, &flags.sourceHost, nil))
|
||||||
Message: "Source host:",
|
|
||||||
Options: sources,
|
|
||||||
PageSize: 20,
|
|
||||||
VimMode: true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var source string
|
if len(mdmap[flags.sourceHost]) == 0 {
|
||||||
check(survey.AskOne(prompt, &source, nil))
|
check(errSourceNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
sort.Slice(mds[source], func(i, j int) bool {
|
for _, md := range mdmap[flags.sourceHost] {
|
||||||
return mds[source][i].Primary < mds[source][j].Primary
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, md := range mds[source] {
|
|
||||||
fmt.Println(md)
|
fmt.Println(md)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,5 +89,7 @@ includes almost all the information but the most sensitive.`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flags.register(listCmd)
|
||||||
|
|
||||||
return listCmd
|
return listCmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,9 +18,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeUpdate(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
func makeUpdate(repo clitypes.ConfigRepo, initClient CredentialsClientInit) *cobra.Command {
|
||||||
var length uint
|
flags := credentialFlagSet{}.withHostFlag().withPasswordFlags()
|
||||||
var noNumbers bool
|
|
||||||
var noSpecials bool
|
|
||||||
|
|
||||||
updateCmd := &cobra.Command{
|
updateCmd := &cobra.Command{
|
||||||
Use: "update",
|
Use: "update",
|
||||||
|
@ -40,12 +38,12 @@ password.`,
|
||||||
masterpass, cfg, err := repo.OpenConfig()
|
masterpass, cfg, err := repo.OpenConfig()
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
client := initClient(ctx)
|
client := initClient(ctx)
|
||||||
|
|
||||||
cred := selectCredential(client)
|
cred := selectCredential(client, flags.sourceHost)
|
||||||
|
|
||||||
mdqs := []*survey.Question{
|
mdqs := []*survey.Question{
|
||||||
{
|
{
|
||||||
|
@ -111,7 +109,7 @@ password.`,
|
||||||
check(survey.AskOne(prompt, &randpass, nil))
|
check(survey.AskOne(prompt, &randpass, nil))
|
||||||
|
|
||||||
if randpass {
|
if randpass {
|
||||||
ci.Password = crypto.GeneratePassword(int(length), !noNumbers, !noSpecials)
|
ci.Password = crypto.GeneratePassword(int(flags.length), !flags.noNumbers, !flags.noSpecials)
|
||||||
|
|
||||||
var copypass bool
|
var copypass bool
|
||||||
prompt = &survey.Confirm{Message: "Copy new pass to clipboard?", Default: true}
|
prompt = &survey.Confirm{Message: "Copy new pass to clipboard?", Default: true}
|
||||||
|
@ -182,9 +180,7 @@ password.`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCmd.Flags().BoolVarP(&noNumbers, "no-numbers", "n", false, "do not use numbers in the generated password")
|
flags.register(updateCmd)
|
||||||
updateCmd.Flags().BoolVarP(&noSpecials, "no-specials", "s", false, "do not use special characters in the generated password")
|
|
||||||
updateCmd.Flags().UintVarP(&length, "length", "l", 32, "length of the generated password")
|
|
||||||
|
|
||||||
return updateCmd
|
return updateCmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ import (
|
||||||
"github.com/mitchell/selfpass/sp/types"
|
"github.com/mitchell/selfpass/sp/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrNoConfigFound = errors.New("no config found, run 'init' command")
|
||||||
|
|
||||||
func NewConfigManager(cfgFile *string) *ConfigManager {
|
func NewConfigManager(cfgFile *string) *ConfigManager {
|
||||||
return &ConfigManager{
|
return &ConfigManager{
|
||||||
cfgFile: cfgFile,
|
cfgFile: cfgFile,
|
||||||
|
@ -57,7 +59,7 @@ func (mgr *ConfigManager) OpenConfig() (output string, v *viper.Viper, err error
|
||||||
var configDecrypted bool
|
var configDecrypted bool
|
||||||
|
|
||||||
if _, err := os.Open(cfg); os.IsNotExist(err) {
|
if _, err := os.Open(cfg); os.IsNotExist(err) {
|
||||||
return output, mgr.v, fmt.Errorf("no config found, run 'init' command")
|
return output, mgr.v, ErrNoConfigFound
|
||||||
}
|
}
|
||||||
|
|
||||||
prompt := &survey.Password{Message: "Master password:"}
|
prompt := &survey.Password{Message: "Master password:"}
|
||||||
|
|
Loading…
Reference in New Issue