feat(M8a): GET /v1/members + devbox members — read the role grants 👥📖
The read companion to devbox-hub member set/rm/list. Additive: new PathMembers endpoint (authed; reads stay open in M8a — gating is M9), proto.Member/MembersResponse, transport Members(), and a devbox members <share> client command that prints 'legacy share' or the role grants (principal, role, +s). Role name<->level mapping moved to meta (ParseRole/RoleName) and the devbox-hub CLI deduped onto it; new meta.ACLMode(share). Test: legacy reads empty, a grant flips it explicit and lists bob/editor/+s. 18 pkgs -race green; windows cross-builds.
This commit is contained in:
parent
a7294fc884
commit
68d25dfd38
7 changed files with 169 additions and 14 deletions
|
|
@ -171,18 +171,6 @@ func revokeCmd() *cobra.Command {
|
|||
return cmd
|
||||
}
|
||||
|
||||
// roleNames maps the CLI role words to their numeric levels (and back).
|
||||
var roleNames = map[string]int{"viewer": meta.RoleViewer, "editor": meta.RoleEditor, "admin": meta.RoleAdmin, "owner": meta.RoleOwner}
|
||||
|
||||
func roleName(level int) string {
|
||||
for n, l := range roleNames {
|
||||
if l == level {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("role%d", level)
|
||||
}
|
||||
|
||||
// memberCmd manages per-share roles (M8a). The FIRST grant on a share flips it
|
||||
// from legacy (every device an implicit owner = v1) to explicit/deny-by-default,
|
||||
// so granting roles is how a share becomes multi-owner — handle with care.
|
||||
|
|
@ -196,7 +184,7 @@ func memberCmd() *cobra.Command {
|
|||
Short: "grant/update a principal's role on a share (flips the share to explicit ACLs)",
|
||||
Args: cobra.ExactArgs(3),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
role, ok := roleNames[args[2]]
|
||||
role, ok := meta.ParseRole(args[2])
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown role %q (want viewer|editor|admin|owner)", args[2])
|
||||
}
|
||||
|
|
@ -255,7 +243,7 @@ func memberCmd() *cobra.Command {
|
|||
if m.CanReshare {
|
||||
s = " +s"
|
||||
}
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "%-20s %s%s\n", m.Principal, roleName(m.Role), s)
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "%-20s %s%s\n", m.Principal, meta.RoleName(m.Role), s)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ func main() {
|
|||
hookCmd(),
|
||||
ignoreCmd(),
|
||||
conflictsCmd(),
|
||||
membersCmd(),
|
||||
startCmd(),
|
||||
stopCmd(),
|
||||
pauseCmd(),
|
||||
|
|
@ -304,6 +305,46 @@ func logCmd() *cobra.Command {
|
|||
}
|
||||
}
|
||||
|
||||
func membersCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "members <share>",
|
||||
Short: "👥 show who can access a share (M8a roles)",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
dir, err := config.Dir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d, err := config.LoadDaemon(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.Hub == "" || d.Bearer == "" {
|
||||
return fmt.Errorf("not joined — run: devbox join <hub> <token>")
|
||||
}
|
||||
c := transport.New(d.Hub)
|
||||
c.SetBearer(d.Bearer)
|
||||
resp, err := c.Members(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out := cmd.OutOrStdout()
|
||||
if resp.Legacy {
|
||||
fmt.Fprintf(out, "%s is a legacy share — every enrolled device is an implicit owner\n", args[0])
|
||||
return nil
|
||||
}
|
||||
for _, m := range resp.Members {
|
||||
s := ""
|
||||
if m.CanReshare {
|
||||
s = " +s"
|
||||
}
|
||||
fmt.Fprintf(out, "%-20s %s%s\n", m.Principal, m.Role, s)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func restoreCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "restore <share> <snapshot> [path]",
|
||||
|
|
|
|||
|
|
@ -144,6 +144,22 @@ const (
|
|||
RoleOwner = 40
|
||||
)
|
||||
|
||||
// roleNames is the canonical name<->level mapping shared by the CLI and wire.
|
||||
var roleNames = map[string]int{"viewer": RoleViewer, "editor": RoleEditor, "admin": RoleAdmin, "owner": RoleOwner}
|
||||
|
||||
// ParseRole maps a role word to its level (ok=false if unknown).
|
||||
func ParseRole(name string) (int, bool) { lvl, ok := roleNames[name]; return lvl, ok }
|
||||
|
||||
// RoleName maps a level back to its word (or "roleN" for an unknown level).
|
||||
func RoleName(level int) string {
|
||||
for n, l := range roleNames {
|
||||
if l == level {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("role%d", level)
|
||||
}
|
||||
|
||||
// DB is the hub metadata store.
|
||||
type DB struct{ sql *sql.DB }
|
||||
|
||||
|
|
@ -451,6 +467,20 @@ func (d *DB) RemoveMember(share, principalID string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// ACLMode returns a share's ACL mode ("legacy" or "explicit"); "legacy" if the
|
||||
// share is unknown, so callers stay v1-permissive.
|
||||
func (d *DB) ACLMode(share string) (string, error) {
|
||||
var mode string
|
||||
err := d.sql.QueryRow(`SELECT acl_mode FROM shares WHERE name=?`, share).Scan(&mode)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "legacy", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return mode, nil
|
||||
}
|
||||
|
||||
// Members lists a share's grants (empty for a legacy share).
|
||||
func (d *DB) Members(share string) ([]Member, error) {
|
||||
rows, err := d.sql.Query(`SELECT principal_id, role, can_reshare FROM members WHERE share=? ORDER BY role DESC, principal_id`, share)
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ func (s *Server) Handler() http.Handler {
|
|||
mux.HandleFunc("POST "+proto.PathPush, s.auth(s.handlePush))
|
||||
mux.HandleFunc("GET "+proto.PathHead, s.auth(s.handleHead))
|
||||
mux.HandleFunc("GET "+proto.PathLog, s.auth(s.handleLog))
|
||||
mux.HandleFunc("GET "+proto.PathMembers, s.auth(s.handleMembers))
|
||||
mux.HandleFunc("GET "+proto.PathEvents, s.auth(s.handleEvents))
|
||||
mux.HandleFunc("GET "+proto.PathMetrics, s.handleMetrics)
|
||||
return mux
|
||||
|
|
@ -356,6 +357,27 @@ func (s *Server) handleLog(w http.ResponseWriter, r *http.Request, _ string) {
|
|||
writeJSON(w, http.StatusOK, proto.LogResponse{Snapshots: out})
|
||||
}
|
||||
|
||||
// handleMembers lists who can access a share (M8a). Reads stay open to any
|
||||
// enrolled device in M8a — read-side gating is M9.
|
||||
func (s *Server) handleMembers(w http.ResponseWriter, r *http.Request, _ string) {
|
||||
share := r.URL.Query().Get("share")
|
||||
mode, err := s.db.ACLMode(share)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ms, err := s.db.Members(share)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
out := make([]proto.Member, len(ms))
|
||||
for i, m := range ms {
|
||||
out[i] = proto.Member{Principal: m.Principal, Role: meta.RoleName(m.Role), CanReshare: m.CanReshare}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, proto.MembersResponse{Legacy: mode != "explicit", Members: out})
|
||||
}
|
||||
|
||||
func (s *Server) handleMetrics(w http.ResponseWriter, _ *http.Request) {
|
||||
gauges := []struct {
|
||||
name, table, help string
|
||||
|
|
|
|||
|
|
@ -272,6 +272,57 @@ func TestPushWriteGate(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestHandleMembers covers GET /v1/members: a fresh share reads as legacy, and
|
||||
// after a grant it lists the member with the role name and reshare bit.
|
||||
func TestHandleMembers(t *testing.T) {
|
||||
base, db := testHub(t)
|
||||
now := time.Now().Unix()
|
||||
if err := db.CreateToken(HashToken("tok"), now+3600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pub, priv, err := ed25519.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, body := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, proto.JoinRequest{
|
||||
Token: "tok", Name: "laptop", Pubkey: pub,
|
||||
Signature: ed25519.Sign(priv, proto.JoinChallenge("tok", pub)),
|
||||
}))
|
||||
var join proto.JoinResponse
|
||||
if err := json.Unmarshal(body, &join); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if st, _ := do(t, "POST", base+proto.PathPublish, join.Bearer, mustJSON(t, proto.PublishRequest{Share: "proj"})); st != http.StatusOK {
|
||||
t.Fatalf("publish = %d", st)
|
||||
}
|
||||
|
||||
get := func() proto.MembersResponse {
|
||||
st, b := do(t, "GET", base+proto.PathMembers+"?share=proj", join.Bearer, nil)
|
||||
if st != http.StatusOK {
|
||||
t.Fatalf("members status = %d, body = %s", st, b)
|
||||
}
|
||||
var resp proto.MembersResponse
|
||||
if err := json.Unmarshal(b, &resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
if resp := get(); !resp.Legacy || len(resp.Members) != 0 {
|
||||
t.Fatalf("fresh share should be legacy with no members, got %+v", resp)
|
||||
}
|
||||
if err := db.SetMember("proj", "bob", meta.RoleEditor, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp := get()
|
||||
if resp.Legacy || len(resp.Members) != 1 {
|
||||
t.Fatalf("after grant want explicit + 1 member, got %+v", resp)
|
||||
}
|
||||
if m := resp.Members[0]; m.Principal != "bob" || m.Role != "editor" || !m.CanReshare {
|
||||
t.Fatalf("member = %+v, want bob/editor/+s", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHubAuthAndHashErrors(t *testing.T) {
|
||||
base, db := testHub(t)
|
||||
|
||||
|
|
|
|||
|
|
@ -175,6 +175,14 @@ func (c *Client) Log(share string) ([]proto.SnapshotInfo, error) {
|
|||
return resp.Snapshots, nil
|
||||
}
|
||||
|
||||
// Members returns who can access a share (M8a).
|
||||
func (c *Client) Members(share string) (proto.MembersResponse, error) {
|
||||
var resp proto.MembersResponse
|
||||
path := proto.PathMembers + "?" + url.Values{"share": {share}}.Encode()
|
||||
err := c.do(http.MethodGet, path, true, nil, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// do sends a JSON request (nil body for GET) and decodes a JSON response into
|
||||
// out (nil out discards the body). It sets the bearer header when auth is true.
|
||||
func (c *Client) do(method, path string, auth bool, body, out any) error {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ const (
|
|||
PathHead = "/v1/head" // GET ?share=, auth: current head snapshot id
|
||||
PathLog = "/v1/log" // GET ?share=, auth: snapshot history
|
||||
PathEvents = "/v1/events" // GET ?share=, auth: SSE stream of change events
|
||||
PathMembers = "/v1/members" // GET ?share=, auth: who can access a share (M8a)
|
||||
PathMetrics = "/metrics" // GET, no auth: Prometheus text exposition
|
||||
)
|
||||
|
||||
|
|
@ -29,6 +30,20 @@ type LogResponse struct {
|
|||
Snapshots []SnapshotInfo `json:"snapshots"`
|
||||
}
|
||||
|
||||
// Member is one principal's role grant on a share (M8a).
|
||||
type Member struct {
|
||||
Principal string `json:"principal"`
|
||||
Role string `json:"role"` // viewer|editor|admin|owner
|
||||
CanReshare bool `json:"can_reshare,omitempty"`
|
||||
}
|
||||
|
||||
// MembersResponse lists a share's role grants. Empty Members + Legacy=true means
|
||||
// a legacy share where every enrolled device is an implicit owner (v1 behavior).
|
||||
type MembersResponse struct {
|
||||
Legacy bool `json:"legacy"`
|
||||
Members []Member `json:"members"`
|
||||
}
|
||||
|
||||
// Event is a hub change notification, delivered as one SSE "data:" line (JSON).
|
||||
type Event struct {
|
||||
Share string `json:"share"`
|
||||
|
|
|
|||
Loading…
Reference in a new issue