From de609da7cd5d69cf16753d1d5d09e5f18d877380 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 19 Dec 2022 12:17:49 +0000 Subject: [PATCH] Fixed internal crawlers access bug, small config fixes --- nodes/node_balancer/README.md | 50 +++++++++++++++---- nodes/node_balancer/cmd/nodebalancer/cli.go | 2 +- .../node_balancer/cmd/nodebalancer/configs.go | 10 ++-- .../node_balancer/cmd/nodebalancer/server.go | 39 ++++++--------- .../node_balancer/cmd/nodebalancer/version.go | 2 +- nodes/node_balancer/sample.env | 2 +- 6 files changed, 63 insertions(+), 42 deletions(-) diff --git a/nodes/node_balancer/README.md b/nodes/node_balancer/README.md index 62fd4de8..a807ad5d 100644 --- a/nodes/node_balancer/README.md +++ b/nodes/node_balancer/README.md @@ -1,17 +1,47 @@ # Node Balancer application -# Installation +## Installation and configuration -- Prepare environment variables +- Prepare environment variables, according with `sample.env`. - Build application ```bash go build -o nodebalancer . ``` -# Work with nodebalancer +- Generate configuration -## add-access +```bash +nodebalancer generate-config +``` + +- Modify configuration. Tags should NOT repeat blockchain, as it is specified in `blockchain` key. Example of configuration: + +```bash +[ + { + "blockchain": "ethereum", + "endpoint": "http://127.0.0.1:8545", + "tags": ["local"] + }, + { + "blockchain": "ethereum", + "endpoint": "http://127.0.0.1:9585", + "tags": ["local"] + }, + { + "blockchain": "ethereum", + "endpoint": "https://cool-name.quiknode.pro/y0urn0de1den1f1cat0r/", + "tags": ["external"] + } +] +``` + +So if with request will be specified tag `local` will be returned node with corresponding tag. + +## Work with nodebalancer + +### add-access Add new access for user: @@ -25,7 +55,7 @@ nodebalancer add-access \ --blockchain--access true ``` -## delete-access +### delete-access Delete user access: @@ -37,7 +67,7 @@ nodebalancer delete-access \ If `access-id` not specified, all user accesses will be deleted. -## users +### users ```bash nodebalancer users | jq . @@ -67,7 +97,7 @@ This command will return a list of bugout resources of registered users to acces `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 +### server ```bash nodebalancer server -host 0.0.0.0 -port 8544 -healthcheck @@ -76,17 +106,17 @@ 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 +## 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=' \ +curl --request POST '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], + "params":["latest", false], "id":1 }' ``` diff --git a/nodes/node_balancer/cmd/nodebalancer/cli.go b/nodes/node_balancer/cmd/nodebalancer/cli.go index 23ccf9fc..15a75aa2 100644 --- a/nodes/node_balancer/cmd/nodebalancer/cli.go +++ b/nodes/node_balancer/cmd/nodebalancer/cli.go @@ -167,7 +167,7 @@ func (s *StateCLI) populateCLI() { // Common flag pointers for _, fs := range []*flag.FlagSet{s.addAccessCmd, s.generateConfigCmd, s.deleteAccessCmd, s.serverCmd, s.usersCmd, s.versionCmd} { fs.BoolVar(&s.helpFlag, "help", false, "Show help message") - fs.StringVar(&s.configPathFlag, "config", "", "Path to configuration file (default: ~/.nodebalancer/config.txt)") + fs.StringVar(&s.configPathFlag, "config", "", "Path to configuration file (default: ~/.nodebalancer/config.json)") } // Add, delete and list user access subcommand flag pointers diff --git a/nodes/node_balancer/cmd/nodebalancer/configs.go b/nodes/node_balancer/cmd/nodebalancer/configs.go index 358eb598..18d31220 100644 --- a/nodes/node_balancer/cmd/nodebalancer/configs.go +++ b/nodes/node_balancer/cmd/nodebalancer/configs.go @@ -18,7 +18,6 @@ var ( nodeConfigs []NodeConfig // 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") @@ -60,8 +59,9 @@ func CheckEnvVarSet() { // Nodes configuration type NodeConfig struct { - Blockchain string `json:"blockchain"` - Endpoint string `json:"endpoint"` + Blockchain string `json:"blockchain"` + Endpoint string `json:"endpoint"` + Tags []string `json:"tags"` } func LoadConfig(configPath string) error { @@ -108,7 +108,7 @@ func GetConfigPath(providedPath string) (*ConfigPlacement, error) { return nil, fmt.Errorf("Unable to find user home directory, %v", err) } configDirPath = fmt.Sprintf("%s/.nodebalancer", homeDir) - configPath = fmt.Sprintf("%s/config.txt", configDirPath) + configPath = fmt.Sprintf("%s/config.json", configDirPath) } else { configPath = strings.TrimSuffix(providedPath, "/") configDirPath = filepath.Dir(configPath) @@ -144,7 +144,7 @@ func GenerateDefaultConfig(config *ConfigPlacement) error { if !config.ConfigExists { tempConfig := []NodeConfig{ - {Blockchain: "ethereum", Endpoint: "http://127.0.0.1:8545"}, + {Blockchain: "ethereum", Endpoint: "http://127.0.0.1:8545", Tags: []string{"local"}}, } tempConfigJson, err := json.Marshal(tempConfig) if err != nil { diff --git a/nodes/node_balancer/cmd/nodebalancer/server.go b/nodes/node_balancer/cmd/nodebalancer/server.go index f7b47c41..185381a0 100644 --- a/nodes/node_balancer/cmd/nodebalancer/server.go +++ b/nodes/node_balancer/cmd/nodebalancer/server.go @@ -5,7 +5,7 @@ package main import ( "context" - "encoding/json" + // "encoding/json" "fmt" "log" "net/http" @@ -130,32 +130,23 @@ func Server() { os.Exit(1) } if len(resources.Resources) != 1 { - fmt.Printf("User with provided access identifier has wrong number of resources, err: %v\n", 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, err: %v\n", err) - os.Exit(1) - } - var clientAccess ClientResourceData - err = json.Unmarshal(resource_data, &clientAccess) - if err != nil { - fmt.Printf("Unable to decode resource data json to structure, err: %v\n", err) - os.Exit(1) + log.Println("There are no access IDs for users in resources") + } else { + log.Println("Found user access IDs in resources") } + + // Set internal crawlers access to bypass requests from internal services + // without fetching data from authn Brood server + internalCrawlersUserID := uuid.New().String() internalCrawlersAccess = ClientResourceData{ - UserID: clientAccess.UserID, - AccessID: clientAccess.AccessID, - Name: clientAccess.Name, - Description: clientAccess.Description, - BlockchainAccess: clientAccess.BlockchainAccess, - ExtendedMethods: clientAccess.ExtendedMethods, + UserID: internalCrawlersUserID, + AccessID: NB_CONTROLLER_ACCESS_ID, + Name: "InternalCrawlersAccess", + Description: "Access for internal crawlers.", + BlockchainAccess: true, + ExtendedMethods: true, } - log.Printf( - "Internal crawlers access set, resource id: %s, blockchain access: %t, extended methods: %t", - resources.Resources[0].Id, clientAccess.BlockchainAccess, clientAccess.ExtendedMethods, - ) + log.Printf("Internal crawlers access set with user ID: %s", internalCrawlersUserID) err = InitDatabaseClient() if err != nil { diff --git a/nodes/node_balancer/cmd/nodebalancer/version.go b/nodes/node_balancer/cmd/nodebalancer/version.go index 0c645931..0a350a65 100644 --- a/nodes/node_balancer/cmd/nodebalancer/version.go +++ b/nodes/node_balancer/cmd/nodebalancer/version.go @@ -1,3 +1,3 @@ package main -var NB_VERSION = "0.2.1" +var NB_VERSION = "0.2.2" diff --git a/nodes/node_balancer/sample.env b/nodes/node_balancer/sample.env index d1aea3b6..6de59eb5 100644 --- a/nodes/node_balancer/sample.env +++ b/nodes/node_balancer/sample.env @@ -1,5 +1,5 @@ # Required environment variables for load balancer -export BUGOUT_AUTH_URL="https://auth.bugout.dev" +export BUGOUT_BROOD_URL="https://auth.bugout.dev" export NB_APPLICATION_ID="" export NB_CONTROLLER_TOKEN="" export NB_CONTROLLER_ACCESS_ID=""