Skip to content
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

How to check which series can be read/ written #648

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/api/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,10 @@ type ApiUser struct {
}

type UserDetail struct {
Name string `json:"name"`
IsAdmin bool `json:"isAdmin"`
Name string `json:"name"`
IsAdmin bool `json:"isAdmin"`
WriteTo string `json:"writeTo"`
ReadFrom string `json:"readFrom"`
}

type ContinuousQuery struct {
Expand Down Expand Up @@ -733,7 +735,7 @@ func (self *HttpServer) listDbUsers(w libhttp.ResponseWriter, r *libhttp.Request

users := make([]*UserDetail, 0, len(dbUsers))
for _, dbUser := range dbUsers {
users = append(users, &UserDetail{dbUser.GetName(), dbUser.IsDbAdmin(db)})
users = append(users, &UserDetail{dbUser.GetName(), dbUser.IsDbAdmin(db), dbUser.GetWritePermission(), dbUser.GetReadPermission()})
}
return libhttp.StatusOK, users
})
Expand All @@ -749,7 +751,7 @@ func (self *HttpServer) showDbUser(w libhttp.ResponseWriter, r *libhttp.Request)
return errorToStatusCode(err), err.Error()
}

userDetail := &UserDetail{user.GetName(), user.IsDbAdmin(db)}
userDetail := &UserDetail{user.GetName(), user.IsDbAdmin(db), user.GetWritePermission(), user.GetReadPermission()}

return libhttp.StatusOK, userDetail
})
Expand Down
4 changes: 2 additions & 2 deletions src/api/http/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ func (self *ApiSuite) TestDbUsersIndex(c *C) {
err = json.Unmarshal(body, &users)
c.Assert(err, IsNil)
c.Assert(users, HasLen, 1)
c.Assert(users[0], DeepEquals, &UserDetail{"db_user1", false})
c.Assert(users[0], DeepEquals, &UserDetail{"db_user1", false, ".*", ".*"})
}

func (self *ApiSuite) TestDbUserShow(c *C) {
Expand All @@ -732,7 +732,7 @@ func (self *ApiSuite) TestDbUserShow(c *C) {
userDetail := &UserDetail{}
err = json.Unmarshal(body, &userDetail)
c.Assert(err, IsNil)
c.Assert(userDetail, DeepEquals, &UserDetail{"db_user1", false})
c.Assert(userDetail, DeepEquals, &UserDetail{"db_user1", false, ".*", ".*"})
}

func (self *ApiSuite) TestDatabasesIndex(c *C) {
Expand Down
8 changes: 8 additions & 0 deletions src/api/http/mock_user_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ func (self MockDbUser) HasWriteAccess(_ string) bool {
return true
}

func (self MockDbUser) GetWritePermission() string {
return ".*"
}

func (self MockDbUser) HasReadAccess(_ string) bool {
return true
}

func (self MockDbUser) GetReadPermission() string {
return ".*"
}

type MockUserManager struct {
UserManager
dbUsers map[string]map[string]MockDbUser
Expand Down
24 changes: 24 additions & 0 deletions src/cluster/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,18 @@ func (self *ClusterAdmin) HasWriteAccess(_ string) bool {
return true
}

func (self *ClusterAdmin) GetWritePermission() string {
return ".*"
}

func (self *ClusterAdmin) HasReadAccess(_ string) bool {
return true
}

func (self *ClusterAdmin) GetReadPermission() string {
return ".*"
}

type DbUser struct {
CommonUser `json:"common"`
Db string `json:"db"`
Expand All @@ -118,6 +126,14 @@ func (self *DbUser) HasWriteAccess(name string) bool {
return false
}

func (self *DbUser) GetWritePermission() string {
if len(self.WriteTo) > 0 {
matcher := self.WriteTo[0]
return matcher.Name
}
return ""
}

func (self *DbUser) HasReadAccess(name string) bool {
for _, matcher := range self.ReadFrom {
if matcher.Matches(name) {
Expand All @@ -128,6 +144,14 @@ func (self *DbUser) HasReadAccess(name string) bool {
return false
}

func (self *DbUser) GetReadPermission() string {
if len(self.ReadFrom) > 0 {
matcher := self.ReadFrom[0]
return matcher.Name
}
return ""
}

func (self *DbUser) GetDb() string {
return self.Db
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ type User interface {
IsDbAdmin(db string) bool
GetDb() string
HasWriteAccess(name string) bool
GetWritePermission() string
HasReadAccess(name string) bool
GetReadPermission() string
}
16 changes: 16 additions & 0 deletions src/integration/single_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,24 @@ func (self *SingleServerSuite) TestSingleServerHostnameChange(c *C) {
func (self *SingleServerSuite) TestUserWritePermissions(c *C) {
rootUser := self.server.GetClient("", c)

verifyPermissions := func(db string, name string, readFrom string, writeTo string) {
users, _ := rootUser.GetDatabaseUserList(db)
matched := false
for _, user := range users {
if user["name"] == name {
c.Assert(user["readFrom"], DeepEquals, readFrom)
c.Assert(user["writeTo"], DeepEquals, writeTo)
matched = true
}
}
c.Assert(matched, Equals, true)
}

// create two users one that can only read and one that can only write. both can access test_should_read
// series only
/* c.Assert(rootUser.CreateDatabase("db1"), IsNil) */
c.Assert(rootUser.CreateDatabaseUser("db1", "limited_user", "pass", "^$", "^$"), IsNil)
verifyPermissions("db1", "limited_user", "^$", "^$")

config := &influxdb.ClientConfig{
Username: "limited_user",
Expand Down Expand Up @@ -251,6 +265,7 @@ func (self *SingleServerSuite) TestUserWritePermissions(c *C) {
content := self.server.RunQueryAsRoot("select * from test_should_write", "m", c)
c.Assert(content, HasLen, 0)
rootUser.ChangeDatabaseUser("db1", "limited_user", "pass", false, "^$", "test_should_write")
verifyPermissions("db1", "limited_user", "^$", "test_should_write")
// write the data to test the write permissions
c.Assert(user.WriteSeries(series), IsNil)
self.server.WaitForServerToSync()
Expand All @@ -263,6 +278,7 @@ func (self *SingleServerSuite) TestUserWritePermissions(c *C) {
content = self.server.RunQueryAsRoot("select * from test_should_not_write", "m", c)
c.Assert(content, HasLen, 0)
rootUser.ChangeDatabaseUser("db1", "limited_user", "pass", false, "^$", "test_.*")
verifyPermissions("db1", "limited_user", "^$", "test_.*")
c.Assert(user.WriteSeries(invalidSeries), IsNil)
self.server.WaitForServerToSync()
content = self.server.RunQueryAsRoot("select * from test_should_not_write", "m", c)
Expand Down