Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hypervisor improvements #126

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cmd/hypervisor/commands/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@ import (
"github.com/SkycoinProject/skywire-mainnet/pkg/util/pathutil"
)

// nolint:gochecknoglobals
var (
output string
replace bool
configLocType = pathutil.WorkingDirLoc
)

// nolint:gochecknoinits
func init() {
outputUsage := "path of output config file. Uses default of 'type' flag if unspecified."
replaceUsage := "whether to allow rewrite of a file that already exists."
configLocTypeUsage := fmt.Sprintf("config generation mode. Valid values: %v", pathutil.AllConfigLocationTypes())

rootCmd.AddCommand(genConfigCmd)
genConfigCmd.Flags().StringVarP(&output, "output", "o", "", "path of output config file. Uses default of 'type' flag if unspecified.")
genConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.")
genConfigCmd.Flags().VarP(&configLocType, "type", "m", fmt.Sprintf("config generation mode. Valid values: %v", pathutil.AllConfigLocationTypes()))
genConfigCmd.Flags().StringVarP(&output, "output", "o", "", outputUsage)
genConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, replaceUsage)
genConfigCmd.Flags().VarP(&configLocType, "type", "m", configLocTypeUsage)
}

// nolint:gochecknoglobals
var genConfigCmd = &cobra.Command{
Use: "gen-config",
Short: "generates a configuration file",
Expand Down
10 changes: 6 additions & 4 deletions cmd/hypervisor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net"
"net/http"
"os"

"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/spf13/cobra"
Expand All @@ -15,6 +14,7 @@ import (

const configEnv = "SW_HYPERVISOR_CONFIG"

// nolint:gochecknoglobals
var (
log = logging.MustGetLogger("hypervisor")

Expand All @@ -26,6 +26,7 @@ var (
mockMaxRoutes int
)

// nolint:gochecknoinits
func init() {
rootCmd.Flags().StringVarP(&configPath, "config", "c", "./hypervisor-config.json", "hypervisor config path")
rootCmd.Flags().BoolVarP(&mock, "mock", "m", false, "whether to run hypervisor with mock data")
Expand All @@ -35,6 +36,7 @@ func init() {
rootCmd.Flags().IntVar(&mockMaxRoutes, "mock-max-routes", 30, "max number of routes per node")
}

// nolint:gochecknoglobals
var rootCmd = &cobra.Command{
Use: "hypervisor",
Short: "Manages Skywire App Nodes",
Expand All @@ -48,7 +50,8 @@ var rootCmd = &cobra.Command{
if err := config.Parse(configPath); err != nil {
log.WithError(err).Fatalln("failed to parse config file")
}
fmt.Println(config)

fmt.Println("Config: \n", config)

var (
httpAddr = config.Interfaces.HTTPAddr
Expand Down Expand Up @@ -95,7 +98,6 @@ var rootCmd = &cobra.Command{
// Execute executes root CLI command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
log.Fatal(err)
}
}
37 changes: 29 additions & 8 deletions pkg/hypervisor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
"github.com/SkycoinProject/skywire-mainnet/pkg/util/pathutil"
)

const (
defaultCookieExpiration = 12 * time.Hour
hashKeyLen = 64
blockKeyLen = 32
)

// Key allows a byte slice to be marshaled or unmarshaled from a hex string.
type Key []byte

Expand All @@ -30,28 +36,32 @@ func (hk Key) MarshalText() ([]byte, error) {
func (hk *Key) UnmarshalText(text []byte) error {
*hk = make([]byte, hex.DecodedLen(len(text)))
_, err := hex.Decode(*hk, text)

return err
}

// Config configures the hypervisor.
type Config struct {
PK cipher.PubKey `json:"public_key"`
SK cipher.SecKey `json:"secret_key"`
DBPath string `json:"db_path"` // Path to store database file.
EnableAuth bool `json:"enable_auth"` // Whether to enable user management.
Cookies CookieConfig `json:"cookies"` // Configures cookies (for session management).
Interfaces InterfaceConfig `json:"interfaces"` // Configures exposed interfaces.
DBPath string `json:"db_path"` // Path to store database file.
EnableAuth bool `json:"enable_auth"` // Whether to enable user management.
PK cipher.PubKey `json:"public_key"`
SK cipher.SecKey `json:"secret_key"`
}

func makeConfig() Config {
var c Config

pk, sk := cipher.GenerateKeyPair()
c.PK = pk
c.SK = sk
c.EnableAuth = true
c.Cookies.HashKey = cipher.RandByte(64)
c.Cookies.BlockKey = cipher.RandByte(32)
c.Cookies.HashKey = cipher.RandByte(hashKeyLen)
c.Cookies.BlockKey = cipher.RandByte(blockKeyLen)

c.FillDefaults()

return c
}

Expand All @@ -61,22 +71,26 @@ func GenerateWorkDirConfig() Config {
if err != nil {
log.Fatalf("failed to generate WD config: %s", dir)
}

c := makeConfig()
c.DBPath = filepath.Join(dir, "users.db")

return c
}

// GenerateHomeConfig generates a config with default values and uses db from user's home folder.
func GenerateHomeConfig() Config {
c := makeConfig()
c.DBPath = filepath.Join(pathutil.HomeDir(), ".skycoin/hypervisor/users.db")

return c
}

// GenerateLocalConfig generates a config with default values and uses db from shared folder.
func GenerateLocalConfig() Config {
c := makeConfig()
c.DBPath = "/usr/local/SkycoinProject/hypervisor/users.db"

return c
}

Expand All @@ -92,11 +106,18 @@ func (c *Config) Parse(path string) error {
if path, err = filepath.Abs(path); err != nil {
return err
}

f, err := os.Open(filepath.Clean(path))
if err != nil {
return err
}
defer func() { catch(f.Close()) }()

defer func() {
if err := f.Close(); err != nil {
log.Fatalf("Failed to close file %s: %v", f.Name(), err)
}
}()

return json.NewDecoder(f).Decode(c)
}

Expand All @@ -116,7 +137,7 @@ type CookieConfig struct {

// FillDefaults fills config with default values.
func (c *CookieConfig) FillDefaults() {
c.ExpiresDuration = time.Hour * 12
c.ExpiresDuration = defaultCookieExpiration
c.Path = "/"
c.Secure = true
c.HTTPOnly = true
Expand Down
Loading