From e2f454af5fd31706d064070ad87ae30e34e2e135 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 22 May 2023 12:30:09 +0000 Subject: [PATCH] Fixed logic of internal usage access --- .../node_balancer/cmd/nodebalancer/configs.go | 9 ++- .../cmd/nodebalancer/middleware.go | 3 +- .../node_balancer/cmd/nodebalancer/server.go | 70 +++++++++++-------- nodes/node_balancer/sample.env | 2 +- 4 files changed, 52 insertions(+), 32 deletions(-) diff --git a/nodes/node_balancer/cmd/nodebalancer/configs.go b/nodes/node_balancer/cmd/nodebalancer/configs.go index ea943739..429bec6d 100644 --- a/nodes/node_balancer/cmd/nodebalancer/configs.go +++ b/nodes/node_balancer/cmd/nodebalancer/configs.go @@ -12,6 +12,8 @@ import ( "path/filepath" "strings" "time" + + "github.com/google/uuid" ) var ( @@ -46,8 +48,6 @@ var ( HUMBUG_REPORTER_NB_TOKEN = os.Getenv("HUMBUG_REPORTER_NB_TOKEN") ) -var () - func CheckEnvVarSet() { if NB_ACCESS_ID_HEADER == "" { NB_ACCESS_ID_HEADER = "x-node-balancer-access-id" @@ -55,6 +55,11 @@ func CheckEnvVarSet() { if NB_DATA_SOURCE_HEADER == "" { NB_DATA_SOURCE_HEADER = "x-node-balancer-data-source" } + _, err := uuid.Parse(NB_CONTROLLER_ACCESS_ID) + if err != nil { + NB_CONTROLLER_ACCESS_ID = uuid.New().String() + log.Printf("Access ID for internal usage in NB_CONTROLLER_ACCESS_ID environment variable is not valid uuid, generated random one: %v", NB_CONTROLLER_ACCESS_ID) + } } // Nodes configuration diff --git a/nodes/node_balancer/cmd/nodebalancer/middleware.go b/nodes/node_balancer/cmd/nodebalancer/middleware.go index 7838b48c..5685d3c9 100644 --- a/nodes/node_balancer/cmd/nodebalancer/middleware.go +++ b/nodes/node_balancer/cmd/nodebalancer/middleware.go @@ -298,10 +298,11 @@ func accessMiddleware(next http.Handler) http.Handler { // If access id does not belong to internal crawlers, then check cache or find it in Bugout resources if accessID == NB_CONTROLLER_ACCESS_ID { - currentClientAccess = internalCrawlersAccess + currentClientAccess = internalUsageAccess if stateCLI.enableDebugFlag { log.Printf("Access ID belongs to internal usage for user with ID %s", currentClientAccess.ClientResourceData.UserID) } + currentClientAccess.LastAccessTs = tsNow currentClientAccess.requestedDataSource = requestedDataSource } else if accessIdCache.FindAccessIdInCache(accessID) != "" { currentClientAccess = accessIdCache.accessIds[accessID] diff --git a/nodes/node_balancer/cmd/nodebalancer/server.go b/nodes/node_balancer/cmd/nodebalancer/server.go index a5324eab..7ce08278 100644 --- a/nodes/node_balancer/cmd/nodebalancer/server.go +++ b/nodes/node_balancer/cmd/nodebalancer/server.go @@ -20,7 +20,7 @@ import ( ) var ( - internalCrawlersAccess ClientAccess + internalUsageAccess ClientAccess // Crash reporter reporter *humbug.HumbugReporter @@ -128,35 +128,49 @@ func Server() { fmt.Printf("Unable to get user with provided access identifier, err: %v\n", err) os.Exit(1) } - if len(resources.Resources) != 1 { - fmt.Printf("User with provided access identifier has wrong number of resources, err: %v\n", err) + if len(resources.Resources) == 1 { + resourceData, 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 clientResourceData ClientResourceData + err = json.Unmarshal(resourceData, &clientResourceData) + if err != nil { + fmt.Printf("Unable to decode resource data json to structure, err: %v\n", err) + os.Exit(1) + } + internalUsageAccess = ClientAccess{ + ClientResourceData: ClientResourceData{ + UserID: clientResourceData.UserID, + AccessID: clientResourceData.AccessID, + Name: clientResourceData.Name, + Description: clientResourceData.Description, + BlockchainAccess: clientResourceData.BlockchainAccess, + ExtendedMethods: clientResourceData.ExtendedMethods, + }, + } + log.Printf( + "Internal crawlers access set, resource id: %s, blockchain access: %t, extended methods: %t", + resources.Resources[0].Id, clientResourceData.BlockchainAccess, clientResourceData.ExtendedMethods, + ) + + } else if len(resources.Resources) == 0 { + internalUsageAccess = ClientAccess{ + ClientResourceData: ClientResourceData{ + UserID: "rnd-user-id", + AccessID: NB_CONTROLLER_ACCESS_ID, + Name: "rnd-name", + Description: "Randomly generated", + BlockchainAccess: true, + ExtendedMethods: true, + }, + } + fmt.Printf("There are no provided NB_CONTROLLER_ACCESS_ID records in Brood resources. Using provided with environment variable or randomly generated\n") + } else { + fmt.Printf("User with provided access identifier has wrong number of resources: %d\n", len(resources.Resources)) 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 clientResourceData ClientResourceData - err = json.Unmarshal(resource_data, &clientResourceData) - if err != nil { - fmt.Printf("Unable to decode resource data json to structure, err: %v\n", err) - os.Exit(1) - } - internalCrawlersAccess = ClientAccess{ - ClientResourceData: ClientResourceData{ - UserID: clientResourceData.UserID, - AccessID: clientResourceData.AccessID, - Name: clientResourceData.Name, - Description: clientResourceData.Description, - BlockchainAccess: clientResourceData.BlockchainAccess, - ExtendedMethods: clientResourceData.ExtendedMethods, - }, - } - log.Printf( - "Internal crawlers access set, resource id: %s, blockchain access: %t, extended methods: %t", - resources.Resources[0].Id, clientResourceData.BlockchainAccess, clientResourceData.ExtendedMethods, - ) // Fill NodeConfigList with initial nodes from environment variables err = LoadConfig(stateCLI.configPathFlag) diff --git a/nodes/node_balancer/sample.env b/nodes/node_balancer/sample.env index ec55536b..1917d023 100644 --- a/nodes/node_balancer/sample.env +++ b/nodes/node_balancer/sample.env @@ -2,7 +2,7 @@ export BUGOUT_BROOD_URL="https://auth.bugout.dev" export NB_APPLICATION_ID="" export NB_CONTROLLER_TOKEN="" -export NB_CONTROLLER_ACCESS_ID="" +export NB_CONTROLLER_ACCESS_ID="" # Error humbug reporter export HUMBUG_REPORTER_NODE_BALANCER_TOKEN=""