今天主要說下我為了給mongodb數據庫添加authorization,大家應該知道,mongo默認是無auth運行的。這可能是方便小伙伴學習命令吧。
由於之前發布的一個項目,在亞馬遜的雲上,處於內部應用,也沒有給數據庫添加auth,沒有想到,被AWS的監控發現了,提示需要安全措施,不能無auth運行,因為這樣子會被外部網絡查詢攻擊。所以今天要將那個應用重新修改一下mongo客戶端程序的應用方式。程序是python寫的,改起來還是比較方便,不像java還要再編譯打包發布,python程序,現場修改即可應用!
這里,說說python調用mongodb的需要auth的API過程,常見的有兩種方式:
方式1:
1 db_conf = config.mongo 2 db = MongoClient(host=db_conf["ip_addr"], port=db_conf["port"])[db_conf["db_name"]] 3 db.authenticate(db_conf["user_name"], db_conf["password"]) #當不需要auth的時候,這行代碼可以不需要
方式2:
uri = "mongodb://"+conf['mongodb_username']+":"+conf['mongodb_password']+"@"+conf['mongodb_host']+":"+str(conf['mongodb_port'])+"/"+conf['mongodb_db_name'] db = pymongo.MongoClient(uri)[conf['mongodb_db_name']]
看上去是不是很簡單,按照API的需要填寫參數即可。
我的應用發布在兩個雲主機上,不幸的是,兩個主機上的mongodb版本不一樣,悲慘的我,之前不知道有這等大的不同,關於auth的屁大點的事情。一個是2.4.1,一個是2.6.6. 他們之間的差別主要是認證用戶信息存放的位置和管理機制不同。我的應用程序的數據庫名字是BlastdDB。
2.4.1的認證用戶信息是存放在對應的database里面的system.users表里面,而admin表里面是沒有內容的,悲慘的是,僅僅在BlastdDB里面添加用戶是不行的,因為這個時候,你會發現,即使mongod這個server程序啟動時帶上--auth,你再用mongo去訪問,他還是照樣什么都可以操作,什么都可以看。。。撕逼的設計啊。。。
1 root@ec2-54-248-13-173:/opt/mongodb/bin# ./mongo #這個是admin表為空的情況下,但是mongod啟動是帶上了--auth操作,BlastdDB此時也有添加認證用戶 2 MongoDB shell version: 2.4.1 3 connecting to: test 4 > show collections 5 system.indexes 6 test 7 > use BlastdDB 8 switched to db BlastdDB 9 > show collections 10 system.indexes 11 system.users 12 target_service 13 user 14 worker
下面再看看,我將admin數據庫中添加一個認證用戶,並操作一下其他的數據庫,看看日志,就清楚了:
1 > use admin 2 switched to db admin 3 > db.addUser("admin", "admin") 4 { 5 "user" : "admin", 6 "readOnly" : false, 7 "pwd" : "7c67ef13bbd4cae106d959320af3f704", 8 "_id" : ObjectId("569da568a93f15901119708a") 9 } 10 > 11 > use BlastdDB 12 switched to db BlastdDB 13 > show collections 14 Tue Jan 19 02:54:50.239 JavaScript execution failed: error: { 15 "$err" : "not authorized for query on BlastdDB.system.namespaces", 16 "code" : 16550 17 } at src/mongo/shell/query.js:L128 18 > use test 19 switched to db test 20 > show collections 21 Tue Jan 19 02:55:07.620 JavaScript execution failed: error: { 22 "$err" : "not authorized for query on test.system.namespaces", 23 "code" : 16550 24 } at src/mongo/shell/query.js:L128 25 > use BlastdDB 26 switched to db BlastdDB 27 > show collections 28 Tue Jan 19 02:57:48.129 JavaScript execution failed: error: { 29 "$err" : "not authorized for query on BlastdDB.system.namespaces", 30 "code" : 16550 31 } at src/mongo/shell/query.js:L128 32 > db.auth("xxxx", "yyyy") 33 1 34 > show collections 35 system.indexes 36 system.users 37 target_service 38 user 39 worker
但是,對於2.6.6這個版本,就不同了,對於我的數據庫BlastdDB添加認證用戶信息,其實,這些信息不在BlastdDB的system.users表中,而是在admin的system.users表中,所以,當啟動mongod的時候,若帶上--auth,則,登錄BlastdDB,會要求正確的用戶名和密碼信息,否則報錯。和2.4.1的區別,主要是少了一步必須給admin數據庫添加認證用戶(對於2.4.1,這個認證用戶信息不必和應用數據庫的認證信息相同,只要有一個就可以)。另外,就是認證用戶信息的存放位置不同,對於2.6.6是集中存放在admin當中了。當然,admin中也可以創建認證用戶,這個用戶可以查看所有的數據庫的信息了,屬於超級用戶。下面看看我的操作日志,反映一下兩個版本的差別:
1 root@ec2-xxx-xxx-xxx-xxx:/usr/local/mongodb-linux-x86_64-2.6.6/bin# ./mongo #這個是去訪問已經帶有--auth選項的mongod服務程序 2 MongoDB shell version: 2.6.6 3 connecting to: test 4 > use admin 5 switched to db admin 6 > show collections 7 2016-01-19T03:22:51.958+0000 error: { 8 "$err" : "not authorized for query on admin.system.namespaces", 9 "code" : 13 10 } at src/mongo/shell/query.js:131 11 > show dbs 12 2016-01-19T03:22:58.754+0000 listDatabases failed:{ 13 "ok" : 0, 14 "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }", 15 "code" : 13 16 } at src/mongo/shell/mongo.js:47 17 > use BlastdDB 18 switched to db BlastdDB 19 > db.auth("xxxx", "yyyy") 20 1 21 > show collections 22 cloud_provider 23 counters 24 system.indexes 25 user
下面這個日志,可以反映出另外一些信息,比如創建認證用戶的輸出在兩個版本中是不同的,最重要的是看collection中的數據結構的不同,可以反映出認證用戶的管理方式的變化,這個變化可以說明新的版本管理理念要比舊版本先進:
2.4.1:
1 >use admin 2 > db.auth("admin","admin") 3 1 4 > show collections 5 system.indexes 6 system.users 7 > db.system.users.find() 8 { "_id" : ObjectId("569da568a93f15901119708a"), "user" : "admin", "readOnly" : false, "pwd" : "7c67ef13bbd4cae106d959320af3f704"
2.6.6:
1 MongoDB shell version: 2.6.6 2 connecting to: test 3 > use admin 4 switched to db admin 5 > show collections 6 system.indexes 7 system.users 8 system.version 9 > db.system.users.find() 10 { "_id" : "BlastdDB.scrc", "user" : "scrc", "db" : "BlastdDB", "credentials" : { "MONGODB-CR" : "53ba09afcf99336d2af2a5d020e5d51e" }, "roles" : [ { "role" : "dbOwner", "db" : "BlastdDB" } ] } 11 > use admin 12 switched to db admin 13 > db.addUser("admin", "admin") 14 WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead 15 Successfully added user: { "user" : "admin", "roles" : [ "root" ] } 16 > db.system.users.find() 17 { "_id" : "BlastdDB.scrc", "user" : "scrc", "db" : "BlastdDB", "credentials" : { "MONGODB-CR" : "53ba09afcf99336d2af2a5d020e5d51e" }, "roles" : [ { "role" : "dbOwner", "db" : "BlastdDB" } ] } 18 { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "MONGODB-CR" : "7c67ef13bbd4cae106d959320af3f704" }, "roles" : [ { "role" : "root", "db" : "admin" } ] } 19 >
這些變化,方便用戶關注,后續應用中遇到了,可以作為借鑒,不要因為版本的差異導致迷途而浪費了時間。