Move service related filed to services folder

This commit is contained in:
mitchell 2019-07-10 22:33:22 -04:00
parent 347fbe7268
commit 7d770ef150
41 changed files with 50 additions and 50 deletions

View file

@ -0,0 +1,50 @@
package commands
import (
"fmt"
"io/ioutil"
"os"
"github.com/spf13/cobra"
"github.com/mitchell/selfpass/services/cli/types"
"github.com/mitchell/selfpass/services/crypto"
)
func makeDecrypt(repo types.ConfigRepo) *cobra.Command {
decryptCmd := &cobra.Command{
Use: "decrypt [file]",
Short: "Decrypt a file using your masterpass and secret key",
Long: `Decrypt a file using your masterpass and secret key, and replace the old file with
the new file.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
masterpass, cfg, err := repo.OpenConfig()
check(err)
file := args[0]
fileout := file
if file[len(file)-4:] == ".enc" {
fileout = file[:len(file)-4]
}
contents, err := ioutil.ReadFile(file)
check(err)
key := cfg.GetString(types.KeyPrivateKey)
passkey := crypto.GeneratePBKDF2Key([]byte(masterpass), []byte(key))
contents, err = crypto.GCMDecrypt(passkey, contents)
check(err)
check(ioutil.WriteFile(fileout, contents, 0600))
check(os.Remove(file))
fmt.Println("Decrypted file: ", fileout)
},
}
return decryptCmd
}

View file

@ -0,0 +1,28 @@
package commands
import (
"fmt"
"github.com/spf13/cobra"
"github.com/mitchell/selfpass/services/cli/types"
)
func makeDecryptCfg(repo types.ConfigRepo) *cobra.Command {
decryptCfg := &cobra.Command{
Use: "decrypt-cfg",
Short: "Decrypt your config file",
Long: "Decrypt your config file, so you can access your private key, host, and certs.",
Run: func(cmd *cobra.Command, args []string) {
_, _, err := repo.OpenConfig()
check(err)
check(repo.DecryptConfig())
fmt.Println("Config decrypted. It will automatically encrypt next run of spc.")
},
}
return decryptCfg
}

View file

@ -0,0 +1,46 @@
package commands
import (
"fmt"
"io/ioutil"
"os"
"github.com/spf13/cobra"
"github.com/mitchell/selfpass/services/cli/types"
"github.com/mitchell/selfpass/services/crypto"
)
func makeEncrypt(repo types.ConfigRepo) *cobra.Command {
encryptCmd := &cobra.Command{
Use: "encrypt [file]",
Short: "Encrypt a file using your masterpass and secret key",
Long: `Encrypt a file using your masterpass and secret key, and replace the old file with the
new file.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
masterpass, cfg, err := repo.OpenConfig()
check(err)
file := args[0]
fileEnc := file + ".enc"
contents, err := ioutil.ReadFile(file)
check(err)
key := cfg.GetString(types.KeyPrivateKey)
passkey := crypto.GeneratePBKDF2Key([]byte(masterpass), []byte(key))
contents, err = crypto.GCMEncrypt(passkey, contents)
check(err)
check(ioutil.WriteFile(fileEnc, contents, 0600))
check(os.Remove(file))
fmt.Println("Encrypted file: ", fileEnc)
},
}
return encryptCmd
}

View file

@ -0,0 +1,87 @@
package commands
import (
"fmt"
"io/ioutil"
"strings"
"github.com/google/uuid"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"github.com/mitchell/selfpass/services/cli/types"
)
func makeInit(repo types.ConfigRepo) *cobra.Command {
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)
)
_, cfg, _ := repo.OpenConfig()
prompt = &survey.Password{Message: "New master password:"}
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"))
}
repo.SetMasterpass(masterpass)
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.Password{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)
cfg.Set(types.KeyConnConfig, map[string]string{
"target": target,
"ca": string(ca),
"cert": string(cert),
"key": string(key),
})
cfg.Set(types.KeyPrivateKey, privateKey)
check(repo.WriteConfig())
},
}
return initCmd
}

View file

@ -0,0 +1,72 @@
package commands
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/mitchell/selfpass/services/cli/repositories"
"github.com/mitchell/selfpass/services/cli/types"
"github.com/mitchell/selfpass/services/credentials/commands"
credrepos "github.com/mitchell/selfpass/services/credentials/repositories"
credtypes "github.com/mitchell/selfpass/services/credentials/types"
)
func Execute() {
rootCmd := &cobra.Command{
Use: "spc",
Short: "This is the CLI client for Selfpass.",
Long: `This is the CLI client for Selfpass, the self-hosted password manager. With this tool you
can interact with the entire Selfpass API.`,
Version: "v0.1.0",
}
cfgFile := rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.spc.toml)")
mgr := repositories.NewConfigManager(cfgFile)
clientInit := credrepos.NewCredentialServiceClient
rootCmd.AddCommand(
makeInit(mgr),
makeEncrypt(mgr),
makeDecrypt(mgr),
makeDecryptCfg(mgr),
commands.MakeList(makeInitClient(mgr, clientInit)),
commands.MakeCreate(mgr, makeInitClient(mgr, clientInit)),
commands.MakeUpdate(mgr, makeInitClient(mgr, clientInit)),
commands.MakeGet(mgr, makeInitClient(mgr, clientInit)),
commands.MakeDelete(makeInitClient(mgr, clientInit)),
commands.MakeGCMToCBC(mgr, makeInitClient(mgr, clientInit)),
)
check(rootCmd.Execute())
}
func makeInitClient(repo types.ConfigRepo, initClient credtypes.CredentialClientInit) commands.CredentialClientInit {
return func(ctx context.Context) credtypes.CredentialClient {
_, cfg, err := repo.OpenConfig()
check(err)
connConfig := cfg.GetStringMapString(types.KeyConnConfig)
client, err := initClient(
ctx,
connConfig["target"],
connConfig["ca"],
connConfig["cert"],
connConfig["key"],
)
check(err)
return client
}
}
func check(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}