package store import ( "database/sql" "testing" ) func TestReadPolicyMatchesPositiveGrants(t *testing.T) { db, err := sql.Open("sqlite", ":memory:") if err != nil { t.Fatal(err) } t.Cleanup(func() { db.Close() }) s := &Store{DB: db} if err := s.EnsureAuthSchema(); err != nil { t.Fatal(err) } statements := []string{ `INSERT INTO principals(uuid,state,created_at_ms,updated_at_ms) VALUES('p','active',1,1)`, `INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES('m','d','p','active',1)`, `INSERT INTO auth_roles(uuid,domain_uuid,name,created_at_ms) VALUES('r','d','reader',1)`, `INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES('m','r',1)`, `INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('g','r','read','path','/exact_%/A',1),('wild','r','read','domain','*',1)`, `INSERT INTO groups(uuid,domain_uuid,name,display_name,created_at_ms,updated_at_ms) VALUES('group','d','readers','Readers',1,1)`, `INSERT INTO group_members(group_uuid,membership_uuid,member_role,source,created_at_ms) VALUES('group','m','member','local',1)`, `INSERT INTO group_permissions(uuid,group_uuid,scope_path,permission,created_at_ms) VALUES('gp','group','/group_%','read',1),('up','group','/drop','upload_only',1),('once','group','/once','read_write_once',1)`, } for _, statement := range statements { if _, err := db.Exec(statement); err != nil { t.Fatal(err) } } paths := []string{"/", "/exact_%/A", "/exact_%/a", "/exact_%/A/child", "/group_%", "/group_%/child", "/groupXX/child", "/drop", "/once/deep/file", "/hidden", "", "relative"} for _, state := range []string{"active", "readonly", "suspended", "quarantined"} { if _, err := db.Exec(`UPDATE principals SET state=? WHERE uuid='p'`, state); err != nil { t.Fatal(err) } policy, err := s.LoadReadPathPolicy("d", "p") if err != nil { t.Fatal(err) } for _, path := range paths { want := s.Authorize(AuthorizationRequest{DomainUUID: "d", PrincipalUUID: "p", Capability: "read", TargetType: "path", TargetID: path}) == nil if got := policy.Allows(path); got != want { t.Fatalf("state=%s path=%q got=%v want=%v", state, path, got, want) } } } if _, err := db.Exec(`UPDATE principals SET state='active' WHERE uuid='p'`); err != nil { t.Fatal(err) } old, err := s.LoadReadPathPolicy("d", "p") if err != nil { t.Fatal(err) } if _, err := db.Exec(`DELETE FROM group_permissions`); err != nil { t.Fatal(err) } current, err := s.LoadReadPathPolicy("d", "p") if err != nil { t.Fatal(err) } if !old.Allows("/group_%/child") || current.Allows("/group_%/child") { t.Fatal("request snapshot/revocation boundary incorrect") } foreign, err := s.LoadReadPathPolicy("other", "p") if err != nil { t.Fatal(err) } if foreign.Allows("/exact_%/A") { t.Fatal("cross-Domain grant accepted") } }