|
| 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 | +} |
0 commit comments