-
Notifications
You must be signed in to change notification settings - Fork 19
feat(rbac): wire RBAC into RESP and binary servers #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
423d07f
feat(rbac): wire RBAC into RESP and binary servers
Saxy 7a1a2bd
fix(rbac): harden ROLE handling across RESP and binary paths
Saxy 528d80e
fix(pr): resolved remaining comments
Saxy 4c04600
fix(pr): resolve comments
Saxy fbf5fdb
fix(pr): resolve comments
Saxy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package client | ||
|
|
||
| // RoleUser is a decoded ROLE GETUSER record. | ||
| type RoleUser struct { | ||
| Role string // empty when the user has no explicit role | ||
| HasPass bool | ||
| } | ||
|
|
||
| // RoleListEntry is one role from a ROLE LIST response. | ||
| type RoleListEntry struct { | ||
| Name string | ||
| Commands []string | ||
| Namespaces [][]byte | ||
| } | ||
|
|
||
| // RoleCreate issues ROLE CREATE <name> <rule>... on the binary protocol. | ||
| // Rule tokens follow the RESP conventions: "+cmd", "-cmd", "+@category", | ||
| // "-@category", "~prefix", "~*". Fails when the role already exists. | ||
| func (c *Client) RoleCreate(role string, rules []string, scratchBuf []byte) error { | ||
| if err := c.valid(); err != nil { | ||
| return err | ||
| } | ||
| return c.c.RoleCreate(role, rules, scratchBuf) | ||
| } | ||
|
|
||
| // RoleSetUser issues ROLE SETUSER <username> <role> [>password] [nopass]. | ||
| // At least one password option is required: pass []byte("nopass") for a | ||
| // passwordless user. The last password option wins; nopass clears the hash. | ||
| // A ">password" option transmits the password in cleartext unless the | ||
| // connection was made with DialTLS — use DialTLS when passing secrets. | ||
| func (c *Client) RoleSetUser(username, role string, passOptions [][]byte, scratchBuf []byte) error { | ||
| if err := c.valid(); err != nil { | ||
| return err | ||
| } | ||
| return c.c.RoleSetUser(username, role, passOptions, scratchBuf) | ||
| } | ||
|
|
||
| // RoleDelUser issues ROLE DELUSER <username>. | ||
| func (c *Client) RoleDelUser(username string, scratchBuf []byte) error { | ||
| if err := c.valid(); err != nil { | ||
| return err | ||
| } | ||
| return c.c.RoleDelUser(username, scratchBuf) | ||
| } | ||
|
|
||
| // RoleDelete issues ROLE DELETE <role>. | ||
| func (c *Client) RoleDelete(role string, scratchBuf []byte) error { | ||
| if err := c.valid(); err != nil { | ||
| return err | ||
| } | ||
| return c.c.RoleDelete(role, scratchBuf) | ||
| } | ||
|
|
||
| // RoleList issues ROLE LIST and returns the decoded roles. | ||
| func (c *Client) RoleList(scratchBuf []byte) ([]RoleListEntry, error) { | ||
| if err := c.valid(); err != nil { | ||
| return nil, err | ||
| } | ||
| entries, err := c.c.RoleList(scratchBuf) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| out := make([]RoleListEntry, 0, len(entries)) | ||
| for _, e := range entries { | ||
| out = append(out, RoleListEntry(e)) | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // RoleGetUser issues ROLE GETUSER <username> and returns the decoded record. | ||
| func (c *Client) RoleGetUser(username string, scratchBuf []byte) (RoleUser, error) { | ||
| if err := c.valid(); err != nil { | ||
| return RoleUser{}, err | ||
| } | ||
| u, err := c.c.RoleGetUser(username, scratchBuf) | ||
| if err != nil { | ||
| return RoleUser{}, err | ||
| } | ||
| return RoleUser(u), nil | ||
| } | ||
|
|
||
| // AuthUser authenticates with a username/password pair (RBAC mode). | ||
| // Must be called after Dial/DialTLS when the server runs with --rbac-config. | ||
| // The password travels in cleartext unless the connection was made with | ||
| // DialTLS — use DialTLS when transmitting secrets. | ||
| func (c *Client) AuthUser(username, password string, scratchBuf []byte) error { | ||
| if err := c.valid(); err != nil { | ||
| return err | ||
| } | ||
| return c.c.AuthUser(username, password, scratchBuf) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| Package main | ||
| Tellstone Cloud-Native In-Memory Database | ||
| File: main.go | ||
| Description: Example that drives the ROLE command family over the binary protocol: authenticate | ||
| as an admin user, create a role and a user bound to it, then verify that the new user can only | ||
| run the commands its role grants. Run a server with --rbac-config pointing at the policy file | ||
| before starting this example. | ||
|
|
||
| Authors: | ||
|
|
||
| Maximilian Hagen | ||
| */ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/Saxy/Tellstone/client" | ||
| ) | ||
|
|
||
| func main() { | ||
| c, err := client.Dial("127.0.0.1:9988", 5*time.Second) | ||
| if err != nil { | ||
| log.Fatalf("failed to dial server: %v", err) | ||
| } | ||
| defer c.Close() | ||
|
|
||
| // 4KB reusable scratch buffer for both building requests and receiving replies | ||
| buf := make([]byte, 4*1024) | ||
|
|
||
| // The server's policy file must already define "admin" with the "admin" role. | ||
| if err = c.AuthUser("admin", "adminsecret", buf); err != nil { | ||
| log.Fatalf("AUTH admin failed: %v", err) | ||
| } | ||
| fmt.Println("AUTH admin => OK") | ||
|
|
||
| // Seed a value under users:1 so alice's GET below returns it instead of a | ||
| // storage-level miss, which would be indistinguishable from an RBAC denial. | ||
| if _, err = c.Set([]byte("users:1"), []byte("alice-in-users"), 0, buf); err != nil { | ||
| log.Fatalf("SET users:1 failed: %v", err) | ||
| } | ||
| fmt.Println("SET users:1 => OK") | ||
|
|
||
| // ROLE CREATE defines a role that may only read keys under the "users:" prefix. | ||
| if err = c.RoleCreate("user-reader", []string{"+get", "~users:*"}, buf); err != nil { | ||
| log.Fatalf("ROLE CREATE failed: %v", err) | ||
| } | ||
| fmt.Println("ROLE CREATE user-reader => OK") | ||
|
|
||
| // ROLE SETUSER binds a password-protected user to that role. | ||
| if err = c.RoleSetUser("alice", "user-reader", [][]byte{[]byte(">alicepw")}, buf); err != nil { | ||
| log.Fatalf("ROLE SETUSER failed: %v", err) | ||
| } | ||
| fmt.Println("ROLE SETUSER alice => OK") | ||
|
|
||
| // ROLE GETUSER confirms the assignment. | ||
| u, err := c.RoleGetUser("alice", buf) | ||
| if err != nil { | ||
| log.Fatalf("ROLE GETUSER failed: %v", err) | ||
| } | ||
| fmt.Printf("ROLE GETUSER alice => role=%q has_password=%v\n", u.Role, u.HasPass) | ||
|
|
||
| // ROLE LIST enumerates every role on the server. | ||
| entries, err := c.RoleList(buf) | ||
| if err != nil { | ||
| log.Fatalf("ROLE LIST failed: %v", err) | ||
| } | ||
| for _, e := range entries { | ||
| ns := make([]string, len(e.Namespaces)) | ||
| for i, p := range e.Namespaces { | ||
| ns[i] = string(p) | ||
| } | ||
| fmt.Printf("ROLE LIST => %s commands=%v namespaces=%v\n", e.Name, e.Commands, ns) | ||
| } | ||
|
|
||
| // Open a second connection as alice and prove the role's limits: GET on a | ||
| // matching key passes, SET and keys outside the whitelist are denied. The | ||
| // client surfaces authorization denials as errors carrying the server's | ||
| // NOT_AUTHORIZED error frame. | ||
| alice, err := client.Dial("127.0.0.1:9988", 5*time.Second) | ||
| if err != nil { | ||
| log.Fatalf("failed to dial server: %v", err) | ||
| } | ||
| defer alice.Close() | ||
| if err := alice.AuthUser("alice", "alicepw", buf); err != nil { | ||
| log.Fatalf("AUTH alice failed: %v", err) | ||
| } | ||
| fmt.Println("AUTH alice => OK") | ||
|
|
||
| // GET on a matching key must pass the role gate. The key was seeded | ||
| // above, so any error here — NOT_AUTHORIZED, a transport fault, or a | ||
| // storage miss — is a bug, not a valid outcome. | ||
| res, err := alice.Get([]byte("users:1"), buf) | ||
| if err != nil { | ||
| log.Fatalf("GET users:1 as alice failed: %v", err) | ||
| } | ||
| fmt.Printf("GET users:1 as alice => %s\n", res) | ||
|
|
||
| // SET is not in alice's role, so it must come back as a NOT_AUTHORIZED | ||
| // denial. Success means the ACL let an op through it should have blocked; | ||
| // any other error means the transport or storage broke, not the role. | ||
| if _, err := alice.Set([]byte("users:1"), []byte("hacked"), 0, buf); err == nil { | ||
| log.Fatalf("SET as alice unexpectedly allowed") | ||
| } else if !strings.Contains(err.Error(), "NOT_AUTHORIZED") { | ||
| log.Fatalf("SET as alice denied with the wrong error: %v", err) | ||
| } else { | ||
| fmt.Printf("SET as alice denied => %v\n", err) | ||
| } | ||
|
|
||
| // Same fail-closed check for a key outside the whitelist: the namespace | ||
| // gate must deny it with NOT_AUTHORIZED. | ||
| if _, err := alice.Get([]byte("accounts:1"), buf); err == nil { | ||
| log.Fatalf("GET accounts:1 as alice unexpectedly allowed") | ||
| } else if !strings.Contains(err.Error(), "NOT_AUTHORIZED") { | ||
| log.Fatalf("GET accounts:1 as alice denied with the wrong error: %v", err) | ||
| } else { | ||
| fmt.Printf("GET accounts:1 as alice denied => %v\n", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.