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-08-12 18:51:39 +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
|
2019-08-31 21:19:25 +00:00
|
|
|
includeCredFlags bool
|
2019-08-01 03:17:38 +00:00
|
|
|
|
|
|
|
sourceHost string
|
2019-08-31 21:19:25 +00:00
|
|
|
primary string
|
2019-08-01 03:17:38 +00:00
|
|
|
noNumbers bool
|
|
|
|
noSpecials bool
|
|
|
|
length uint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set credentialFlagSet) withPasswordFlags() credentialFlagSet {
|
|
|
|
set.includePasswordFlags = true
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
2019-08-31 21:19:25 +00:00
|
|
|
func (set credentialFlagSet) withCredFlags() credentialFlagSet {
|
|
|
|
set.includeCredFlags = true
|
2019-08-01 03:17:38 +00:00
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set *credentialFlagSet) register(cmd *cobra.Command) {
|
2019-08-31 21:19:25 +00:00
|
|
|
if set.includeCredFlags {
|
2019-08-01 03:17:38 +00:00
|
|
|
cmd.Flags().StringVarP(&set.sourceHost, "source-host", "s", "", "filter results to this source host")
|
2019-08-31 21:19:25 +00:00
|
|
|
cmd.Flags().StringVarP(&set.primary, "primary", "p", "", "specify a primary user key (must include tag if applicable)")
|
2019-08-01 03:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if set.includePasswordFlags {
|
|
|
|
cmd.Flags().BoolVarP(&set.noNumbers, "no-numbers", "n", false, "do not use numbers in the generated password")
|
2019-08-31 21:19:25 +00:00
|
|
|
cmd.Flags().BoolVarP(&set.noSpecials, "no-specials", "e", false, "do not use special characters in the generated password")
|
2019-08-01 03:17:38 +00:00
|
|
|
cmd.Flags().UintVarP(&set.length, "length", "l", 32, "length of the generated password")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 21:19:25 +00:00
|
|
|
func (set *credentialFlagSet) resetValues() {
|
|
|
|
set.sourceHost = ""
|
|
|
|
set.primary = ""
|
|
|
|
set.noNumbers = false
|
|
|
|
set.noSpecials = false
|
|
|
|
set.length = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkPromptMode = false
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
2019-08-31 21:19:25 +00:00
|
|
|
if checkPromptMode {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(os.Stdout, err)
|
2019-05-22 15:22:40 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 11:02:49 +00:00
|
|
|
|
2019-08-31 21:19:25 +00:00
|
|
|
func selectCredential(client types.CredentialsClient, sourceHost string, primary string) types.Credential {
|
|
|
|
var prompt survey.Prompt
|
2019-06-16 11:02:49 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-31 21:19:25 +00:00
|
|
|
if primary == "" {
|
|
|
|
var idKey string
|
|
|
|
prompt = &survey.Select{
|
|
|
|
Message: "Primary user key (and tag):",
|
|
|
|
Options: keys,
|
|
|
|
PageSize: 20,
|
|
|
|
VimMode: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
check(survey.AskOne(prompt, &idKey, nil))
|
2019-06-16 11:02:49 +00:00
|
|
|
|
2019-08-31 21:19:25 +00:00
|
|
|
primary = idKey
|
|
|
|
}
|
2019-06-16 11:02:49 +00:00
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
defer cancel()
|
|
|
|
|
2019-08-31 21:19:25 +00:00
|
|
|
cred, err := client.Get(ctx, keyIDMap[primary])
|
2019-06-16 11:02:49 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
return cred
|
|
|
|
}
|