main.go 2.79 KB
Newer Older
1 2 3 4
package main

import (
	"dfss"
5
	"dfss/dfssp/authority"
Caro Axel's avatar
Caro Axel committed
6
	"dfss/mgdb"
7 8 9 10 11 12
	"flag"
	"fmt"
	"runtime"
)

var (
Caro Axel's avatar
Caro Axel committed
13
	verbose bool
Caro Axel's avatar
Caro Axel committed
14
	// Private key and certificate
Caro Axel's avatar
Caro Axel committed
15 16
	path, country, org, unit, cn string
	keySize, validity            int
17
	pid                          *authority.PlatformID
Caro Axel's avatar
Caro Axel committed
18
	// MongoDB connection
Caro Axel's avatar
Caro Axel committed
19 20
	dbName, dbEnvVarName string
	dbManager            *mgdb.MongoManager
21 22 23 24 25 26
)

func init() {

	flag.BoolVar(&verbose, "v", false, "Print verbose messages")

27
	flag.StringVar(&path, "path", authority.GetHomeDir(), "Path for the platform's private key and root certificate")
Caro Axel's avatar
Caro Axel committed
28 29 30 31 32 33 34 35
	flag.StringVar(&country, "country", "France", "Country for the root certificate")
	flag.StringVar(&org, "org", "DFSS", "Organization for the root certificate")
	flag.StringVar(&unit, "unit", "INSA Rennes", "Organizational unit for the root certificate")
	flag.StringVar(&cn, "cn", "dfssp", "Common name for the root certificate")

	flag.IntVar(&keySize, "keySize", 512, "Encoding size for the private key")
	flag.IntVar(&validity, "validity", 21, "Root certificate's validity duration (days)")

Caro Axel's avatar
Caro Axel committed
36 37 38
	flag.StringVar(&dbName, "dbn", "myDatabase", "Name of the mongo database to connect to")
	flag.StringVar(&dbEnvVarName, "dbenv", mgdb.DefaultDBUrl, "Name of the environment variable containing the server url in standard MongoDB format")

39 40 41 42 43 44 45 46
	flag.Usage = func() {
		fmt.Println("DFSS platform v" + dfss.Version)
		fmt.Println("Users and contracts manager")

		fmt.Println("\nUsage:")
		fmt.Println("  dfssp command [flags]")

		fmt.Println("\nThe commands are:")
Caro Axel's avatar
Caro Axel committed
47 48
		fmt.Println("  init     [cn, country, keySize, org, path, unit, validity]")
		fmt.Println("           create and save the platform's private key and root certificate")
Caro Axel's avatar
Caro Axel committed
49
		fmt.Println("  start    [path, dbn, dbenv]")
Caro Axel's avatar
Caro Axel committed
50
		fmt.Println("           start the platform after loading its private key and root certificate")
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		fmt.Println("  help     print this help")
		fmt.Println("  version  print dfss client version")

		fmt.Println("\nFlags:")
		flag.PrintDefaults()

		fmt.Println()
	}
}

func main() {
	flag.Parse()
	command := flag.Arg(0)

	switch command {
	case "version":
		fmt.Println("v"+dfss.Version, runtime.GOOS, runtime.GOARCH)
Caro Axel's avatar
Caro Axel committed
68
	case "init":
69
		err := authority.Initialize(keySize, validity, country, org, unit, cn, path)
Caro Axel's avatar
Caro Axel committed
70 71 72 73 74 75
		if err != nil {
			fmt.Println("An error occured during the initialization operation")
			fmt.Println(err)
			panic(err)
		}
	case "start":
76
		pid, err := authority.Start(path)
Caro Axel's avatar
Caro Axel committed
77
		if err != nil {
Caro Axel's avatar
Caro Axel committed
78
			fmt.Println("An error occured during the private key and root certificate retrieval")
Caro Axel's avatar
Caro Axel committed
79 80 81 82 83
			fmt.Println(err)
			panic(err)
		}
		// TODO: use pid
		_ = pid
Caro Axel's avatar
Caro Axel committed
84

Caro Axel's avatar
Caro Axel committed
85 86 87 88 89 90 91 92
		dbManager, err := mgdb.NewManager(dbName, dbEnvVarName)
		if err != nil {
			fmt.Println("An error occured during the connection to Mongo DB")
			fmt.Println(err)
			panic(err)
		}
		// TODO: use dbManager
		_ = dbManager
93 94 95 96
	default:
		flag.Usage()
	}
}