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

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 ...@@ -15,8 +15,9 @@ install: nocache
install_all: install install_all: install
git stash git stash
rm -rf gui rm -rf gui
rm -rf dfssd/cmd
rm -rf dfssd/gui rm -rf dfssd/gui
rm -f dfssd/cmd/gui.go rm dfssd/main.go
go install ./... go install ./...
git reset --hard git reset --hard
......
...@@ -4,9 +4,8 @@ import ( ...@@ -4,9 +4,8 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/spf13/cobra"
"dfss/dfssc/user" "dfss/dfssc/user"
"github.com/spf13/cobra"
) )
var authCmd = &cobra.Command{ var authCmd = &cobra.Command{
...@@ -22,7 +21,7 @@ var authCmd = &cobra.Command{ ...@@ -22,7 +21,7 @@ var authCmd = &cobra.Command{
err := user.Authenticate(mail, token) err := user.Authenticate(mail, token)
if err != nil { if err != nil {
fmt.Println("An error occurred : ", err.Error()) fmt.Println("An error occurred : ", err.Error())
os.Exit(3) os.Exit(1)
} }
}, },
} }
...@@ -5,9 +5,8 @@ import ( ...@@ -5,9 +5,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/spf13/cobra"
"dfss/dfssc/sign" "dfss/dfssc/sign"
"github.com/spf13/cobra"
) )
var fetchCmd = &cobra.Command{ var fetchCmd = &cobra.Command{
......
...@@ -4,10 +4,9 @@ import ( ...@@ -4,10 +4,9 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/spf13/cobra"
"dfss/dfssc/common" "dfss/dfssc/common"
"dfss/dfssc/user" "dfss/dfssc/user"
"github.com/spf13/cobra"
) )
// export the certificate and private key of the user // export the certificate and private key of the user
...@@ -15,6 +14,12 @@ var exportCmd = &cobra.Command{ ...@@ -15,6 +14,12 @@ var exportCmd = &cobra.Command{
Use: "export <c>", Use: "export <c>",
Short: "export certificate and private key of the user to file c", Short: "export certificate and private key of the user to file c",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
os.Exit(1)
return
}
confFile := args[0] confFile := args[0]
fmt.Println("Export user configuration") fmt.Println("Export user configuration")
var keyPassphrase, confPassphrase string var keyPassphrase, confPassphrase string
...@@ -47,6 +52,12 @@ var importCmd = &cobra.Command{ ...@@ -47,6 +52,12 @@ var importCmd = &cobra.Command{
Use: "import <c>", Use: "import <c>",
Short: "import private key and certificate of the user from file c", Short: "import private key and certificate of the user from file c",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
os.Exit(1)
return
}
confFile := args[0] confFile := args[0]
var keyPassphrase, confPassphrase string var keyPassphrase, confPassphrase string
err := readPassphrases(&keyPassphrase, &confPassphrase, false) err := readPassphrases(&keyPassphrase, &confPassphrase, false)
......
...@@ -4,9 +4,8 @@ import ( ...@@ -4,9 +4,8 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/spf13/cobra"
"dfss/dfssc/sign" "dfss/dfssc/sign"
"github.com/spf13/cobra"
) )
var newCmd = &cobra.Command{ var newCmd = &cobra.Command{
......
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"strings" "strings"
"dfss/dfssc/user" "dfss/dfssc/user"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"dfss" "dfss"
dapi "dfss/dfssd/api" dapi "dfss/dfssd/api"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
...@@ -50,6 +49,6 @@ func init() { ...@@ -50,6 +49,6 @@ func init() {
_ = viper.BindPFlag("platform_addrport", RootCmd.PersistentFlags().Lookup("host")) _ = viper.BindPFlag("platform_addrport", RootCmd.PersistentFlags().Lookup("host"))
// Bind subcommands to root // 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 ( ...@@ -8,7 +8,6 @@ import (
"dfss/dfssc/common" "dfss/dfssc/common"
"dfss/dfssp/contract" "dfss/dfssp/contract"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -31,6 +30,11 @@ var showCmd = &cobra.Command{ ...@@ -31,6 +30,11 @@ var showCmd = &cobra.Command{
} }
func showContract(cmd *cobra.Command, args []string) { func showContract(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
return
}
filename := args[0] filename := args[0]
c := getContract(filename) c := getContract(filename)
if c == nil { if c == nil {
......
...@@ -4,15 +4,19 @@ import ( ...@@ -4,15 +4,19 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/spf13/cobra"
"dfss/dfssc/sign" "dfss/dfssc/sign"
"github.com/spf13/cobra"
) )
var signCmd = &cobra.Command{ var signCmd = &cobra.Command{
Use: "sign <c>", Use: "sign <c>",
Short: "sign contract from file c", Short: "sign contract from file c",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
os.Exit(1)
}
filename := args[0] filename := args[0]
fmt.Println("You are going to sign the following contract:") fmt.Println("You are going to sign the following contract:")
showContract(cmd, args) 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 package main
import ( import (
"fmt"
"os" "os"
"dfss/dfssc/cmd" "dfss/dfssc/cmd"
...@@ -9,7 +8,6 @@ import ( ...@@ -9,7 +8,6 @@ import (
func main() { func main() {
if err := cmd.RootCmd.Execute(); err != nil { if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
} }
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"dfss/dfssd/gui" "dfss/dfssd/gui"
"dfss/dfssd/server" "dfss/dfssd/server"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/visualfc/goqt/ui" "github.com/visualfc/goqt/ui"
...@@ -28,7 +27,3 @@ var guiCmd = &cobra.Command{ ...@@ -28,7 +27,3 @@ var guiCmd = &cobra.Command{
}) })
}, },
} }
func init() {
RootCmd.AddCommand(guiCmd)
}
package cmd package cmd
import ( import (
"dfss/dfssd/api"
"dfss/dfssd/server"
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
"dfss/dfssd/api"
"dfss/dfssd/server"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
......
...@@ -15,18 +15,16 @@ var RootCmd = &cobra.Command{ ...@@ -15,18 +15,16 @@ var RootCmd = &cobra.Command{
Distributed Fair Signing System project Distributed Fair Signing System project
Debug tool to trace remote transmissions`, Debug tool to trace remote transmissions`,
Run: func(cmd *cobra.Command, args []string) { Run: guiCmd.Run,
_ = cmd.Help()
},
} }
func init() { func init() {
// Add flag to the command // 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 // Bind the flag to viper
_ = viper.BindPFlag("port", RootCmd.PersistentFlags().Lookup("port")) _ = viper.BindPFlag("port", RootCmd.PersistentFlags().Lookup("port"))
// Register subcommands // 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 package main
import ( import (
"fmt"
"os" "os"
"dfss/dfssd/cmd" "dfss/dfssd/cmd"
...@@ -9,7 +8,6 @@ import ( ...@@ -9,7 +8,6 @@ import (
func main() { func main() {
if err := cmd.RootCmd.Execute(); err != nil { if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
} }
...@@ -38,11 +38,11 @@ func Initialize(v *viper.Viper, ca *x509.Certificate, rKey *rsa.PrivateKey) erro ...@@ -38,11 +38,11 @@ func Initialize(v *viper.Viper, ca *x509.Certificate, rKey *rsa.PrivateKey) erro
if ca == nil { if ca == nil {
// Generate the root certificate, using the private key. // 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 { } else {
csr, _ := auth.GetCertificateRequest(v.GetString("country"), v.GetString("organization"), v.GetString("unit"), v.GetString("cn"), key) csr, _ := auth.GetCertificateRequest(v.GetString("country"), v.GetString("organization"), v.GetString("unit"), v.GetString("cn"), key)
request, _ := auth.PEMToCertificateRequest(csr) 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 // Override default path values
certPath = filepath.Join(path, "cert.pem") certPath = filepath.Join(path, "cert.pem")
keyPath = filepath.Join(path, "key.pem") keyPath = filepath.Join(path, "key.pem")
......
...@@ -35,7 +35,7 @@ func TestInitialize(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestInitialize(t *testing.T) {
keyPath := filepath.Join(path, PkeyFileName) keyPath := filepath.Join(path, PkeyFileName)
certPath := filepath.Join(path, RootCAFileName) 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) err := Initialize(v, nil, nil)
if err != nil { if err != nil {
...@@ -59,7 +59,7 @@ func Example() { ...@@ -59,7 +59,7 @@ func Example() {
certPath := filepath.Join(path, RootCAFileName) certPath := filepath.Join(path, RootCAFileName)
// Generate root certificate and key // 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) err := Initialize(v, nil, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
...@@ -78,7 +78,7 @@ func Example() { ...@@ -78,7 +78,7 @@ func Example() {
// Generate child certificate and key // Generate child certificate and key
childPath := filepath.Join(path, "child") 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) err = Initialize(v, pid.RootCA, pid.Pkey)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
...@@ -106,7 +106,7 @@ func CheckFile(path, name string) { ...@@ -106,7 +106,7 @@ func CheckFile(path, name string) {
func TestStart(t *testing.T) { func TestStart(t *testing.T) {
path, _ := ioutil.TempDir("", "") 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) _ = Initialize(v, nil, nil)
pid, err := Start(path) pid, err := Start(path)
......
...@@ -7,15 +7,23 @@ import ( ...@@ -7,15 +7,23 @@ import (
"dfss/dfssc/common" "dfss/dfssc/common"
dapi "dfss/dfssd/api" dapi "dfss/dfssd/api"
"dfss/dfssp/authority" "dfss/dfssp/authority"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
var initCmd = &cobra.Command{ 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", Short: "create and save the platform's private key and root certificate",
Run: func(cmd *cobra.Command, args []string) { 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 { if err != nil {
fmt.Fprintln(os.Stderr, "An error occured during the initialization operation:", err) fmt.Fprintln(os.Stderr, "An error occured during the initialization operation:", err)
os.Exit(1) os.Exit(1)
......
...@@ -3,7 +3,6 @@ package cmd ...@@ -3,7 +3,6 @@ package cmd
import ( import (
"dfss" "dfss"
dapi "dfss/dfssd/api" dapi "dfss/dfssd/api"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
...@@ -31,37 +30,35 @@ func init() { ...@@ -31,37 +30,35 @@ func init() {
// Add flags to dfssp // Add flags to dfssp
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "print verbose messages") 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("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("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") initCmd.Flags().String("cn", "dfssp", "common name for the root certificate")
RootCmd.PersistentFlags().String("unit", "INSA Rennes", "Organizational unit for the rot certificate") initCmd.Flags().IntP("validity", "r", 365, "validity duration for the root certificate (days)")
RootCmd.PersistentFlags().String("cn", "dfssp", "Common name for the root certificate") initCmd.Flags().String("country", "FR", "country for the root certificate")
RootCmd.PersistentFlags().IntP("key", "k", 512, "Encoding size for the private key") initCmd.Flags().String("org", "DFSS", "organization for the root certificate")
RootCmd.PersistentFlags().IntP("root-validity", "r", 365, "Validity duration for the root certificate (days)") initCmd.Flags().String("unit", "INSA Rennes", "organizational unit for the root certificate")
RootCmd.PersistentFlags().IntP("cert-validity", "c", 365, "Validity duration for the child certificates (days)") initCmd.Flags().IntP("key", "k", 2048, "encoding size for the private key of the platform")
RootCmd.PersistentFlags().String("db", "mongodb://localhost/dfss", "server url in standard MongoDB format for accessing database")
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 // Bind viper to flags
_ = viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose")) _ = viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose"))
_ = viper.BindPFlag("demo", RootCmd.PersistentFlags().Lookup("demo")) _ = 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("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("pkey_filename", "dfssp_pkey.pem")
viper.SetDefault("ca_filename", "dfssp_rootCA.pem") viper.SetDefault("ca_filename", "dfssp_rootCA.pem")
// Register subcommands here // Register subcommands here
RootCmd.AddCommand(versionCmd, ttpCmd, initCmd, startCmd) RootCmd.AddCommand(dfss.VersionCmd, ttpCmd, initCmd, startCmd)
} }
...@@ -7,15 +7,20 @@ import ( ...@@ -7,15 +7,20 @@ import (
dapi "dfss/dfssd/api" dapi "dfss/dfssd/api"
"dfss/dfssp/server" "dfss/dfssp/server"
"dfss/net" "dfss/net"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var startCmd = &cobra.Command{ var startCmd = &cobra.Command{
Use: "start [path, db, address, port, cert-validity]", Use: "start",
Short: "start the platform after loading its private key and root certificate", Short: "start the platform after loading its private key and root certificate",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
_ = viper.BindPFlag("dbURI", cmd.Flags().Lookup("db"))
_ = viper.BindPFlag("address", cmd.Flags().Lookup("address"))
_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
_ = viper.BindPFlag("validity", cmd.Flags().Lookup("validity"))
address := viper.GetString("address") address := viper.GetString("address")
port := viper.GetString("port") port := viper.GetString("port")
......
...@@ -8,27 +8,36 @@ import (