Skip to content

Commit

Permalink
Merge pull request #1369 from skycoin/feat/privacyjson
Browse files Browse the repository at this point in the history
`skywire-cli config priv` subcommand
  • Loading branch information
ersonp authored Oct 5, 2022
2 parents 6658b87 + c1af0ed commit 9824aba
Show file tree
Hide file tree
Showing 163 changed files with 26,218 additions and 153 deletions.
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
123 changes: 123 additions & 0 deletions cmd/skywire-cli/commands/config/private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package cliconfig

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
coincipher "github.com/skycoin/skycoin/src/cipher"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire/pkg/skyenv"
)

var (
displayNodeIP bool
rewardAddress string
out string
pathstr string
fullpathstr string
getpathstr string
dummy string
)

func init() {

privacyConfigCmd.Flags().SortFlags = false
RootCmd.AddCommand(privacyConfigCmd)
privacyConfigCmd.AddCommand(setPrivacyConfigCmd)
privacyConfigCmd.AddCommand(getPrivacyConfigCmd)
setPrivacyConfigCmd.Flags().BoolVarP(&displayNodeIP, "publicip", "i", false, "display node ip")
// default is genesis address for skycoin blockchain ; for testing
setPrivacyConfigCmd.Flags().StringVarP(&rewardAddress, "address", "a", "2jBbGxZRGoQG1mqhPBnXnLTxK6oxsTf8os6", "reward address")
//use the correct path for the available pemissions
pathstr = skyenv.PackageConfig().LocalPath
fullpathstr = strings.Join([]string{pathstr, skyenv.PrivFile}, "/")
getpathstr = fullpathstr
if _, err := os.Stat(getpathstr); os.IsNotExist(err) {
getpathstr = ""
}
setPrivacyConfigCmd.Flags().StringVarP(&out, "out", "o", "", "output config: "+fullpathstr)
getPrivacyConfigCmd.Flags().StringVarP(&out, "out", "o", "", "read config from: "+getpathstr)
RootCmd.PersistentFlags().StringVar(&dummy, "rpc", "localhost:3435", "RPC server address")
RootCmd.PersistentFlags().MarkHidden("rpc") // nolint

}

var privacyConfigCmd = &cobra.Command{
SilenceErrors: true,
SilenceUsage: true,
Use: "priv",
Short: "rewards & privacy setting",
Long: `rewards & privacy setting
Sets the skycoin rewards address and ip public for the visor.
The config is written to the root of the default local directory
Run this command with root permissions for visors running as root via systemd
this config is served via dmsghttp along with transport logs
and the system hardware survey for automating rewards distribution`,
}

var setPrivacyConfigCmd = &cobra.Command{
Use: "set <address>",
Short: "set reward address & node privacy",
Long: "set reward address & node privacy",
Run: func(cmd *cobra.Command, args []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
if out == "" {
out = fullpathstr
}
if len(args) > 0 {
if args[0] != "" {
rewardAddress = args[0]
}
}
_, err := coincipher.DecodeBase58Address(rewardAddress)
if err != nil {
logger.WithError(err).Fatal("invalid address specified")
}

confp := &skyenv.Privacy{}
confp.DisplayNodeIP = displayNodeIP
confp.RewardAddress = rewardAddress

// Print results.
j, err := json.MarshalIndent(confp, "", "\t")
if err != nil {
logger.WithError(err).Fatal("Could not marshal json.")
}
if _, err := os.Stat(pathstr); os.IsNotExist(err) {
logger.WithError(err).Fatal("\n local directory not found ; run skywire first to create this path\n ")
}
err = os.WriteFile(out, j, 0644) //nolint
if err != nil {
logger.WithError(err).Fatal("Failed to write config to file.")
}
logger.Infof("Updated file '%s' to:\n%s\n", out, j)
},
}
var getPrivacyConfigCmd = &cobra.Command{
Use: "get",
Short: "read reward address & privacy setting from file",
Long: `read reward address & privacy setting from file`,
Run: func(cmd *cobra.Command, args []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
if out == "" {
out = getpathstr
}
if out == "" {
logger.Fatal("config was not detected and no path was specified.")
}
p, err := os.ReadFile(filepath.Clean(out))
if err != nil {
logger.WithError(err).Fatal("Failed to read config file.")
}
fmt.Printf("%s\n", p)
},
}
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
70 changes: 70 additions & 0 deletions cmd/skywire-cli/commands/visor/privacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package clivisor

import (
"fmt"
"strings"

"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 = strings.Join([]string{skyenv.Config().LocalPath, skyenv.PrivFile}, "/")
setPrivacyCmd.Flags().StringVarP(&out, "out", "o", "", "output config: "+pathstr)
}

var privacyCmd = &cobra.Command{
Use: "priv",
Short: "privacy settings",
Long: "configure privacy settings\n\ntest of the api endpoints GetPrivacy & SetPrivacy",
Hidden: true,
}
var setPrivacyCmd = &cobra.Command{
Use: "set",
Short: "set privacy.json via rpc",
Long: "configure privacy settings\n\ntest of the api endpoint SetPrivacy",
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())
resp, 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("Privacy settings updated to:\n", resp)
},
}
var getPrivacyCmd = &cobra.Command{
Use: "get",
Short: "read privacy setting from file",
Long: "configure privacy settings\n\ntest of the api endpoints GetPrivacy",
Run: func(cmd *cobra.Command, args []string) {
p, err := clirpc.Client(cmd.Flags()).GetPrivacy()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
fmt.Printf("%s", p)
},
}
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")
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.SystemSurvey()
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/lib/pq v1.10.7
github.com/skycoin/dmsg v0.0.0-20220904231115-c313c992c788
github.com/skycoin/skywire-utilities v0.0.0-20220712142443-abafa30105ce
Expand All @@ -58,17 +59,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 @@ -83,7 +87,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

0 comments on commit 9824aba

Please sign in to comment.