2021-12-20 12:53:54 +00:00
/ *
Configurations for load balancer server .
* /
2022-07-12 18:48:44 +00:00
package main
2021-12-20 12:53:54 +00:00
import (
2022-07-12 18:48:44 +00:00
"encoding/json"
2022-05-17 12:36:08 +00:00
"fmt"
2022-07-12 18:48:44 +00:00
"io/ioutil"
2021-12-20 12:53:54 +00:00
"log"
"os"
2022-07-11 13:53:03 +00:00
"path/filepath"
2023-05-31 14:07:29 +00:00
"strconv"
2022-07-11 13:53:03 +00:00
"strings"
2021-12-20 12:53:54 +00:00
"time"
2023-05-22 12:30:09 +00:00
2023-05-31 14:07:29 +00:00
bugout "github.com/bugout-dev/bugout-go/pkg"
2023-05-22 12:30:09 +00:00
"github.com/google/uuid"
2021-12-20 12:53:54 +00:00
)
2022-03-16 21:33:48 +00:00
var (
2022-08-08 12:54:43 +00:00
nodeConfigs [ ] NodeConfig
2023-05-16 10:47:41 +00:00
supportedBlockchains map [ string ] bool
2023-05-31 14:07:29 +00:00
// Bugout client
2023-05-31 15:44:41 +00:00
// TODO(kompotkot): Find out why it cuts out the port
BUGOUT_BROOD_URL = "https://auth.bugout.dev"
// BUGOUT_BROOD_URL = os.Getenv("BUGOUT_BROOD_URL")
2023-05-31 14:07:29 +00:00
NB_BUGOUT_TIMEOUT_SECONDS_RAW = os . Getenv ( "NB_BUGOUT_TIMEOUT_SECONDS" )
2022-03-16 21:33:48 +00:00
// Bugout and application configuration
2023-06-05 10:28:03 +00:00
BUGOUT_AUTH_CALL_TIMEOUT = time . Second * 5
MOONSTREAM_APPLICATION_ID = os . Getenv ( "MOONSTREAM_APPLICATION_ID" )
NB_CONTROLLER_TOKEN = os . Getenv ( "NB_CONTROLLER_TOKEN" )
NB_CONTROLLER_ACCESS_ID = os . Getenv ( "NB_CONTROLLER_ACCESS_ID" )
MOONSTREAM_CORS_ALLOWED_ORIGINS = os . Getenv ( "MOONSTREAM_CORS_ALLOWED_ORIGINS" )
2022-03-16 21:33:48 +00:00
NB_CONNECTION_RETRIES = 2
NB_CONNECTION_RETRIES_INTERVAL = time . Millisecond * 10
2024-01-23 07:01:23 +00:00
NB_HEALTH_CHECK_INTERVAL = os . Getenv ( "NB_HEALTH_CHECK_INTERVAL" )
2022-03-16 21:33:48 +00:00
NB_HEALTH_CHECK_CALL_TIMEOUT = time . Second * 2
2023-05-22 11:38:41 +00:00
NB_CACHE_CLEANING_INTERVAL = time . Second * 10
2023-05-22 11:43:28 +00:00
NB_CACHE_ACCESS_ID_LIFETIME = int64 ( 120 ) // 2 minutes
NB_CACHE_ACCESS_ID_SESSION_LIFETIME = int64 ( 600 ) // 10 minutes
2022-06-06 12:58:33 +00:00
2022-07-15 19:51:12 +00:00
NB_MAX_COUNTER_NUMBER = uint64 ( 10000000 )
2022-03-16 21:33:48 +00:00
// Client configuration
2023-01-25 13:37:57 +00:00
NB_CLIENT_NODE_KEEP_ALIVE = int64 ( 5 ) // How long to store node in hot list for client in seconds
2022-03-16 21:33:48 +00:00
NB_ACCESS_ID_HEADER = os . Getenv ( "NB_ACCESS_ID_HEADER" )
NB_DATA_SOURCE_HEADER = os . Getenv ( "NB_DATA_SOURCE_HEADER" )
// Humbug configuration
HUMBUG_REPORTER_NB_TOKEN = os . Getenv ( "HUMBUG_REPORTER_NB_TOKEN" )
2023-05-23 13:40:15 +00:00
// Moonstream resources types
2023-05-24 16:49:41 +00:00
BUGOUT_RESOURCE_TYPE_NODEBALANCER_ACCESS = "nodebalancer-access"
2023-06-05 07:50:33 +00:00
DEFAULT_AUTOGENERATED_USER_PERMISSIONS = [ ] string { "read" }
2023-05-24 16:49:41 +00:00
DEFAULT_AUTOGENERATED_PERIOD_DURATION = int64 ( 86400 )
DEFAULT_AUTOGENERATED_MAX_CALLS_PER_PERIOD = int64 ( 1000 )
2022-03-16 21:33:48 +00:00
)
2021-12-20 12:53:54 +00:00
2023-05-31 14:20:53 +00:00
func CreateBugoutClient ( ) ( bugout . BugoutClient , error ) {
2023-05-31 14:07:29 +00:00
bugoutTimeoutSeconds , err := strconv . Atoi ( NB_BUGOUT_TIMEOUT_SECONDS_RAW )
if err != nil {
2023-05-31 14:20:53 +00:00
return bugout . BugoutClient { } , fmt . Errorf ( "unable to parse environment variable as integer: %v" , err )
2023-05-31 14:07:29 +00:00
}
NB_BUGOUT_TIMEOUT_SECONDS := time . Duration ( bugoutTimeoutSeconds ) * time . Second
broodClient := bugout . ClientBrood ( BUGOUT_BROOD_URL , NB_BUGOUT_TIMEOUT_SECONDS )
2023-05-31 14:20:53 +00:00
return broodClient , nil
2023-05-31 14:07:29 +00:00
}
2022-05-16 19:54:22 +00:00
func CheckEnvVarSet ( ) {
2022-05-17 12:36:08 +00:00
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"
2021-12-21 15:52:58 +00:00
}
2023-05-22 12:30:09 +00:00
_ , 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 )
}
2022-07-12 18:48:44 +00:00
}
// Nodes configuration
type NodeConfig struct {
2023-01-25 13:37:57 +00:00
Blockchain string ` json:"blockchain" `
Endpoint string ` json:"endpoint" `
2022-07-12 18:48:44 +00:00
}
2022-01-11 18:53:15 +00:00
2022-08-08 12:54:43 +00:00
func LoadConfig ( configPath string ) error {
2022-07-12 18:48:44 +00:00
rawBytes , err := ioutil . ReadFile ( configPath )
if err != nil {
2022-08-08 12:54:43 +00:00
return err
2022-07-12 18:48:44 +00:00
}
2022-08-08 12:54:43 +00:00
nodeConfigsTemp := & [ ] NodeConfig { }
err = json . Unmarshal ( rawBytes , nodeConfigsTemp )
2022-07-12 18:48:44 +00:00
if err != nil {
2022-08-08 12:54:43 +00:00
return err
2022-05-17 12:36:08 +00:00
}
2022-08-08 12:54:43 +00:00
nodeConfigs = * nodeConfigsTemp
return nil
2022-05-17 12:36:08 +00:00
}
2022-07-12 18:48:44 +00:00
type ConfigPlacement struct {
2022-07-11 13:53:03 +00:00
ConfigDirPath string
ConfigDirExists bool
ConfigPath string
ConfigExists bool
}
2022-07-12 18:48:44 +00:00
func CheckPathExists ( path string ) ( bool , error ) {
2022-07-11 13:53:03 +00:00
var exists = true
_ , err := os . Stat ( path )
2022-05-17 12:36:08 +00:00
if err != nil {
2022-07-11 13:53:03 +00:00
if os . IsNotExist ( err ) {
exists = false
} else {
2023-05-16 11:23:52 +00:00
return exists , fmt . Errorf ( "error due checking file path exists, err: %v" , err )
2022-07-11 13:53:03 +00:00
}
2022-05-17 12:36:08 +00:00
}
2022-07-12 18:48:44 +00:00
return exists , nil
2022-07-11 13:53:03 +00:00
}
2022-05-17 12:36:08 +00:00
2022-07-12 18:48:44 +00:00
func GetConfigPath ( providedPath string ) ( * ConfigPlacement , error ) {
2022-07-11 13:53:03 +00:00
var configDirPath , configPath string
if providedPath == "" {
homeDir , err := os . UserHomeDir ( )
if err != nil {
2023-05-16 11:23:52 +00:00
return nil , fmt . Errorf ( "unable to find user home directory, %v" , err )
2022-07-11 13:53:03 +00:00
}
configDirPath = fmt . Sprintf ( "%s/.nodebalancer" , homeDir )
2023-05-16 10:47:41 +00:00
configPath = fmt . Sprintf ( "%s/config.json" , configDirPath )
2022-07-11 13:53:03 +00:00
} else {
configPath = strings . TrimSuffix ( providedPath , "/" )
configDirPath = filepath . Dir ( configPath )
2022-01-17 11:37:08 +00:00
}
2022-05-17 12:36:08 +00:00
2022-07-12 18:48:44 +00:00
configDirPathExists , err := CheckPathExists ( configDirPath )
if err != nil {
return nil , err
}
configPathExists , err := CheckPathExists ( configPath )
if err != nil {
return nil , err
}
config := & ConfigPlacement {
2022-07-11 13:53:03 +00:00
ConfigDirPath : configDirPath ,
2022-07-12 18:48:44 +00:00
ConfigDirExists : configDirPathExists ,
2022-07-11 13:53:03 +00:00
ConfigPath : configPath ,
2022-07-12 18:48:44 +00:00
ConfigExists : configPathExists ,
2022-07-11 13:53:03 +00:00
}
2022-07-12 18:48:44 +00:00
return config , nil
2022-07-11 13:53:03 +00:00
}
2022-07-12 18:48:44 +00:00
func GenerateDefaultConfig ( config * ConfigPlacement ) error {
2022-07-11 13:53:03 +00:00
if ! config . ConfigDirExists {
if err := os . MkdirAll ( config . ConfigDirPath , os . ModePerm ) ; err != nil {
2023-05-16 11:23:52 +00:00
return fmt . Errorf ( "unable to create directory, %v" , err )
2022-07-11 13:53:03 +00:00
}
log . Printf ( "Config directory created at: %s" , config . ConfigDirPath )
}
if ! config . ConfigExists {
2022-07-12 18:48:44 +00:00
tempConfig := [ ] NodeConfig {
2023-01-25 13:37:57 +00:00
{ Blockchain : "ethereum" , Endpoint : "http://127.0.0.1:8545" } ,
2022-07-12 18:48:44 +00:00
}
tempConfigJson , err := json . Marshal ( tempConfig )
if err != nil {
2023-05-16 11:23:52 +00:00
return fmt . Errorf ( "unable to marshal configuration data, err: %v" , err )
2022-07-12 18:48:44 +00:00
}
err = ioutil . WriteFile ( config . ConfigPath , tempConfigJson , os . ModePerm )
2022-05-17 12:36:08 +00:00
if err != nil {
2023-05-16 11:23:52 +00:00
return fmt . Errorf ( "unable to write default config to file %s, err: %v" , config . ConfigPath , err )
2022-05-17 12:36:08 +00:00
}
2022-07-11 13:53:03 +00:00
log . Printf ( "Created default configuration at %s" , config . ConfigPath )
2022-05-17 12:36:08 +00:00
}
2022-07-12 18:48:44 +00:00
return nil
2021-12-21 15:52:58 +00:00
}