Skip to content

Commit

Permalink
v2http: allow empty role for GET /users
Browse files Browse the repository at this point in the history
Fix #5246.
  • Loading branch information
gyuho committed May 6, 2016
1 parent 879cfe7 commit 153c866
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
15 changes: 8 additions & 7 deletions etcdserver/api/v2http/client_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ type userWithRoles struct {
Roles []auth.Role `json:"roles,omitempty"`
}

type usersCollections struct {
Users []userWithRoles `json:"users"`
}

func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
if !allowMethod(w, r.Method, "GET") {
return
Expand All @@ -311,9 +315,7 @@ func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
return
}

var usersCollections struct {
Users []userWithRoles `json:"users"`
}
ucs := usersCollections{}
for _, userName := range users {
var user auth.User
user, err = sh.sec.GetUser(userName)
Expand All @@ -327,15 +329,14 @@ func (sh *authHandler) baseUsers(w http.ResponseWriter, r *http.Request) {
var role auth.Role
role, err = sh.sec.GetRole(roleName)
if err != nil {
writeError(w, r, err)
return
continue
}
uwr.Roles = append(uwr.Roles, role)
}

usersCollections.Users = append(usersCollections.Users, uwr)
ucs.Users = append(ucs.Users, uwr)
}
err = json.NewEncoder(w).Encode(usersCollections)
err = json.NewEncoder(w).Encode(ucs)

if err != nil {
plog.Warningf("baseUsers error encoding on %s", r.URL)
Expand Down
76 changes: 73 additions & 3 deletions etcdserver/api/v2http/client_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package v2http

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"path"
"strings"
"testing"
Expand Down Expand Up @@ -67,9 +70,15 @@ func (s *mockAuthStore) UpdateUser(user auth.User) (auth.User, error) {
func (s *mockAuthStore) AllRoles() ([]string, error) {
return []string{"awesome", "guest", "root"}, s.err
}
func (s *mockAuthStore) GetRole(name string) (auth.Role, error) { return *s.roles[name], s.err }
func (s *mockAuthStore) CreateRole(role auth.Role) error { return s.err }
func (s *mockAuthStore) DeleteRole(name string) error { return s.err }
func (s *mockAuthStore) GetRole(name string) (auth.Role, error) {
r, ok := s.roles[name]
if ok {
return *r, s.err
}
return auth.Role{}, fmt.Errorf("%q does not exist (%v)", name, s.err)
}
func (s *mockAuthStore) CreateRole(role auth.Role) error { return s.err }
func (s *mockAuthStore) DeleteRole(name string) error { return s.err }
func (s *mockAuthStore) UpdateRole(role auth.Role) (auth.Role, error) {
return *s.roles[role.Role], s.err
}
Expand Down Expand Up @@ -361,6 +370,67 @@ func TestAuthFlow(t *testing.T) {
}
}

func (sh *authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sh.baseUsers(w, r)
}

func TestGetUserGrantedWithNonexistingRole(t *testing.T) {
sh := &authHandler{
sec: &mockAuthStore{
users: map[string]*auth.User{
"root": {
User: "root",
Roles: []string{"root", "foo"},
},
},
roles: map[string]*auth.Role{
"root": {
Role: "root",
},
},
},
cluster: &fakeCluster{id: 1},
}
srv := httptest.NewServer(sh)
defer srv.Close()

req, err := http.NewRequest("GET", "", nil)
if err != nil {
t.Fatal(err)
}
req.URL, err = url.Parse(srv.URL)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")

cli := http.DefaultClient
resp, err := cli.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

var uc usersCollections
if err := json.NewDecoder(resp.Body).Decode(&uc); err != nil {
t.Fatal(err)
}
rootExist := false
for _, u := range uc.Users {
if u.User == "root" {
rootExist = true
for _, rl := range u.Roles {
if rl.Role == "foo" {
t.Fatalf("unexpected role 'foo' (%+v)", uc)
}
}
}
}
if !rootExist {
t.Fatalf("expected 'root' user, got %+v", uc.Users)
}
}

func mustAuthRequest(method, username, password string) *http.Request {
req, err := http.NewRequest(method, "path", strings.NewReader(""))
if err != nil {
Expand Down

0 comments on commit 153c866

Please sign in to comment.