Commit aed14c50 authored by Loïck Bonniot's avatar Loïck Bonniot
Browse files

Improve viper and cobra integration

- Add sub-commands flags
- Factorize version commands
- Fix import format
- Fix GUI
parent 96ca0646
Pipeline #827 passed with stage
......@@ -15,8 +15,9 @@ install: nocache
install_all: install
git stash
rm -rf gui
rm -rf dfssd/cmd
rm -rf dfssd/gui
rm -f dfssd/cmd/gui.go
rm dfssd/main.go
go install ./...
git reset --hard
......
......@@ -4,9 +4,8 @@ import (
"fmt"
"os"
"github.com/spf13/cobra"
"dfss/dfssc/user"
"github.com/spf13/cobra"
)
var authCmd = &cobra.Command{
......@@ -22,7 +21,7 @@ var authCmd = &cobra.Command{
err := user.Authenticate(mail, token)
if err != nil {
fmt.Println("An error occurred : ", err.Error())
os.Exit(3)
os.Exit(1)
}
},
}
......@@ -5,9 +5,8 @@ import (
"os"
"path/filepath"
"github.com/spf13/cobra"
"dfss/dfssc/sign"
"github.com/spf13/cobra"
)
var fetchCmd = &cobra.Command{
......
......@@ -4,10 +4,9 @@ import (
"fmt"
"os"
"github.com/spf13/cobra"
"dfss/dfssc/common"
"dfss/dfssc/user"
"github.com/spf13/cobra"
)
// export the certificate and private key of the user
......@@ -15,6 +14,12 @@ var exportCmd = &cobra.Command{
Use: "export <c>",
Short: "export certificate and private key of the user to file c",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
os.Exit(1)
return
}
confFile := args[0]
fmt.Println("Export user configuration")
var keyPassphrase, confPassphrase string
......@@ -47,6 +52,12 @@ var importCmd = &cobra.Command{
Use: "import <c>",
Short: "import private key and certificate of the user from file c",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
os.Exit(1)
return
}
confFile := args[0]
var keyPassphrase, confPassphrase string
err := readPassphrases(&keyPassphrase, &confPassphrase, false)
......
......@@ -4,9 +4,8 @@ import (
"fmt"
"os"
"github.com/spf13/cobra"
"dfss/dfssc/sign"
"github.com/spf13/cobra"
)
var newCmd = &cobra.Command{
......
......@@ -10,7 +10,6 @@ import (
"strings"
"dfss/dfssc/user"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
)
......
......@@ -4,7 +4,6 @@ import (
"dfss"
dapi "dfss/dfssd/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
......@@ -50,6 +49,6 @@ func init() {
_ = viper.BindPFlag("platform_addrport", RootCmd.PersistentFlags().Lookup("host"))
// Bind subcommands to root
RootCmd.AddCommand(versionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd)
RootCmd.AddCommand(dfss.VersionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd)
}
......@@ -8,7 +8,6 @@ import (
"dfss/dfssc/common"
"dfss/dfssp/contract"
"github.com/spf13/cobra"
)
......@@ -31,6 +30,11 @@ var showCmd = &cobra.Command{
}
func showContract(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
return
}
filename := args[0]
c := getContract(filename)
if c == nil {
......
......@@ -4,15 +4,19 @@ import (
"fmt"
"os"
"github.com/spf13/cobra"
"dfss/dfssc/sign"
"github.com/spf13/cobra"
)
var signCmd = &cobra.Command{
Use: "sign <c>",
Short: "sign contract from file c",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
os.Exit(1)
}
filename := args[0]
fmt.Println("You are going to sign the following contract:")
showContract(cmd, args)
......
package cmd
import (
"dfss"
"fmt"
"runtime"
"github.com/spf13/cobra"
)
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print dfss protocol version",
Long: "Print dfss protocol version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
},
}
package main
import (
"fmt"
"os"
"dfss/dfssc/cmd"
......@@ -9,7 +8,6 @@ import (
func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
......@@ -5,7 +5,6 @@ import (
"dfss/dfssd/gui"
"dfss/dfssd/server"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/visualfc/goqt/ui"
......@@ -28,7 +27,3 @@ var guiCmd = &cobra.Command{
})
},
}
func init() {
RootCmd.AddCommand(guiCmd)
}
package cmd
import (
"dfss/dfssd/api"
"dfss/dfssd/server"
"fmt"
"os"
"strconv"
"dfss/dfssd/api"
"dfss/dfssd/server"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
......
......@@ -15,18 +15,16 @@ var RootCmd = &cobra.Command{
Distributed Fair Signing System project
Debug tool to trace remote transmissions`,
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
Run: guiCmd.Run,
}
func init() {
// Add flag to the command
RootCmd.PersistentFlags().IntP("port", "p", 3000, "port to use for listening transmissions")
RootCmd.PersistentFlags().IntP("port", "p", 9099, "port to use for listening transmissions")
// Bind the flag to viper
_ = viper.BindPFlag("port", RootCmd.PersistentFlags().Lookup("port"))
// Register subcommands
RootCmd.AddCommand(versionCmd, noguiCmd)
RootCmd.AddCommand(dfss.VersionCmd, noguiCmd, guiCmd)
}
package cmd
import (
"dfss"
"fmt"
"runtime"
"github.com/spf13/cobra"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "print version of dfss protocol",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("DFSS v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
},
}
package main
import (
"fmt"
"os"
"dfss/dfssd/cmd"
......@@ -9,7 +8,6 @@ import (
func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
......@@ -38,11 +38,11 @@ func Initialize(v *viper.Viper, ca *x509.Certificate, rKey *rsa.PrivateKey) erro
if ca == nil {
// Generate the root certificate, using the private key.
cert, err = auth.GetSelfSignedCertificate(v.GetInt("root_validity"), auth.GenerateUID(), v.GetString("country"), v.GetString("organization"), v.GetString("unit"), v.GetString("cn"), key)
cert, err = auth.GetSelfSignedCertificate(v.GetInt("validity"), auth.GenerateUID(), v.GetString("country"), v.GetString("organization"), v.GetString("unit"), v.GetString("cn"), key)
} else {
csr, _ := auth.GetCertificateRequest(v.GetString("country"), v.GetString("organization"), v.GetString("unit"), v.GetString("cn"), key)
request, _ := auth.PEMToCertificateRequest(csr)
cert, err = auth.GetCertificate(v.GetInt("cert_validity"), auth.GenerateUID(), request, ca, rKey)
cert, err = auth.GetCertificate(v.GetInt("validity"), auth.GenerateUID(), request, ca, rKey)
// Override default path values
certPath = filepath.Join(path, "cert.pem")
keyPath = filepath.Join(path, "key.pem")
......
......@@ -35,7 +35,7 @@ func TestInitialize(t *testing.T) {
keyPath := filepath.Join(path, PkeyFileName)
certPath := filepath.Join(path, RootCAFileName)
v := common.MockViper("key_size", 1024, "root_validity", 365, "country", "country", "organization", "organization", "unit", "unit", "cn", "cn", "path", path)
v := common.MockViper("key_size", 1024, "validity", 365, "country", "country", "organization", "organization", "unit", "unit", "cn", "cn", "path", path)
err := Initialize(v, nil, nil)
if err != nil {
......@@ -59,7 +59,7 @@ func Example() {
certPath := filepath.Join(path, RootCAFileName)
// Generate root certificate and key
v := common.MockViper("key_size", 1024, "root_validity", 365, "country", "UK", "organization", "DFSS", "unit", "unit", "cn", "ROOT", "path", path)
v := common.MockViper("key_size", 1024, "validity", 365, "country", "UK", "organization", "DFSS", "unit", "unit", "cn", "ROOT", "path", path)
err := Initialize(v, nil, nil)
if err != nil {
fmt.Println(err)
......@@ -78,7 +78,7 @@ func Example() {
// Generate child certificate and key
childPath := filepath.Join(path, "child")
v = common.MockViper("key_size", 1024, "cert_validity", 10, "country", "FR", "organization", "DFSS", "unit", "unit", "cn", "CHILD", "path", childPath)
v = common.MockViper("key_size", 1024, "validity", 10, "country", "FR", "organization", "DFSS", "unit", "unit", "cn", "CHILD", "path", childPath)
err = Initialize(v, pid.RootCA, pid.Pkey)
if err != nil {
fmt.Println(err)
......@@ -106,7 +106,7 @@ func CheckFile(path, name string) {
func TestStart(t *testing.T) {
path, _ := ioutil.TempDir("", "")
v := common.MockViper("key_size", 1024, "root_validity", 365, "country", "country", "organization", "organization", "unit", "unit", "cn", "cn", "path", path)
v := common.MockViper("key_size", 1024, "validity", 365, "country", "country", "organization", "organization", "unit", "unit", "cn", "cn", "path", path)
_ = Initialize(v, nil, nil)
pid, err := Start(path)
......
......@@ -7,15 +7,23 @@ import (
"dfss/dfssc/common"
dapi "dfss/dfssd/api"
"dfss/dfssp/authority"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var initCmd = &cobra.Command{
Use: "init [cn, country, key, org, path, unit, root-validity]",
Use: "init",
Short: "create and save the platform's private key and root certificate",
Run: func(cmd *cobra.Command, args []string) {
err := authority.Initialize(common.SubViper("key_size", "root_validity", "country", "organization", "unit", "cn", "path"), nil, nil)
_ = viper.BindPFlag("cn", cmd.Flags().Lookup("cn"))
_ = viper.BindPFlag("validity", cmd.Flags().Lookup("validity"))
_ = viper.BindPFlag("country", cmd.Flags().Lookup("country"))
_ = viper.BindPFlag("organization", cmd.Flags().Lookup("org"))
_ = viper.BindPFlag("unit", cmd.Flags().Lookup("unit"))
_ = viper.BindPFlag("key_size", cmd.Flags().Lookup("key"))
err := authority.Initialize(common.SubViper("key_size", "validity", "country", "organization", "unit", "cn", "path"), nil, nil)
if err != nil {
fmt.Fprintln(os.Stderr, "An error occured during the initialization operation:", err)
os.Exit(1)
......
......@@ -3,7 +3,6 @@ package cmd
import (
"dfss"
dapi "dfss/dfssd/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
......@@ -31,37 +30,35 @@ func init() {
// Add flags to dfssp
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "print verbose messages")
RootCmd.PersistentFlags().StringP("demo", "d", "", "demonstrator address and port, let empty for no debug")
RootCmd.PersistentFlags().StringP("address", "a", "0.0.0.0", "address to bind for listening")
RootCmd.PersistentFlags().StringP("port", "p", "9000", "port to bind for listening")
RootCmd.PersistentFlags().String("path", ".", "path to get the platform's private key and root certificate")
RootCmd.PersistentFlags().String("country", "France", "country for the root certificate")
RootCmd.PersistentFlags().String("org", "DFSS", "Organization for the root certificate")
RootCmd.PersistentFlags().String("unit", "INSA Rennes", "Organizational unit for the rot certificate")
RootCmd.PersistentFlags().String("cn", "dfssp", "Common name for the root certificate")
RootCmd.PersistentFlags().IntP("key", "k", 512, "Encoding size for the private key")
RootCmd.PersistentFlags().IntP("root-validity", "r", 365, "Validity duration for the root certificate (days)")
RootCmd.PersistentFlags().IntP("cert-validity", "c", 365, "Validity duration for the child certificates (days)")
RootCmd.PersistentFlags().String("db", "mongodb://localhost/dfss", "server url in standard MongoDB format for accessing database")
initCmd.Flags().String("cn", "dfssp", "common name for the root certificate")
initCmd.Flags().IntP("validity", "r", 365, "validity duration for the root certificate (days)")
initCmd.Flags().String("country", "FR", "country for the root certificate")
initCmd.Flags().String("org", "DFSS", "organization for the root certificate")
initCmd.Flags().String("unit", "INSA Rennes", "organizational unit for the root certificate")
initCmd.Flags().IntP("key", "k", 2048, "encoding size for the private key of the platform")
ttpCmd.Flags().String("cn", "ttp", "common name for the ttp certificate")
ttpCmd.Flags().IntP("validity", "c", 365, "validity duration for the ttp certificate (days)")
ttpCmd.Flags().String("country", "FR", "country for the ttp certificate")
ttpCmd.Flags().String("org", "DFSS", "organization for the ttp certificate")
ttpCmd.Flags().String("unit", "INSA Rennes", "organizational unit for the ttp certificate")
ttpCmd.Flags().IntP("key", "k", 2048, "encoding size for the private key of the ttp")
startCmd.Flags().IntP("validity", "c", 365, "validity duration for the child certificates (days)")
startCmd.Flags().StringP("address", "a", "0.0.0.0", "address to bind for listening")
startCmd.Flags().StringP("port", "p", "9000", "port to bind for listening")
startCmd.Flags().String("db", "mongodb://localhost/dfss", "server url in standard MongoDB format for accessing database")
// Bind viper to flags
_ = viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose"))
_ = viper.BindPFlag("demo", RootCmd.PersistentFlags().Lookup("demo"))
_ = viper.BindPFlag("address", RootCmd.PersistentFlags().Lookup("address"))
_ = viper.BindPFlag("port", RootCmd.PersistentFlags().Lookup("port"))
_ = viper.BindPFlag("path", RootCmd.PersistentFlags().Lookup("path"))
_ = viper.BindPFlag("country", RootCmd.PersistentFlags().Lookup("country"))
_ = viper.BindPFlag("organization", RootCmd.PersistentFlags().Lookup("org"))
_ = viper.BindPFlag("unit", RootCmd.PersistentFlags().Lookup("unit"))
_ = viper.BindPFlag("cn", RootCmd.PersistentFlags().Lookup("cn"))
_ = viper.BindPFlag("key_size", RootCmd.PersistentFlags().Lookup("key"))
_ = viper.BindPFlag("root_validity", RootCmd.PersistentFlags().Lookup("root-validity"))
_ = viper.BindPFlag("cert_validity", RootCmd.PersistentFlags().Lookup("cert-validity"))
_ = viper.BindPFlag("dbURI", RootCmd.PersistentFlags().Lookup("db"))
viper.SetDefault("pkey_filename", "dfssp_pkey.pem")
viper.SetDefault("ca_filename", "dfssp_rootCA.pem")
// Register subcommands here
RootCmd.AddCommand(versionCmd, ttpCmd, initCmd, startCmd)
RootCmd.AddCommand(dfss.VersionCmd, ttpCmd, initCmd, startCmd)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment