mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-21 06:59:24 +08:00
Return error from GetConfig function
This commit is contained in:
37
src/acl.go
37
src/acl.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Password struct {
|
type Password struct {
|
||||||
@@ -16,7 +17,6 @@ type Password struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UserPassword struct {
|
type UserPassword struct {
|
||||||
Enabled bool `json:"Enabled" yaml:"Enabled"`
|
|
||||||
Passwords []Password `json:"Passwords" yaml:"Passwords"`
|
Passwords []Password `json:"Passwords" yaml:"Passwords"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,10 +45,29 @@ type ACL struct {
|
|||||||
Users []User
|
Users []User
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPasswordType(password string) string {
|
||||||
|
if strings.Split(password, "")[0] == "#" {
|
||||||
|
return "SHA256"
|
||||||
|
}
|
||||||
|
return "plaintext"
|
||||||
|
}
|
||||||
|
|
||||||
func NewACL(config utils.Config) *ACL {
|
func NewACL(config utils.Config) *ACL {
|
||||||
users := []User{}
|
users := []User{}
|
||||||
|
|
||||||
// 1. Initialise default ACL user
|
// 1. Initialise default ACL user
|
||||||
|
defaultUser := User{
|
||||||
|
Username: "default",
|
||||||
|
Enabled: true,
|
||||||
|
Authentication: UserPassword{
|
||||||
|
Passwords: []Password{
|
||||||
|
{
|
||||||
|
PasswordType: "plaintext",
|
||||||
|
PasswordValue: config.Password,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Read and parse the ACL config file and set the
|
// 2. Read and parse the ACL config file and set the
|
||||||
if config.AclConfig != "" {
|
if config.AclConfig != "" {
|
||||||
@@ -78,7 +97,21 @@ func NewACL(config utils.Config) *ACL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Validate the ACL Config that has been loaded from the file
|
// 3. If users parsed from file do not contain "default" user, add the one we initialised in step 1
|
||||||
|
hasDefault := false
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
if user.Username == "default" {
|
||||||
|
hasDefault = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasDefault {
|
||||||
|
users = append([]User{defaultUser}, users...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Validate the ACL Config that has been loaded from the file
|
||||||
|
|
||||||
return &ACL{
|
return &ACL{
|
||||||
Users: users,
|
Users: users,
|
||||||
|
10
src/main.go
10
src/main.go
@@ -50,7 +50,7 @@ type Server struct {
|
|||||||
broadcastQueue *memberlist.TransmitLimitedQueue
|
broadcastQueue *memberlist.TransmitLimitedQueue
|
||||||
numOfNodes int
|
numOfNodes int
|
||||||
|
|
||||||
cancelCh *chan (os.Signal)
|
cancelCh *chan os.Signal
|
||||||
|
|
||||||
ACL *ACL
|
ACL *ACL
|
||||||
}
|
}
|
||||||
@@ -388,7 +388,11 @@ func (server *Server) ShutDown(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config := utils.GetConfig()
|
config, err := utils.GetConfig()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
ctx := context.WithValue(context.Background(), utils.ContextServerID("ServerID"), config.ServerID)
|
ctx := context.WithValue(context.Background(), utils.ContextServerID("ServerID"), config.ServerID)
|
||||||
|
|
||||||
@@ -401,7 +405,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cancelCh := make(chan (os.Signal), 1)
|
cancelCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(cancelCh, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
signal.Notify(cancelCh, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||||
|
|
||||||
server := &Server{
|
server := &Server{
|
||||||
|
@@ -2,8 +2,8 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ type Config struct {
|
|||||||
Password string `json:"password" yaml:"password"`
|
Password string `json:"password" yaml:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfig() Config {
|
func GetConfig() (Config, error) {
|
||||||
tls := flag.Bool("tls", false, "Start the server in TLS mode. Default is false")
|
tls := flag.Bool("tls", false, "Start the server in TLS mode. Default is false")
|
||||||
key := flag.String("key", "", "The private key file path.")
|
key := flag.String("key", "", "The private key file path.")
|
||||||
cert := flag.String("cert", "", "The signed certificate file path.")
|
cert := flag.String("cert", "", "The signed certificate file path.")
|
||||||
@@ -96,20 +96,28 @@ It is a plain text value by default but you can provide a SHA256 hash by adding
|
|||||||
ext := path.Ext(f.Name())
|
ext := path.Ext(f.Name())
|
||||||
|
|
||||||
if ext == ".json" {
|
if ext == ".json" {
|
||||||
json.NewDecoder(f).Decode(&conf)
|
err := json.NewDecoder(f).Decode(&conf)
|
||||||
|
if err != nil {
|
||||||
|
return Config{}, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ext == ".yaml" || ext == ".yml" {
|
if ext == ".yaml" || ext == ".yml" {
|
||||||
yaml.NewDecoder(f).Decode(&conf)
|
err := yaml.NewDecoder(f).Decode(&conf)
|
||||||
|
if err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If requirePass is set to true, then password must be provided as well
|
// If requirePass is set to true, then password must be provided as well
|
||||||
|
var err error = nil
|
||||||
|
|
||||||
if conf.RequirePass && conf.Password == "" {
|
if conf.RequirePass && conf.Password == "" {
|
||||||
log.Fatal("password cannot be empty if requirePass is set to true.")
|
err = errors.New("password cannot be empty if requirePass is set to true")
|
||||||
}
|
}
|
||||||
|
|
||||||
return conf
|
return conf, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user