mirror of
https://github.com/luxfi/mpc.git
synced 2026-09-24 00:26:24 +00:00
Multi-tenancy isolation (addresses Dragonfire review MPC audit): - Add OrgID field to all event messages (keygen, sign, reshare) - Scope session dedup keys as orgID-walletID-txID - Namespace KVStore keys with org prefix (org:<orgID>:<key>) - Backward-compatible fallback to legacy unscoped keys - New org_kvstore.go with OrgScopedKey/GetKeyShareWithFallback Naming cleanup: - Rename all badger references to zapdb in code, config, tests - config.yaml: badger_password → zapdb_password - AppConfig: BadgerPassword → ZapDBPassword - Fix MPCiumVersion typo → MPCVersion
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/luxfi/mpc/pkg/logger"
|
|
)
|
|
|
|
type AppConfig struct {
|
|
Consul *ConsulConfig `mapstructure:"consul"`
|
|
NATs *NATsConfig `mapstructure:"nats"`
|
|
|
|
ZapDBPassword string `mapstructure:"zapdb_password"`
|
|
}
|
|
|
|
// Implement masking serializer AppConfig
|
|
func (c AppConfig) MarshalJSONMask() string {
|
|
// clone app config
|
|
c.ZapDBPassword = strings.Repeat("*", len(c.ZapDBPassword))
|
|
c.Consul.Password = strings.Repeat("*", len(c.Consul.Password))
|
|
c.Consul.Token = strings.Repeat("*", len(c.Consul.Token))
|
|
c.NATs.Password = strings.Repeat("*", len(c.NATs.Password))
|
|
|
|
bytes, err := json.Marshal(c)
|
|
if err != nil {
|
|
logger.Error("Failed to marshal app config", err)
|
|
}
|
|
return string(bytes)
|
|
}
|
|
|
|
type ConsulConfig struct {
|
|
Address string `mapstructure:"address"`
|
|
Username string `mapstructure:"username"`
|
|
Password string `mapstructure:"password"`
|
|
Token string `mapstructure:"token"`
|
|
}
|
|
|
|
type NATsConfig struct {
|
|
URL string `mapstructure:"url"`
|
|
Username string `mapstructure:"username"`
|
|
Password string `mapstructure:"password"`
|
|
}
|
|
|
|
func InitViperConfig() {
|
|
viper.SetConfigName("config") // name of config file (without extension)
|
|
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
|
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
viper.AutomaticEnv()
|
|
err := viper.ReadInConfig() // Find and read the config file
|
|
if err != nil { // Handle errors reading the config file
|
|
log.Fatal("Fatal error config file: ", err)
|
|
}
|
|
|
|
log.Println("Reading config file:", viper.ConfigFileUsed())
|
|
log.Println("Initialized config successfully!")
|
|
}
|
|
|
|
func LoadConfig() *AppConfig {
|
|
var config AppConfig
|
|
decoderConfig := &mapstructure.DecoderConfig{
|
|
Result: &config,
|
|
WeaklyTypedInput: true,
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
|
mapstructure.StringToSliceHookFunc(","),
|
|
),
|
|
}
|
|
|
|
decoder, err := mapstructure.NewDecoder(decoderConfig)
|
|
if err != nil {
|
|
log.Fatal("Failed to create decoder", err)
|
|
}
|
|
|
|
if err := decoder.Decode(viper.AllSettings()); err != nil {
|
|
log.Fatal("Failed to decode config", err)
|
|
}
|
|
|
|
return &config
|
|
}
|