2019-05-28 01:16:50 +00:00
|
|
|
package commands
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/AlecAivazis/survey.v1"
|
|
|
|
|
2019-07-11 06:05:59 +00:00
|
|
|
"github.com/mitchell/selfpass/sp/types"
|
2019-05-22 15:22:40 +00:00
|
|
|
)
|
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
func makeInit(repo types.ConfigRepo) *cobra.Command {
|
2019-05-22 15:22:40 +00:00
|
|
|
initCmd := &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "This command initializes SPC for the first time",
|
|
|
|
Long: `This command initializes SPC for the first time. Writing to the user configuration
|
|
|
|
the users private key, and server certificates. (All of which will be encrypted)`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
var (
|
|
|
|
hasPK bool
|
|
|
|
masterpass string
|
|
|
|
cmasterpass string
|
|
|
|
target string
|
|
|
|
caFile string
|
|
|
|
certFile string
|
|
|
|
keyFile string
|
|
|
|
prompt survey.Prompt
|
|
|
|
privateKey = strings.Replace(uuid.New().String(), "-", "", -1)
|
|
|
|
)
|
2019-07-12 01:02:46 +00:00
|
|
|
_, cfg, err := repo.OpenConfig()
|
|
|
|
check(err)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
prompt = &survey.Password{Message: "New master password:"}
|
2019-05-22 15:22:40 +00:00
|
|
|
check(survey.AskOne(prompt, &masterpass, nil))
|
|
|
|
|
|
|
|
prompt = &survey.Password{Message: "Confirm master password:"}
|
|
|
|
check(survey.AskOne(prompt, &cmasterpass, nil))
|
|
|
|
if masterpass != cmasterpass {
|
|
|
|
check(fmt.Errorf("master passwords didn't match"))
|
|
|
|
}
|
|
|
|
|
2019-06-02 08:47:19 +00:00
|
|
|
repo.SetMasterpass(masterpass)
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
prompt = &survey.Input{Message: "Selfpass server address:"}
|
|
|
|
check(survey.AskOne(prompt, &target, nil))
|
|
|
|
|
|
|
|
prompt = &survey.Confirm{Message: "Do you have a private key?"}
|
|
|
|
check(survey.AskOne(prompt, &hasPK, nil))
|
|
|
|
|
|
|
|
if hasPK {
|
2019-06-02 08:47:19 +00:00
|
|
|
prompt = &survey.Password{Message: "Private key:"}
|
2019-05-22 15:22:40 +00:00
|
|
|
check(survey.AskOne(prompt, &privateKey, nil))
|
|
|
|
privateKey = strings.Replace(privateKey, "-", "", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt = &survey.Input{Message: "CA certificate file:"}
|
|
|
|
check(survey.AskOne(prompt, &caFile, nil))
|
|
|
|
ca, err := ioutil.ReadFile(caFile)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
prompt = &survey.Input{Message: "Client certificate file:"}
|
|
|
|
check(survey.AskOne(prompt, &certFile, nil))
|
|
|
|
cert, err := ioutil.ReadFile(certFile)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
prompt = &survey.Input{Message: "Client key file:"}
|
|
|
|
check(survey.AskOne(prompt, &keyFile, nil))
|
|
|
|
key, err := ioutil.ReadFile(keyFile)
|
|
|
|
check(err)
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
cfg.Set(types.KeyConnConfig, map[string]string{
|
2019-05-22 15:22:40 +00:00
|
|
|
"target": target,
|
|
|
|
"ca": string(ca),
|
|
|
|
"cert": string(cert),
|
|
|
|
"key": string(key),
|
|
|
|
})
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
cfg.Set(types.KeyPrivateKey, privateKey)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
check(repo.WriteConfig())
|
2019-05-22 15:22:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return initCmd
|
|
|
|
}
|