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/mitchellh/go-homedir"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gopkg.in/AlecAivazis/survey.v1"
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
"github.com/mitchell/selfpass/credentials/commands"
|
2019-05-22 15:22:40 +00:00
|
|
|
)
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
func makeInit(cfg *viper.Viper) *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-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"))
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
prompt = &survey.Input{Message: "Private key:"}
|
|
|
|
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-05-28 01:16:50 +00:00
|
|
|
cfg.Set(keyConnConfig, map[string]string{
|
2019-05-22 15:22:40 +00:00
|
|
|
"target": target,
|
|
|
|
"ca": string(ca),
|
|
|
|
"cert": string(cert),
|
|
|
|
"key": string(key),
|
|
|
|
})
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
cfg.Set(commands.KeyPrivateKey, privateKey)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
|
|
|
if err := cfg.WriteConfig(); err != nil {
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
check(cfg.WriteConfigAs(home + "/.spc.toml"))
|
|
|
|
cfg.SetConfigFile(home + "/.spc.toml")
|
|
|
|
fmt.Println("Wrote new config to: " + home + "/.spc.toml")
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptConfig(masterpass, cfg)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return initCmd
|
|
|
|
}
|