Skip to content

Commit

Permalink
fix #42. support name when creating cluster admins and db users as …
Browse files Browse the repository at this point in the history
…well as `username`
  • Loading branch information
jvshahid committed Nov 12, 2013
1 parent 09379a2 commit 69a1989
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@
### Bugfixes

- [Issue #36](https://github.com/influxdb/influxdb/issues/36). The regex operator should be =~ not ~=
- Preparing to deprecate `username` field for a more consistent `name` field in the `/db/:db/users`
- Preparing to deprecate endpoints `/db/:db/admins/:user` in favor of using `/db/:db/users/:user` which should
be used to update user flags, password, etc.
19 changes: 14 additions & 5 deletions src/api/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ func (self *HttpServer) tryAsClusterAdmin(w libhttp.ResponseWriter, r *libhttp.R
}

type NewUser struct {
Name string `json:"username"`
Username string `json:"username"`
Name string `json:"name"`
Password string `json:"password"`
}

Expand Down Expand Up @@ -589,11 +590,15 @@ func (self *HttpServer) createClusterAdmin(w libhttp.ResponseWriter, r *libhttp.
}

self.tryAsClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.CreateClusterAdminUser(u, newUser.Name); err != nil {
username := newUser.Name
if username == "" {
username = newUser.Username
}
if err := self.userManager.CreateClusterAdminUser(u, username); err != nil {
errorStr := err.Error()
return errorToStatusCode(err), errorStr
}
if err := self.userManager.ChangeClusterAdminPassword(u, newUser.Name, newUser.Password); err != nil {
if err := self.userManager.ChangeClusterAdminPassword(u, username, newUser.Password); err != nil {
return errorToStatusCode(err), err.Error()
}
return libhttp.StatusOK, nil
Expand Down Expand Up @@ -725,10 +730,14 @@ func (self *HttpServer) createDbUser(w libhttp.ResponseWriter, r *libhttp.Reques
db := r.URL.Query().Get(":db")

self.tryAsDbUserAndClusterAdmin(w, r, func(u common.User) (int, interface{}) {
if err := self.userManager.CreateDbUser(u, db, newUser.Name); err != nil {
username := newUser.Name
if username == "" {
username = newUser.Username
}
if err := self.userManager.CreateDbUser(u, db, username); err != nil {
return errorToStatusCode(err), err.Error()
}
if err := self.userManager.ChangeDbUserPassword(u, db, newUser.Name, newUser.Password); err != nil {
if err := self.userManager.ChangeDbUserPassword(u, db, username, newUser.Password); err != nil {
return libhttp.StatusUnauthorized, err.Error()
}
return libhttp.StatusOK, nil
Expand Down
18 changes: 16 additions & 2 deletions src/api/http/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,23 @@ func (self *ApiSuite) TestClusterAdminOperations(c *C) {
}

func (self *ApiSuite) TestDbUserOperations(c *C) {
// create user using the `name` field
url := self.formatUrl("/db/db1/users?u=root&p=root")
resp, err := libhttp.Post(url, "", bytes.NewBufferString(`{"username":"dbuser", "password": "password"}`))
resp, err := libhttp.Post(url, "", bytes.NewBufferString(`{"name":"dbuser", "password": "password"}`))
c.Assert(err, IsNil)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, libhttp.StatusOK)
c.Assert(self.manager.ops, HasLen, 2)
c.Assert(self.manager.ops[0].operation, Equals, "db_user_add")
c.Assert(self.manager.ops[0].username, Equals, "dbuser")
c.Assert(self.manager.ops[1].operation, Equals, "db_user_passwd")
c.Assert(self.manager.ops[1].username, Equals, "dbuser")
c.Assert(self.manager.ops[1].password, Equals, "password")
self.manager.ops = nil

// create user using the `username` field
url = self.formatUrl("/db/db1/users?u=root&p=root")
resp, err = libhttp.Post(url, "", bytes.NewBufferString(`{"username":"dbuser", "password": "password"}`))
c.Assert(err, IsNil)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, libhttp.StatusOK)
Expand Down Expand Up @@ -582,7 +597,6 @@ func (self *ApiSuite) TestBasicAuthentication(c *C) {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
fmt.Printf("body: %s\n", string(body))
c.Assert(resp.StatusCode, Equals, libhttp.StatusOK)
users := []*Database{}
err = json.Unmarshal(body, &users)
Expand Down

0 comments on commit 69a1989

Please sign in to comment.