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
......@@ -7,15 +7,20 @@ import (
dapi "dfss/dfssd/api"
"dfss/dfssp/server"
"dfss/net"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
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",
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")
port := viper.GetString("port")
......
......@@ -8,27 +8,36 @@ import (
"dfss/dfssc/common"
dapi "dfss/dfssd/api"
"dfss/dfssp/authority"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var ttpCmd = &cobra.Command{
Use: "ttp [cn, country, key, org, path, unit, cert-validity]",
Use: "ttp",
Short: "create and save the TTP's private key and certificate",
Run: func(cmd *cobra.Command, args []string) {
_ = 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"))
path := viper.GetString("path")
pid, err := authority.Start(path)
if err != nil {
fmt.Fprintln(os.Stderr, "Bad root CA or key; please use the `init` command before the `ttp` one.\n", err)
os.Exit(1)
}
ttpPath := filepath.Join(path, "ttp")
v := common.SubViper("key_size", "root_validity", "country", "organization", "unit", "cn")
v := common.SubViper("key_size", "validity", "country", "organization", "unit", "cn")
v.Set("path", ttpPath)
err = authority.Initialize(v, pid.RootCA, pid.Pkey)
if err != nil {
fmt.Fprintln(os.Stderr, "An error occured during TTP credentials generation:", err)
os.Exit(1)
}
dapi.DLog("Private key and certificate generated for TTP")
},
......
package cmd
import (
"dfss"
"fmt"
"runtime"
"github.com/spf13/cobra"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "version of the dfss protocol",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("DFSS v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
},
}
......@@ -45,7 +45,7 @@ func TestMain(m *testing.M) {
keyPath := filepath.Join(os.Getenv("GOPATH"), "src", "dfss", "dfssp", "testdata")
viper.Set("path", keyPath)
viper.Set("dbURI", dbURI)
viper.Set("root_validity", 365)
viper.Set("validity", 365)
viper.Set("verbose", true)
srv := server.GetServer()
......
package main
import (
"fmt"
"os"
"dfss/dfssp/cmd"
......@@ -9,7 +8,6 @@ import (
func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
......@@ -135,7 +135,7 @@ func checkAuthRequest(in *api.AuthRequest) error {
return errors.New("Invalid token length")
}
if viper.GetInt("cert_validity") < 1 {
if viper.GetInt("validity") < 1 {
return errors.New("Invalid validity duration")
}
......@@ -162,7 +162,7 @@ func generateUserCert(csr string, parent *x509.Certificate, key *rsa.PrivateKey)
return nil, nil, err
}
cert, err := auth.GetCertificate(viper.GetInt("cert_validity"), auth.GenerateUID(), x509csr, parent, key)
cert, err := auth.GetCertificate(viper.GetInt("validity"), auth.GenerateUID(), x509csr, parent, key)
if err != nil {
return nil, nil, err
}
......@@ -224,7 +224,7 @@ func Auth(pid *authority.PlatformID, manager *mgdb.MongoManager, in *api.AuthReq
user.Certificate = string(cert)
user.CertHash = certHash
user.Expiration = time.Now().AddDate(0, 0, viper.GetInt("cert_validity"))
user.Expiration = time.Now().AddDate(0, 0, viper.GetInt("validity"))
// Updating the database
ok, err := manager.Get("users").UpdateByID(user)
......
......@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
"dfss/auth"
"dfss/dfssp/api"
......@@ -15,8 +16,6 @@ import (
"dfss/dfssp/server"
"dfss/mgdb"
"dfss/net"
"time"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
......@@ -73,16 +72,11 @@ func TestMain(m *testing.M) {
// Valid server
viper.Set("path", keyPath)
viper.Set("dbURI", dbURI)
viper.Set("root_validity", 365)
viper.Set("cert_validity", 365)
viper.Set("validity", 365)
viper.Set("verbose", true)
srv := server.GetServer()
go func() { _ = net.Listen(ValidServ, srv) }()
// Server using invalid certificate duration
// srv2 := server.GetServer(keyPath, dbURI, -1, true)
// go func() { _ = net.Listen(InvalidServ, srv2) }()
// Run
err = collection.Drop()
code := m.Run()
......
package cmd
import (
"dfss"
"fmt"
"dfss"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
......@@ -32,9 +32,10 @@ func init() {
RootCmd.PersistentFlags().String("cert", "cert.pem", "path to the ttp's certificate")
RootCmd.PersistentFlags().String("key", "key.pem", "path to the ttp's private key")
RootCmd.PersistentFlags().StringP("demo", "d", "", "demonstrator address and port, empty will disable it")
RootCmd.PersistentFlags().StringP("address", "a", "0.0.0.0", "address to bind for listening")
RootCmd.PersistentFlags().String("db", "mongodb://localhost/dfss", "server url in standard MongoDB format to access the database")
RootCmd.PersistentFlags().IntP("port", "p", 9020, "port to bind for listening")
startCmd.Flags().StringP("address", "a", "0.0.0.0", "address to bind for listening")
startCmd.Flags().String("db", "mongodb://localhost/dfss", "server url in standard MongoDB format to access the database")
startCmd.Flags().IntP("port", "p", 9020, "port to bind for listening")
// Store flag values into viper
_ = viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose"))
......@@ -42,9 +43,10 @@ func init() {
_ = viper.BindPFlag("file_cert", RootCmd.PersistentFlags().Lookup("cert"))
_ = viper.BindPFlag("file_key", RootCmd.PersistentFlags().Lookup("key"))
_ = viper.BindPFlag("demo", RootCmd.PersistentFlags().Lookup("demo"))
_ = viper.BindPFlag("port", RootCmd.PersistentFlags().Lookup("port"))
_ = viper.BindPFlag("address", RootCmd.PersistentFlags().Lookup("address"))
_ = viper.BindPFlag("dbURI", RootCmd.PersistentFlags().Lookup("db"))
_ = viper.BindPFlag("port", startCmd.Flags().Lookup("port"))
_ = viper.BindPFlag("address", startCmd.Flags().Lookup("address"))
_ = viper.BindPFlag("dbURI", startCmd.Flags().Lookup("db"))
if err := viper.BindEnv("password", "DFSS_TTP_PASSWORD"); err != nil {
fmt.Println("Warning: The DFSS_TTP_PASSWORD environment variable is not set, assuming the private key is decrypted")
......@@ -52,6 +54,6 @@ func init() {
}
// Register Sub Commands
RootCmd.AddCommand(versionCmd, startCmd)
RootCmd.AddCommand(dfss.VersionCmd, startCmd)
}
......@@ -8,7 +8,6 @@ import (
dapi "dfss/dfssd/api"
"dfss/dfsst/server"
"dfss/net"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
......
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/dfsst/cmd"
......@@ -9,7 +8,6 @@ import (
func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
......@@ -25,10 +25,7 @@ func Load() {
path := GetHomeDir()
viper.AddConfigPath(GetHomeDir())
viper.SetConfigName(viper.GetString("filename_config"))
err := viper.ReadInConfig()
if err != nil {
return
}
viper.ReadInConfig()
// Alias for platform
viper.RegisterAlias("platform_addrport", "platform")
......@@ -38,11 +35,12 @@ func Load() {
viper.Set("file_ca", filepath.Join(path, viper.GetString("filename_ca")))
viper.Set("file_cert", filepath.Join(path, viper.GetString("filename_cert")))
viper.Set("file_key", filepath.Join(path, viper.GetString("filename_key")))
viper.Set("file_config", filepath.Join(path, viper.GetString("filename_config")))
viper.Set("file_config", filepath.Join(path, viper.GetString("filename_config")) + ".json")
// Fill virtual-only fields
viper.Set("registered", isFileValid(viper.GetString("file_key")))
viper.Set("authenticated", isFileValid(viper.GetString("file_cert")))
return
}
......
......@@ -7,7 +7,6 @@ import (
"dfss/gui/contractform"
"dfss/gui/signform"
"dfss/gui/userform"
"github.com/spf13/viper"
"github.com/visualfc/goqt/ui"
)
......@@ -27,7 +26,7 @@ func init() {
viper.Set("filename_ca", "ca.pem")
viper.Set("filename_cert", "cert.pem")
viper.Set("filename_key", "key.pem")
viper.Set("filename_config", "config.json")
viper.Set("filename_config", "config")
}
func main() {
......
......@@ -6,7 +6,7 @@ import (
"dfss/dfssc/sign"
"dfss/dfssp/contract"
"dfss/gui/config"
"github.com/spf13/viper"
"github.com/visualfc/goqt/ui"
)
......@@ -29,7 +29,7 @@ type Widget struct {
feedback string
}
func NewWidget(conf *config.Config, filename, pwd string) *Widget {
func NewWidget(filename, pwd string) *Widget {
loadIcons()
file := ui.NewFileWithName(":/signform/signform.ui")
loader := ui.NewUiLoader()
......@@ -51,14 +51,8 @@ func NewWidget(conf *config.Config, filename, pwd string) *Widget {
return nil
}
home := config.GetHomeDir()
m, err := sign.NewSignatureManager(
home+config.CAFile,
home+config.CertFile,
home+config.KeyFile,
conf.Platform,
pwd,
9005, // TODO change port
w.contract,
)
if err != nil {
......@@ -73,7 +67,7 @@ func NewWidget(conf *config.Config, filename, pwd string) *Widget {
}
w.initLines()
w.signerUpdated(conf.Email, sign.StatusConnected, "It's you!")
w.signerUpdated(viper.GetString("email"), sign.StatusConnected, "It's you!")
go func() {
err = w.execute()
if err != nil {
......
package dfss
import (
"fmt"
"runtime"
"github.com/spf13/cobra"
)
// Version represents the current version of the DFSS software suite
const Version = "0.2.0-dev"
// VersionCmd is the cobra command common to all dfss modules
var VersionCmd = &cobra.Command{
Use: "version",
Short: "print version of dfss protocol",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("DFSS v"+Version, runtime.GOOS, runtime.GOARCH)
},
}
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