Skip to content

Commit

Permalink
pull and fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kifen committed Jan 24, 2020
2 parents 8845d9c + d11e0f5 commit bcd7f48
Show file tree
Hide file tree
Showing 9 changed files with 879 additions and 466 deletions.
2 changes: 2 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Did you run `make format && make check`?

Fixes #

Changes:
Expand Down
3 changes: 3 additions & 0 deletions cmd/hypervisor/commands/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/SkycoinProject/skywire-mainnet/pkg/util/pathutil"
)

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

// nolint:gochecknoinits
func init() {
rootCmd.AddCommand(genConfigCmd)
genConfigCmd.Flags().StringVarP(&output, "output", "o", "", "path of output config file. Uses default of 'type' flag if unspecified.")
Expand All @@ -25,6 +27,7 @@ func init() {
genConfigCmd.Flags().BoolVarP(&testenv, "testing-environment", "t", false, "whether to use production or test deployment service.")
}

// 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 @@ -3,7 +3,6 @@ package commands
import (
"fmt"
"net/http"
"os"

"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/disc"
Expand All @@ -17,6 +16,7 @@ import (

const configEnv = "SW_HYPERVISOR_CONFIG"

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

Expand All @@ -28,6 +28,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 @@ -37,6 +38,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 @@ -50,7 +52,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 @@ -106,7 +109,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)
}
}
28 changes: 24 additions & 4 deletions pkg/hypervisor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 @@ -32,6 +38,7 @@ 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
}

Expand All @@ -48,6 +55,7 @@ type Config struct {

func makeConfig(testenv bool) Config {
var c Config

pk, sk := cipher.GenerateKeyPair()
c.PK = pk
c.SK = sk
Expand All @@ -59,9 +67,11 @@ func makeConfig(testenv bool) Config {
}

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 @@ -73,20 +83,23 @@ func GenerateWorkDirConfig(testenv bool) Config {
}
c := makeConfig(testenv)
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(testenv bool) Config {
c := makeConfig(testenv)
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(testenv bool) Config {
c := makeConfig(testenv)
c.DBPath = "/usr/local/SkycoinProject/hypervisor/users.db"

return c
}

Expand All @@ -102,11 +115,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 @@ -126,7 +146,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

0 comments on commit bcd7f48

Please sign in to comment.