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

skywire-cli config priv subcommand #1369

Merged
merged 19 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
9 changes: 0 additions & 9 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

Expand Down Expand Up @@ -187,14 +186,6 @@ var genConfigCmd = &cobra.Command{
//don't write file with stdout
if !isStdout {
if skyenv.OS == "linux" {
userLvl, err := user.Current()
if err != nil {
logger.WithError(err).Error("Failed to detect user.")
} else {
if userLvl.Username == "root" {
isRoot = true
}
}
//warn when writing config as root to non root owned dir & fail on the reverse instance
if _, err = exec.LookPath("stat"); err == nil {
confPath1, _ := filepath.Split(confPath)
Expand Down
3 changes: 2 additions & 1 deletion cmd/skywire-cli/commands/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/logging"
utilenv "github.com/skycoin/skywire-utilities/pkg/skyenv"
"github.com/skycoin/skywire/pkg/skyenv"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ var (
isAll bool
isOutUnset bool
ver string
isRoot bool
isRoot = skyenv.IsRoot()
svcconf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") //skyenv.DefaultServiceConfAddr
testconf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") //skyenv.DefaultServiceConfAddr
ghiddenflags []string
Expand Down
4 changes: 2 additions & 2 deletions cmd/skywire-cli/commands/rpc/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"net"
"time"

"github.com/spf13/pflag"

"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/visor"

"github.com/spf13/pflag"
)

var (
Expand Down
98 changes: 98 additions & 0 deletions cmd/skywire-cli/commands/visor/logserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package clivisor

import (
"context"
"net/http"
"time"

"github.com/sirupsen/logrus"
"github.com/skycoin/dmsg/pkg/disc"
dmsg "github.com/skycoin/dmsg/pkg/dmsg"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/cmdutil"
"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire/pkg/skyenv"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)

var (
dir = skyenv.PackageConfig().LocalPath // local dir to serve via http
0pcom marked this conversation as resolved.
Show resolved Hide resolved
dmsgDisc = "http://dmsgd.skywire.skycoin.com"
0pcom marked this conversation as resolved.
Show resolved Hide resolved
dmsgPort = uint(81)
pubkey, seckey = cipher.GenerateKeyPair() //nolint
0pcom marked this conversation as resolved.
Show resolved Hide resolved
)

func init() {
RootCmd.AddCommand(logserverCmd)
logserverCmd.Flags().SortFlags = false
logserverCmd.Flags().StringVarP(&dir, "dir", "d", dir, "local dir to serve via http")
logserverCmd.Flags().StringVarP(&dmsgDisc, "disc", "e", dmsgDisc, "dmsg discovery address")
logserverCmd.Flags().UintVarP(&dmsgPort, "port", "p", dmsgPort, "dmsg port to serve from")
logserverCmd.Flags().Var(&seckey, "sk", "dmsg secret key")
logserverCmd.Flags().MarkHidden("sk") //nolint
}

var logserverCmd = &cobra.Command{
Use: "logserver",
Short: "log server",
Long: `dmsghttp log server

0pcom marked this conversation as resolved.
Show resolved Hide resolved
Serves the local folder via dmsghttp`,
Run: func(cmd *cobra.Command, args []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
log := logging.MustGetLogger("dmsghttp-logserver")
0pcom marked this conversation as resolved.
Show resolved Hide resolved
ctx, cancel := cmdutil.SignalContext(context.Background(), log)
defer cancel()
if !skyenv.IsRoot() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it need to run as root/

log.Fatal("Log server is designed to run as root.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe saying something like Log server requires root permissions would be more explicit if we indeed need root permissions.

}
log.WithField("config filepath", skyenv.SkywirePath+"/"+skyenv.Configjson).Info()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this log at INFO level? Maybe helpful for you as you test this server?


conf, err := visorconfig.ReadFile(skyenv.SkywirePath + "/" + skyenv.Configjson)
if err != nil {
log.WithError(err).Fatal("Failed to read in config.")
}

seckey = conf.SK
pubkey, err := seckey.PubKey()
if err != nil {
log.WithError(err).Fatal("bad secret key.")
}
c := dmsg.NewClient(pubkey, seckey, disc.NewHTTP(dmsgDisc, &http.Client{}, log), dmsg.DefaultConfig())
defer func() {
if err := c.Close(); err != nil {
log.WithError(err).Error()
}
}()
go c.Serve(context.Background())
select {
case <-ctx.Done():
log.WithError(ctx.Err()).Warn()
return
case <-c.Ready():
}
lis, err := c.Listen(uint16(dmsgPort))
if err != nil {
log.WithError(err).Fatal()
}
go func() {
<-ctx.Done()
if err := lis.Close(); err != nil {
log.WithError(err).Error()
}
}()
srv := &http.Server{
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: http.FileServer(http.Dir(dir)),
}
log.WithField("dir", dir).
WithField("dmsg_addr", lis.Addr().String()).
Info("Serving...")
log.Fatal(srv.Serve(lis))
},
}
87 changes: 87 additions & 0 deletions cmd/skywire-cli/commands/visor/privacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package clivisor

import (
"encoding/json"
"fmt"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-utilities/pkg/logging"
clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/skyenv"
)

var (
displayNodeIP bool
rewardAddress string
out string
pathstr string
)

func init() {

RootCmd.AddCommand(privacyCmd)
privacyCmd.AddCommand(setPrivacyCmd)
privacyCmd.AddCommand(getPrivacyCmd)
privacyCmd.Flags().SortFlags = false
setPrivacyCmd.Flags().BoolVarP(&displayNodeIP, "publicip", "i", false, "display node ip")
// default is genesis address for skycoin blockchain ; for testing
setPrivacyCmd.Flags().StringVarP(&rewardAddress, "address", "a", "2jBbGxZRGoQG1mqhPBnXnLTxK6oxsTf8os6", "reward address")
//use the correct path for the available pemissions
pathstr = skyenv.Config().LocalPath + "/privacy.json"
setPrivacyCmd.Flags().StringVarP(&out, "out", "o", "", "output config: "+pathstr)
//getPrivacyCmd.Flags().StringVarP(&out, "out", "o", "", "read from: "+pathstr)

}

var privacyCmd = &cobra.Command{
Use: "priv",
Short: "privacy settings",
Long: `configure privacy settings

test of the api endpoints GetPrivacy & SetPrivacy`,
Hidden: true,
}
var setPrivacyCmd = &cobra.Command{
Use: "set",
Short: "set privacy.json via rpc",
Long: `configure privacy settings

test of the api endpoint SetPrivacy`,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
log := logging.MustGetLogger("skywire-cli visor priv set")
client := clirpc.Client(cmd.Flags())
err := client.SetPrivacy(skyenv.Privacy{DisplayNodeIP: displayNodeIP, RewardAddress: rewardAddress})
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
log.Info("OK")
},
}
var getPrivacyCmd = &cobra.Command{
Use: "get",
Short: "read privacy setting from file",
Long: `configure privacy settings

test of the api endpoints GetPrivacy & SetPrivacy`,
Run: func(cmd *cobra.Command, args []string) {
//mLog := logging.NewMasterLogger()
//mLog.SetLevel(logrus.InfoLevel)
log := logging.MustGetLogger("skywire-cli visor priv get")
client := clirpc.Client(cmd.Flags())
p, err := client.GetPrivacy()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
j, err := json.MarshalIndent(p, "", "\t")
if err != nil {
log.WithError(err).Fatal("Could not unmarshal json.")
}
fmt.Printf("%s", j)
0pcom marked this conversation as resolved.
Show resolved Hide resolved
},
}
35 changes: 24 additions & 11 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"embed"
"encoding/json"
"fmt"
"io"
"io/fs"
Expand All @@ -12,7 +13,6 @@ import (
_ "net/http/pprof" // nolint:gosec // https://golang.org/doc/diagnostics.html#profiling
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -55,6 +55,7 @@ var (
stdin bool
launchBrowser bool
hypervisorUI bool
noHypervisorUI bool
remoteHypervisorPKs string
disableHypervisorPKs bool
isAutoPeer bool
Expand All @@ -66,23 +67,17 @@ var (
all bool
pkg bool
usr bool
localIPs []net.IP
localIPs []net.IP // nolint:unused
// root indicates process is run with root permissions
root bool // nolint:unused
// visorBuildInfo holds information about the build
visorBuildInfo *buildinfo.Info
)

func init() {
usrLvl, err := user.Current()
if err != nil {
panic(err)
}
if usrLvl.Username == "root" {
root = true
}
root = skyenv.IsRoot()

localIPs, err = netutil.DefaultNetworkInterfaceIPs()
localIPs, err := netutil.DefaultNetworkInterfaceIPs()
if err != nil {
logger.WithError(err).Warn("Could not determine network interface IP address")
if len(localIPs) == 0 {
Expand All @@ -97,6 +92,8 @@ func init() {
rootCmd.Flags().BoolVarP(&launchBrowser, "browser", "b", false, "open hypervisor ui in default web browser")
}
rootCmd.Flags().BoolVarP(&hypervisorUI, "hvui", "i", false, "run as hypervisor")
rootCmd.Flags().BoolVarP(&noHypervisorUI, "nohvui", "x", false, "disable hypervisor")
0pcom marked this conversation as resolved.
Show resolved Hide resolved
hiddenflags = append(hiddenflags, "nohvui")
rootCmd.Flags().StringVarP(&remoteHypervisorPKs, "hv", "j", "", "add remote hypervisor PKs at runtime")
hiddenflags = append(hiddenflags, "hv")
rootCmd.Flags().BoolVarP(&disableHypervisorPKs, "xhv", "k", false, "disable remote hypervisors set in config file")
Expand Down Expand Up @@ -252,6 +249,18 @@ func runVisor(conf *visorconfig.V1) {
conf = initConfig(log, confPath)
}

survey := skyenv.HwSurvey()
survey.PubKey = conf.PK
// Print results.
s, err := json.MarshalIndent(survey, "", "\t")
if err != nil {
log.WithError(err).Error("Could not marshal json.")
}
err = os.WriteFile(conf.LocalPath+"/"+skyenv.SurveyFile, s, 0644) //nolint
if err != nil {
log.WithError(err).Error("Failed to write system hardware survey to file.")
}

if skyenv.OS == "linux" {
//warn about creating files & directories as root in non root-owned dir
if _, err := exec.LookPath("stat"); err == nil {
Expand All @@ -268,7 +277,7 @@ func runVisor(conf *visorconfig.V1) {
log.Error("cannot stat: /root")
}
if (owner != rootOwner) && root {
log.Warn("writing config as root to directory not owned by root")
log.Warn("writing as root to directory not owned by root")
}
if !root && (owner == rootOwner) {
log.Fatal("Insufficient permissions to write to the specified path")
Expand Down Expand Up @@ -466,6 +475,10 @@ func initConfig(mLog *logging.MasterLogger, confPath string) *visorconfig.V1 { /
if conf.Hypervisor != nil {
conf.Hypervisor.UIAssets = uiAssets
}
if noHypervisorUI {
conf.Hypervisor = nil
}

return conf
}

Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f
github.com/ivanpirog/coloredcobra v1.0.0
github.com/james-barrow/golang-ipc v0.0.0-20210227130457-95e7cc81f5e2
github.com/jaypipes/ghw v0.9.0
github.com/skycoin/dmsg v0.0.0-20220904231115-c313c992c788
github.com/skycoin/skywire-utilities v0.0.0-20220712142443-abafa30105ce
github.com/skycoin/systray v1.10.1-0.20220630135132-48d2a1fb85d8
Expand All @@ -57,17 +58,20 @@ require (
github.com/ActiveState/termtest/conpty v0.5.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/creack/pty v1.1.15 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jaypipes/pcidb v1.0.0 // indirect
github.com/klauspost/compress v1.11.0 // indirect
github.com/klauspost/cpuid v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -82,7 +86,9 @@ require (
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/mod v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
howett.net/plist v1.0.0 // indirect
)

// Uncomment for tests with alternate branches of 'dmsg'
Expand Down
Loading