2019-05-28 01:16:50 +00:00
|
|
|
package commands
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-01 03:17:38 +00:00
|
|
|
"errors"
|
2019-05-22 15:22:40 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-06-16 11:02:49 +00:00
|
|
|
"time"
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-06-16 11:02:49 +00:00
|
|
|
"gopkg.in/AlecAivazis/survey.v1"
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-07-11 02:33:22 +00:00
|
|
|
"github.com/mitchell/selfpass/services/credentials/types"
|
2019-05-22 15:22:40 +00:00
|
|
|
)
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
type CredentialsClientInit func(ctx context.Context) (c types.CredentialsClient)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 11:02:49 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
func selectCredential(client types.CredentialsClient, sourceHost string) types.Credential {
|
2019-06-16 11:02:49 +00:00
|
|
|
var (
|
|
|
|
idKey string
|
|
|
|
prompt survey.Prompt
|
|
|
|
)
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
2019-06-16 11:02:49 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
mdch, errch := client.GetAllMetadata(ctx, sourceHost)
|
|
|
|
var mds []types.Metadata
|
2019-06-16 11:02:49 +00:00
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
receive:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
check(ctx.Err())
|
|
|
|
|
|
|
|
case err := <-errch:
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
case md, ok := <-mdch:
|
|
|
|
if !ok {
|
|
|
|
break receive
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
mds = append(mds, md)
|
2019-06-16 11:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
var sources []string
|
|
|
|
mdmap := map[string][]types.Metadata{}
|
|
|
|
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)
|
2019-06-16 11:02:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
if sourceHost == "" {
|
|
|
|
prompt = &survey.Select{
|
|
|
|
Message: "Source host:",
|
|
|
|
Options: sources,
|
|
|
|
PageSize: 20,
|
|
|
|
VimMode: true,
|
|
|
|
}
|
2019-06-16 11:02:49 +00:00
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
check(survey.AskOne(prompt, &sourceHost, nil))
|
2019-06-16 11:02:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:17:38 +00:00
|
|
|
if len(mdmap[sourceHost]) == 0 {
|
|
|
|
check(errSourceNotFound)
|
|
|
|
}
|
2019-06-16 11:02:49 +00:00
|
|
|
|
|
|
|
keys := []string{}
|
|
|
|
keyIDMap := map[string]string{}
|
2019-08-01 03:17:38 +00:00
|
|
|
for _, md := range mdmap[sourceHost] {
|
2019-06-16 11:02:49 +00:00
|
|
|
key := md.Primary
|
|
|
|
if md.Tag != "" {
|
|
|
|
key += "-" + md.Tag
|
|
|
|
}
|
|
|
|
keys = append(keys, key)
|
|
|
|
keyIDMap[key] = md.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt = &survey.Select{
|
|
|
|
Message: "Primary user key (and tag):",
|
|
|
|
Options: keys,
|
|
|
|
PageSize: 20,
|
|
|
|
VimMode: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
check(survey.AskOne(prompt, &idKey, nil))
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cred, err := client.Get(ctx, keyIDMap[idKey])
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
return cred
|
|
|
|
}
|