From 7faf68094a8af4fe309e08f8643d7684fc7c1a7d Mon Sep 17 00:00:00 2001 From: Todd Persen Date: Wed, 7 May 2014 01:21:22 -0400 Subject: [PATCH] Fix #152. Don't automatically create databases when creating database users. --- src/cluster/cluster_configuration.go | 9 +++++++++ src/coordinator/coordinator.go | 5 ++++- src/integration/multiple_servers_test.go | 2 ++ src/integration/single_server_test.go | 12 ++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/cluster/cluster_configuration.go b/src/cluster/cluster_configuration.go index 524865097df..3d0d5057ddd 100644 --- a/src/cluster/cluster_configuration.go +++ b/src/cluster/cluster_configuration.go @@ -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() diff --git a/src/coordinator/coordinator.go b/src/coordinator/coordinator.go index 8cd0ad3af34..bd071d6f36f 100644 --- a/src/coordinator/coordinator.go +++ b/src/coordinator/coordinator.go @@ -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) } diff --git a/src/integration/multiple_servers_test.go b/src/integration/multiple_servers_test.go index 6d6bee4fc59..124bd28f59f 100644 --- a/src/integration/multiple_servers_test.go +++ b/src/integration/multiple_servers_test.go @@ -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() @@ -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) } diff --git a/src/integration/single_server_test.go b/src/integration/single_server_test.go index 36f8fdadcbb..45980a2fdfc 100644 --- a/src/integration/single_server_test.go +++ b/src/integration/single_server_test.go @@ -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) } @@ -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{ @@ -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 := ` @@ -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 @@ -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) +}