Skip to content

Commit

Permalink
Fix #152. Don't automatically create databases when creating database…
Browse files Browse the repository at this point in the history
… users.
  • Loading branch information
toddboom committed May 7, 2014
1 parent f831e80 commit 7faf680
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/cluster/cluster_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ func (self *ClusterConfiguration) GetDatabases() []*Database {
return dbs
}

func (self *ClusterConfiguration) DatabaseExists(name string) bool {
fmt.Println(self.DatabaseReplicationFactors)
if _, ok := self.DatabaseReplicationFactors[name]; ok {
return true
} else {
return false
}
}

func (self *ClusterConfiguration) CreateDatabase(name string, replicationFactor uint8) error {
self.createDatabaseLock.Lock()
defer self.createDatabaseLock.Unlock()
Expand Down
5 changes: 4 additions & 1 deletion src/coordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,10 @@ func (self *CoordinatorImpl) CreateDbUser(requester common.User, db, username, p
return err
}

self.CreateDatabase(requester, db, uint8(1)) // ignore the error since the db may exist
if !self.clusterConfiguration.DatabaseExists(db) {
return fmt.Errorf("No such database %s", db)
}

if self.clusterConfiguration.GetDbUser(db, username) != nil {
return fmt.Errorf("User %s already exists", username)
}
Expand Down
2 changes: 2 additions & 0 deletions src/integration/multiple_servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func (self *ServerSuite) TestUdpInterface(c *C) {
}

func (self *ServerSuite) TestShouldNotResetRootsPassword(c *C) {
self.serverProcesses[0].Post("/db?u=root&p=root", "{\"name\":\"dummy_db\"}", c)
resp := self.serverProcesses[0].Post("/db/dummy_db/users?u=root&p=root", "{\"name\":\"root\", \"password\":\"pass\"}", c)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
self.serverProcesses[0].WaitForServerToSync()
Expand Down Expand Up @@ -409,6 +410,7 @@ func (self *ServerSuite) TestDropSeries(c *C) {
case 2:
resp := self.serverProcesses[0].Delete("/db/drop_series?u=root&p=root", "", c)
c.Assert(resp.StatusCode, Equals, http.StatusNoContent)
self.serverProcesses[0].Post("/db?u=root&p=root", `{"name": "drop_series"}`, c)
self.serverProcesses[0].Post("/db/drop_series/users?u=root&p=root", `{"name": "paul", "password": "pass"}`, c)
}

Expand Down
12 changes: 12 additions & 0 deletions src/integration/single_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ = Suite(&SingleServerSuite{})
func (self *SingleServerSuite) createUser(c *C) {
client, err := influxdb.NewClient(&influxdb.ClientConfig{})
c.Assert(err, IsNil)
c.Assert(client.CreateDatabase("db1"), IsNil)
c.Assert(client.CreateDatabaseUser("db1", "user", "pass"), IsNil)
c.Assert(client.AlterDatabasePrivilege("db1", "user", true), IsNil)
}
Expand Down Expand Up @@ -178,6 +179,7 @@ func (self *SingleServerSuite) TestUserWritePermissions(c *C) {

// 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)

config := &influxdb.ClientConfig{
Expand Down Expand Up @@ -238,6 +240,7 @@ func (self *SingleServerSuite) TestUserReadPermissions(c *C) {

// 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_user2", "pass", "test_should_read", "^$"), IsNil)

data := `
Expand Down Expand Up @@ -306,6 +309,8 @@ func (self *SingleServerSuite) TestAdminPermissionToDeleteData(c *C) {
func (self *SingleServerSuite) TestShortPasswords(c *C) {
client, err := influxdb.NewClient(&influxdb.ClientConfig{})
c.Assert(err, IsNil)

c.Assert(client.CreateDatabase("shahid"), IsNil)
c.Assert(client.CreateDatabaseUser("shahid", "shahid", "1"), Not(ErrorMatches), ".*blowfish.*")

// should be able to recreate the user
Expand Down Expand Up @@ -530,3 +535,10 @@ func (self *SingleServerSuite) TestDeleteQuery(c *C) {
c.Assert(data, HasLen, 0)
}
}

func (self *SingleServerSuite) TestInvalidDbUserCreation(c *C) {
client, err := influxdb.NewClient(&influxdb.ClientConfig{})
c.Assert(err, IsNil)

c.Assert(client.CreateDatabaseUser("db999", "user", "pass"), NotNil)
}

0 comments on commit 7faf680

Please sign in to comment.