From ea1aaedba36f9272f13cffab5c60b9484eaf3db2 Mon Sep 17 00:00:00 2001 From: Pietralberto Mazza <18440657+altafan@users.noreply.github.com> Date: Tue, 13 Feb 2024 19:09:01 +0100 Subject: [PATCH] Fix exposed volumes & add var to customise cli datadir (#87) * Fix config init & customize cli datadir * Fix exposed volumes * Fix * Fix * Fix --- Dockerfile | 3 +- cmd/ocean/config.go | 13 +++---- cmd/ocean/main.go | 38 ++++++++++++++++--- cmd/ocean/util.go | 5 +-- cmd/ocean/wallet.go | 5 --- .../blockchain-scanner/elements/rpc_client.go | 2 +- 6 files changed, 43 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5c24bb4..e16b2e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,7 +31,8 @@ ENV OCEAN_CLI_DATADIR=/app/data/ocean ENV PATH="/app:${PATH}" # Expose volume containing all `oceand` data -VOLUME /app/data +VOLUME /app/data/oceand +VOLUME /app/data/ocean # Expose ports of grpc server and profiler EXPOSE 18000 diff --git a/cmd/ocean/config.go b/cmd/ocean/config.go index 78dbd41..e18868d 100644 --- a/cmd/ocean/config.go +++ b/cmd/ocean/config.go @@ -39,7 +39,7 @@ var ( func init() { configInitCmd.Flags().StringVar( - &rpcServer, "rpcserver", initialState["rpcserver"], + &rpcServer, "rpcserver", initialState()["rpcserver"], "address of the ocean wallet to connect to", ) configInitCmd.Flags().BoolVar( @@ -47,7 +47,7 @@ func init() { "this must be set if the ocean wallet has TLS disabled", ) configInitCmd.Flags().StringVar( - &tlsCertPath, "tls-cert-path", initialState["tls_cert_path"], + &tlsCertPath, "tls-cert-path", initialState()["tls_cert_path"], "the path of the TLS certificate file to use to connect to the ocean "+ "wallet if it has TLS enabled", ) @@ -59,7 +59,7 @@ func configSet(cmd *cobra.Command, args []string) error { value := args[1] // Prevent setting anything that is not part of the state. - if _, ok := initialState[key]; !ok { + if _, ok := initialState()[key]; !ok { return nil } @@ -67,7 +67,7 @@ func configSet(cmd *cobra.Command, args []string) error { if key == "no_tls" { partialState["tls_cert_path"] = "" if val, _ := strconv.ParseBool(value); !val { - partialState["tls_cert_path"] = initialState["tls_cert_path"] + partialState["tls_cert_path"] = initialState()["tls_cert_path"] } } if key == "tls_cert_path" { @@ -87,10 +87,9 @@ func configSet(cmd *cobra.Command, args []string) error { } func configInit(cmd *cobra.Command, args []string) error { - if _, err := getState(); err != nil { - return err + if noTLS { + tlsCertPath = "" } - if err := setState(map[string]string{ "rpcserver": rpcServer, "no_tls": strconv.FormatBool(noTLS), diff --git a/cmd/ocean/main.go b/cmd/ocean/main.go index b84c49b..0719ca7 100644 --- a/cmd/ocean/main.go +++ b/cmd/ocean/main.go @@ -10,17 +10,18 @@ import ( "github.com/spf13/cobra" ) +const ( + datadirKey = "OCEAN_CLI_DATADIR" + dbFile = "state.json" +) + var ( version = "dev" commit = "none" date = "unknown" - walletDatadir = btcutil.AppDataDir("oceand", false) - initialState = map[string]string{ - "rpcserver": "localhost:18000", - "no_tls": strconv.FormatBool(false), - "tls_cert_path": filepath.Join(walletDatadir, "tls", "cert.pem"), - } + datadir = btcutil.AppDataDir("ocean-cli", false) + statePath string rootCmd = &cobra.Command{ Use: "ocean", @@ -36,6 +37,8 @@ var ( ) func init() { + initCLIEnv() + rootCmd.AddCommand(configCmd, walletCmd, accountCmd, txCmd) } @@ -45,3 +48,26 @@ func main() { os.Exit(1) } } + +func initCLIEnv() { + dir := cleanAndExpandPath(os.Getenv(datadirKey)) + if len(dir) > 0 { + datadir = dir + } + + statePath = filepath.Join(datadir, dbFile) + +} + +func initialState() map[string]string { + dir := cleanAndExpandPath(os.Getenv(datadirKey)) + if len(dir) > 0 { + datadir = dir + } + + return map[string]string{ + "rpcserver": "localhost:18000", + "no_tls": strconv.FormatBool(false), + "tls_cert_path": filepath.Join(datadir, "tls", "cert.pem"), + } +} diff --git a/cmd/ocean/util.go b/cmd/ocean/util.go index 7bb0cf1..a917c6b 100644 --- a/cmd/ocean/util.go +++ b/cmd/ocean/util.go @@ -96,10 +96,10 @@ func getState() (map[string]string, error) { if !os.IsNotExist(err) { return nil, err } - if err := writeState(initialState); err != nil { + if err := writeState(initialState()); err != nil { return nil, err } - return initialState, nil + return initialState(), nil } data := map[string]string{} @@ -120,7 +120,6 @@ func setState(partialState map[string]string) error { } func writeState(state map[string]string) error { - dir := filepath.Dir(statePath) if _, err := os.Stat(dir); os.IsNotExist(err) { err = os.MkdirAll(dir, 0755) diff --git a/cmd/ocean/wallet.go b/cmd/ocean/wallet.go index 69e0e9a..d281fec 100644 --- a/cmd/ocean/wallet.go +++ b/cmd/ocean/wallet.go @@ -4,17 +4,12 @@ import ( "context" "fmt" "io" - "path/filepath" - "github.com/btcsuite/btcd/btcutil" "github.com/spf13/cobra" pb "github.com/vulpemventures/ocean/api-spec/protobuf/gen/go/ocean/v1" ) var ( - datadir = btcutil.AppDataDir("ocean-cli", false) - statePath = filepath.Join(datadir, "state.json") - mnemonic, password, oldPassword, diff --git a/internal/infrastructure/blockchain-scanner/elements/rpc_client.go b/internal/infrastructure/blockchain-scanner/elements/rpc_client.go index d029253..7840684 100644 --- a/internal/infrastructure/blockchain-scanner/elements/rpc_client.go +++ b/internal/infrastructure/blockchain-scanner/elements/rpc_client.go @@ -39,8 +39,8 @@ func newRpcClient( useSSL := strings.HasPrefix(addr, "https") if useSSL { - // #nosec t := &http.Transport{ + // #nosec G402 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } httpClient = &http.Client{Transport: t}