Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1382ef4

Browse files
committedDec 7, 2023
Implemented ACL Init on server.
Implemented reading of ACL from JSON and YAML files into user list in memory.
1 parent 5ef457c commit 1382ef4

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed
 

‎.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.idea
22
bin
33
openssl
4-
config*.json
5-
config*.y*ml
64
docker-compose.y*ml
75
volumes
6+
/config/

‎Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ RUN mkdir -p /etc/ssl/certs/memstore
77
COPY ./bin/linux/x86_64/plugins /usr/local/lib/memstore
88
COPY ./bin/linux/x86_64/server /opt/memstore/bin
99
COPY ./openssl/server /etc/ssl/certs/memstore
10+
COPY ./config /etc/config/memstore
1011

1112
WORKDIR /opt/memstore/bin
1213

@@ -24,4 +25,5 @@ CMD "./server" \
2425
"--http=${HTTP}" \
2526
"--tls=${TLS}" \
2627
"--inMemory=${INMEMORY}" \
27-
"--bootstrapCluster=${BOOTSTRAP_CLUSTER}" \
28+
"--bootstrapCluster=${BOOTSTRAP_CLUSTER}" \
29+
"--aclConfig=${ACL_CONFIG}" \

‎src/acl.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"gopkg.in/yaml.v3"
7+
"log"
8+
"os"
9+
"path"
10+
)
11+
12+
type Password struct {
13+
PasswordType string `json:"PasswordType" yaml:"PasswordType"` // plaintext, SHA256
14+
PasswordValue string `json:"PasswordValue" yaml:"PasswordValue"`
15+
}
16+
17+
type UserPassword struct {
18+
Enabled bool `json:"Enabled" yaml:"Enabled"`
19+
Passwords []Password `json:"Passwords" yaml:"Passwords"`
20+
}
21+
22+
type User struct {
23+
Username string `json:"Username" yaml:"Username"`
24+
Enabled bool `json:"Enabled" yaml:"Enabled"`
25+
26+
Authentication UserPassword `json:"Authentication" yaml:"Authentication"`
27+
28+
IncludedCategories []string `json:"IncludedCategories" yaml:"IncludedCategories"`
29+
ExcludedCategories []string `json:"ExcludedCategories" yaml:"ExcludedCategories"`
30+
31+
IncludedCommands []string `json:"IncludedCommands" yaml:"IncludedCommands"`
32+
ExcludedCommands []string `json:"ExcludedCommands" yaml:"ExcludedCommands"`
33+
34+
IncludedKeys []string `json:"IncludedKeys" yaml:"IncludedKeys"`
35+
ExcludedKeys []string `json:"ExcludedKeys" yaml:"ExcludedKeys"`
36+
IncludedReadKeys []string `json:"IncludedReadKeys" yaml:"IncludedReadKeys"`
37+
IncludedWriteKeys []string `json:"IncludedWriteKeys" yaml:"IncludedWriteKeys"`
38+
39+
IncludedPubSubChannels []string `json:"IncludedPubSubChannels" yaml:"IncludedPubSubChannels"`
40+
ExcludedPubSubChannels []string `json:"ExcludedPubSubChannels" yaml:"ExcludedPubSubChannels"`
41+
}
42+
43+
type ACL struct {
44+
Users []User
45+
}
46+
47+
func NewACL(aclConfig string) *ACL {
48+
users := []User{}
49+
50+
// 1. Initialise default ACL user
51+
52+
// 2. Read and parse the ACL config file and set the
53+
if aclConfig != "" {
54+
// Override acl configurations from file
55+
if f, err := os.Open(aclConfig); err != nil {
56+
panic(err)
57+
} else {
58+
defer func() {
59+
if err := f.Close(); err != nil {
60+
fmt.Println("acl config file close error: ", err)
61+
}
62+
}()
63+
64+
ext := path.Ext(f.Name())
65+
66+
if ext == ".json" {
67+
if err := json.NewDecoder(f).Decode(&users); err != nil {
68+
log.Fatal("could not load JSON ACL config: ", err)
69+
}
70+
}
71+
72+
if ext == ".yaml" || ext == ".yml" {
73+
if err := yaml.NewDecoder(f).Decode(&users); err != nil {
74+
log.Fatal("could not load YAML ACL config: ", err)
75+
}
76+
}
77+
}
78+
}
79+
80+
// 3. Validate the ACL Config that has been loaded from the file
81+
82+
return &ACL{
83+
Users: users,
84+
}
85+
}

‎src/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type Server struct {
5151
numOfNodes int
5252

5353
cancelCh *chan (os.Signal)
54+
55+
ACL *ACL
5456
}
5557

5658
func (server *Server) KeyLock(ctx context.Context, key string) (bool, error) {
@@ -410,6 +412,8 @@ func main() {
410412
broadcastQueue: new(memberlist.TransmitLimitedQueue),
411413
numOfNodes: 0,
412414

415+
ACL: NewACL(config.AclConfig),
416+
413417
cancelCh: &cancelCh,
414418
}
415419

‎src/utils/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Config struct {
2424
InMemory bool `json:"inMemory" yaml:"inMemory"`
2525
DataDir string `json:"dataDir" yaml:"dataDir"`
2626
BootstrapCluster bool `json:"BootstrapCluster" yaml:"bootstrapCluster"`
27+
AclConfig string `json:"AclConfig" yaml:"AclConfig"`
2728
}
2829

2930
func GetConfig() Config {
@@ -41,6 +42,7 @@ func GetConfig() Config {
4142
inMemory := flag.Bool("inMemory", false, "Whether to use memory or persisten storage for raft logs and snapshots.")
4243
dataDir := flag.String("dataDir", "/var/lib/memstore", "Directory to store raft snapshots and logs.")
4344
bootstrapCluster := flag.Bool("bootstrapCluster", false, "Whether this instance should bootstrap a new cluster.")
45+
aclConfig := flag.String("aclConfig", "", "ACL config file path.")
4446

4547
config := flag.String(
4648
"config",
@@ -65,6 +67,7 @@ func GetConfig() Config {
6567
InMemory: *inMemory,
6668
DataDir: *dataDir,
6769
BootstrapCluster: *bootstrapCluster,
70+
AclConfig: *aclConfig,
6871
}
6972

7073
if len(*config) > 0 {

0 commit comments

Comments
 (0)
Please sign in to comment.