Fixed logic of internal usage access

pull/786/head
kompotkot 2023-05-22 12:30:09 +00:00
rodzic 647cccb99f
commit e2f454af5f
4 zmienionych plików z 52 dodań i 32 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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]

Wyświetl plik

@ -20,7 +20,7 @@ import (
)
var (
internalCrawlersAccess ClientAccess
internalUsageAccess ClientAccess
// Crash reporter
reporter *humbug.HumbugReporter
@ -128,22 +128,19 @@ 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)
os.Exit(1)
}
resource_data, err := json.Marshal(resources.Resources[0].ResourceData)
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(resource_data, &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)
}
internalCrawlersAccess = ClientAccess{
internalUsageAccess = ClientAccess{
ClientResourceData: ClientResourceData{
UserID: clientResourceData.UserID,
AccessID: clientResourceData.AccessID,
@ -158,6 +155,23 @@ func Server() {
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)
}
// Fill NodeConfigList with initial nodes from environment variables
err = LoadConfig(stateCLI.configPathFlag)
if err != nil {

Wyświetl plik

@ -2,7 +2,7 @@
export BUGOUT_BROOD_URL="https://auth.bugout.dev"
export NB_APPLICATION_ID="<application_id_to_controll_access>"
export NB_CONTROLLER_TOKEN="<token_of_controller_user>"
export NB_CONTROLLER_ACCESS_ID="<controller_access_id_for_internal_crawlers>"
export NB_CONTROLLER_ACCESS_ID="<controller_access_id_for_internal_usage>"
# Error humbug reporter
export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="<bugout_humbug_token_for_crash_reports>"