From 3405f6746fb7ee63a92b1d8a3143a7a3bdfecc69 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 8 Mar 2022 14:19:12 +0000 Subject: [PATCH 01/23] Auth middleware for node balancer --- .gitignore | 1 + nodes/node_balancer/cmd/data.go | 4 ++ nodes/node_balancer/cmd/middleware.go | 68 +++++++++++++++++++++++++ nodes/node_balancer/cmd/server.go | 2 +- nodes/node_balancer/configs/settings.go | 17 ++++--- nodes/node_balancer/sample.env | 7 ++- 6 files changed, 88 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 51858600..9ad5297a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.secrets/ .vscode/ .DS_Store \ No newline at end of file diff --git a/nodes/node_balancer/cmd/data.go b/nodes/node_balancer/cmd/data.go index 0a1d3e0a..b46c30d1 100644 --- a/nodes/node_balancer/cmd/data.go +++ b/nodes/node_balancer/cmd/data.go @@ -17,6 +17,10 @@ type NodeStatusResponse struct { CurrentBlock uint64 `json:"current_block"` } +type BugoutUserResponse struct { + ID string `json:"user_id"` +} + // Node - which one node client worked with // LastCallTs - timestamp from last call type Client struct { diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 1bc69fda..b952936d 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -4,9 +4,15 @@ Server API middlewares. package cmd import ( + "encoding/json" + "fmt" + "io/ioutil" "log" "net" "net/http" + "strings" + + "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" humbug "github.com/bugout-dev/humbug/go/pkg" ) @@ -40,3 +46,65 @@ func logMiddleware(next http.Handler) http.Handler { } }) } + +// Bugout authentication +func authMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + authHeaders := r.Header["Authorization"] + authHeadersLen := len(authHeaders) + if authHeadersLen == 0 { + http.Error(w, "Authorization header not found", http.StatusForbidden) + return + } + if authHeadersLen > 1 { + http.Error(w, "Too many authorization headers provided", http.StatusBadRequest) + return + } + authHeader := authHeaders[0] + + // Extract Bearer token + headerSlice := strings.Split(authHeader, " ") + if len(headerSlice) != 2 { + http.Error(w, "Unacceptable token format provided", http.StatusBadRequest) + return + } + if headerSlice[0] != "Bearer" { + http.Error(w, "Unacceptable token format provided", http.StatusBadRequest) + return + } + + // Check token is active + client := http.Client{Timeout: configs.BUGOUT_AUTH_CALL_TIMEOUT} + authReq, err := http.NewRequest("GET", fmt.Sprintf("%s/user", configs.BUGOUT_AUTH_URL), nil) + if err != nil { + http.Error(w, "Unable to construct authorization request", http.StatusInternalServerError) + return + } + authReq.Header.Set("Authorization", authHeader) + resp, err := client.Do(authReq) + if err != nil { + http.Error(w, "Unable to reach authorization server", http.StatusInternalServerError) + return + } + defer resp.Body.Close() + + // Parse response from authorization server + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + http.Error(w, "Unable to read respose from authorization server", http.StatusInternalServerError) + return + } + var userResponse BugoutUserResponse + err = json.Unmarshal(body, &userResponse) + if err != nil { + http.Error(w, "Unable to parse respose from authorization server", http.StatusInternalServerError) + return + } + if userResponse.ID == "" { + http.Error(w, "Wrong authorization header", http.StatusForbidden) + return + } + + next.ServeHTTP(w, r) + }) +} diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index be57b3ec..85e883cb 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -157,8 +157,8 @@ func InitServer() { } serveMux := http.NewServeMux() + serveMux.Handle("/nb/", authMiddleware(http.HandlerFunc(lbHandler))) serveMux.HandleFunc("/ping", pingRoute) - serveMux.HandleFunc("/nb/", lbHandler) // Set common middlewares, from bottom to top commonHandler := logMiddleware(serveMux) diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/configs/settings.go index 5eb88113..3f6cb47e 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/configs/settings.go @@ -10,6 +10,13 @@ import ( "time" ) +// Bugout config +var BUGOUT_AUTH_URL = os.Getenv("BUGOUT_AUTH_URL") +var BUGOUT_NODE_BALANCER_APPLICATION_ID = os.Getenv("BUGOUT_NODE_BALANCER_APPLICATION_ID") +var BUGOUT_INTERNAL_CRAWLERS_USER_ID = os.Getenv("BUGOUT_INTERNAL_CRAWLERS_USER_ID") +var BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 1 + +// Node config type BlockchainConfig struct { Blockchain string IPs []string @@ -30,10 +37,8 @@ var ConfigList NodeConfigList var MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR") var MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR") -var MOONSTREAM_NODE_ETHEREUM_IPC_PORT = os.Getenv("MOONSTREAM_NODE_ETHEREUM_IPC_PORT") var MOONSTREAM_NODE_POLYGON_A_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_A_IPC_ADDR") var MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_B_IPC_ADDR") -var MOONSTREAM_NODE_POLYGON_IPC_PORT = os.Getenv("MOONSTREAM_NODE_POLYGON_IPC_PORT") var MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT") var MOONSTREAM_CLIENT_ID_HEADER = os.Getenv("MOONSTREAM_CLIENT_ID_HEADER") @@ -56,8 +61,8 @@ func checkEnvVarSet() { MOONSTREAM_CLIENT_ID_HEADER = "x-moonstream-client-id" } - if MOONSTREAM_NODES_SERVER_PORT == "" || MOONSTREAM_NODE_ETHEREUM_IPC_PORT == "" || MOONSTREAM_NODE_POLYGON_IPC_PORT == "" { - log.Fatal("Some of environment variables not set") + if MOONSTREAM_NODES_SERVER_PORT == "" { + log.Fatal("MOONSTREAM_NODES_SERVER_PORT environment variable not set") } } @@ -70,12 +75,12 @@ func (nc *NodeConfigList) InitNodeConfigList() { blockchainConfigList = append(blockchainConfigList, BlockchainConfig{ Blockchain: "ethereum", IPs: []string{MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR, MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR}, - Port: MOONSTREAM_NODE_ETHEREUM_IPC_PORT, + Port: "8545", }) blockchainConfigList = append(blockchainConfigList, BlockchainConfig{ Blockchain: "polygon", IPs: []string{MOONSTREAM_NODE_POLYGON_A_IPC_ADDR, MOONSTREAM_NODE_POLYGON_B_IPC_ADDR}, - Port: MOONSTREAM_NODE_POLYGON_IPC_PORT, + Port: "8545", }) // Parse node addr, ip and blockchain diff --git a/nodes/node_balancer/sample.env b/nodes/node_balancer/sample.env index 97b56c8d..f186fd33 100644 --- a/nodes/node_balancer/sample.env +++ b/nodes/node_balancer/sample.env @@ -1,15 +1,14 @@ # Required environment variables for load balancer +export BUGOUT_AUTH_URL="https://auth.bugout.dev" +export BUGOUT_NODE_BALANCER_APPLICATION_ID="" +export BUGOUT_INTERNAL_CRAWLERS_USER_ID="" export MOONSTREAM_NODES_SERVER_PORT="" export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="" # Ethereum nodes depends variables -export MOONSTREAM_NODE_ETHEREUM_IPC_PORT="" - export MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR="" export MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR="" # Polygon nodes depends variables -export MOONSTREAM_NODE_POLYGON_IPC_PORT="" - export MOONSTREAM_NODE_POLYGON_A_IPC_ADDR="" export MOONSTREAM_NODE_POLYGON_B_IPC_ADDR="" From ca5bca10ae1a420acb04cccfb6907fe3ba8c1abc Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 8 Mar 2022 14:25:08 +0000 Subject: [PATCH 02/23] Application id check in auth middleware --- nodes/node_balancer/cmd/data.go | 1 + nodes/node_balancer/cmd/middleware.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/nodes/node_balancer/cmd/data.go b/nodes/node_balancer/cmd/data.go index b46c30d1..a7df1904 100644 --- a/nodes/node_balancer/cmd/data.go +++ b/nodes/node_balancer/cmd/data.go @@ -19,6 +19,7 @@ type NodeStatusResponse struct { type BugoutUserResponse struct { ID string `json:"user_id"` + ApplicationID string `json:"application_id"` } // Node - which one node client worked with diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index b952936d..5e013e30 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -104,6 +104,10 @@ func authMiddleware(next http.Handler) http.Handler { http.Error(w, "Wrong authorization header", http.StatusForbidden) return } + if userResponse.ApplicationID != configs.BUGOUT_NODE_BALANCER_APPLICATION_ID { + http.Error(w, "Wrong authorization header", http.StatusForbidden) + return + } next.ServeHTTP(w, r) }) From 06d16457a0d02697958d2374d78edcf928ea096f Mon Sep 17 00:00:00 2001 From: kompotkot Date: Wed, 9 Mar 2022 11:07:53 +0000 Subject: [PATCH 03/23] Write user data in request context --- nodes/node_balancer/cmd/middleware.go | 5 ++++- nodes/node_balancer/cmd/routes.go | 15 +++++++++++++-- nodes/node_balancer/configs/settings.go | 3 +++ nodes/node_balancer/sample.env | 3 +++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 5e013e30..1263e41c 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -4,6 +4,7 @@ Server API middlewares. package cmd import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -109,6 +110,8 @@ func authMiddleware(next http.Handler) http.Handler { return } - next.ServeHTTP(w, r) + ctxUser := context.WithValue(r.Context(), "user", userResponse) + + next.ServeHTTP(w, r.WithContext(ctxUser)) }) } diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index 672d89e4..4a3f93b3 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -22,6 +22,13 @@ func pingRoute(w http.ResponseWriter, r *http.Request) { // lbHandler load balances the incoming requests to nodes func lbHandler(w http.ResponseWriter, r *http.Request) { + userRaw := r.Context().Value("user") + user, ok := userRaw.(BugoutUserResponse) + if !ok { + http.Error(w, "Internal server error", http.StatusInternalServerError) + return + } + attempts := GetAttemptsFromContext(r) if attempts > configs.NB_CONNECTION_RETRIES { log.Printf("Max attempts reached from %s %s, terminating\n", r.RemoteAddr, r.URL.Path) @@ -73,8 +80,12 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { node.StatusReverseProxy.ServeHTTP(w, r) return case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/jsonrpc", blockchain)): - r.URL.Path = "/" - node.GethReverseProxy.ServeHTTP(w, r) + if user.ID == configs.BUGOUT_INTERNAL_CRAWLERS_USER_ID { + r.URL.Path = "/" + node.GethReverseProxy.ServeHTTP(w, r) + } else { + fmt.Println("Fetch from db") + } return default: http.Error(w, fmt.Sprintf("Unacceptable path for %s blockchain %s", blockchain, r.URL.Path), http.StatusBadRequest) diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/configs/settings.go index 3f6cb47e..06fdd0d8 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/configs/settings.go @@ -110,3 +110,6 @@ var NB_CLIENT_NODE_KEEP_ALIVE = int64(5) // How long to store node in hot list f // Humbug config var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN") + +// Database config +var MOONSTREAM_DB_URI = os.Getenv("MOONSTREAM_DB_URI") diff --git a/nodes/node_balancer/sample.env b/nodes/node_balancer/sample.env index f186fd33..dec6968b 100644 --- a/nodes/node_balancer/sample.env +++ b/nodes/node_balancer/sample.env @@ -5,6 +5,9 @@ export BUGOUT_INTERNAL_CRAWLERS_USER_ID=" Date: Wed, 16 Mar 2022 14:49:39 +0000 Subject: [PATCH 04/23] Get block and uth workflow --- nodes/node_balancer/cmd/bugout.go | 92 ++++++++++++++++++++ nodes/node_balancer/cmd/cli.go | 78 +++++++++++++++++ nodes/node_balancer/cmd/data.go | 17 +++- nodes/node_balancer/cmd/db.go | 108 ++++++++++++++++++++++++ nodes/node_balancer/cmd/routes.go | 105 +++++++++++++++++++---- nodes/node_balancer/cmd/server.go | 47 +++++------ nodes/node_balancer/configs/settings.go | 14 +-- nodes/node_balancer/configs/version.go | 2 +- nodes/node_balancer/go.mod | 1 + nodes/node_balancer/go.sum | 2 + nodes/node_balancer/main.go | 2 +- 11 files changed, 420 insertions(+), 48 deletions(-) create mode 100644 nodes/node_balancer/cmd/bugout.go create mode 100644 nodes/node_balancer/cmd/cli.go create mode 100644 nodes/node_balancer/cmd/db.go diff --git a/nodes/node_balancer/cmd/bugout.go b/nodes/node_balancer/cmd/bugout.go new file mode 100644 index 00000000..6db91432 --- /dev/null +++ b/nodes/node_balancer/cmd/bugout.go @@ -0,0 +1,92 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" +) + +var ( + bugoutClient BugoutClient +) + +type BugoutClient struct { + Client http.Client + AuthURL string +} + +// Initialize Bugout http client +func InitBugoutClient() { + client := http.Client{Timeout: configs.BUGOUT_AUTH_CALL_TIMEOUT} + bugoutClient = BugoutClient{ + Client: client, + AuthURL: configs.BUGOUT_AUTH_URL, + } +} + +// Get Bugout user +func (bc *BugoutClient) GetUser(token string) (BugoutUserResponse, error) { + url := fmt.Sprintf("%s/user", configs.BUGOUT_AUTH_URL) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return BugoutUserResponse{}, err + } + + req.Header = http.Header{ + "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, + } + resp, err := bc.Client.Do(req) + if err != nil { + return BugoutUserResponse{}, err + } + defer resp.Body.Close() + + // Parse response + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return BugoutUserResponse{}, err + } + var userResponse BugoutUserResponse + err = json.Unmarshal(body, &userResponse) + if err != nil { + return BugoutUserResponse{}, err + } + + return userResponse, nil +} + +// Get Bugout resources +func (bc *BugoutClient) GetResources(token string, userID string) (BugoutResourcesResponse, error) { + url := fmt.Sprintf("%s/resources?application_id=%s", configs.BUGOUT_AUTH_URL, configs.BUGOUT_NODE_BALANCER_APPLICATION_ID) + if userID != "" { + url += fmt.Sprintf("&user_id=%s", userID) + } + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return BugoutResourcesResponse{}, err + } + req.Header = http.Header{ + "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, + } + resp, err := bc.Client.Do(req) + if err != nil { + return BugoutResourcesResponse{}, err + } + defer resp.Body.Close() + + // Parse response + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return BugoutResourcesResponse{}, err + } + var resourcesResponse BugoutResourcesResponse + err = json.Unmarshal(body, &resourcesResponse) + if err != nil { + return BugoutResourcesResponse{}, err + } + + return resourcesResponse, nil +} diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go new file mode 100644 index 00000000..13558026 --- /dev/null +++ b/nodes/node_balancer/cmd/cli.go @@ -0,0 +1,78 @@ +package cmd + +import ( + "encoding/json" + "flag" + "fmt" + "os" + + "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" +) + +var ( + stateCLI StateCLI +) + +// Command Line Interface state +type StateCLI struct { + serverCmd *flag.FlagSet + clientsCmd *flag.FlagSet + + // Common flags + showVersion bool + + // Server flags + listeningAddr string + listeningPort string + enableHealthCheck bool + enableDebug bool +} + +func (s *StateCLI) populateCLI() { + // Subcommands setup + s.serverCmd = flag.NewFlagSet("server", flag.ExitOnError) + s.clientsCmd = flag.NewFlagSet("clients", flag.ExitOnError) + + // Server subcommand flag pointers + s.serverCmd.StringVar(&s.listeningAddr, "host", "127.0.0.1", "Server listening address") + s.serverCmd.StringVar(&s.listeningPort, "port", "8544", "Server listening port") + s.serverCmd.BoolVar(&s.enableHealthCheck, "healthcheck", false, "To enable healthcheck ser healthcheck flag") + s.serverCmd.BoolVar(&s.enableDebug, "debug", false, "To enable debug mode with extended log set debug flag") +} + +func init() { + InitBugoutClient() +} + +func CLI() { + stateCLI.populateCLI() + if len(os.Args) < 2 { + fmt.Println("Command: server or version is required") + os.Exit(1) + } + + // Parse subcommands and appropriate FlagSet + switch os.Args[1] { + case "server": + stateCLI.serverCmd.Parse(os.Args[2:]) + Server() + case "clients": + stateCLI.clientsCmd.Parse(os.Args[2:]) + resources, err := bugoutClient.GetResources(configs.BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN, "") + if err != nil { + fmt.Printf("Unable to get resources %v", err) + return + } + resourcesJson, err := json.Marshal(resources) + if err != nil { + fmt.Printf("Unable to marshal resources %v", err) + return + } + fmt.Println(string(resourcesJson)) + case "version": + fmt.Printf("v%s\n", configs.NODE_BALANCER_VERSION) + default: + flag.PrintDefaults() + os.Exit(1) + } +} diff --git a/nodes/node_balancer/cmd/data.go b/nodes/node_balancer/cmd/data.go index a7df1904..48646742 100644 --- a/nodes/node_balancer/cmd/data.go +++ b/nodes/node_balancer/cmd/data.go @@ -17,11 +17,26 @@ type NodeStatusResponse struct { CurrentBlock uint64 `json:"current_block"` } +// Bugout responses type BugoutUserResponse struct { - ID string `json:"user_id"` + ID string `json:"user_id"` ApplicationID string `json:"application_id"` } +type BugoutResourceDataResponse struct { + UserID string `json:"user_id"` + BlockchainAccess bool `json:"blockchain_access"` +} + +type BugoutResourceResponse struct { + ID string `json:"id"` + ResourceData BugoutResourceDataResponse `json:"resource_data"` +} + +type BugoutResourcesResponse struct { + Resources []BugoutResourceResponse `json:"resources"` +} + // Node - which one node client worked with // LastCallTs - timestamp from last call type Client struct { diff --git a/nodes/node_balancer/cmd/db.go b/nodes/node_balancer/cmd/db.go new file mode 100644 index 00000000..e31b9d24 --- /dev/null +++ b/nodes/node_balancer/cmd/db.go @@ -0,0 +1,108 @@ +package cmd + +import ( + "database/sql" + "fmt" + + configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" + + _ "github.com/lib/pq" +) + +var ( + databaseClient DatabaseClient +) + +type DatabaseClient struct { + Client *sql.DB +} + +// Establish connection with database +func InitDatabaseClient() error { + db, err := sql.Open("postgres", configs.MOONSTREAM_DB_URI_READ_ONLY) + if err != nil { + return fmt.Errorf("DSN parse error or another database initialization error: %v", err) + } + + // Set the maximum number of concurrently idle connections, + // by default sql.DB allows a maximum of 2 idle connections. + db.SetMaxIdleConns(configs.MOONSTREAM_DB_MAX_IDLE_CONNS) + + // Set the maximum lifetime of a connection. + // Longer lifetime increase memory usage. + db.SetConnMaxLifetime(configs.MOONSTREAM_DB_CONN_MAX_LIFETIME) + + databaseClient = DatabaseClient{ + Client: db, + } + + return nil +} + +type Block struct { + BlockNumber uint64 `json:"block_number"` + Difficulty uint64 `json:"difficulty"` + ExtraData string `json:"extra_data"` + GasLimit uint64 `json:"gas_limit"` + GasUsed uint64 `json:"gas_used"` + BaseFeePerGas interface{} `json:"base_fee_per_gas"` + Hash string `json:"hash"` + LogsBloom string `json:"logs_bloom"` + Miner string `json:"miner"` + Nonce string `json:"nonce"` + ParentHash string `json:"parent_hash"` + ReceiptRoot string `json:"receipt_root"` + Uncles string `json:"uncles"` + Size float64 `json:"size"` + StateRoot string `json:"state_root"` + Timestamp uint64 `json:"timestamp"` + TotalDifficulty string `json:"total_difficulty"` + TransactionsRoot string `json:"transactions_root"` + + IndexedAt string `json:"indexed_at"` +} + +// Get block from database +func (dbc *DatabaseClient) GetBlock(blockchain string, blockNumber uint64) (Block, error) { + var block Block + + // var tableName string + // if blockchain == "ethereum" { + // tableName = "ethereum_blocks" + // } else if blockchain == "polygon" { + // tableName = "polygon_blocks" + // } else { + // return block, fmt.Errorf("Unsupported blockchain") + // } + row := dbc.Client.QueryRow( + "SELECT block_number,difficulty,extra_data,gas_limit,gas_used,base_fee_per_gas,hash,logs_bloom,miner,nonce,parent_hash,receipt_root,uncles,size,state_root,timestamp,total_difficulty,transactions_root,indexed_at FROM ethereum_blocks WHERE block_number = $1", + // tableName, + blockNumber, + ) + + if err := row.Scan( + &block.BlockNumber, + &block.Difficulty, + &block.ExtraData, + &block.GasLimit, + &block.GasUsed, + &block.BaseFeePerGas, + &block.Hash, + &block.LogsBloom, + &block.Miner, + &block.Nonce, + &block.ParentHash, + &block.ReceiptRoot, + &block.Uncles, + &block.Size, + &block.StateRoot, + &block.Timestamp, + &block.TotalDifficulty, + &block.TransactionsRoot, + &block.IndexedAt, + ); err != nil { + return block, err + } + + return block, nil +} diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index 4a3f93b3..5b80c401 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -6,8 +6,10 @@ package cmd import ( "encoding/json" "fmt" + "io/ioutil" "log" "net/http" + "strconv" "strings" configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" @@ -47,13 +49,6 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { return } - clientId := w.Header().Get(configs.MOONSTREAM_CLIENT_ID_HEADER) - if clientId == "" { - // TODO(kompotkot): After all internal crawlers and services start - // providing client id header, then replace to http.Error - clientId = "none" - } - // Chose one node var node *Node cpool, err := GetClientPool(blockchain) @@ -61,14 +56,14 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("Unacceptable blockchain provided %s", blockchain), http.StatusBadRequest) return } - node = cpool.GetClientNode(clientId) + node = cpool.GetClientNode(user.ID) if node == nil { node = blockchainPool.GetNextNode(blockchain) if node == nil { http.Error(w, "There are no nodes available", http.StatusServiceUnavailable) return } - cpool.AddClientNode(clientId, node) + cpool.AddClientNode(user.ID, node) } // Save origin path, to use in proxyErrorHandler if node will not response @@ -80,15 +75,95 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { node.StatusReverseProxy.ServeHTTP(w, r) return case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/jsonrpc", blockchain)): - if user.ID == configs.BUGOUT_INTERNAL_CRAWLERS_USER_ID { - r.URL.Path = "/" - node.GethReverseProxy.ServeHTTP(w, r) - } else { - fmt.Println("Fetch from db") - } + lbJSONRPCHandler(w, r, blockchain, node, user) return default: http.Error(w, fmt.Sprintf("Unacceptable path for %s blockchain %s", blockchain, r.URL.Path), http.StatusBadRequest) return } } + +func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, node *Node, user BugoutUserResponse) { + var dataSource string + dataSources := r.Header[configs.MOONSTREAM_DATA_SOURCE_HEADER] + // TODO(kompotkot): Re-write it, to be able to work without database + if len(dataSources) == 0 { + dataSource = "database" + } else { + dataSource = dataSources[0] + } + + switch { + case dataSource == "blockchain": + if user.ID != controllerUserID { + resources, err := bugoutClient.GetResources(configs.BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN, user.ID) + if err != nil { + http.Error(w, fmt.Sprintf("not allowed %s", dataSource), http.StatusBadRequest) + return + } + + blockchainAccess := false + for _, resource := range resources.Resources { + if resource.ResourceData.BlockchainAccess == true { + blockchainAccess = true + } + } + + if blockchainAccess == false { + http.Error(w, fmt.Sprintf("not allowed %s", dataSource), http.StatusBadRequest) + return + } + } + + fmt.Println("proxied to node") + // r.URL.Path = "/" + // node.GethReverseProxy.ServeHTTP(w, r) + return + case dataSource == "database": + lbDatabaseHandler(w, r, blockchain) + return + default: + http.Error(w, fmt.Sprintf("Unacceptable data source %s", dataSource), http.StatusBadRequest) + return + } +} + +type JSONRPCRequest struct { + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params []interface{} `json:"params"` + ID uint64 `json:"id"` +} + +// var ALLOWED_ETH_ENDPOINTS = []string{"eth_getBlockByNumber"} + +func lbDatabaseHandler(w http.ResponseWriter, r *http.Request, blockchain string) { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + fmt.Println(err) + return + } + var jsonrpcRequest JSONRPCRequest + err = json.Unmarshal(body, &jsonrpcRequest) + if err != nil { + fmt.Println(err) + return + } + + switch { + case jsonrpcRequest.Method == "eth_getBlockByNumber": + var blockNumber uint64 + blockNumber, _ = strconv.ParseUint(jsonrpcRequest.Params[0].(string), 10, 32) + + block, err := databaseClient.GetBlock(blockchain, blockNumber) + if err != nil { + fmt.Printf("Unable to get block from database %v", err) + http.Error(w, fmt.Sprintf("no such block %v", blockNumber), http.StatusBadRequest) + return + } + fmt.Println(block) + default: + http.Error(w, fmt.Sprintf("Unsupported method %s by database, please use blockchain as data source", jsonrpcRequest.Method), http.StatusBadRequest) + return + } +} diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 85e883cb..3b1b83a3 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -5,7 +5,6 @@ package cmd import ( "context" - "flag" "fmt" "log" "net/http" @@ -18,7 +17,13 @@ import ( "github.com/google/uuid" ) -var reporter *humbug.HumbugReporter +var ( + // User id to controll access to blockchain nodes + controllerUserID string + + // Crash reporter + reporter *humbug.HumbugReporter +) // initHealthCheck runs a routine for check status of the nodes every 5 seconds func initHealthCheck(debug bool) { @@ -92,24 +97,7 @@ func proxyErrorHandler(proxy *httputil.ReverseProxy, url *url.URL) { } } -func InitServer() { - var listeningAddr string - var listeningPort string - var enableHealthCheck bool - var enableDebug bool - var showVersion bool - flag.StringVar(&listeningAddr, "host", "127.0.0.1", "Server listening address") - flag.StringVar(&listeningPort, "port", "8544", "Server listening port") - flag.BoolVar(&enableHealthCheck, "healthcheck", false, "To enable healthcheck ser healthcheck flag") - flag.BoolVar(&enableDebug, "debug", false, "To enable debug mode with extended log set debug flag") - flag.BoolVar(&showVersion, "version", false, "Print version") - flag.Parse() - - if showVersion { - fmt.Printf("Node balancer version: v%s\n", configs.NODE_BALANCER_VERSION) - return - } - +func Server() { // Generate map of clients CreateClientPools() @@ -124,6 +112,17 @@ func InitServer() { // Record system information reporter.Publish(humbug.SystemReport()) + user, err := bugoutClient.GetUser(configs.BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN) + if err != nil { + fmt.Printf("Unable to access Bugout authentication server %v", err) + } + controllerUserID = user.ID + + err = InitDatabaseClient() + if err != nil { + fmt.Printf("Unable to initialize database connection %v", err) + } + // Fill NodeConfigList with initial nodes from environment variables configs.ConfigList.InitNodeConfigList() @@ -165,18 +164,18 @@ func InitServer() { commonHandler = panicMiddleware(commonHandler) server := http.Server{ - Addr: fmt.Sprintf("%s:%s", listeningAddr, listeningPort), + Addr: fmt.Sprintf("%s:%s", stateCLI.listeningAddr, stateCLI.listeningPort), Handler: commonHandler, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, } // Start node health checking and current block fetching - if enableHealthCheck { - go initHealthCheck(enableDebug) + if stateCLI.enableHealthCheck { + go initHealthCheck(stateCLI.enableDebug) } - log.Printf("Starting server at %s:%s\n", listeningAddr, listeningPort) + log.Printf("Starting server at %s:%s\n", stateCLI.listeningAddr, stateCLI.listeningPort) err = server.ListenAndServe() if err != nil { log.Fatal(err) diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/configs/settings.go index 06fdd0d8..77789f27 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/configs/settings.go @@ -13,8 +13,8 @@ import ( // Bugout config var BUGOUT_AUTH_URL = os.Getenv("BUGOUT_AUTH_URL") var BUGOUT_NODE_BALANCER_APPLICATION_ID = os.Getenv("BUGOUT_NODE_BALANCER_APPLICATION_ID") -var BUGOUT_INTERNAL_CRAWLERS_USER_ID = os.Getenv("BUGOUT_INTERNAL_CRAWLERS_USER_ID") -var BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 1 +var BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN = os.Getenv("BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN") +var BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 5 // Node config type BlockchainConfig struct { @@ -40,7 +40,7 @@ var MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_B_ var MOONSTREAM_NODE_POLYGON_A_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_A_IPC_ADDR") var MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_B_IPC_ADDR") var MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT") -var MOONSTREAM_CLIENT_ID_HEADER = os.Getenv("MOONSTREAM_CLIENT_ID_HEADER") +var MOONSTREAM_DATA_SOURCE_HEADER = os.Getenv("MOONSTREAM_DATA_SOURCE_HEADER") func checkEnvVarSet() { if MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR == "" { @@ -57,8 +57,8 @@ func checkEnvVarSet() { MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = "b.polygon.moonstream.internal" } - if MOONSTREAM_CLIENT_ID_HEADER == "" { - MOONSTREAM_CLIENT_ID_HEADER = "x-moonstream-client-id" + if MOONSTREAM_DATA_SOURCE_HEADER == "" { + MOONSTREAM_DATA_SOURCE_HEADER = "X-Moonstream-Data-Source" } if MOONSTREAM_NODES_SERVER_PORT == "" { @@ -112,4 +112,6 @@ var NB_CLIENT_NODE_KEEP_ALIVE = int64(5) // How long to store node in hot list f var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN") // Database config -var MOONSTREAM_DB_URI = os.Getenv("MOONSTREAM_DB_URI") +var MOONSTREAM_DB_URI_READ_ONLY = os.Getenv("MOONSTREAM_DB_URI_READ_ONLY") +var MOONSTREAM_DB_MAX_IDLE_CONNS int = 30 +var MOONSTREAM_DB_CONN_MAX_LIFETIME = 30 * time.Minute diff --git a/nodes/node_balancer/configs/version.go b/nodes/node_balancer/configs/version.go index 94bcab6c..55d8e00b 100644 --- a/nodes/node_balancer/configs/version.go +++ b/nodes/node_balancer/configs/version.go @@ -1,3 +1,3 @@ package configs -var NODE_BALANCER_VERSION = "0.0.1" +var NODE_BALANCER_VERSION = "0.0.2" diff --git a/nodes/node_balancer/go.mod b/nodes/node_balancer/go.mod index ca93c68e..ca2e9d9d 100644 --- a/nodes/node_balancer/go.mod +++ b/nodes/node_balancer/go.mod @@ -5,4 +5,5 @@ go 1.17 require ( github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205 github.com/google/uuid v1.3.0 + github.com/lib/pq v1.10.4 ) diff --git a/nodes/node_balancer/go.sum b/nodes/node_balancer/go.sum index e1a1fadb..aa0c0342 100644 --- a/nodes/node_balancer/go.sum +++ b/nodes/node_balancer/go.sum @@ -2,3 +2,5 @@ github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205 h1:UQ7XGjvoOV github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205/go.mod h1:U/NXHfc3tzGeQz+xVfpifXdPZi7p6VV8xdP/4ZKeWJU= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk= +github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= diff --git a/nodes/node_balancer/main.go b/nodes/node_balancer/main.go index bfe77464..91a1764c 100644 --- a/nodes/node_balancer/main.go +++ b/nodes/node_balancer/main.go @@ -5,5 +5,5 @@ import ( ) func main() { - cmd.InitServer() + cmd.CLI() } From e3a08730cc5f68126e57fe1fe123ee8f0382196b Mon Sep 17 00:00:00 2001 From: kompotkot Date: Wed, 16 Mar 2022 21:33:48 +0000 Subject: [PATCH 05/23] Working nodebalancer with authorization via resources --- nodes/node_balancer/README.md | 60 +++++++++++- nodes/node_balancer/cmd/balancer.go | 34 +++++++ nodes/node_balancer/cmd/blockchain.go | 61 ++++++++++++ nodes/node_balancer/cmd/bugout.go | 55 +++++++++-- nodes/node_balancer/cmd/cli.go | 20 ++-- nodes/node_balancer/cmd/clients.go | 20 +++- nodes/node_balancer/cmd/data.go | 79 ---------------- nodes/node_balancer/cmd/middleware.go | 111 ++++++++++------------ nodes/node_balancer/cmd/routes.go | 95 ++++++++++--------- nodes/node_balancer/cmd/server.go | 23 +++-- nodes/node_balancer/configs/settings.go | 119 ++++++++---------------- nodes/node_balancer/configs/version.go | 2 +- nodes/node_balancer/sample.env | 21 +++-- 13 files changed, 394 insertions(+), 306 deletions(-) create mode 100644 nodes/node_balancer/cmd/blockchain.go delete mode 100644 nodes/node_balancer/cmd/data.go diff --git a/nodes/node_balancer/README.md b/nodes/node_balancer/README.md index 413a7085..8a030485 100644 --- a/nodes/node_balancer/README.md +++ b/nodes/node_balancer/README.md @@ -1,16 +1,66 @@ # Node Balancer application -## Installation +# Installation -- Prepare environment variables -- Build application +- Prepare environment variables +- Build application ```bash go build -o nodebalancer ``` -- Run with following parameters: +# Work with nodebalancer + +## clients ```bash -nodebalancer -host 0.0.0.0 -port 8544 -healthcheck +nodebalancer clients | jq . +``` + +This command will return a list of bugout resources of registered users to access node balancer with their `crawlers/app/project` (in our project we will call it `crawlers`). + +```json +[ + { + "user_id": "", + "access_id": "", + "name": "", + "description": "", + "blockchain_access": true, + "extended_methods": false + } +] +``` + +`access_id` - token which allow access to nodebalancer, could be specified in both ways: + +- as a header `x-moonstream-access-id` with value `access_id` +- as query parameter `access_id=access_id` + +`blockchain_access` - boolean which allow you or not to have access to blockchain node, otherwise you will be redirected to database + +`extended_methods` - boolean which allow you to call not whitelisted method to blockchain node, by default for new user this is equal to `false` + +## server + +```bash +nodebalancer server -host 0.0.0.0 -port 8544 -healthcheck +``` + +Flag `--healthcheck` will execute background process to ping-pong available nodes to keep their status and current block number. +Flag `--debug` will extend output of each request to server and healthchecks summary. + +# Work with node + +Common request to fetch block number + +```bash +curl --request GET 'http://127.0.0.1:8544/nb/ethereum/jsonrpc?access_id=&data_source=' \ + --header 'Content-Type: application/json' \ + --data-raw '{ + "jsonrpc":"2.0", + "method":"eth_getBlockByNumber", + "params":["0xb71b64", false], + "id":1 + }' ``` diff --git a/nodes/node_balancer/cmd/balancer.go b/nodes/node_balancer/cmd/balancer.go index c0939b01..f384d7d2 100644 --- a/nodes/node_balancer/cmd/balancer.go +++ b/nodes/node_balancer/cmd/balancer.go @@ -9,7 +9,9 @@ import ( "io/ioutil" "log" "net/http" + "net/http/httputil" "net/url" + "sync" "sync/atomic" configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" @@ -19,6 +21,38 @@ import ( // for each blockchain we work during session. var blockchainPool BlockchainPool +// Node structure with +// StatusURL for status server at node endpoint +// GethURL for geth/bor/etc node http.server endpoint +type Node struct { + StatusURL *url.URL + GethURL *url.URL + + Alive bool + CurrentBlock uint64 + + mux sync.RWMutex + + StatusReverseProxy *httputil.ReverseProxy + GethReverseProxy *httputil.ReverseProxy +} + +type NodePool struct { + Blockchain string + Nodes []*Node + + // Counter to observe all nodes + Current uint64 +} + +type BlockchainPool struct { + Blockchains []*NodePool +} + +type NodeStatusResponse struct { + CurrentBlock uint64 `json:"current_block"` +} + // AddNode to the nodes pool func (bpool *BlockchainPool) AddNode(node *Node, blockchain string) { var nodePool *NodePool diff --git a/nodes/node_balancer/cmd/blockchain.go b/nodes/node_balancer/cmd/blockchain.go new file mode 100644 index 00000000..6d9ef68e --- /dev/null +++ b/nodes/node_balancer/cmd/blockchain.go @@ -0,0 +1,61 @@ +package cmd + +import ( + "log" + "strconv" + + configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" +) + +var ( + nodeConfigs NodeConfigs +) + +// Node conf +type BlockchainConfig struct { + Blockchain string + IPs []string + Port string +} + +type NodeConfig struct { + Blockchain string + Addr string + Port uint16 +} + +type NodeConfigs struct { + NodeConfigs []NodeConfig +} + +// Return list of NodeConfig structures +func (nc *NodeConfigs) InitNodeConfiguration() { + // Define available blockchain nodes + blockchainConfigList := make([]BlockchainConfig, 0, 2) + blockchainConfigList = append(blockchainConfigList, BlockchainConfig{ + Blockchain: "ethereum", + IPs: []string{configs.MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR, configs.MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR}, + Port: "8545", + }) + blockchainConfigList = append(blockchainConfigList, BlockchainConfig{ + Blockchain: "polygon", + IPs: []string{configs.MOONSTREAM_NODE_POLYGON_A_IPC_ADDR, configs.MOONSTREAM_NODE_POLYGON_B_IPC_ADDR}, + Port: "8545", + }) + + // Parse node addr, ip and blockchain + for _, b := range blockchainConfigList { + for _, nodeIP := range b.IPs { + port, err := strconv.ParseInt(b.Port, 0, 16) + if err != nil { + log.Printf("Unable to parse port number: %s", b.Port) + continue + } + nc.NodeConfigs = append(nc.NodeConfigs, NodeConfig{ + Blockchain: b.Blockchain, + Addr: nodeIP, + Port: uint16(port), + }) + } + } +} diff --git a/nodes/node_balancer/cmd/bugout.go b/nodes/node_balancer/cmd/bugout.go index 6db91432..f00b000f 100644 --- a/nodes/node_balancer/cmd/bugout.go +++ b/nodes/node_balancer/cmd/bugout.go @@ -18,6 +18,36 @@ type BugoutClient struct { AuthURL string } +type PingResponse struct { + Status string `json:"status"` +} + +// Bugout responses +type BugoutUserResponse struct { + ID string `json:"user_id"` + ApplicationID string `json:"application_id"` +} + +type UserAccess struct { + UserID string `json:"user_id"` + AccessID string `json:"access_id"` + Name string `json:"name"` + Description string `json:"description"` + BlockchainAccess bool `json:"blockchain_access"` + ExtendedMethods bool `json:"extended_methods"` + + dataSource string +} + +type BugoutResourceResponse struct { + ID string `json:"id"` + ResourceData UserAccess `json:"resource_data"` +} + +type BugoutResourcesResponse struct { + Resources []BugoutResourceResponse `json:"resources"` +} + // Initialize Bugout http client func InitBugoutClient() { client := http.Client{Timeout: configs.BUGOUT_AUTH_CALL_TIMEOUT} @@ -58,35 +88,44 @@ func (bc *BugoutClient) GetUser(token string) (BugoutUserResponse, error) { return userResponse, nil } -// Get Bugout resources -func (bc *BugoutClient) GetResources(token string, userID string) (BugoutResourcesResponse, error) { - url := fmt.Sprintf("%s/resources?application_id=%s", configs.BUGOUT_AUTH_URL, configs.BUGOUT_NODE_BALANCER_APPLICATION_ID) +// Get user accesses from Bugout resources +func (bc *BugoutClient) GetUserAccesses(token, userID, accessID string) ([]UserAccess, error) { + var userAccesses []UserAccess + + url := fmt.Sprintf("%s/resources?application_id=%s", configs.BUGOUT_AUTH_URL, configs.NB_APPLICATION_ID) if userID != "" { url += fmt.Sprintf("&user_id=%s", userID) } + if accessID != "" { + url += fmt.Sprintf("&access_id=%s", accessID) + } req, err := http.NewRequest("GET", url, nil) if err != nil { - return BugoutResourcesResponse{}, err + return userAccesses, err } req.Header = http.Header{ "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, } resp, err := bc.Client.Do(req) if err != nil { - return BugoutResourcesResponse{}, err + return userAccesses, err } defer resp.Body.Close() // Parse response body, err := ioutil.ReadAll(resp.Body) if err != nil { - return BugoutResourcesResponse{}, err + return userAccesses, err } var resourcesResponse BugoutResourcesResponse err = json.Unmarshal(body, &resourcesResponse) if err != nil { - return BugoutResourcesResponse{}, err + return userAccesses, err } - return resourcesResponse, nil + for _, resourceData := range resourcesResponse.Resources { + userAccesses = append(userAccesses, resourceData.ResourceData) + } + + return userAccesses, nil } diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index 13558026..650b6251 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -15,8 +15,8 @@ var ( // Command Line Interface state type StateCLI struct { - serverCmd *flag.FlagSet - clientsCmd *flag.FlagSet + serverCmd *flag.FlagSet + usersCmd *flag.FlagSet // Common flags showVersion bool @@ -31,7 +31,7 @@ type StateCLI struct { func (s *StateCLI) populateCLI() { // Subcommands setup s.serverCmd = flag.NewFlagSet("server", flag.ExitOnError) - s.clientsCmd = flag.NewFlagSet("clients", flag.ExitOnError) + s.usersCmd = flag.NewFlagSet("users", flag.ExitOnError) // Server subcommand flag pointers s.serverCmd.StringVar(&s.listeningAddr, "host", "127.0.0.1", "Server listening address") @@ -41,6 +41,8 @@ func (s *StateCLI) populateCLI() { } func init() { + configs.VerifyEnvironments() + InitBugoutClient() } @@ -56,21 +58,21 @@ func CLI() { case "server": stateCLI.serverCmd.Parse(os.Args[2:]) Server() - case "clients": - stateCLI.clientsCmd.Parse(os.Args[2:]) - resources, err := bugoutClient.GetResources(configs.BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN, "") + case "users": + stateCLI.usersCmd.Parse(os.Args[2:]) + userAccesses, err := bugoutClient.GetUserAccesses(configs.NB_CONTROLLER_TOKEN, "", "") if err != nil { fmt.Printf("Unable to get resources %v", err) return } - resourcesJson, err := json.Marshal(resources) + userAccessesJson, err := json.Marshal(userAccesses) if err != nil { fmt.Printf("Unable to marshal resources %v", err) return } - fmt.Println(string(resourcesJson)) + fmt.Println(string(userAccessesJson)) case "version": - fmt.Printf("v%s\n", configs.NODE_BALANCER_VERSION) + fmt.Printf("v%s\n", configs.NB_VERSION) default: flag.PrintDefaults() os.Exit(1) diff --git a/nodes/node_balancer/cmd/clients.go b/nodes/node_balancer/cmd/clients.go index 95c9f42d..59ed8bda 100644 --- a/nodes/node_balancer/cmd/clients.go +++ b/nodes/node_balancer/cmd/clients.go @@ -3,13 +3,29 @@ package cmd import ( "errors" "reflect" + "sync" "time" configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) -var ethereumClientPool ClientPool -var polygonClientPool ClientPool +var ( + ethereumClientPool ClientPool + polygonClientPool ClientPool +) + +// Node - which one node client worked with +// LastCallTs - timestamp from last call +type Client struct { + Node *Node + LastCallTs int64 + + mux sync.RWMutex +} + +type ClientPool struct { + Client map[string]*Client +} // Generate client pools for different blockchains func CreateClientPools() { diff --git a/nodes/node_balancer/cmd/data.go b/nodes/node_balancer/cmd/data.go deleted file mode 100644 index 48646742..00000000 --- a/nodes/node_balancer/cmd/data.go +++ /dev/null @@ -1,79 +0,0 @@ -/* -Data structure. -*/ -package cmd - -import ( - "net/http/httputil" - "net/url" - "sync" -) - -type PingResponse struct { - Status string `json:"status"` -} - -type NodeStatusResponse struct { - CurrentBlock uint64 `json:"current_block"` -} - -// Bugout responses -type BugoutUserResponse struct { - ID string `json:"user_id"` - ApplicationID string `json:"application_id"` -} - -type BugoutResourceDataResponse struct { - UserID string `json:"user_id"` - BlockchainAccess bool `json:"blockchain_access"` -} - -type BugoutResourceResponse struct { - ID string `json:"id"` - ResourceData BugoutResourceDataResponse `json:"resource_data"` -} - -type BugoutResourcesResponse struct { - Resources []BugoutResourceResponse `json:"resources"` -} - -// Node - which one node client worked with -// LastCallTs - timestamp from last call -type Client struct { - Node *Node - LastCallTs int64 - - mux sync.RWMutex -} - -type ClientPool struct { - Client map[string]*Client -} - -// Node structure with -// StatusURL for status server at node endpoint -// GethURL for geth/bor/etc node http.server endpoint -type Node struct { - StatusURL *url.URL - GethURL *url.URL - - Alive bool - CurrentBlock uint64 - - mux sync.RWMutex - - StatusReverseProxy *httputil.ReverseProxy - GethReverseProxy *httputil.ReverseProxy -} - -type NodePool struct { - Blockchain string - Nodes []*Node - - // Counter to observe all nodes - Current uint64 -} - -type BlockchainPool struct { - Blockchains []*NodePool -} diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 1263e41c..db83b8d5 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -5,13 +5,9 @@ package cmd import ( "context" - "encoding/json" - "fmt" - "io/ioutil" "log" "net" "net/http" - "strings" "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" @@ -48,69 +44,62 @@ func logMiddleware(next http.Handler) http.Handler { }) } -// Bugout authentication -func authMiddleware(next http.Handler) http.Handler { +// Check access id was provided correctly and save user access configuration to request context +func accessMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - authHeaders := r.Header["Authorization"] - authHeadersLen := len(authHeaders) - if authHeadersLen == 0 { - http.Error(w, "Authorization header not found", http.StatusForbidden) - return - } - if authHeadersLen > 1 { - http.Error(w, "Too many authorization headers provided", http.StatusBadRequest) - return - } - authHeader := authHeaders[0] + var currentUserAccess UserAccess - // Extract Bearer token - headerSlice := strings.Split(authHeader, " ") - if len(headerSlice) != 2 { - http.Error(w, "Unacceptable token format provided", http.StatusBadRequest) - return - } - if headerSlice[0] != "Bearer" { - http.Error(w, "Unacceptable token format provided", http.StatusBadRequest) - return + var accessID string + accessIDHeaders := r.Header[configs.NB_ACCESS_ID_HEADER] + for _, h := range accessIDHeaders { + accessID = h } - // Check token is active - client := http.Client{Timeout: configs.BUGOUT_AUTH_CALL_TIMEOUT} - authReq, err := http.NewRequest("GET", fmt.Sprintf("%s/user", configs.BUGOUT_AUTH_URL), nil) - if err != nil { - http.Error(w, "Unable to construct authorization request", http.StatusInternalServerError) - return - } - authReq.Header.Set("Authorization", authHeader) - resp, err := client.Do(authReq) - if err != nil { - http.Error(w, "Unable to reach authorization server", http.StatusInternalServerError) - return - } - defer resp.Body.Close() - - // Parse response from authorization server - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - http.Error(w, "Unable to read respose from authorization server", http.StatusInternalServerError) - return - } - var userResponse BugoutUserResponse - err = json.Unmarshal(body, &userResponse) - if err != nil { - http.Error(w, "Unable to parse respose from authorization server", http.StatusInternalServerError) - return - } - if userResponse.ID == "" { - http.Error(w, "Wrong authorization header", http.StatusForbidden) - return - } - if userResponse.ApplicationID != configs.BUGOUT_NODE_BALANCER_APPLICATION_ID { - http.Error(w, "Wrong authorization header", http.StatusForbidden) - return + dataSource := "database" + dataSources := r.Header[configs.NB_DATA_SOURCE_HEADER] + for _, h := range dataSources { + dataSource = h } - ctxUser := context.WithValue(r.Context(), "user", userResponse) + queries := r.URL.Query() + for k, v := range queries { + if k == "access_id" { + accessID = v[0] + } + if k == "data_source" { + dataSource = v[0] + } + } + + // If access id does not belong to controller, then find it in Bugout resources + if accessID == configs.NB_CONTROLLER_ACCESS_ID { + currentUserAccess = controllerUserAccess + currentUserAccess.dataSource = dataSource + } else { + userAccesses, err := bugoutClient.GetUserAccesses(configs.NB_CONTROLLER_TOKEN, "", accessID) + if err != nil { + http.Error(w, "Unable to get user with provided access identifier", http.StatusForbidden) + return + } + if len(userAccesses) == 0 { + http.Error(w, "User with provided access identifier not found", http.StatusForbidden) + return + } + userAccess := userAccesses[0] + + currentUserAccess = UserAccess{ + UserID: userAccess.UserID, + AccessID: userAccess.AccessID, + Name: userAccess.Name, + Description: userAccess.Description, + BlockchainAccess: userAccess.BlockchainAccess, + ExtendedMethods: userAccess.ExtendedMethods, + + dataSource: dataSource, + } + } + + ctxUser := context.WithValue(r.Context(), "currentUserAccess", currentUserAccess) next.ServeHTTP(w, r.WithContext(ctxUser)) }) diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index 5b80c401..9b839d68 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -24,13 +24,15 @@ func pingRoute(w http.ResponseWriter, r *http.Request) { // lbHandler load balances the incoming requests to nodes func lbHandler(w http.ResponseWriter, r *http.Request) { - userRaw := r.Context().Value("user") - user, ok := userRaw.(BugoutUserResponse) + currentUserAccessRaw := r.Context().Value("currentUserAccess") + currentUserAccess, ok := currentUserAccessRaw.(UserAccess) if !ok { http.Error(w, "Internal server error", http.StatusInternalServerError) return } + fmt.Println(currentUserAccess) + attempts := GetAttemptsFromContext(r) if attempts > configs.NB_CONNECTION_RETRIES { log.Printf("Max attempts reached from %s %s, terminating\n", r.RemoteAddr, r.URL.Path) @@ -56,14 +58,14 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("Unacceptable blockchain provided %s", blockchain), http.StatusBadRequest) return } - node = cpool.GetClientNode(user.ID) + node = cpool.GetClientNode(currentUserAccess.AccessID) if node == nil { node = blockchainPool.GetNextNode(blockchain) if node == nil { http.Error(w, "There are no nodes available", http.StatusServiceUnavailable) return } - cpool.AddClientNode(user.ID, node) + cpool.AddClientNode(currentUserAccess.AccessID, node) } // Save origin path, to use in proxyErrorHandler if node will not response @@ -75,7 +77,7 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { node.StatusReverseProxy.ServeHTTP(w, r) return case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/jsonrpc", blockchain)): - lbJSONRPCHandler(w, r, blockchain, node, user) + lbJSONRPCHandler(w, r, blockchain, node, currentUserAccess) return default: http.Error(w, fmt.Sprintf("Unacceptable path for %s blockchain %s", blockchain, r.URL.Path), http.StatusBadRequest) @@ -83,51 +85,64 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { } } -func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, node *Node, user BugoutUserResponse) { - var dataSource string - dataSources := r.Header[configs.MOONSTREAM_DATA_SOURCE_HEADER] - // TODO(kompotkot): Re-write it, to be able to work without database - if len(dataSources) == 0 { - dataSource = "database" - } else { - dataSource = dataSources[0] - } - +func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, node *Node, currentUserAccess UserAccess) { switch { - case dataSource == "blockchain": - if user.ID != controllerUserID { - resources, err := bugoutClient.GetResources(configs.BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN, user.ID) + case currentUserAccess.dataSource == "blockchain": + if currentUserAccess.BlockchainAccess == false { + http.Error(w, "Access to blockchain node not allowed with provided access id", http.StatusForbidden) + return + } + if currentUserAccess.ExtendedMethods == false { + jsonrpcRequest, err := parseJSONRPCRequest(r) if err != nil { - http.Error(w, fmt.Sprintf("not allowed %s", dataSource), http.StatusBadRequest) + http.Error(w, "Unable to parse JSON RPC request", http.StatusBadRequest) return } - - blockchainAccess := false - for _, resource := range resources.Resources { - if resource.ResourceData.BlockchainAccess == true { - blockchainAccess = true - } - } - - if blockchainAccess == false { - http.Error(w, fmt.Sprintf("not allowed %s", dataSource), http.StatusBadRequest) + err = verifyMethodWhitelisted(jsonrpcRequest.Method) + if err != nil { + http.Error(w, "Method for provided access id not allowed", http.StatusForbidden) return } } - fmt.Println("proxied to node") - // r.URL.Path = "/" - // node.GethReverseProxy.ServeHTTP(w, r) + r.URL.Path = "/" + node.GethReverseProxy.ServeHTTP(w, r) return - case dataSource == "database": + case currentUserAccess.dataSource == "database": lbDatabaseHandler(w, r, blockchain) return default: - http.Error(w, fmt.Sprintf("Unacceptable data source %s", dataSource), http.StatusBadRequest) + http.Error(w, fmt.Sprintf("Unacceptable data source %s", currentUserAccess.dataSource), http.StatusBadRequest) return } } +func parseJSONRPCRequest(r *http.Request) (JSONRPCRequest, error) { + var jsonrpcRequest JSONRPCRequest + + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return jsonrpcRequest, err + } + err = json.Unmarshal(body, &jsonrpcRequest) + if err != nil { + return jsonrpcRequest, err + } + + return jsonrpcRequest, nil +} + +var ALLOWED_METHODS = []string{"eth_getBlockByNumber"} + +func verifyMethodWhitelisted(method string) error { + for _, m := range ALLOWED_METHODS { + if method == m { + return nil + } + } + return fmt.Errorf("Method not allowed") +} + type JSONRPCRequest struct { Jsonrpc string `json:"jsonrpc"` Method string `json:"method"` @@ -135,18 +150,10 @@ type JSONRPCRequest struct { ID uint64 `json:"id"` } -// var ALLOWED_ETH_ENDPOINTS = []string{"eth_getBlockByNumber"} - func lbDatabaseHandler(w http.ResponseWriter, r *http.Request, blockchain string) { - body, err := ioutil.ReadAll(r.Body) + jsonrpcRequest, err := parseJSONRPCRequest(r) if err != nil { - fmt.Println(err) - return - } - var jsonrpcRequest JSONRPCRequest - err = json.Unmarshal(body, &jsonrpcRequest) - if err != nil { - fmt.Println(err) + http.Error(w, "Unable to parse JSON RPC request", http.StatusBadRequest) return } diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 3b1b83a3..47713c36 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -18,8 +18,7 @@ import ( ) var ( - // User id to controll access to blockchain nodes - controllerUserID string + controllerUserAccess UserAccess // Crash reporter reporter *humbug.HumbugReporter @@ -105,18 +104,26 @@ func Server() { var err error sessionID := uuid.New().String() consent := humbug.CreateHumbugConsent(humbug.True) - reporter, err = humbug.CreateHumbugReporter(consent, "moonstream-node-balancer", sessionID, configs.HUMBUG_REPORTER_NODE_BALANCER_TOKEN) + reporter, err = humbug.CreateHumbugReporter(consent, "moonstream-node-balancer", sessionID, configs.HUMBUG_REPORTER_NB_TOKEN) if err != nil { panic(fmt.Sprintf("Invalid Humbug Crash configuration: %s", err.Error())) } // Record system information reporter.Publish(humbug.SystemReport()) - user, err := bugoutClient.GetUser(configs.BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN) + userAccesses, err := bugoutClient.GetUserAccesses(configs.NB_CONTROLLER_TOKEN, "", configs.NB_CONTROLLER_ACCESS_ID) if err != nil { fmt.Printf("Unable to access Bugout authentication server %v", err) } - controllerUserID = user.ID + userAccess := userAccesses[0] + controllerUserAccess = UserAccess{ + UserID: userAccess.UserID, + AccessID: userAccess.AccessID, + Name: userAccess.Name, + Description: userAccess.Description, + BlockchainAccess: userAccess.BlockchainAccess, + ExtendedMethods: userAccess.ExtendedMethods, + } err = InitDatabaseClient() if err != nil { @@ -124,10 +131,10 @@ func Server() { } // Fill NodeConfigList with initial nodes from environment variables - configs.ConfigList.InitNodeConfigList() + nodeConfigs.InitNodeConfiguration() // Parse nodes and set list of proxies - for i, nodeConfig := range configs.ConfigList.Configs { + for i, nodeConfig := range nodeConfigs.NodeConfigs { gethUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", nodeConfig.Addr, nodeConfig.Port)) if err != nil { log.Fatal(err) @@ -156,7 +163,7 @@ func Server() { } serveMux := http.NewServeMux() - serveMux.Handle("/nb/", authMiddleware(http.HandlerFunc(lbHandler))) + serveMux.Handle("/nb/", accessMiddleware(http.HandlerFunc(lbHandler))) serveMux.HandleFunc("/ping", pingRoute) // Set common middlewares, from bottom to top diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/configs/settings.go index 77789f27..5b49df98 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/configs/settings.go @@ -6,43 +6,48 @@ package configs import ( "log" "os" - "strconv" "time" ) -// Bugout config -var BUGOUT_AUTH_URL = os.Getenv("BUGOUT_AUTH_URL") -var BUGOUT_NODE_BALANCER_APPLICATION_ID = os.Getenv("BUGOUT_NODE_BALANCER_APPLICATION_ID") -var BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN = os.Getenv("BUGOUT_NODE_BALANCER_CONTROLLER_TOKEN") -var BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 5 +var ( + // Bugout and application configuration + BUGOUT_AUTH_URL = os.Getenv("BUGOUT_AUTH_URL") + BUGOUT_AUTH_CALL_TIMEOUT = time.Second * 5 + NB_APPLICATION_ID = os.Getenv("NB_APPLICATION_ID") + NB_CONTROLLER_TOKEN = os.Getenv("NB_CONTROLLER_TOKEN") + NB_CONTROLLER_ACCESS_ID = os.Getenv("NB_CONTROLLER_ACCESS_ID") -// Node config -type BlockchainConfig struct { - Blockchain string - IPs []string - Port string -} + NB_CONNECTION_RETRIES = 2 + NB_CONNECTION_RETRIES_INTERVAL = time.Millisecond * 10 + NB_HEALTH_CHECK_INTERVAL = time.Second * 5 + NB_HEALTH_CHECK_CALL_TIMEOUT = time.Second * 2 -type NodeConfig struct { - Blockchain string - Addr string - Port uint16 -} + // Client configuration + NB_CLIENT_NODE_KEEP_ALIVE = int64(5) // How long to store node in hot list for client in seconds -type NodeConfigList struct { - Configs []NodeConfig -} + // Hardcoded node addresses + // TODO(kompotkot): Write CLI to be able to add nodes + MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR") + MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR") + MOONSTREAM_NODE_POLYGON_A_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_A_IPC_ADDR") + MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_B_IPC_ADDR") -var ConfigList NodeConfigList + MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT") -var MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR") -var MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR") -var MOONSTREAM_NODE_POLYGON_A_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_A_IPC_ADDR") -var MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = os.Getenv("MOONSTREAM_NODE_POLYGON_B_IPC_ADDR") -var MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT") -var MOONSTREAM_DATA_SOURCE_HEADER = os.Getenv("MOONSTREAM_DATA_SOURCE_HEADER") + NB_ACCESS_ID_HEADER = os.Getenv("NB_ACCESS_ID_HEADER") + NB_DATA_SOURCE_HEADER = os.Getenv("NB_DATA_SOURCE_HEADER") -func checkEnvVarSet() { + // Humbug configuration + HUMBUG_REPORTER_NB_TOKEN = os.Getenv("HUMBUG_REPORTER_NB_TOKEN") + + // Database configuration + MOONSTREAM_DB_URI_READ_ONLY = os.Getenv("MOONSTREAM_DB_URI_READ_ONLY") + MOONSTREAM_DB_MAX_IDLE_CONNS int = 30 + MOONSTREAM_DB_CONN_MAX_LIFETIME = 30 * time.Minute +) + +// Verify required environment variables are set +func VerifyEnvironments() { if MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR == "" { MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR = "a.ethereum.moonstream.internal" } @@ -57,61 +62,15 @@ func checkEnvVarSet() { MOONSTREAM_NODE_POLYGON_B_IPC_ADDR = "b.polygon.moonstream.internal" } - if MOONSTREAM_DATA_SOURCE_HEADER == "" { - MOONSTREAM_DATA_SOURCE_HEADER = "X-Moonstream-Data-Source" + if NB_ACCESS_ID_HEADER == "" { + NB_ACCESS_ID_HEADER = "X-Node-Balancer-Access-Id" + } + + if NB_DATA_SOURCE_HEADER == "" { + NB_DATA_SOURCE_HEADER = "X-Node-Balancer-Data-Source" } if MOONSTREAM_NODES_SERVER_PORT == "" { log.Fatal("MOONSTREAM_NODES_SERVER_PORT environment variable not set") } } - -// Return list of NodeConfig structures -func (nc *NodeConfigList) InitNodeConfigList() { - checkEnvVarSet() - - // Define available blockchain nodes - blockchainConfigList := make([]BlockchainConfig, 0, 2) - blockchainConfigList = append(blockchainConfigList, BlockchainConfig{ - Blockchain: "ethereum", - IPs: []string{MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR, MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR}, - Port: "8545", - }) - blockchainConfigList = append(blockchainConfigList, BlockchainConfig{ - Blockchain: "polygon", - IPs: []string{MOONSTREAM_NODE_POLYGON_A_IPC_ADDR, MOONSTREAM_NODE_POLYGON_B_IPC_ADDR}, - Port: "8545", - }) - - // Parse node addr, ip and blockchain - for _, b := range blockchainConfigList { - for _, nodeIP := range b.IPs { - port, err := strconv.ParseInt(b.Port, 0, 16) - if err != nil { - log.Printf("Unable to parse port number: %s", b.Port) - continue - } - nc.Configs = append(nc.Configs, NodeConfig{ - Blockchain: b.Blockchain, - Addr: nodeIP, - Port: uint16(port), - }) - } - } -} - -var NB_CONNECTION_RETRIES = 2 -var NB_CONNECTION_RETRIES_INTERVAL = time.Millisecond * 10 -var NB_HEALTH_CHECK_INTERVAL = time.Second * 5 -var NB_HEALTH_CHECK_CALL_TIMEOUT = time.Second * 2 - -// Client config -var NB_CLIENT_NODE_KEEP_ALIVE = int64(5) // How long to store node in hot list for client in seconds - -// Humbug config -var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN") - -// Database config -var MOONSTREAM_DB_URI_READ_ONLY = os.Getenv("MOONSTREAM_DB_URI_READ_ONLY") -var MOONSTREAM_DB_MAX_IDLE_CONNS int = 30 -var MOONSTREAM_DB_CONN_MAX_LIFETIME = 30 * time.Minute diff --git a/nodes/node_balancer/configs/version.go b/nodes/node_balancer/configs/version.go index 55d8e00b..50b18240 100644 --- a/nodes/node_balancer/configs/version.go +++ b/nodes/node_balancer/configs/version.go @@ -1,3 +1,3 @@ package configs -var NODE_BALANCER_VERSION = "0.0.2" +var NB_VERSION = "0.0.2" diff --git a/nodes/node_balancer/sample.env b/nodes/node_balancer/sample.env index dec6968b..54f2bc5d 100644 --- a/nodes/node_balancer/sample.env +++ b/nodes/node_balancer/sample.env @@ -1,17 +1,20 @@ # Required environment variables for load balancer export BUGOUT_AUTH_URL="https://auth.bugout.dev" -export BUGOUT_NODE_BALANCER_APPLICATION_ID="" -export BUGOUT_INTERNAL_CRAWLERS_USER_ID="" -export MOONSTREAM_NODES_SERVER_PORT="" -export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="" +export NODE_BALANCER_APPLICATION_ID="" +export NODE_BALANCER_CONTROLLER_TOKEN="" +export NODE_BALANCER_CONTROLLER_ACCESS_ID="" # Database variables export MOONSTREAM_DB_URI="postgresql://:@:/" +# Nodes +export MOONSTREAM_NODES_SERVER_PORT="" # Ethereum nodes depends variables -export MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR="" -export MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR="" - +export MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR="127.0.0.1" +export MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR="127.0.0.2" # Polygon nodes depends variables -export MOONSTREAM_NODE_POLYGON_A_IPC_ADDR="" -export MOONSTREAM_NODE_POLYGON_B_IPC_ADDR="" +export MOONSTREAM_NODE_POLYGON_A_IPC_ADDR="127.0.0.1" +export MOONSTREAM_NODE_POLYGON_B_IPC_ADDR="127.0.0.2" + +# Error humbug reporter +export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="" From 26472d2ab9c1f77ac7610d6d67a21d26a3d13a63 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Wed, 16 Mar 2022 21:35:28 +0000 Subject: [PATCH 06/23] Fixed env var naming --- nodes/node_balancer/sample.env | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nodes/node_balancer/sample.env b/nodes/node_balancer/sample.env index 54f2bc5d..0cdd071a 100644 --- a/nodes/node_balancer/sample.env +++ b/nodes/node_balancer/sample.env @@ -1,8 +1,8 @@ # Required environment variables for load balancer export BUGOUT_AUTH_URL="https://auth.bugout.dev" -export NODE_BALANCER_APPLICATION_ID="" -export NODE_BALANCER_CONTROLLER_TOKEN="" -export NODE_BALANCER_CONTROLLER_ACCESS_ID="" +export NB_APPLICATION_ID="" +export NB_CONTROLLER_TOKEN="" +export NB_CONTROLLER_ACCESS_ID="" # Database variables export MOONSTREAM_DB_URI="postgresql://:@:/" From 3e47047c73fa597b13aaed2ed2ce5ad9474dd0c8 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 17 Mar 2022 10:49:30 +0000 Subject: [PATCH 07/23] Whitelisted json rpc methods --- nodes/node_balancer/cmd/routes.go | 44 ++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index 9b839d68..a9161df1 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -15,6 +15,38 @@ import ( configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) +var ALLOWED_METHODS = []string{ + "eth_blockNumber", + "eth_estimateGas", + "eth_gasPrice", + "eth_getBalance", + "eth_getBlockByHash", + "eth_getBlockByNumber", + "eth_getBlockTransactionCountByHash", + "eth_getBlockTransactionCountByNumber", + "eth_getCode", + "eth_getStorageAt", + "eth_getTransactionByHash", + "eth_getTransactionByBlockHashAndIndex", + "eth_getTransactionByBlockNumberAndIndex", + "eth_getTransactionCount", + "eth_getTransactionReceipt", + "eth_getUncleByBlockHashAndIndex", + "eth_getUncleByBlockNumberAndIndex", + "eth_getUncleCountByBlockHash", + "eth_getUncleCountByBlockNumber", + "eth_getWork", + "eth_protocolVersion", + "eth_syncing", +} + +type JSONRPCRequest struct { + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params []interface{} `json:"params"` + ID uint64 `json:"id"` +} + // pingRoute response with status of load balancer server itself func pingRoute(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -109,7 +141,8 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, node.GethReverseProxy.ServeHTTP(w, r) return case currentUserAccess.dataSource == "database": - lbDatabaseHandler(w, r, blockchain) + // lbDatabaseHandler(w, r, blockchain) + http.Error(w, "Database access under development", http.StatusInternalServerError) return default: http.Error(w, fmt.Sprintf("Unacceptable data source %s", currentUserAccess.dataSource), http.StatusBadRequest) @@ -132,8 +165,6 @@ func parseJSONRPCRequest(r *http.Request) (JSONRPCRequest, error) { return jsonrpcRequest, nil } -var ALLOWED_METHODS = []string{"eth_getBlockByNumber"} - func verifyMethodWhitelisted(method string) error { for _, m := range ALLOWED_METHODS { if method == m { @@ -143,13 +174,6 @@ func verifyMethodWhitelisted(method string) error { return fmt.Errorf("Method not allowed") } -type JSONRPCRequest struct { - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params []interface{} `json:"params"` - ID uint64 `json:"id"` -} - func lbDatabaseHandler(w http.ResponseWriter, r *http.Request, blockchain string) { jsonrpcRequest, err := parseJSONRPCRequest(r) if err != nil { From 6739f9c3df7c4502118e4ff6d978d712ec8190d1 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 17 Mar 2022 11:28:19 +0000 Subject: [PATCH 08/23] Usage in nodebalancer cli --- nodes/node_balancer/cmd/cli.go | 83 ++++++++++++++++++++++++------- nodes/node_balancer/cmd/server.go | 8 +-- nodes/node_balancer/dev.sh | 7 +++ 3 files changed, 75 insertions(+), 23 deletions(-) create mode 100755 nodes/node_balancer/dev.sh diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index 650b6251..dd65fb94 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -15,41 +15,73 @@ var ( // Command Line Interface state type StateCLI struct { - serverCmd *flag.FlagSet - usersCmd *flag.FlagSet + serverCmd *flag.FlagSet + usersCmd *flag.FlagSet + versionCmd *flag.FlagSet // Common flags - showVersion bool + helpFlag bool // Server flags - listeningAddr string - listeningPort string - enableHealthCheck bool - enableDebug bool + listeningAddrFlag string + listeningPortFlag string + enableHealthCheckFlag bool + enableDebugFlag bool +} + +func (s *StateCLI) usage() { + usage := fmt.Sprintf(`usage: nodebalancer [-h] {%[1]s,%[2]s,%[3]s} ... + +Moonstream node balancer CLI + +optional arguments: + -h, --help show this help message and exit + +subcommands: + {%[1]s,%[2]s,%[3]s} +`, s.serverCmd.Name(), s.usersCmd.Name(), s.versionCmd.Name()) + + fmt.Println(usage) +} + +func (s *StateCLI) checkRequirements() { + if s.helpFlag { + switch { + case s.serverCmd.Parsed(): + s.serverCmd.PrintDefaults() + case s.usersCmd.Parsed(): + s.usersCmd.PrintDefaults() + case s.versionCmd.Parsed(): + s.versionCmd.PrintDefaults() + default: + s.usage() + } + os.Exit(1) + } } func (s *StateCLI) populateCLI() { // Subcommands setup s.serverCmd = flag.NewFlagSet("server", flag.ExitOnError) s.usersCmd = flag.NewFlagSet("users", flag.ExitOnError) + s.versionCmd = flag.NewFlagSet("version", flag.ExitOnError) + + // Common flag pointers + for _, fs := range []*flag.FlagSet{s.serverCmd, s.usersCmd, s.versionCmd} { + fs.BoolVar(&s.helpFlag, "help", false, "Show help message") + } // Server subcommand flag pointers - s.serverCmd.StringVar(&s.listeningAddr, "host", "127.0.0.1", "Server listening address") - s.serverCmd.StringVar(&s.listeningPort, "port", "8544", "Server listening port") - s.serverCmd.BoolVar(&s.enableHealthCheck, "healthcheck", false, "To enable healthcheck ser healthcheck flag") - s.serverCmd.BoolVar(&s.enableDebug, "debug", false, "To enable debug mode with extended log set debug flag") -} - -func init() { - configs.VerifyEnvironments() - - InitBugoutClient() + s.serverCmd.StringVar(&s.listeningAddrFlag, "host", "127.0.0.1", "Server listening address") + s.serverCmd.StringVar(&s.listeningPortFlag, "port", "8544", "Server listening port") + s.serverCmd.BoolVar(&s.enableHealthCheckFlag, "healthcheck", false, "To enable healthcheck ser healthcheck flag") + s.serverCmd.BoolVar(&s.enableDebugFlag, "debug", false, "To enable debug mode with extended log set debug flag") } func CLI() { stateCLI.populateCLI() if len(os.Args) < 2 { - fmt.Println("Command: server or version is required") + stateCLI.usage() os.Exit(1) } @@ -57,9 +89,13 @@ func CLI() { switch os.Args[1] { case "server": stateCLI.serverCmd.Parse(os.Args[2:]) + stateCLI.checkRequirements() + Server() case "users": stateCLI.usersCmd.Parse(os.Args[2:]) + stateCLI.checkRequirements() + userAccesses, err := bugoutClient.GetUserAccesses(configs.NB_CONTROLLER_TOKEN, "", "") if err != nil { fmt.Printf("Unable to get resources %v", err) @@ -72,9 +108,18 @@ func CLI() { } fmt.Println(string(userAccessesJson)) case "version": + stateCLI.versionCmd.Parse(os.Args[2:]) + stateCLI.checkRequirements() + fmt.Printf("v%s\n", configs.NB_VERSION) default: - flag.PrintDefaults() + stateCLI.usage() os.Exit(1) } } + +func init() { + configs.VerifyEnvironments() + + InitBugoutClient() +} diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 47713c36..8fc02f57 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -171,18 +171,18 @@ func Server() { commonHandler = panicMiddleware(commonHandler) server := http.Server{ - Addr: fmt.Sprintf("%s:%s", stateCLI.listeningAddr, stateCLI.listeningPort), + Addr: fmt.Sprintf("%s:%s", stateCLI.listeningAddrFlag, stateCLI.listeningPortFlag), Handler: commonHandler, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, } // Start node health checking and current block fetching - if stateCLI.enableHealthCheck { - go initHealthCheck(stateCLI.enableDebug) + if stateCLI.enableHealthCheckFlag { + go initHealthCheck(stateCLI.enableDebugFlag) } - log.Printf("Starting server at %s:%s\n", stateCLI.listeningAddr, stateCLI.listeningPort) + log.Printf("Starting server at %s:%s\n", stateCLI.listeningAddrFlag, stateCLI.listeningPortFlag) err = server.ListenAndServe() if err != nil { log.Fatal(err) diff --git a/nodes/node_balancer/dev.sh b/nodes/node_balancer/dev.sh new file mode 100755 index 00000000..eef8dc96 --- /dev/null +++ b/nodes/node_balancer/dev.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +PROGRAM_NAME="nodebalancer" + +go build -o "$PROGRAM_NAME" . + +./"$PROGRAM_NAME" "$@" From ae3dfa1449fe179c80611add52b844beb4400bc5 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 17 Mar 2022 11:58:23 +0000 Subject: [PATCH 09/23] return after print defaults at cli --- nodes/node_balancer/cmd/cli.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index dd65fb94..31d3a587 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -49,14 +49,17 @@ func (s *StateCLI) checkRequirements() { switch { case s.serverCmd.Parsed(): s.serverCmd.PrintDefaults() + return case s.usersCmd.Parsed(): s.usersCmd.PrintDefaults() + return case s.versionCmd.Parsed(): s.versionCmd.PrintDefaults() + return default: s.usage() + return } - os.Exit(1) } } @@ -92,6 +95,7 @@ func CLI() { stateCLI.checkRequirements() Server() + case "users": stateCLI.usersCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() @@ -107,11 +111,13 @@ func CLI() { return } fmt.Println(string(userAccessesJson)) + case "version": stateCLI.versionCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() fmt.Printf("v%s\n", configs.NB_VERSION) + default: stateCLI.usage() os.Exit(1) From abd570b6a0a119e8d004d74fd3323498e411aef6 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 17 Mar 2022 14:41:03 +0000 Subject: [PATCH 10/23] cli to work with nodebalancer users access --- nodes/node_balancer/README.md | 30 ++++- nodes/node_balancer/cmd/bugout.go | 146 ++++++++++++++++++++++--- nodes/node_balancer/cmd/cli.go | 152 ++++++++++++++++++++++---- nodes/node_balancer/cmd/middleware.go | 7 +- nodes/node_balancer/cmd/server.go | 4 +- 5 files changed, 294 insertions(+), 45 deletions(-) diff --git a/nodes/node_balancer/README.md b/nodes/node_balancer/README.md index 8a030485..2039435e 100644 --- a/nodes/node_balancer/README.md +++ b/nodes/node_balancer/README.md @@ -11,10 +11,36 @@ go build -o nodebalancer # Work with nodebalancer -## clients +## add-access + +Add new access for user: ```bash -nodebalancer clients | jq . +nodebalancer add-access \ + --user-id "" \ + --access-id "" \ + --name "Access name" \ + --description "Description of access" \ + --extended-methods false \ + --blockchain--access true +``` + +## delete-access + +Delete user access: + +```bash +nodebalancer delete-access \ + --user-id "" \ + --access-id "" +``` + +If `access-id` not specified, all user accesses will be deleted. + +## users + +```bash +nodebalancer users | jq . ``` This command will return a list of bugout resources of registered users to access node balancer with their `crawlers/app/project` (in our project we will call it `crawlers`). diff --git a/nodes/node_balancer/cmd/bugout.go b/nodes/node_balancer/cmd/bugout.go index f00b000f..a5fd9c84 100644 --- a/nodes/node_balancer/cmd/bugout.go +++ b/nodes/node_balancer/cmd/bugout.go @@ -1,6 +1,7 @@ package cmd import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -59,10 +60,12 @@ func InitBugoutClient() { // Get Bugout user func (bc *BugoutClient) GetUser(token string) (BugoutUserResponse, error) { + var userResponse BugoutUserResponse + url := fmt.Sprintf("%s/user", configs.BUGOUT_AUTH_URL) req, err := http.NewRequest("GET", url, nil) if err != nil { - return BugoutUserResponse{}, err + return userResponse, err } req.Header = http.Header{ @@ -70,27 +73,109 @@ func (bc *BugoutClient) GetUser(token string) (BugoutUserResponse, error) { } resp, err := bc.Client.Do(req) if err != nil { - return BugoutUserResponse{}, err + return userResponse, err } defer resp.Body.Close() // Parse response body, err := ioutil.ReadAll(resp.Body) if err != nil { - return BugoutUserResponse{}, err + return userResponse, err } - var userResponse BugoutUserResponse err = json.Unmarshal(body, &userResponse) if err != nil { - return BugoutUserResponse{}, err + return userResponse, err } return userResponse, nil } -// Get user accesses from Bugout resources -func (bc *BugoutClient) GetUserAccesses(token, userID, accessID string) ([]UserAccess, error) { - var userAccesses []UserAccess +// Find Bugout user +func (bc *BugoutClient) FindUser(token, userID string) (BugoutUserResponse, error) { + var userResponse BugoutUserResponse + + url := fmt.Sprintf("%s/user/find?user_id=%s", configs.BUGOUT_AUTH_URL, userID) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return userResponse, err + } + + req.Header = http.Header{ + "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, + } + resp, err := bc.Client.Do(req) + if err != nil { + return userResponse, err + } + defer resp.Body.Close() + + // Parse response + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return userResponse, err + } + err = json.Unmarshal(body, &userResponse) + if err != nil { + return userResponse, err + } + + return userResponse, nil +} + +func (bc *BugoutClient) AddUserAccess(token string, proposedUserAccess UserAccess) (UserAccess, error) { + var userAccess UserAccess + + // Check user exists + user, err := bc.FindUser(token, proposedUserAccess.UserID) + if err != nil { + return userAccess, err + } + if user == (BugoutUserResponse{}) { + return userAccess, fmt.Errorf("User with id %s not found", proposedUserAccess.UserID) + } + + resource := map[string]interface{}{ + "application_id": configs.NB_APPLICATION_ID, + "resource_data": proposedUserAccess, + } + resourceJson, err := json.Marshal(resource) + if err != nil { + return userAccess, err + } + url := fmt.Sprintf("%s/resources", configs.BUGOUT_AUTH_URL) + req, err := http.NewRequest("POST", url, bytes.NewBuffer(resourceJson)) + if err != nil { + return userAccess, err + } + req.Header = http.Header{ + "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, + "Content-Type": []string{"application/json"}, + } + resp, err := bc.Client.Do(req) + if err != nil { + return userAccess, err + } + defer resp.Body.Close() + + // Parse response + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return userAccess, err + } + var resourceResponse BugoutResourceResponse + err = json.Unmarshal(body, &resourceResponse) + if err != nil { + return userAccess, err + } + + userAccess = resourceResponse.ResourceData + + return userAccess, nil +} + +// Get Bugout resource +func (bc *BugoutClient) GetResources(token, userID, accessID string) (BugoutResourcesResponse, error) { + var resourcesResponse BugoutResourcesResponse url := fmt.Sprintf("%s/resources?application_id=%s", configs.BUGOUT_AUTH_URL, configs.NB_APPLICATION_ID) if userID != "" { @@ -101,31 +186,56 @@ func (bc *BugoutClient) GetUserAccesses(token, userID, accessID string) ([]UserA } req, err := http.NewRequest("GET", url, nil) if err != nil { - return userAccesses, err + return resourcesResponse, err } req.Header = http.Header{ "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, } resp, err := bc.Client.Do(req) if err != nil { - return userAccesses, err + return resourcesResponse, err } defer resp.Body.Close() // Parse response body, err := ioutil.ReadAll(resp.Body) if err != nil { - return userAccesses, err + return resourcesResponse, err } - var resourcesResponse BugoutResourcesResponse err = json.Unmarshal(body, &resourcesResponse) if err != nil { - return userAccesses, err + return resourcesResponse, err } - for _, resourceData := range resourcesResponse.Resources { - userAccesses = append(userAccesses, resourceData.ResourceData) - } - - return userAccesses, nil + return resourcesResponse, nil +} + +func (bc *BugoutClient) DeleteResource(token, resourceID string) (BugoutResourceResponse, error) { + var resourceResponse BugoutResourceResponse + + url := fmt.Sprintf("%s/resources/%s", configs.BUGOUT_AUTH_URL, resourceID) + req, err := http.NewRequest("DELETE", url, nil) + if err != nil { + return resourceResponse, err + } + req.Header = http.Header{ + "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, + } + resp, err := bc.Client.Do(req) + if err != nil { + return resourceResponse, err + } + defer resp.Body.Close() + + // Parse response + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return resourceResponse, err + } + err = json.Unmarshal(body, &resourceResponse) + if err != nil { + return resourceResponse, err + } + + return resourceResponse, nil } diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index 31d3a587..95268eb8 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -7,6 +7,8 @@ import ( "os" "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" + + "github.com/google/uuid" ) var ( @@ -15,13 +17,23 @@ var ( // Command Line Interface state type StateCLI struct { - serverCmd *flag.FlagSet - usersCmd *flag.FlagSet - versionCmd *flag.FlagSet + addAccessCmd *flag.FlagSet + deleteAccessCmd *flag.FlagSet + serverCmd *flag.FlagSet + usersCmd *flag.FlagSet + versionCmd *flag.FlagSet // Common flags helpFlag bool + // Add user access flags + userIDFlag string + accessIDFlag string + accessNameFlag string + accessDescriptionFlag string + blockchainAccessFlag bool + extendedMethodsFlag bool + // Server flags listeningAddrFlag string listeningPortFlag string @@ -30,50 +42,96 @@ type StateCLI struct { } func (s *StateCLI) usage() { - usage := fmt.Sprintf(`usage: nodebalancer [-h] {%[1]s,%[2]s,%[3]s} ... + fmt.Printf(`usage: nodebalancer [-h] {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s} ... Moonstream node balancer CLI optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit subcommands: - {%[1]s,%[2]s,%[3]s} -`, s.serverCmd.Name(), s.usersCmd.Name(), s.versionCmd.Name()) - - fmt.Println(usage) + {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s} +`, s.addAccessCmd.Name(), s.deleteAccessCmd.Name(), s.serverCmd.Name(), s.usersCmd.Name(), s.versionCmd.Name()) } func (s *StateCLI) checkRequirements() { if s.helpFlag { switch { + case s.addAccessCmd.Parsed(): + fmt.Println("add new user access token") + s.addAccessCmd.PrintDefaults() + os.Exit(0) + case s.deleteAccessCmd.Parsed(): + fmt.Println("delete user access token") + s.deleteAccessCmd.PrintDefaults() + os.Exit(0) case s.serverCmd.Parsed(): + fmt.Println("start nodebalancer server") s.serverCmd.PrintDefaults() - return + os.Exit(0) case s.usersCmd.Parsed(): + fmt.Println("list user access tokens") s.usersCmd.PrintDefaults() - return + os.Exit(0) case s.versionCmd.Parsed(): + fmt.Println("show version") s.versionCmd.PrintDefaults() - return + os.Exit(0) default: s.usage() - return + os.Exit(0) + } + } + + switch { + case s.addAccessCmd.Parsed(): + if s.userIDFlag == "" { + fmt.Println("User ID should be specified") + s.addAccessCmd.PrintDefaults() + os.Exit(1) + } + if s.accessIDFlag == "" { + s.accessIDFlag = uuid.New().String() + } + if s.accessNameFlag == "" { + fmt.Println("Access name should be specified") + s.addAccessCmd.PrintDefaults() + os.Exit(1) + } + case s.deleteAccessCmd.Parsed(): + if s.userIDFlag == "" && s.accessIDFlag == "" { + fmt.Println("User or access ID flag should be specified") + s.deleteAccessCmd.PrintDefaults() + os.Exit(1) } } } func (s *StateCLI) populateCLI() { // Subcommands setup + s.addAccessCmd = flag.NewFlagSet("add-access", flag.ExitOnError) + s.deleteAccessCmd = flag.NewFlagSet("delete-access", flag.ExitOnError) s.serverCmd = flag.NewFlagSet("server", flag.ExitOnError) s.usersCmd = flag.NewFlagSet("users", flag.ExitOnError) s.versionCmd = flag.NewFlagSet("version", flag.ExitOnError) // Common flag pointers - for _, fs := range []*flag.FlagSet{s.serverCmd, s.usersCmd, s.versionCmd} { + for _, fs := range []*flag.FlagSet{s.addAccessCmd, s.deleteAccessCmd, s.serverCmd, s.usersCmd, s.versionCmd} { fs.BoolVar(&s.helpFlag, "help", false, "Show help message") } + // Add, delete and list user access subcommand flag pointers + for _, fs := range []*flag.FlagSet{s.addAccessCmd, s.deleteAccessCmd, s.usersCmd} { + fs.StringVar(&s.userIDFlag, "user-id", "", "Bugout user ID") + fs.StringVar(&s.accessIDFlag, "access-id", "", "UUID for access identification") + } + + // Add user access subcommand flag pointers + s.addAccessCmd.StringVar(&s.accessNameFlag, "name", "", "Name of access") + s.addAccessCmd.StringVar(&s.accessDescriptionFlag, "description", "", "Description of access") + s.addAccessCmd.BoolVar(&s.blockchainAccessFlag, "blockchain-access", false, "Provide if allow to access blockchain nodes") + s.addAccessCmd.BoolVar(&s.extendedMethodsFlag, "extended-methods", false, "Provide to be able to execute not whitelisted methods") + // Server subcommand flag pointers s.serverCmd.StringVar(&s.listeningAddrFlag, "host", "127.0.0.1", "Server listening address") s.serverCmd.StringVar(&s.listeningPortFlag, "port", "8544", "Server listening port") @@ -90,6 +148,57 @@ func CLI() { // Parse subcommands and appropriate FlagSet switch os.Args[1] { + case "add-access": + stateCLI.addAccessCmd.Parse(os.Args[2:]) + stateCLI.checkRequirements() + + proposedUserAccess := UserAccess{ + UserID: stateCLI.userIDFlag, + AccessID: stateCLI.accessIDFlag, + Name: stateCLI.accessNameFlag, + Description: stateCLI.accessDescriptionFlag, + BlockchainAccess: stateCLI.blockchainAccessFlag, + ExtendedMethods: stateCLI.extendedMethodsFlag, + } + userAccess, err := bugoutClient.AddUserAccess(configs.NB_CONTROLLER_TOKEN, proposedUserAccess) + if err != nil { + fmt.Printf("Unable to create user access %v\n", err) + os.Exit(1) + } + userAccessJson, err := json.Marshal(userAccess) + if err != nil { + fmt.Printf("Unable to marshal user access struct %v\n", err) + os.Exit(1) + } + fmt.Println(string(userAccessJson)) + + case "delete-access": + stateCLI.deleteAccessCmd.Parse(os.Args[2:]) + stateCLI.checkRequirements() + + resources, err := bugoutClient.GetResources(configs.NB_CONTROLLER_TOKEN, stateCLI.userIDFlag, stateCLI.accessIDFlag) + if err != nil { + fmt.Printf("Unable to get Bugout resources %v\n", err) + os.Exit(1) + } + + var userAccesses []UserAccess + for _, resource := range resources.Resources { + deletedResource, err := bugoutClient.DeleteResource(configs.NB_CONTROLLER_TOKEN, resource.ID) + if err != nil { + fmt.Printf("Unable to delete resource with id %s %v\n", resource.ID, err) + continue + } + userAccesses = append(userAccesses, deletedResource.ResourceData) + } + + userAccessesJson, err := json.Marshal(userAccesses) + if err != nil { + fmt.Printf("Unable to marshal user access struct %v\n", err) + os.Exit(1) + } + fmt.Println(string(userAccessesJson)) + case "server": stateCLI.serverCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() @@ -100,15 +209,20 @@ func CLI() { stateCLI.usersCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() - userAccesses, err := bugoutClient.GetUserAccesses(configs.NB_CONTROLLER_TOKEN, "", "") + resources, err := bugoutClient.GetResources(configs.NB_CONTROLLER_TOKEN, stateCLI.userIDFlag, stateCLI.accessIDFlag) if err != nil { - fmt.Printf("Unable to get resources %v", err) - return + fmt.Printf("Unable to get Bugout resources %v\n", err) + os.Exit(1) + } + + var userAccesses []UserAccess + for _, resourceData := range resources.Resources { + userAccesses = append(userAccesses, resourceData.ResourceData) } userAccessesJson, err := json.Marshal(userAccesses) if err != nil { - fmt.Printf("Unable to marshal resources %v", err) - return + fmt.Printf("Unable to marshal user accesses struct %v\n", err) + os.Exit(1) } fmt.Println(string(userAccessesJson)) diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index db83b8d5..2a59d9b3 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -76,17 +76,16 @@ func accessMiddleware(next http.Handler) http.Handler { currentUserAccess = controllerUserAccess currentUserAccess.dataSource = dataSource } else { - userAccesses, err := bugoutClient.GetUserAccesses(configs.NB_CONTROLLER_TOKEN, "", accessID) + resources, err := bugoutClient.GetResources(configs.NB_CONTROLLER_TOKEN, "", accessID) if err != nil { http.Error(w, "Unable to get user with provided access identifier", http.StatusForbidden) return } - if len(userAccesses) == 0 { + if len(resources.Resources) == 0 { http.Error(w, "User with provided access identifier not found", http.StatusForbidden) return } - userAccess := userAccesses[0] - + userAccess := resources.Resources[0].ResourceData currentUserAccess = UserAccess{ UserID: userAccess.UserID, AccessID: userAccess.AccessID, diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 8fc02f57..a164134c 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -111,11 +111,11 @@ func Server() { // Record system information reporter.Publish(humbug.SystemReport()) - userAccesses, err := bugoutClient.GetUserAccesses(configs.NB_CONTROLLER_TOKEN, "", configs.NB_CONTROLLER_ACCESS_ID) + resources, err := bugoutClient.GetResources(configs.NB_CONTROLLER_TOKEN, "", configs.NB_CONTROLLER_ACCESS_ID) if err != nil { fmt.Printf("Unable to access Bugout authentication server %v", err) } - userAccess := userAccesses[0] + userAccess := resources.Resources[0].ResourceData controllerUserAccess = UserAccess{ UserID: userAccess.UserID, AccessID: userAccess.AccessID, From 884eb82376b35244fe05066d14da60fe51a7c4b0 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 17 Mar 2022 14:51:12 +0000 Subject: [PATCH 11/23] removed print --- nodes/node_balancer/cmd/routes.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index a9161df1..41dd9fab 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -63,8 +63,6 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { return } - fmt.Println(currentUserAccess) - attempts := GetAttemptsFromContext(r) if attempts > configs.NB_CONNECTION_RETRIES { log.Printf("Max attempts reached from %s %s, terminating\n", r.RemoteAddr, r.URL.Path) From 34ec0f3b930c1db638806b59db8a25d14db69188 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 17 Mar 2022 14:58:40 +0000 Subject: [PATCH 12/23] check if access id not specified --- nodes/node_balancer/cmd/middleware.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 2a59d9b3..2675557c 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -71,6 +71,11 @@ func accessMiddleware(next http.Handler) http.Handler { } } + if accessID == "" { + http.Error(w, "No authorization header passed with request", http.StatusForbidden) + return + } + // If access id does not belong to controller, then find it in Bugout resources if accessID == configs.NB_CONTROLLER_ACCESS_ID { currentUserAccess = controllerUserAccess From 6dcb63b5dff5660edbe28bc9f514afd55e605aa9 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Thu, 17 Mar 2022 17:35:53 +0000 Subject: [PATCH 13/23] pagination for cli resources --- nodes/node_balancer/README.md | 9 ++++++- nodes/node_balancer/cmd/cli.go | 42 ++++++++++++++++++++++++------- nodes/node_balancer/cmd/routes.go | 12 +++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/nodes/node_balancer/README.md b/nodes/node_balancer/README.md index 2039435e..62fd4de8 100644 --- a/nodes/node_balancer/README.md +++ b/nodes/node_balancer/README.md @@ -6,7 +6,7 @@ - Build application ```bash -go build -o nodebalancer +go build -o nodebalancer . ``` # Work with nodebalancer @@ -90,3 +90,10 @@ curl --request GET 'http://127.0.0.1:8544/nb/ethereum/jsonrpc?access_id=' +--header 'x-node-balancer-access-id: ' +``` diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index 95268eb8..2130fcd1 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -39,6 +39,10 @@ type StateCLI struct { listeningPortFlag string enableHealthCheckFlag bool enableDebugFlag bool + + // Users list flags + limitFlag int + offsetFlag int } func (s *StateCLI) usage() { @@ -58,23 +62,23 @@ func (s *StateCLI) checkRequirements() { if s.helpFlag { switch { case s.addAccessCmd.Parsed(): - fmt.Println("add new user access token") + fmt.Printf("Add new user access token\n\n") s.addAccessCmd.PrintDefaults() os.Exit(0) case s.deleteAccessCmd.Parsed(): - fmt.Println("delete user access token") + fmt.Printf("Delete user access token\n\n") s.deleteAccessCmd.PrintDefaults() os.Exit(0) case s.serverCmd.Parsed(): - fmt.Println("start nodebalancer server") + fmt.Printf("Start nodebalancer server\n\n") s.serverCmd.PrintDefaults() os.Exit(0) case s.usersCmd.Parsed(): - fmt.Println("list user access tokens") + fmt.Printf("List user access tokens\n\n") s.usersCmd.PrintDefaults() os.Exit(0) case s.versionCmd.Parsed(): - fmt.Println("show version") + fmt.Printf("Show version\n\n") s.versionCmd.PrintDefaults() os.Exit(0) default: @@ -86,7 +90,7 @@ func (s *StateCLI) checkRequirements() { switch { case s.addAccessCmd.Parsed(): if s.userIDFlag == "" { - fmt.Println("User ID should be specified") + fmt.Printf("User ID should be specified\n\n") s.addAccessCmd.PrintDefaults() os.Exit(1) } @@ -94,16 +98,22 @@ func (s *StateCLI) checkRequirements() { s.accessIDFlag = uuid.New().String() } if s.accessNameFlag == "" { - fmt.Println("Access name should be specified") + fmt.Printf("Access name should be specified\n\n") s.addAccessCmd.PrintDefaults() os.Exit(1) } case s.deleteAccessCmd.Parsed(): if s.userIDFlag == "" && s.accessIDFlag == "" { - fmt.Println("User or access ID flag should be specified") + fmt.Printf("User or access ID flag should be specified\n\n") s.deleteAccessCmd.PrintDefaults() os.Exit(1) } + case s.usersCmd.Parsed(): + if s.offsetFlag < 0 || s.limitFlag < 0 { + fmt.Printf("Offset and limit flags should be greater then zero\n\n") + s.usersCmd.PrintDefaults() + os.Exit(1) + } } } @@ -137,6 +147,10 @@ func (s *StateCLI) populateCLI() { s.serverCmd.StringVar(&s.listeningPortFlag, "port", "8544", "Server listening port") s.serverCmd.BoolVar(&s.enableHealthCheckFlag, "healthcheck", false, "To enable healthcheck ser healthcheck flag") s.serverCmd.BoolVar(&s.enableDebugFlag, "debug", false, "To enable debug mode with extended log set debug flag") + + // Users list subcommand flag pointers + s.usersCmd.IntVar(&s.limitFlag, "limit", 10, "Output result limit") + s.usersCmd.IntVar(&s.offsetFlag, "offset", 0, "Result output offset") } func CLI() { @@ -216,7 +230,17 @@ func CLI() { } var userAccesses []UserAccess - for _, resourceData := range resources.Resources { + + offset := stateCLI.offsetFlag + if stateCLI.offsetFlag > len(resources.Resources) { + offset = len(resources.Resources) + } + limit := stateCLI.offsetFlag + stateCLI.limitFlag + if limit > len(resources.Resources) { + limit = len(resources.Resources[offset:]) + offset + } + + for _, resourceData := range resources.Resources[offset:limit] { userAccesses = append(userAccesses, resourceData.ResourceData) } userAccessesJson, err := json.Marshal(userAccesses) diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index 41dd9fab..94856916 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -17,7 +17,10 @@ import ( var ALLOWED_METHODS = []string{ "eth_blockNumber", + "eth_call", + "eth_chainId", "eth_estimateGas", + "eth_feeHistory", "eth_gasPrice", "eth_getBalance", "eth_getBlockByHash", @@ -25,6 +28,7 @@ var ALLOWED_METHODS = []string{ "eth_getBlockTransactionCountByHash", "eth_getBlockTransactionCountByNumber", "eth_getCode", + "eth_getLogs", "eth_getStorageAt", "eth_getTransactionByHash", "eth_getTransactionByBlockHashAndIndex", @@ -36,8 +40,16 @@ var ALLOWED_METHODS = []string{ "eth_getUncleCountByBlockHash", "eth_getUncleCountByBlockNumber", "eth_getWork", + "eth_mining", + // "eth_sendRawTransaction", "eth_protocolVersion", "eth_syncing", + + "net_listening", + "net_peerCount", + "net_version", + + "web3_clientVersion", } type JSONRPCRequest struct { From 0cd3c357963eab2b38fe9ea2597c7f965b7e3a99 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Fri, 18 Mar 2022 17:10:37 +0000 Subject: [PATCH 14/23] fixed behaviour with req body, extended log middleware --- nodes/node_balancer/cmd/blockchain.go | 54 ++++++++++++++ nodes/node_balancer/cmd/middleware.go | 103 ++++++++++++++++++++------ nodes/node_balancer/cmd/routes.go | 98 +++++------------------- nodes/node_balancer/cmd/server.go | 3 +- 4 files changed, 152 insertions(+), 106 deletions(-) diff --git a/nodes/node_balancer/cmd/blockchain.go b/nodes/node_balancer/cmd/blockchain.go index 6d9ef68e..48f8782c 100644 --- a/nodes/node_balancer/cmd/blockchain.go +++ b/nodes/node_balancer/cmd/blockchain.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "log" "strconv" @@ -9,8 +10,52 @@ import ( var ( nodeConfigs NodeConfigs + + ALLOWED_METHODS = []string{ + "eth_blockNumber", + "eth_call", + "eth_chainId", + "eth_estimateGas", + "eth_feeHistory", + "eth_gasPrice", + "eth_getBalance", + "eth_getBlockByHash", + "eth_getBlockByNumber", + "eth_getBlockTransactionCountByHash", + "eth_getBlockTransactionCountByNumber", + "eth_getCode", + "eth_getLogs", + "eth_getStorageAt", + "eth_getTransactionByHash", + "eth_getTransactionByBlockHashAndIndex", + "eth_getTransactionByBlockNumberAndIndex", + "eth_getTransactionCount", + "eth_getTransactionReceipt", + "eth_getUncleByBlockHashAndIndex", + "eth_getUncleByBlockNumberAndIndex", + "eth_getUncleCountByBlockHash", + "eth_getUncleCountByBlockNumber", + "eth_getWork", + "eth_mining", + // "eth_sendRawTransaction", + "eth_protocolVersion", + "eth_syncing", + + "net_listening", + "net_peerCount", + "net_version", + + "web3_clientVersion", + } ) +type JSONRPCRequest struct { + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params []interface{} `json:"params"` + ID uint64 `json:"id"` +} + // Node conf type BlockchainConfig struct { Blockchain string @@ -28,6 +73,15 @@ type NodeConfigs struct { NodeConfigs []NodeConfig } +func verifyMethodWhitelisted(method string) error { + for _, m := range ALLOWED_METHODS { + if method == m { + return nil + } + } + return fmt.Errorf("Method not allowed") +} + // Return list of NodeConfig structures func (nc *NodeConfigs) InitNodeConfiguration() { // Define available blockchain nodes diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 2675557c..0fc66cee 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -4,16 +4,59 @@ Server API middlewares. package cmd import ( + "bytes" "context" + "encoding/json" + "fmt" + "io/ioutil" "log" "net" "net/http" + "strings" "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" humbug "github.com/bugout-dev/humbug/go/pkg" ) +// Extract access_id from header and query. Query takes precedence over header. +func extractAccessID(r *http.Request) string { + var accessID string + + accessIDHeaders := r.Header[configs.NB_ACCESS_ID_HEADER] + for _, h := range accessIDHeaders { + accessID = h + } + + queries := r.URL.Query() + for k, v := range queries { + if k == "access_id" { + accessID = v[0] + } + } + + return accessID +} + +// Extract data_source from header and query. Query takes precedence over header. +func extractDataSource(r *http.Request) string { + dataSource := "database" + + dataSources := r.Header[configs.NB_DATA_SOURCE_HEADER] + for _, h := range dataSources { + dataSource = h + } + + queries := r.URL.Query() + for k, v := range queries { + if k == "data_source" { + dataSource = v[0] + } + } + + return dataSource +} + // Handle panic errors to prevent server shutdown func panicMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -34,13 +77,44 @@ func panicMiddleware(next http.Handler) http.Handler { // Log access requests in proper format func logMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, "Unable to read body", http.StatusBadRequest) + return + } + r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + if len(body) > 0 { + defer r.Body.Close() + } + next.ServeHTTP(w, r) + ip, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { - log.Printf("Unable to parse client IP: %s\n", r.RemoteAddr) - } else { - log.Printf("%s %s %s\n", ip, r.Method, r.URL.Path) + http.Error(w, fmt.Sprintf("Unable to parse client IP: %s", r.RemoteAddr), http.StatusBadRequest) + return } + logStr := fmt.Sprintf("%s %s %s", ip, r.Method, r.URL.Path) + + // Parse body and log method if jsonrpc path + pathSlice := strings.Split(r.URL.Path, "/") + if r.Method == "POST" && pathSlice[len(pathSlice)-1] == "jsonrpc" { + var jsonrpcRequest JSONRPCRequest + err = json.Unmarshal(body, &jsonrpcRequest) + if err != nil { + log.Printf("Unable to parse body %v", err) + } + logStr += fmt.Sprintf(" %s", jsonrpcRequest.Method) + } + + if stateCLI.enableDebugFlag { + accessID := extractAccessID(r) + if accessID != "" { + dataSource := extractDataSource(r) + logStr += fmt.Sprintf(" %s %s", dataSource, accessID) + } + } + log.Printf("%s\n", logStr) }) } @@ -49,27 +123,8 @@ func accessMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var currentUserAccess UserAccess - var accessID string - accessIDHeaders := r.Header[configs.NB_ACCESS_ID_HEADER] - for _, h := range accessIDHeaders { - accessID = h - } - - dataSource := "database" - dataSources := r.Header[configs.NB_DATA_SOURCE_HEADER] - for _, h := range dataSources { - dataSource = h - } - - queries := r.URL.Query() - for k, v := range queries { - if k == "access_id" { - accessID = v[0] - } - if k == "data_source" { - dataSource = v[0] - } - } + accessID := extractAccessID(r) + dataSource := extractDataSource(r) if accessID == "" { http.Error(w, "No authorization header passed with request", http.StatusForbidden) diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index 94856916..3973e371 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -4,6 +4,7 @@ Handle routes for load balancer API. package cmd import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -15,50 +16,6 @@ import ( configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) -var ALLOWED_METHODS = []string{ - "eth_blockNumber", - "eth_call", - "eth_chainId", - "eth_estimateGas", - "eth_feeHistory", - "eth_gasPrice", - "eth_getBalance", - "eth_getBlockByHash", - "eth_getBlockByNumber", - "eth_getBlockTransactionCountByHash", - "eth_getBlockTransactionCountByNumber", - "eth_getCode", - "eth_getLogs", - "eth_getStorageAt", - "eth_getTransactionByHash", - "eth_getTransactionByBlockHashAndIndex", - "eth_getTransactionByBlockNumberAndIndex", - "eth_getTransactionCount", - "eth_getTransactionReceipt", - "eth_getUncleByBlockHashAndIndex", - "eth_getUncleByBlockNumberAndIndex", - "eth_getUncleCountByBlockHash", - "eth_getUncleCountByBlockNumber", - "eth_getWork", - "eth_mining", - // "eth_sendRawTransaction", - "eth_protocolVersion", - "eth_syncing", - - "net_listening", - "net_peerCount", - "net_version", - - "web3_clientVersion", -} - -type JSONRPCRequest struct { - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params []interface{} `json:"params"` - ID uint64 `json:"id"` -} - // pingRoute response with status of load balancer server itself func pingRoute(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -128,6 +85,20 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { } func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, node *Node, currentUserAccess UserAccess) { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, "Unable to read body", http.StatusBadRequest) + return + } + r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + + var jsonrpcRequest JSONRPCRequest + err = json.Unmarshal(body, &jsonrpcRequest) + if err != nil { + http.Error(w, "Unable to parse JSON RPC request", http.StatusBadRequest) + return + } + switch { case currentUserAccess.dataSource == "blockchain": if currentUserAccess.BlockchainAccess == false { @@ -135,11 +106,6 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, return } if currentUserAccess.ExtendedMethods == false { - jsonrpcRequest, err := parseJSONRPCRequest(r) - if err != nil { - http.Error(w, "Unable to parse JSON RPC request", http.StatusBadRequest) - return - } err = verifyMethodWhitelisted(jsonrpcRequest.Method) if err != nil { http.Error(w, "Method for provided access id not allowed", http.StatusForbidden) @@ -151,7 +117,7 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, node.GethReverseProxy.ServeHTTP(w, r) return case currentUserAccess.dataSource == "database": - // lbDatabaseHandler(w, r, blockchain) + // lbDatabaseHandler(w, r, blockchain, jsonrpcRequest) http.Error(w, "Database access under development", http.StatusInternalServerError) return default: @@ -160,37 +126,7 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, } } -func parseJSONRPCRequest(r *http.Request) (JSONRPCRequest, error) { - var jsonrpcRequest JSONRPCRequest - - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return jsonrpcRequest, err - } - err = json.Unmarshal(body, &jsonrpcRequest) - if err != nil { - return jsonrpcRequest, err - } - - return jsonrpcRequest, nil -} - -func verifyMethodWhitelisted(method string) error { - for _, m := range ALLOWED_METHODS { - if method == m { - return nil - } - } - return fmt.Errorf("Method not allowed") -} - -func lbDatabaseHandler(w http.ResponseWriter, r *http.Request, blockchain string) { - jsonrpcRequest, err := parseJSONRPCRequest(r) - if err != nil { - http.Error(w, "Unable to parse JSON RPC request", http.StatusBadRequest) - return - } - +func lbDatabaseHandler(w http.ResponseWriter, r *http.Request, blockchain string, jsonrpcRequest JSONRPCRequest) { switch { case jsonrpcRequest.Method == "eth_getBlockByNumber": var blockNumber uint64 diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index a164134c..7c13bd48 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -33,7 +33,7 @@ func initHealthCheck(debug bool) { blockchainPool.HealthCheck() ethereumClients := ethereumClientPool.CleanInactiveClientNodes() polygonClients := polygonClientPool.CleanInactiveClientNodes() - log.Printf("Active etehereum clients: %d, polygon clients: %d\n", ethereumClients, polygonClients) + log.Printf("Active ethereum clients: %d, polygon clients: %d\n", ethereumClients, polygonClients) if debug { blockchainPool.StatusLog() } @@ -178,6 +178,7 @@ func Server() { } // Start node health checking and current block fetching + blockchainPool.HealthCheck() if stateCLI.enableHealthCheckFlag { go initHealthCheck(stateCLI.enableDebugFlag) } From b6b99e5f7ac5a2dbf63b8ab8a61866d0e943eb5c Mon Sep 17 00:00:00 2001 From: kompotkot Date: Fri, 18 Mar 2022 17:19:56 +0000 Subject: [PATCH 15/23] log query if provided --- nodes/node_balancer/cmd/middleware.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 0fc66cee..2bff690f 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -108,6 +108,9 @@ func logMiddleware(next http.Handler) http.Handler { } if stateCLI.enableDebugFlag { + if r.URL.RawQuery != "" { + logStr += fmt.Sprintf(" %s", r.URL.RawQuery) + } accessID := extractAccessID(r) if accessID != "" { dataSource := extractDataSource(r) From 0fd2b841d9b7d43570819bd9ce3d551e64c7bcf6 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Fri, 18 Mar 2022 19:11:18 +0000 Subject: [PATCH 16/23] small fixes of timeouts and dev sh --- nodes/node_balancer/cmd/routes.go | 2 ++ nodes/node_balancer/cmd/server.go | 4 ++-- nodes/node_balancer/dev.sh | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index 3973e371..430d50ca 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -114,6 +114,8 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, } r.URL.Path = "/" + // If required detailed timeout configuration, define node.GethReverseProxy.Transport = &http.Transport{} + // as modified structure of DefaultTransport net/http/transport/DefaultTransport node.GethReverseProxy.ServeHTTP(w, r) return case currentUserAccess.dataSource == "database": diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 7c13bd48..5e44ee1f 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -173,8 +173,8 @@ func Server() { server := http.Server{ Addr: fmt.Sprintf("%s:%s", stateCLI.listeningAddrFlag, stateCLI.listeningPortFlag), Handler: commonHandler, - ReadTimeout: 10 * time.Second, - WriteTimeout: 10 * time.Second, + ReadTimeout: 40 * time.Second, + WriteTimeout: 40 * time.Second, } // Start node health checking and current block fetching diff --git a/nodes/node_balancer/dev.sh b/nodes/node_balancer/dev.sh index eef8dc96..0f983d75 100755 --- a/nodes/node_balancer/dev.sh +++ b/nodes/node_balancer/dev.sh @@ -1,5 +1,8 @@ #!/usr/bin/env sh +# Colpile application and run with provided arguments +set -e + PROGRAM_NAME="nodebalancer" go build -o "$PROGRAM_NAME" . From 1406d85e0575fa3c59409df2319bdd127c1e99c6 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 21 Mar 2022 10:30:04 +0000 Subject: [PATCH 17/23] allowed methods work as map --- nodes/node_balancer/cmd/blockchain.go | 76 ++++++++++++--------------- nodes/node_balancer/cmd/routes.go | 4 +- 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/nodes/node_balancer/cmd/blockchain.go b/nodes/node_balancer/cmd/blockchain.go index 48f8782c..a1214b9d 100644 --- a/nodes/node_balancer/cmd/blockchain.go +++ b/nodes/node_balancer/cmd/blockchain.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "log" "strconv" @@ -11,41 +10,41 @@ import ( var ( nodeConfigs NodeConfigs - ALLOWED_METHODS = []string{ - "eth_blockNumber", - "eth_call", - "eth_chainId", - "eth_estimateGas", - "eth_feeHistory", - "eth_gasPrice", - "eth_getBalance", - "eth_getBlockByHash", - "eth_getBlockByNumber", - "eth_getBlockTransactionCountByHash", - "eth_getBlockTransactionCountByNumber", - "eth_getCode", - "eth_getLogs", - "eth_getStorageAt", - "eth_getTransactionByHash", - "eth_getTransactionByBlockHashAndIndex", - "eth_getTransactionByBlockNumberAndIndex", - "eth_getTransactionCount", - "eth_getTransactionReceipt", - "eth_getUncleByBlockHashAndIndex", - "eth_getUncleByBlockNumberAndIndex", - "eth_getUncleCountByBlockHash", - "eth_getUncleCountByBlockNumber", - "eth_getWork", - "eth_mining", - // "eth_sendRawTransaction", - "eth_protocolVersion", - "eth_syncing", + ALLOWED_METHODS = map[string]bool{ + "eth_blockNumber": true, + "eth_call": true, + "eth_chainId": true, + "eth_estimateGas": true, + "eth_feeHistory": true, + "eth_gasPrice": true, + "eth_getBalance": true, + "eth_getBlockByHash": true, + "eth_getBlockByNumber": true, + "eth_getBlockTransactionCountByHash": true, + "eth_getBlockTransactionCountByNumber": true, + "eth_getCode": true, + "eth_getLogs": true, + "eth_getStorageAt": true, + "eth_getTransactionByHash": true, + "eth_getTransactionByBlockHashAndIndex": true, + "eth_getTransactionByBlockNumberAndIndex": true, + "eth_getTransactionCount": true, + "eth_getTransactionReceipt": true, + "eth_getUncleByBlockHashAndIndex": true, + "eth_getUncleByBlockNumberAndIndex": true, + "eth_getUncleCountByBlockHash": true, + "eth_getUncleCountByBlockNumber": true, + "eth_getWork": true, + "eth_mining": true, + // "eth_sendRawTransaction": true, + "eth_protocolVersion": true, + "eth_syncing": true, - "net_listening", - "net_peerCount", - "net_version", + "net_listening": true, + "net_peerCount": true, + "net_version": true, - "web3_clientVersion", + "web3_clientVersion": true, } ) @@ -73,15 +72,6 @@ type NodeConfigs struct { NodeConfigs []NodeConfig } -func verifyMethodWhitelisted(method string) error { - for _, m := range ALLOWED_METHODS { - if method == m { - return nil - } - } - return fmt.Errorf("Method not allowed") -} - // Return list of NodeConfig structures func (nc *NodeConfigs) InitNodeConfiguration() { // Define available blockchain nodes diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index 430d50ca..ee50b951 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -106,8 +106,8 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, return } if currentUserAccess.ExtendedMethods == false { - err = verifyMethodWhitelisted(jsonrpcRequest.Method) - if err != nil { + _, exists := ALLOWED_METHODS[jsonrpcRequest.Method] + if !exists { http.Error(w, "Method for provided access id not allowed", http.StatusForbidden) return } From b0623f738c7c623e7c8f6f32e83444ea1c2f1b35 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 21 Mar 2022 10:32:33 +0000 Subject: [PATCH 18/23] modified auth exception message to access message --- nodes/node_balancer/cmd/middleware.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 2bff690f..73d2c862 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -130,7 +130,7 @@ func accessMiddleware(next http.Handler) http.Handler { dataSource := extractDataSource(r) if accessID == "" { - http.Error(w, "No authorization header passed with request", http.StatusForbidden) + http.Error(w, "No access id passed with request", http.StatusForbidden) return } From aa0795469a22ba894bc4ce77c81356ab1aa35171 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 21 Mar 2022 20:36:12 +0000 Subject: [PATCH 19/23] work with bugout-go --- nodes/node_balancer/cmd/bugout.go | 241 -------------------------- nodes/node_balancer/cmd/cli.go | 103 +++++++++-- nodes/node_balancer/cmd/middleware.go | 18 +- nodes/node_balancer/cmd/server.go | 23 ++- 4 files changed, 127 insertions(+), 258 deletions(-) delete mode 100644 nodes/node_balancer/cmd/bugout.go diff --git a/nodes/node_balancer/cmd/bugout.go b/nodes/node_balancer/cmd/bugout.go deleted file mode 100644 index a5fd9c84..00000000 --- a/nodes/node_balancer/cmd/bugout.go +++ /dev/null @@ -1,241 +0,0 @@ -package cmd - -import ( - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" -) - -var ( - bugoutClient BugoutClient -) - -type BugoutClient struct { - Client http.Client - AuthURL string -} - -type PingResponse struct { - Status string `json:"status"` -} - -// Bugout responses -type BugoutUserResponse struct { - ID string `json:"user_id"` - ApplicationID string `json:"application_id"` -} - -type UserAccess struct { - UserID string `json:"user_id"` - AccessID string `json:"access_id"` - Name string `json:"name"` - Description string `json:"description"` - BlockchainAccess bool `json:"blockchain_access"` - ExtendedMethods bool `json:"extended_methods"` - - dataSource string -} - -type BugoutResourceResponse struct { - ID string `json:"id"` - ResourceData UserAccess `json:"resource_data"` -} - -type BugoutResourcesResponse struct { - Resources []BugoutResourceResponse `json:"resources"` -} - -// Initialize Bugout http client -func InitBugoutClient() { - client := http.Client{Timeout: configs.BUGOUT_AUTH_CALL_TIMEOUT} - bugoutClient = BugoutClient{ - Client: client, - AuthURL: configs.BUGOUT_AUTH_URL, - } -} - -// Get Bugout user -func (bc *BugoutClient) GetUser(token string) (BugoutUserResponse, error) { - var userResponse BugoutUserResponse - - url := fmt.Sprintf("%s/user", configs.BUGOUT_AUTH_URL) - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return userResponse, err - } - - req.Header = http.Header{ - "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, - } - resp, err := bc.Client.Do(req) - if err != nil { - return userResponse, err - } - defer resp.Body.Close() - - // Parse response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return userResponse, err - } - err = json.Unmarshal(body, &userResponse) - if err != nil { - return userResponse, err - } - - return userResponse, nil -} - -// Find Bugout user -func (bc *BugoutClient) FindUser(token, userID string) (BugoutUserResponse, error) { - var userResponse BugoutUserResponse - - url := fmt.Sprintf("%s/user/find?user_id=%s", configs.BUGOUT_AUTH_URL, userID) - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return userResponse, err - } - - req.Header = http.Header{ - "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, - } - resp, err := bc.Client.Do(req) - if err != nil { - return userResponse, err - } - defer resp.Body.Close() - - // Parse response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return userResponse, err - } - err = json.Unmarshal(body, &userResponse) - if err != nil { - return userResponse, err - } - - return userResponse, nil -} - -func (bc *BugoutClient) AddUserAccess(token string, proposedUserAccess UserAccess) (UserAccess, error) { - var userAccess UserAccess - - // Check user exists - user, err := bc.FindUser(token, proposedUserAccess.UserID) - if err != nil { - return userAccess, err - } - if user == (BugoutUserResponse{}) { - return userAccess, fmt.Errorf("User with id %s not found", proposedUserAccess.UserID) - } - - resource := map[string]interface{}{ - "application_id": configs.NB_APPLICATION_ID, - "resource_data": proposedUserAccess, - } - resourceJson, err := json.Marshal(resource) - if err != nil { - return userAccess, err - } - url := fmt.Sprintf("%s/resources", configs.BUGOUT_AUTH_URL) - req, err := http.NewRequest("POST", url, bytes.NewBuffer(resourceJson)) - if err != nil { - return userAccess, err - } - req.Header = http.Header{ - "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, - "Content-Type": []string{"application/json"}, - } - resp, err := bc.Client.Do(req) - if err != nil { - return userAccess, err - } - defer resp.Body.Close() - - // Parse response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return userAccess, err - } - var resourceResponse BugoutResourceResponse - err = json.Unmarshal(body, &resourceResponse) - if err != nil { - return userAccess, err - } - - userAccess = resourceResponse.ResourceData - - return userAccess, nil -} - -// Get Bugout resource -func (bc *BugoutClient) GetResources(token, userID, accessID string) (BugoutResourcesResponse, error) { - var resourcesResponse BugoutResourcesResponse - - url := fmt.Sprintf("%s/resources?application_id=%s", configs.BUGOUT_AUTH_URL, configs.NB_APPLICATION_ID) - if userID != "" { - url += fmt.Sprintf("&user_id=%s", userID) - } - if accessID != "" { - url += fmt.Sprintf("&access_id=%s", accessID) - } - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return resourcesResponse, err - } - req.Header = http.Header{ - "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, - } - resp, err := bc.Client.Do(req) - if err != nil { - return resourcesResponse, err - } - defer resp.Body.Close() - - // Parse response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return resourcesResponse, err - } - err = json.Unmarshal(body, &resourcesResponse) - if err != nil { - return resourcesResponse, err - } - - return resourcesResponse, nil -} - -func (bc *BugoutClient) DeleteResource(token, resourceID string) (BugoutResourceResponse, error) { - var resourceResponse BugoutResourceResponse - - url := fmt.Sprintf("%s/resources/%s", configs.BUGOUT_AUTH_URL, resourceID) - req, err := http.NewRequest("DELETE", url, nil) - if err != nil { - return resourceResponse, err - } - req.Header = http.Header{ - "Authorization": []string{fmt.Sprintf("Bearer %s", token)}, - } - resp, err := bc.Client.Do(req) - if err != nil { - return resourceResponse, err - } - defer resp.Body.Close() - - // Parse response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return resourceResponse, err - } - err = json.Unmarshal(body, &resourceResponse) - if err != nil { - return resourceResponse, err - } - - return resourceResponse, nil -} diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index 2130fcd1..207d7a54 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -6,6 +6,7 @@ import ( "fmt" "os" + bugout "github.com/bugout-dev/bugout-go/pkg" "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" "github.com/google/uuid" @@ -13,6 +14,8 @@ import ( var ( stateCLI StateCLI + + bugoutClient bugout.BugoutClient ) // Command Line Interface state @@ -45,6 +48,21 @@ type StateCLI struct { offsetFlag int } +type PingResponse struct { + Status string `json:"status"` +} + +type UserAccess struct { + UserID string `json:"user_id"` + AccessID string `json:"access_id"` + Name string `json:"name"` + Description string `json:"description"` + BlockchainAccess bool `json:"blockchain_access"` + ExtendedMethods bool `json:"extended_methods"` + + dataSource string +} + func (s *StateCLI) usage() { fmt.Printf(`usage: nodebalancer [-h] {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s} ... @@ -174,23 +192,45 @@ func CLI() { BlockchainAccess: stateCLI.blockchainAccessFlag, ExtendedMethods: stateCLI.extendedMethodsFlag, } - userAccess, err := bugoutClient.AddUserAccess(configs.NB_CONTROLLER_TOKEN, proposedUserAccess) + _, err := bugoutClient.Brood.FindUser( + configs.NB_CONTROLLER_TOKEN, + map[string]string{ + "user_id": proposedUserAccess.UserID, + "application_id": configs.NB_APPLICATION_ID, + }, + ) + if err != nil { + fmt.Printf("User does not exists %v\n", err) + os.Exit(1) + } + resource, err := bugoutClient.Brood.CreateResource(configs.NB_CONTROLLER_TOKEN, configs.NB_APPLICATION_ID, proposedUserAccess) if err != nil { fmt.Printf("Unable to create user access %v\n", err) os.Exit(1) } - userAccessJson, err := json.Marshal(userAccess) + resource_data, err := json.Marshal(resource.ResourceData) if err != nil { - fmt.Printf("Unable to marshal user access struct %v\n", err) + fmt.Printf("Unable to encode resource %s data interface to json %v", resource.Id, err) os.Exit(1) } - fmt.Println(string(userAccessJson)) + fmt.Println(string(resource_data)) case "delete-access": stateCLI.deleteAccessCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() - resources, err := bugoutClient.GetResources(configs.NB_CONTROLLER_TOKEN, stateCLI.userIDFlag, stateCLI.accessIDFlag) + queryParameters := make(map[string]string) + if stateCLI.userIDFlag != "" { + queryParameters["user_id"] = stateCLI.userIDFlag + } + if stateCLI.accessIDFlag != "" { + queryParameters["access_id"] = stateCLI.accessIDFlag + } + resources, err := bugoutClient.Brood.GetResources( + configs.NB_CONTROLLER_TOKEN, + configs.NB_APPLICATION_ID, + queryParameters, + ) if err != nil { fmt.Printf("Unable to get Bugout resources %v\n", err) os.Exit(1) @@ -198,12 +238,23 @@ func CLI() { var userAccesses []UserAccess for _, resource := range resources.Resources { - deletedResource, err := bugoutClient.DeleteResource(configs.NB_CONTROLLER_TOKEN, resource.ID) + deletedResource, err := bugoutClient.Brood.DeleteResource(configs.NB_CONTROLLER_TOKEN, resource.Id) if err != nil { - fmt.Printf("Unable to delete resource with id %s %v\n", resource.ID, err) + fmt.Printf("Unable to delete resource %s %v\n", resource.Id, err) continue } - userAccesses = append(userAccesses, deletedResource.ResourceData) + resource_data, err := json.Marshal(deletedResource.ResourceData) + if err != nil { + fmt.Printf("Unable to encode resource %s data interface to json %v", resource.Id, err) + continue + } + var userAccess UserAccess + err = json.Unmarshal(resource_data, &userAccess) + if err != nil { + fmt.Printf("Unable to decode resource %s data json to structure %v", resource.Id, err) + continue + } + userAccesses = append(userAccesses, userAccess) } userAccessesJson, err := json.Marshal(userAccesses) @@ -223,7 +274,18 @@ func CLI() { stateCLI.usersCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() - resources, err := bugoutClient.GetResources(configs.NB_CONTROLLER_TOKEN, stateCLI.userIDFlag, stateCLI.accessIDFlag) + var queryParameters map[string]string + if stateCLI.userIDFlag != "" { + queryParameters["user_id"] = stateCLI.userIDFlag + } + if stateCLI.accessIDFlag != "" { + queryParameters["access_id"] = stateCLI.accessIDFlag + } + resources, err := bugoutClient.Brood.GetResources( + configs.NB_CONTROLLER_TOKEN, + configs.NB_APPLICATION_ID, + queryParameters, + ) if err != nil { fmt.Printf("Unable to get Bugout resources %v\n", err) os.Exit(1) @@ -240,8 +302,19 @@ func CLI() { limit = len(resources.Resources[offset:]) + offset } - for _, resourceData := range resources.Resources[offset:limit] { - userAccesses = append(userAccesses, resourceData.ResourceData) + for _, resource := range resources.Resources[offset:limit] { + resource_data, err := json.Marshal(resource.ResourceData) + if err != nil { + fmt.Printf("Unable to encode resource %s data interface to json %v", resource.Id, err) + continue + } + var userAccess UserAccess + err = json.Unmarshal(resource_data, &userAccess) + if err != nil { + fmt.Printf("Unable to decode resource %s data json to structure %v", resource.Id, err) + continue + } + userAccesses = append(userAccesses, userAccess) } userAccessesJson, err := json.Marshal(userAccesses) if err != nil { @@ -265,5 +338,11 @@ func CLI() { func init() { configs.VerifyEnvironments() - InitBugoutClient() + // Init bugout client + bc, err := bugout.ClientFromEnv() + if err != nil { + fmt.Printf("Unable to initialize bugout client %v", err) + os.Exit(1) + } + bugoutClient = bc } diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 73d2c862..b2dcdc67 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -139,7 +139,11 @@ func accessMiddleware(next http.Handler) http.Handler { currentUserAccess = controllerUserAccess currentUserAccess.dataSource = dataSource } else { - resources, err := bugoutClient.GetResources(configs.NB_CONTROLLER_TOKEN, "", accessID) + resources, err := bugoutClient.Brood.GetResources( + configs.NB_CONTROLLER_TOKEN, + configs.NB_APPLICATION_ID, + map[string]string{"access_id": accessID}, + ) if err != nil { http.Error(w, "Unable to get user with provided access identifier", http.StatusForbidden) return @@ -148,7 +152,17 @@ func accessMiddleware(next http.Handler) http.Handler { http.Error(w, "User with provided access identifier not found", http.StatusForbidden) return } - userAccess := resources.Resources[0].ResourceData + resource_data, err := json.Marshal(resources.Resources[0].ResourceData) + if err != nil { + http.Error(w, "Unable to encode resource data interface to json", http.StatusInternalServerError) + return + } + var userAccess UserAccess + err = json.Unmarshal(resource_data, &userAccess) + if err != nil { + http.Error(w, "Unable to decode resource data json to structure", http.StatusInternalServerError) + return + } currentUserAccess = UserAccess{ UserID: userAccess.UserID, AccessID: userAccess.AccessID, diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 5e44ee1f..991dda5a 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -5,6 +5,7 @@ package cmd import ( "context" + "encoding/json" "fmt" "log" "net/http" @@ -111,11 +112,27 @@ func Server() { // Record system information reporter.Publish(humbug.SystemReport()) - resources, err := bugoutClient.GetResources(configs.NB_CONTROLLER_TOKEN, "", configs.NB_CONTROLLER_ACCESS_ID) + // TODO(kompotkot): Remove, make it work without brood for internal crawlers + resources, err := bugoutClient.Brood.GetResources( + configs.NB_CONTROLLER_TOKEN, + configs.NB_APPLICATION_ID, + map[string]string{"access_id": configs.NB_CONTROLLER_ACCESS_ID}, + ) if err != nil { - fmt.Printf("Unable to access Bugout authentication server %v", err) + fmt.Printf("Unable to get user with provided access identifier %v", err) + } + if len(resources.Resources) == 0 { + fmt.Printf("User with provided access identifier not found %v", err) + } + resource_data, err := json.Marshal(resources.Resources[0].ResourceData) + if err != nil { + fmt.Printf("Unable to encode resource data interface to json %v", err) + } + var userAccess UserAccess + err = json.Unmarshal(resource_data, &userAccess) + if err != nil { + fmt.Printf("Unable to decode resource data json to structure %v", err) } - userAccess := resources.Resources[0].ResourceData controllerUserAccess = UserAccess{ UserID: userAccess.UserID, AccessID: userAccess.AccessID, From a68cc186dc5bd2f15aa0f6e7763d1820bd8b8f5e Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 16 May 2022 19:54:22 +0000 Subject: [PATCH 20/23] Fixed errors after merge --- nodes/node_balancer/cmd/blockchain.go | 40 ++-- nodes/node_balancer/cmd/cli.go | 56 ++--- nodes/node_balancer/cmd/server.go | 5 +- nodes/node_balancer/configs/settings.go | 53 +---- nodes/node_balancer/go.mod | 1 + nodes/node_balancer/go.sum | 285 ++++++++++++++++++++++++ 6 files changed, 341 insertions(+), 99 deletions(-) diff --git a/nodes/node_balancer/cmd/blockchain.go b/nodes/node_balancer/cmd/blockchain.go index a1214b9d..f64f3e7b 100644 --- a/nodes/node_balancer/cmd/blockchain.go +++ b/nodes/node_balancer/cmd/blockchain.go @@ -1,8 +1,10 @@ package cmd import ( + "io/ioutil" "log" "strconv" + "strings" configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) @@ -73,31 +75,29 @@ type NodeConfigs struct { } // Return list of NodeConfig structures -func (nc *NodeConfigs) InitNodeConfiguration() { - // Define available blockchain nodes - blockchainConfigList := make([]BlockchainConfig, 0, 2) - blockchainConfigList = append(blockchainConfigList, BlockchainConfig{ - Blockchain: "ethereum", - IPs: []string{configs.MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR, configs.MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR}, - Port: "8545", - }) - blockchainConfigList = append(blockchainConfigList, BlockchainConfig{ - Blockchain: "polygon", - IPs: []string{configs.MOONSTREAM_NODE_POLYGON_A_IPC_ADDR, configs.MOONSTREAM_NODE_POLYGON_B_IPC_ADDR}, - Port: "8545", - }) +func (nc *NodeConfigs) InitNodeConfigList(configPath string) { + configs.CheckEnvVarSet() - // Parse node addr, ip and blockchain - for _, b := range blockchainConfigList { - for _, nodeIP := range b.IPs { - port, err := strconv.ParseInt(b.Port, 0, 16) + rawBytes, err := ioutil.ReadFile(configPath) + if err != nil { + log.Fatalf("Unable to read config file, %v", err) + } + text := string(rawBytes) + lines := strings.Split(text, "\n") + + // Define available blockchain nodes + for _, line := range lines { + fields := strings.Split(line, ",") + if len(fields) == 3 { + port, err := strconv.ParseInt(fields[2], 0, 16) if err != nil { - log.Printf("Unable to parse port number: %s", b.Port) + log.Printf("Unable to parse port number, %v", err) continue } + nc.NodeConfigs = append(nc.NodeConfigs, NodeConfig{ - Blockchain: b.Blockchain, - Addr: nodeIP, + Blockchain: fields[0], + Addr: fields[1], Port: uint16(port), }) } diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index b0f04358..f03dd7e7 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -1,12 +1,16 @@ package cmd import ( + "encoding/json" "flag" "fmt" "log" "os" "strings" + bugout "github.com/bugout-dev/bugout-go/pkg" + "github.com/google/uuid" + "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) @@ -37,7 +41,7 @@ type StateCLI struct { // Common flags configPathFlag string - helpFlag bool + helpFlag bool // Add user access flags userIDFlag string @@ -48,8 +52,8 @@ type StateCLI struct { extendedMethodsFlag bool // Server flags - listeningAddrFlag string - listeningPortFlag string + listeningAddrFlag string + listeningPortFlag string enableHealthCheckFlag bool enableDebugFlag bool @@ -73,6 +77,7 @@ type UserAccess struct { dataSource string } + func (s *StateCLI) usage() { fmt.Printf(`usage: nodebalancer [-h] {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s} ... @@ -141,30 +146,31 @@ func (s *StateCLI) checkRequirements() { s.usersCmd.PrintDefaults() os.Exit(1) } - if s.configPathFlag == "" { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatalf("Unable to find user home directory, %v", err) - } - - configDirPath := fmt.Sprintf("%s/.nodebalancer", homeDir) - configPath := fmt.Sprintf("%s/config.txt", configDirPath) - - err = os.MkdirAll(configDirPath, os.ModePerm) - if err != nil { - log.Fatalf("Unable to create directory, %v", err) - } - - _, err = os.Stat(configPath) - if err != nil { - tempConfigB := []byte("ethereum,http://127.0.0.1,8545") - err = os.WriteFile(configPath, tempConfigB, 0644) + if s.configPathFlag == "" { + homeDir, err := os.UserHomeDir() if err != nil { - log.Fatalf("Unable to write config, %v", err) + log.Fatalf("Unable to find user home directory, %v", err) } - } - s.configPathFlag = configPath + configDirPath := fmt.Sprintf("%s/.nodebalancer", homeDir) + configPath := fmt.Sprintf("%s/config.txt", configDirPath) + + err = os.MkdirAll(configDirPath, os.ModePerm) + if err != nil { + log.Fatalf("Unable to create directory, %v", err) + } + + _, err = os.Stat(configPath) + if err != nil { + tempConfigB := []byte("ethereum,http://127.0.0.1,8545") + err = os.WriteFile(configPath, tempConfigB, 0644) + if err != nil { + log.Fatalf("Unable to write config, %v", err) + } + } + + s.configPathFlag = configPath + } } } @@ -370,7 +376,7 @@ func CLI() { } func init() { - configs.VerifyEnvironments() + configs.CheckEnvVarSet() // Init bugout client bc, err := bugout.ClientFromEnv() diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index e2d02ab9..9a7b17f1 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -13,8 +13,9 @@ import ( "net/url" "time" - humbug "github.com/bugout-dev/humbug/go/pkg" configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" + + humbug "github.com/bugout-dev/humbug/go/pkg" "github.com/google/uuid" ) @@ -149,7 +150,7 @@ func Server() { } // Fill NodeConfigList with initial nodes from environment variables - configs.ConfigList.InitNodeConfigList(stateCLI.configPathFlag) + nodeConfigs.InitNodeConfigList(stateCLI.configPathFlag) // Parse nodes and set list of proxies for i, nodeConfig := range nodeConfigs.NodeConfigs { diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/configs/settings.go index 02ddfb93..9851bb80 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/configs/settings.go @@ -4,11 +4,8 @@ Configurations for load balancer server. package configs import ( - "io/ioutil" "log" "os" - "strconv" - "strings" "time" ) @@ -40,28 +37,10 @@ var ( MOONSTREAM_DB_CONN_MAX_LIFETIME = 30 * time.Minute ) -type BlockchainConfig struct { - Blockchain string - IPs []string - Port string -} - -type NodeConfig struct { - Blockchain string - Addr string - Port uint16 -} - -type NodeConfigList struct { - Configs []NodeConfig -} - -var ConfigList NodeConfigList - var MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT") var MOONSTREAM_CLIENT_ID_HEADER = os.Getenv("MOONSTREAM_CLIENT_ID_HEADER") -func checkEnvVarSet() { +func CheckEnvVarSet() { if MOONSTREAM_CLIENT_ID_HEADER == "" { MOONSTREAM_CLIENT_ID_HEADER = "x-moonstream-client-id" } @@ -70,33 +49,3 @@ func checkEnvVarSet() { log.Fatal("Environment variable MOONSTREAM_NODES_SERVER_PORT not set") } } - -// Return list of NodeConfig structures -func (nc *NodeConfigList) InitNodeConfigList(configPath string) { - checkEnvVarSet() - - rawBytes, err := ioutil.ReadFile(configPath) - if err != nil { - log.Fatalf("Unable to read config file, %v", err) - } - text := string(rawBytes) - lines := strings.Split(text, "\n") - - // Define available blockchain nodes - for _, line := range lines { - fields := strings.Split(line, ",") - if len(fields) == 3 { - port, err := strconv.ParseInt(fields[2], 0, 16) - if err != nil { - log.Printf("Unable to parse port number, %v", err) - continue - } - - nc.Configs = append(nc.Configs, NodeConfig{ - Blockchain: fields[0], - Addr: fields[1], - Port: uint16(port), - }) - } - } -} diff --git a/nodes/node_balancer/go.mod b/nodes/node_balancer/go.mod index ca2e9d9d..a2e51e1f 100644 --- a/nodes/node_balancer/go.mod +++ b/nodes/node_balancer/go.mod @@ -3,6 +3,7 @@ module github.com/bugout-dev/moonstream/nodes/node_balancer go 1.17 require ( + github.com/bugout-dev/bugout-go v0.3.4 github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205 github.com/google/uuid v1.3.0 github.com/lib/pq v1.10.4 diff --git a/nodes/node_balancer/go.sum b/nodes/node_balancer/go.sum index aa0c0342..0fdb81fe 100644 --- a/nodes/node_balancer/go.sum +++ b/nodes/node_balancer/go.sum @@ -1,6 +1,291 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= +cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= +cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY= +cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= +cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= +cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= +cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= +cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk= +cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= +cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= +dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= +github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= +github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= +github.com/bugout-dev/bugout-go v0.3.4 h1:UJVaXv7ACcChoYIl0Zx38axV65s2vLH2kWZ76H/YK2s= +github.com/bugout-dev/bugout-go v0.3.4/go.mod h1:P4+788iHtt/32u2wIaRTaiXTWpvSVBYxZ01qQ8N7eB8= github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205 h1:UQ7XGjvoOVKGRIuTFXgqGtU/UgMOk8+ikpoHWrWefjQ= github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205/go.mod h1:U/NXHfc3tzGeQz+xVfpifXdPZi7p6VV8xdP/4ZKeWJU= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= +github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= +github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= +github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= +github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= +github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU= +github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= +github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= +github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= +github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= +github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk= github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= +github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= +github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= +github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= +go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= +golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= +golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= +golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= +google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= +google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= +google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= +google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= +gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= From e760416d3f775c3a59290f101d506b2ba1120e2a Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 17 May 2022 12:36:08 +0000 Subject: [PATCH 21/23] Working example with explicit check of internal crawlers access id --- nodes/node_balancer/cmd/cli.go | 66 ++++++++----------------- nodes/node_balancer/cmd/middleware.go | 8 +-- nodes/node_balancer/cmd/routes.go | 4 ++ nodes/node_balancer/cmd/server.go | 39 ++++++++++----- nodes/node_balancer/configs/settings.go | 42 ++++++++++++++-- nodes/node_balancer/sample.env | 10 ---- 6 files changed, 93 insertions(+), 76 deletions(-) diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index f03dd7e7..995d6dcb 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -4,7 +4,6 @@ import ( "encoding/json" "flag" "fmt" - "log" "os" "strings" @@ -15,6 +14,7 @@ import ( ) var ( + // Storing CLI definitions at server startup stateCLI StateCLI bugoutClient bugout.BugoutClient @@ -52,9 +52,8 @@ type StateCLI struct { extendedMethodsFlag bool // Server flags - listeningAddrFlag string - listeningPortFlag string - + listeningAddrFlag string + listeningPortFlag string enableHealthCheckFlag bool enableDebugFlag bool @@ -63,10 +62,6 @@ type StateCLI struct { offsetFlag int } -type PingResponse struct { - Status string `json:"status"` -} - type UserAccess struct { UserID string `json:"user_id"` AccessID string `json:"access_id"` @@ -90,6 +85,7 @@ subcommands: `, s.addAccessCmd.Name(), s.deleteAccessCmd.Name(), s.serverCmd.Name(), s.usersCmd.Name(), s.versionCmd.Name()) } +// Check if required flags are set func (s *StateCLI) checkRequirements() { if s.helpFlag { switch { @@ -146,31 +142,11 @@ func (s *StateCLI) checkRequirements() { s.usersCmd.PrintDefaults() os.Exit(1) } - if s.configPathFlag == "" { - homeDir, err := os.UserHomeDir() - if err != nil { - log.Fatalf("Unable to find user home directory, %v", err) - } + } - configDirPath := fmt.Sprintf("%s/.nodebalancer", homeDir) - configPath := fmt.Sprintf("%s/config.txt", configDirPath) - - err = os.MkdirAll(configDirPath, os.ModePerm) - if err != nil { - log.Fatalf("Unable to create directory, %v", err) - } - - _, err = os.Stat(configPath) - if err != nil { - tempConfigB := []byte("ethereum,http://127.0.0.1,8545") - err = os.WriteFile(configPath, tempConfigB, 0644) - if err != nil { - log.Fatalf("Unable to write config, %v", err) - } - } - - s.configPathFlag = configPath - } + if s.configPathFlag == "" { + configPath := configs.GenerateDefaultConfig() + s.configPathFlag = configPath } } @@ -197,13 +173,13 @@ func (s *StateCLI) populateCLI() { // Add user access subcommand flag pointers s.addAccessCmd.StringVar(&s.accessNameFlag, "name", "", "Name of access") s.addAccessCmd.StringVar(&s.accessDescriptionFlag, "description", "", "Description of access") - s.addAccessCmd.BoolVar(&s.blockchainAccessFlag, "blockchain-access", false, "Provide if allow to access blockchain nodes") + s.addAccessCmd.BoolVar(&s.blockchainAccessFlag, "blockchain-access", false, "Provide if allow direct access to blockchain nodes") s.addAccessCmd.BoolVar(&s.extendedMethodsFlag, "extended-methods", false, "Provide to be able to execute not whitelisted methods") // Server subcommand flag pointers s.serverCmd.StringVar(&s.listeningAddrFlag, "host", "127.0.0.1", "Server listening address") s.serverCmd.StringVar(&s.listeningPortFlag, "port", "8544", "Server listening port") - s.serverCmd.BoolVar(&s.enableHealthCheckFlag, "healthcheck", false, "To enable healthcheck ser healthcheck flag") + s.serverCmd.BoolVar(&s.enableHealthCheckFlag, "healthcheck", false, "To enable healthcheck set healthcheck flag") s.serverCmd.BoolVar(&s.enableDebugFlag, "debug", false, "To enable debug mode with extended log set debug flag") // Users list subcommand flag pointers @@ -218,6 +194,14 @@ func CLI() { os.Exit(1) } + // Init bugout client + bc, err := bugout.ClientFromEnv() + if err != nil { + fmt.Printf("Unable to initialize bugout client %v", err) + os.Exit(1) + } + bugoutClient = bc + // Parse subcommands and appropriate FlagSet switch os.Args[1] { case "add-access": @@ -308,6 +292,8 @@ func CLI() { stateCLI.serverCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() + configs.CheckEnvVarSet() + Server() case "users": @@ -374,15 +360,3 @@ func CLI() { os.Exit(1) } } - -func init() { - configs.CheckEnvVarSet() - - // Init bugout client - bc, err := bugout.ClientFromEnv() - if err != nil { - fmt.Printf("Unable to initialize bugout client %v", err) - os.Exit(1) - } - bugoutClient = bc -} diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index b2dcdc67..85febff0 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -23,7 +23,7 @@ import ( func extractAccessID(r *http.Request) string { var accessID string - accessIDHeaders := r.Header[configs.NB_ACCESS_ID_HEADER] + accessIDHeaders := r.Header[strings.Title(configs.NB_ACCESS_ID_HEADER)] for _, h := range accessIDHeaders { accessID = h } @@ -42,7 +42,7 @@ func extractAccessID(r *http.Request) string { func extractDataSource(r *http.Request) string { dataSource := "database" - dataSources := r.Header[configs.NB_DATA_SOURCE_HEADER] + dataSources := r.Header[strings.Title(configs.NB_DATA_SOURCE_HEADER)] for _, h := range dataSources { dataSource = h } @@ -134,9 +134,9 @@ func accessMiddleware(next http.Handler) http.Handler { return } - // If access id does not belong to controller, then find it in Bugout resources + // If access id does not belong to internal crawlers, then find it in Bugout resources if accessID == configs.NB_CONTROLLER_ACCESS_ID { - currentUserAccess = controllerUserAccess + currentUserAccess = internalCrawlersAccess currentUserAccess.dataSource = dataSource } else { resources, err := bugoutClient.Brood.GetResources( diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index f222305d..a300aa32 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -16,6 +16,10 @@ import ( configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" ) +type PingResponse struct { + Status string `json:"status"` +} + // pingRoute response with status of load balancer server itself func pingRoute(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 9a7b17f1..616fcf78 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -11,6 +11,7 @@ import ( "net/http" "net/http/httputil" "net/url" + "os" "time" configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs" @@ -20,7 +21,7 @@ import ( ) var ( - controllerUserAccess UserAccess + internalCrawlersAccess UserAccess // Crash reporter reporter *humbug.HumbugReporter @@ -109,12 +110,12 @@ func Server() { consent := humbug.CreateHumbugConsent(humbug.True) reporter, err = humbug.CreateHumbugReporter(consent, "moonstream-node-balancer", sessionID, configs.HUMBUG_REPORTER_NB_TOKEN) if err != nil { - panic(fmt.Sprintf("Invalid Humbug Crash configuration: %s", err.Error())) + fmt.Printf("Invalid Humbug Crash configuration: %v", err) + os.Exit(1) } // Record system information reporter.Publish(humbug.SystemReport()) - // TODO(kompotkot): Remove, make it work without brood for internal crawlers resources, err := bugoutClient.Brood.GetResources( configs.NB_CONTROLLER_TOKEN, configs.NB_APPLICATION_ID, @@ -122,20 +123,24 @@ func Server() { ) if err != nil { fmt.Printf("Unable to get user with provided access identifier %v", err) + os.Exit(1) } - if len(resources.Resources) == 0 { - fmt.Printf("User with provided access identifier not found %v", err) + if len(resources.Resources) != 1 { + fmt.Printf("User with provided access identifier has wrong number of resources %v", err) + os.Exit(1) } resource_data, err := json.Marshal(resources.Resources[0].ResourceData) if err != nil { fmt.Printf("Unable to encode resource data interface to json %v", err) + os.Exit(1) } var userAccess UserAccess err = json.Unmarshal(resource_data, &userAccess) if err != nil { fmt.Printf("Unable to decode resource data json to structure %v", err) + os.Exit(1) } - controllerUserAccess = UserAccess{ + internalCrawlersAccess = UserAccess{ UserID: userAccess.UserID, AccessID: userAccess.AccessID, Name: userAccess.Name, @@ -143,10 +148,16 @@ func Server() { BlockchainAccess: userAccess.BlockchainAccess, ExtendedMethods: userAccess.ExtendedMethods, } + log.Printf( + "Internal crawlers access set, resource id: %s, blockchain access: %t, extended methods: %t", + resources.Resources[0].Id, userAccess.BlockchainAccess, userAccess.ExtendedMethods, + ) err = InitDatabaseClient() if err != nil { - fmt.Printf("Unable to initialize database connection %v", err) + log.Printf("Unable to initialize database connection %v\n", err) + } else { + log.Printf("Connection with database established\n") } // Fill NodeConfigList with initial nodes from environment variables @@ -156,11 +167,13 @@ func Server() { for i, nodeConfig := range nodeConfigs.NodeConfigs { gethUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", nodeConfig.Addr, nodeConfig.Port)) if err != nil { - log.Fatal(err) + fmt.Printf("Unable to parse gethUrl with addr: %s and port: %d\n", nodeConfig.Addr, nodeConfig.Port) + continue } statusUrl, err := url.Parse(fmt.Sprintf("http://%s:%s", nodeConfig.Addr, configs.MOONSTREAM_NODES_SERVER_PORT)) if err != nil { - log.Fatal(err) + fmt.Printf("Unable to parse statusUrl with addr: %s and port: %s\n", nodeConfig.Addr, configs.MOONSTREAM_NODES_SERVER_PORT) + continue } proxyToStatus := httputil.NewSingleHostReverseProxy(statusUrl) @@ -177,12 +190,13 @@ func Server() { GethReverseProxy: proxyToGeth, }, nodeConfig.Blockchain) log.Printf( - "Added new %s proxy %d with geth url: %s and status url: %s\n", + "Added new %s proxy blockchain under index %d from config file with geth url: %s and status url: %s\n", nodeConfig.Blockchain, i, gethUrl, statusUrl) } serveMux := http.NewServeMux() serveMux.Handle("/nb/", accessMiddleware(http.HandlerFunc(lbHandler))) + log.Println("Authentication middleware enabled") serveMux.HandleFunc("/ping", pingRoute) // Set common middlewares, from bottom to top @@ -202,9 +216,10 @@ func Server() { go initHealthCheck(stateCLI.enableDebugFlag) } - log.Printf("Starting server at %s:%s\n", stateCLI.listeningAddrFlag, stateCLI.listeningPortFlag) + log.Printf("Starting node load balancer HTTP server at %s:%s\n", stateCLI.listeningAddrFlag, stateCLI.listeningPortFlag) err = server.ListenAndServe() if err != nil { - log.Fatal(err) + fmt.Printf("Failed to start server listener %v", err) + os.Exit(1) } } diff --git a/nodes/node_balancer/configs/settings.go b/nodes/node_balancer/configs/settings.go index 9851bb80..c66afd55 100644 --- a/nodes/node_balancer/configs/settings.go +++ b/nodes/node_balancer/configs/settings.go @@ -4,6 +4,7 @@ Configurations for load balancer server. package configs import ( + "fmt" "log" "os" "time" @@ -38,14 +39,47 @@ var ( ) var MOONSTREAM_NODES_SERVER_PORT = os.Getenv("MOONSTREAM_NODES_SERVER_PORT") -var MOONSTREAM_CLIENT_ID_HEADER = os.Getenv("MOONSTREAM_CLIENT_ID_HEADER") func CheckEnvVarSet() { - if MOONSTREAM_CLIENT_ID_HEADER == "" { - MOONSTREAM_CLIENT_ID_HEADER = "x-moonstream-client-id" + if NB_ACCESS_ID_HEADER == "" { + NB_ACCESS_ID_HEADER = "x-node-balancer-access-id" + } + if NB_DATA_SOURCE_HEADER == "" { + NB_DATA_SOURCE_HEADER = "x-node-balancer-data-source" } if MOONSTREAM_NODES_SERVER_PORT == "" { - log.Fatal("Environment variable MOONSTREAM_NODES_SERVER_PORT not set") + fmt.Println("Environment variable MOONSTREAM_NODES_SERVER_PORT not set") + os.Exit(1) } } + +func GenerateDefaultConfig() string { + homeDir, err := os.UserHomeDir() + if err != nil { + fmt.Printf("Unable to find user home directory, %v", err) + os.Exit(1) + } + + configDirPath := fmt.Sprintf("%s/.nodebalancer", homeDir) + configPath := fmt.Sprintf("%s/config.txt", configDirPath) + + err = os.MkdirAll(configDirPath, os.ModePerm) + if err != nil { + fmt.Printf("Unable to create directory, %v", err) + os.Exit(1) + } + + _, err = os.Stat(configPath) + if err != nil { + tempConfigB := []byte("ethereum,127.0.0.1,8545") + err = os.WriteFile(configPath, tempConfigB, 0644) + if err != nil { + fmt.Printf("Unable to create directory, %v", err) + os.Exit(1) + } + log.Printf("Config directory were not found, created default configuration at %s", configPath) + } + + return configPath +} diff --git a/nodes/node_balancer/sample.env b/nodes/node_balancer/sample.env index 0cdd071a..ce99f7ca 100644 --- a/nodes/node_balancer/sample.env +++ b/nodes/node_balancer/sample.env @@ -3,18 +3,8 @@ export BUGOUT_AUTH_URL="https://auth.bugout.dev" export NB_APPLICATION_ID="" export NB_CONTROLLER_TOKEN="" export NB_CONTROLLER_ACCESS_ID="" - -# Database variables export MOONSTREAM_DB_URI="postgresql://:@:/" - -# Nodes export MOONSTREAM_NODES_SERVER_PORT="" -# Ethereum nodes depends variables -export MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR="127.0.0.1" -export MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR="127.0.0.2" -# Polygon nodes depends variables -export MOONSTREAM_NODE_POLYGON_A_IPC_ADDR="127.0.0.1" -export MOONSTREAM_NODE_POLYGON_B_IPC_ADDR="127.0.0.2" # Error humbug reporter export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="" From 30e1630df213d09d7d5d0283568ec5608eb32208 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 17 May 2022 13:30:28 +0000 Subject: [PATCH 22/23] Client structure cleaning --- nodes/node_balancer/cmd/cli.go | 21 +++++---------------- nodes/node_balancer/cmd/clients.go | 20 ++++++++++++++++---- nodes/node_balancer/cmd/middleware.go | 26 +++++++++++++------------- nodes/node_balancer/cmd/routes.go | 27 ++++++++++++++++----------- nodes/node_balancer/cmd/server.go | 25 ++++++++++++++----------- 5 files changed, 64 insertions(+), 55 deletions(-) diff --git a/nodes/node_balancer/cmd/cli.go b/nodes/node_balancer/cmd/cli.go index 995d6dcb..878f28e8 100644 --- a/nodes/node_balancer/cmd/cli.go +++ b/nodes/node_balancer/cmd/cli.go @@ -62,17 +62,6 @@ type StateCLI struct { offsetFlag int } -type UserAccess struct { - UserID string `json:"user_id"` - AccessID string `json:"access_id"` - Name string `json:"name"` - Description string `json:"description"` - BlockchainAccess bool `json:"blockchain_access"` - ExtendedMethods bool `json:"extended_methods"` - - dataSource string -} - func (s *StateCLI) usage() { fmt.Printf(`usage: nodebalancer [-h] {%[1]s,%[2]s,%[3]s,%[4]s,%[5]s} ... @@ -208,7 +197,7 @@ func CLI() { stateCLI.addAccessCmd.Parse(os.Args[2:]) stateCLI.checkRequirements() - proposedUserAccess := UserAccess{ + proposedUserAccess := ClientResourceData{ UserID: stateCLI.userIDFlag, AccessID: stateCLI.accessIDFlag, Name: stateCLI.accessNameFlag, @@ -260,7 +249,7 @@ func CLI() { os.Exit(1) } - var userAccesses []UserAccess + var userAccesses []ClientResourceData for _, resource := range resources.Resources { deletedResource, err := bugoutClient.Brood.DeleteResource(configs.NB_CONTROLLER_TOKEN, resource.Id) if err != nil { @@ -272,7 +261,7 @@ func CLI() { fmt.Printf("Unable to encode resource %s data interface to json %v", resource.Id, err) continue } - var userAccess UserAccess + var userAccess ClientResourceData err = json.Unmarshal(resource_data, &userAccess) if err != nil { fmt.Printf("Unable to decode resource %s data json to structure %v", resource.Id, err) @@ -317,7 +306,7 @@ func CLI() { os.Exit(1) } - var userAccesses []UserAccess + var userAccesses []ClientResourceData offset := stateCLI.offsetFlag if stateCLI.offsetFlag > len(resources.Resources) { @@ -334,7 +323,7 @@ func CLI() { fmt.Printf("Unable to encode resource %s data interface to json %v", resource.Id, err) continue } - var userAccess UserAccess + var userAccess ClientResourceData err = json.Unmarshal(resource_data, &userAccess) if err != nil { fmt.Printf("Unable to decode resource %s data json to structure %v", resource.Id, err) diff --git a/nodes/node_balancer/cmd/clients.go b/nodes/node_balancer/cmd/clients.go index 5656dd0e..8765899b 100644 --- a/nodes/node_balancer/cmd/clients.go +++ b/nodes/node_balancer/cmd/clients.go @@ -15,6 +15,18 @@ var ( xdaiClientPool ClientPool ) +// Structure to define user access according with Brood resources +type ClientResourceData struct { + UserID string `json:"user_id"` + AccessID string `json:"access_id"` + Name string `json:"name"` + Description string `json:"description"` + BlockchainAccess bool `json:"blockchain_access"` + ExtendedMethods bool `json:"extended_methods"` + + dataSource string +} + // Node - which one node client worked with // LastCallTs - timestamp from last call type Client struct { @@ -24,18 +36,19 @@ type Client struct { mux sync.RWMutex } +// Where id is a key and equal to ClientResourceData -> AccessID type ClientPool struct { Client map[string]*Client } -// Generate client pools for different blockchains +// Generate pools for clients for different blockchains func CreateClientPools() { ethereumClientPool.Client = make(map[string]*Client) polygonClientPool.Client = make(map[string]*Client) xdaiClientPool.Client = make(map[string]*Client) } -// Return client pool correspongin to blockchain +// Return client pool corresponding to provided blockchain func GetClientPool(blockchain string) (*ClientPool, error) { var cpool *ClientPool if blockchain == "ethereum" { @@ -45,7 +58,7 @@ func GetClientPool(blockchain string) (*ClientPool, error) { } else if blockchain == "xdai" { cpool = &xdaiClientPool } else { - return nil, errors.New("Unexisting blockchain provided") + return nil, errors.New("Unsupported blockchain provided") } return cpool, nil } @@ -73,7 +86,6 @@ func (client *Client) GetClientLastCallDiff() (lastCallTs int64) { // Find clint with same ID and update timestamp or // add new one if doesn't exist func (cpool *ClientPool) AddClientNode(id string, node *Node) { - if cpool.Client[id] != nil { if reflect.DeepEqual(cpool.Client[id].Node, node) { cpool.Client[id].UpdateClientLastCall() diff --git a/nodes/node_balancer/cmd/middleware.go b/nodes/node_balancer/cmd/middleware.go index 85febff0..dffc7cb9 100644 --- a/nodes/node_balancer/cmd/middleware.go +++ b/nodes/node_balancer/cmd/middleware.go @@ -124,7 +124,7 @@ func logMiddleware(next http.Handler) http.Handler { // Check access id was provided correctly and save user access configuration to request context func accessMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var currentUserAccess UserAccess + var currentClientAccess ClientResourceData accessID := extractAccessID(r) dataSource := extractDataSource(r) @@ -136,8 +136,8 @@ func accessMiddleware(next http.Handler) http.Handler { // If access id does not belong to internal crawlers, then find it in Bugout resources if accessID == configs.NB_CONTROLLER_ACCESS_ID { - currentUserAccess = internalCrawlersAccess - currentUserAccess.dataSource = dataSource + currentClientAccess = internalCrawlersAccess + currentClientAccess.dataSource = dataSource } else { resources, err := bugoutClient.Brood.GetResources( configs.NB_CONTROLLER_TOKEN, @@ -157,25 +157,25 @@ func accessMiddleware(next http.Handler) http.Handler { http.Error(w, "Unable to encode resource data interface to json", http.StatusInternalServerError) return } - var userAccess UserAccess - err = json.Unmarshal(resource_data, &userAccess) + var clientResourceData ClientResourceData + err = json.Unmarshal(resource_data, &clientResourceData) if err != nil { http.Error(w, "Unable to decode resource data json to structure", http.StatusInternalServerError) return } - currentUserAccess = UserAccess{ - UserID: userAccess.UserID, - AccessID: userAccess.AccessID, - Name: userAccess.Name, - Description: userAccess.Description, - BlockchainAccess: userAccess.BlockchainAccess, - ExtendedMethods: userAccess.ExtendedMethods, + currentClientAccess = ClientResourceData{ + UserID: clientResourceData.UserID, + AccessID: clientResourceData.AccessID, + Name: clientResourceData.Name, + Description: clientResourceData.Description, + BlockchainAccess: clientResourceData.BlockchainAccess, + ExtendedMethods: clientResourceData.ExtendedMethods, dataSource: dataSource, } } - ctxUser := context.WithValue(r.Context(), "currentUserAccess", currentUserAccess) + ctxUser := context.WithValue(r.Context(), "currentClientAccess", currentClientAccess) next.ServeHTTP(w, r.WithContext(ctxUser)) }) diff --git a/nodes/node_balancer/cmd/routes.go b/nodes/node_balancer/cmd/routes.go index a300aa32..36559915 100644 --- a/nodes/node_balancer/cmd/routes.go +++ b/nodes/node_balancer/cmd/routes.go @@ -27,10 +27,15 @@ func pingRoute(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(response) } +func debugRoute(w http.ResponseWriter, r *http.Request) { + log.Printf("Clients: %v", ethereumClientPool) + return +} + // lbHandler load balances the incoming requests to nodes func lbHandler(w http.ResponseWriter, r *http.Request) { - currentUserAccessRaw := r.Context().Value("currentUserAccess") - currentUserAccess, ok := currentUserAccessRaw.(UserAccess) + currentClientAccessRaw := r.Context().Value("currentClientAccess") + currentClientAccess, ok := currentClientAccessRaw.(ClientResourceData) if !ok { http.Error(w, "Internal server error", http.StatusInternalServerError) return @@ -63,14 +68,14 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("Unacceptable blockchain provided %s", blockchain), http.StatusBadRequest) return } - node = cpool.GetClientNode(currentUserAccess.AccessID) + node = cpool.GetClientNode(currentClientAccess.AccessID) if node == nil { node = blockchainPool.GetNextNode(blockchain) if node == nil { http.Error(w, "There are no nodes available", http.StatusServiceUnavailable) return } - cpool.AddClientNode(currentUserAccess.AccessID, node) + cpool.AddClientNode(currentClientAccess.AccessID, node) } // Save origin path, to use in proxyErrorHandler if node will not response @@ -82,7 +87,7 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { node.StatusReverseProxy.ServeHTTP(w, r) return case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/jsonrpc", blockchain)): - lbJSONRPCHandler(w, r, blockchain, node, currentUserAccess) + lbJSONRPCHandler(w, r, blockchain, node, currentClientAccess) return default: http.Error(w, fmt.Sprintf("Unacceptable path for %s blockchain %s", blockchain, r.URL.Path), http.StatusBadRequest) @@ -90,7 +95,7 @@ func lbHandler(w http.ResponseWriter, r *http.Request) { } } -func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, node *Node, currentUserAccess UserAccess) { +func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, node *Node, currentClientAccess ClientResourceData) { body, err := ioutil.ReadAll(r.Body) if err != nil { http.Error(w, "Unable to read body", http.StatusBadRequest) @@ -106,12 +111,12 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, } switch { - case currentUserAccess.dataSource == "blockchain": - if currentUserAccess.BlockchainAccess == false { + case currentClientAccess.dataSource == "blockchain": + if currentClientAccess.BlockchainAccess == false { http.Error(w, "Access to blockchain node not allowed with provided access id", http.StatusForbidden) return } - if currentUserAccess.ExtendedMethods == false { + if currentClientAccess.ExtendedMethods == false { _, exists := ALLOWED_METHODS[jsonrpcRequest.Method] if !exists { http.Error(w, "Method for provided access id not allowed", http.StatusForbidden) @@ -124,12 +129,12 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string, // as modified structure of DefaultTransport net/http/transport/DefaultTransport node.GethReverseProxy.ServeHTTP(w, r) return - case currentUserAccess.dataSource == "database": + case currentClientAccess.dataSource == "database": // lbDatabaseHandler(w, r, blockchain, jsonrpcRequest) http.Error(w, "Database access under development", http.StatusInternalServerError) return default: - http.Error(w, fmt.Sprintf("Unacceptable data source %s", currentUserAccess.dataSource), http.StatusBadRequest) + http.Error(w, fmt.Sprintf("Unacceptable data source %s", currentClientAccess.dataSource), http.StatusBadRequest) return } } diff --git a/nodes/node_balancer/cmd/server.go b/nodes/node_balancer/cmd/server.go index 616fcf78..3985ba07 100644 --- a/nodes/node_balancer/cmd/server.go +++ b/nodes/node_balancer/cmd/server.go @@ -21,7 +21,7 @@ import ( ) var ( - internalCrawlersAccess UserAccess + internalCrawlersAccess ClientResourceData // Crash reporter reporter *humbug.HumbugReporter @@ -134,23 +134,23 @@ func Server() { fmt.Printf("Unable to encode resource data interface to json %v", err) os.Exit(1) } - var userAccess UserAccess - err = json.Unmarshal(resource_data, &userAccess) + var clientAccess ClientResourceData + err = json.Unmarshal(resource_data, &clientAccess) if err != nil { fmt.Printf("Unable to decode resource data json to structure %v", err) os.Exit(1) } - internalCrawlersAccess = UserAccess{ - UserID: userAccess.UserID, - AccessID: userAccess.AccessID, - Name: userAccess.Name, - Description: userAccess.Description, - BlockchainAccess: userAccess.BlockchainAccess, - ExtendedMethods: userAccess.ExtendedMethods, + internalCrawlersAccess = ClientResourceData{ + UserID: clientAccess.UserID, + AccessID: clientAccess.AccessID, + Name: clientAccess.Name, + Description: clientAccess.Description, + BlockchainAccess: clientAccess.BlockchainAccess, + ExtendedMethods: clientAccess.ExtendedMethods, } log.Printf( "Internal crawlers access set, resource id: %s, blockchain access: %t, extended methods: %t", - resources.Resources[0].Id, userAccess.BlockchainAccess, userAccess.ExtendedMethods, + resources.Resources[0].Id, clientAccess.BlockchainAccess, clientAccess.ExtendedMethods, ) err = InitDatabaseClient() @@ -197,6 +197,9 @@ func Server() { serveMux := http.NewServeMux() serveMux.Handle("/nb/", accessMiddleware(http.HandlerFunc(lbHandler))) log.Println("Authentication middleware enabled") + if stateCLI.enableDebugFlag { + serveMux.HandleFunc("/debug", debugRoute) + } serveMux.HandleFunc("/ping", pingRoute) // Set common middlewares, from bottom to top From 3d97c4cb25f76deb259d41891e11e9f50fb7e393 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Tue, 17 May 2022 14:49:11 +0000 Subject: [PATCH 23/23] Moonworm crawler update to work with nodebalancer access id --- crawlers/deploy/moonworm-unicorns-mainnet.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crawlers/deploy/moonworm-unicorns-mainnet.service b/crawlers/deploy/moonworm-unicorns-mainnet.service index e8f0d2cb..e1dc8a64 100644 --- a/crawlers/deploy/moonworm-unicorns-mainnet.service +++ b/crawlers/deploy/moonworm-unicorns-mainnet.service @@ -11,7 +11,7 @@ EnvironmentFile=/home/ubuntu/moonstream-secrets/app.env Restart=on-failure RestartSec=15s ExecStart=/home/ubuntu/moonworm-env/bin/python -m moonworm.cli watch-cu \ - -w "$MOONSTREAM_POLYGON_WEB3_PROVIDER_URI" \ + -w "${MOONSTREAM_POLYGON_WEB3_PROVIDER_URI}?access_id=${NB_CONTROLLER_ACCESS_ID}&data_source=blockchain" \ -c 0xdC0479CC5BbA033B3e7De9F178607150B3AbCe1f \ -d 21418707 \ --confirmations 60