MongoDB的訪問(wèn)控制能夠有效保證數(shù)據(jù)庫(kù)的安全,訪問(wèn)控制是指綁定Application監(jiān)聽(tīng)的IP地址频祝,設(shè)置監(jiān)聽(tīng)端口尤泽,使用賬戶和密碼登錄
一,訪問(wèn)控制的參數(shù)
1巾钉,綁定IP地址
mongod 參數(shù):--bind_ip?
默認(rèn)值是所有的IP地址都能訪問(wèn)悦冀,該參數(shù)指定MongoDB對(duì)外提供服務(wù)的綁定IP地址,用于監(jiān)聽(tīng)客戶端?Application的連接睛琳,客戶端只能使用綁定的IP地址才能訪問(wèn)mongod盒蟆,其他IP地址是無(wú)法訪問(wèn)的。
2师骗,設(shè)置監(jiān)聽(tīng)端口
mongod 參數(shù):--port
MongoDB 默認(rèn)監(jiān)聽(tīng)的端口是27017历等,該參數(shù)顯式指定MongoDB實(shí)例監(jiān)聽(tīng)的TCP 端口,只有當(dāng)客戶端Application連接的端口和MongoDB實(shí)例監(jiān)聽(tīng)的端口一致時(shí)辟癌,才能連接到MongoDB實(shí)例寒屯。
3,啟用用戶驗(yàn)證
mongod 參數(shù):--auth
默認(rèn)值是不需要驗(yàn)證黍少,即?--noauth寡夹,該參數(shù)啟用用戶訪問(wèn)權(quán)限控制;當(dāng)mongod 使用該參數(shù)啟動(dòng)時(shí)厂置,MongoDB會(huì)驗(yàn)證客戶端連接的賬戶和密碼菩掏,以確定其是否有訪問(wèn)的權(quán)限。如果認(rèn)證不通過(guò)昵济,那么客戶端不能訪問(wèn)MongoDB的數(shù)據(jù)庫(kù)智绸。
Enables authorization to control user’s access to database resources and operations. When authorization is enabled, MongoDB requires all clients to authenticate themselves first in order to determine the access for the client.
4野揪,權(quán)限認(rèn)證
mongo 參數(shù):--username, -u
mongo 參數(shù):--password, -p
mongo 參數(shù):--authenticationDatabase??指定創(chuàng)建User的數(shù)據(jù)庫(kù);在特定的數(shù)據(jù)庫(kù)中創(chuàng)建User瞧栗,該DB就是User的authentication database斯稳。
在連接mongo時(shí),使用參數(shù)--authenticationDatabase迹恐,會(huì)認(rèn)證 -u 和 -p 參數(shù)指定的賬戶和密碼挣惰。如果沒(méi)有指定驗(yàn)證數(shù)據(jù)庫(kù),mongo使用連接字符串中指定的DB作為驗(yàn)證數(shù)據(jù)塊殴边。
二憎茂,基于角色的訪問(wèn)控制(Role-Based Access Control)
角色是授予User在指定資源上執(zhí)行指定操作的權(quán)限,MongoDB官方手冊(cè)對(duì)角色的定義是:
A role grants privileges to perform the specified actions on resource.
MongoDB為了方便管理員管理權(quán)限找都,在DB級(jí)別上預(yù)先定義了內(nèi)置角色唇辨;如果用戶需要對(duì)權(quán)限進(jìn)行更為細(xì)致的管理廊酣,MongoDB允許用戶創(chuàng)建自定義的角色能耻,能夠在集合級(jí)別上控制User能夠執(zhí)行的操作。
MongoDB使用角色(Role)授予User訪問(wèn)資源的權(quán)限亡驰,Role決定User能夠訪問(wèn)的數(shù)據(jù)庫(kù)資源和執(zhí)行的操作晓猛。一個(gè)User能夠被授予一個(gè)或多個(gè)Role,如果User沒(méi)有被授予Role凡辱,那么就沒(méi)有訪問(wèn)MongoDB系統(tǒng)的權(quán)限戒职。
A user is granted one or more?roles?that determine the user’s access to database resources and operations. Outside of role assignments, the user has no access to the system.
1,內(nèi)置角色(Built-In Roles)
內(nèi)置角色是MongoDB預(yù)定義的角色透乾,操作的資源是在DB級(jí)別上洪燥。MongoDB擁有一個(gè)SuperUser的角色:root,擁有最大權(quán)限乳乌,能夠在系統(tǒng)的所有資源上執(zhí)行任意操作捧韵。
數(shù)據(jù)庫(kù)用戶角色(Database User Roles):
read:授予User只讀數(shù)據(jù)的權(quán)限
readWrite:授予User讀寫(xiě)數(shù)據(jù)的權(quán)限
數(shù)據(jù)庫(kù)管理角色(Database Administration Roles):
dbAdmin:在當(dāng)前dB中執(zhí)行管理操作
dbOwner:在當(dāng)前DB中執(zhí)行任意操作
userAdmin:在當(dāng)前DB中管理User
備份和還原角色(Backup and Restoration Roles):
backup
restore
跨庫(kù)角色(All-Database Roles):
readAnyDatabase:授予在所有數(shù)據(jù)庫(kù)上讀取數(shù)據(jù)的權(quán)限
readWriteAnyDatabase:授予在所有數(shù)據(jù)庫(kù)上讀寫(xiě)數(shù)據(jù)的權(quán)限
userAdminAnyDatabase:授予在所有數(shù)據(jù)庫(kù)上管理User的權(quán)限
dbAdminAnyDatabase:授予管理所有數(shù)據(jù)庫(kù)的權(quán)限
集群管理角色(Cluster Administration Roles):
clusterAdmin:授予管理集群的最高權(quán)限
clusterManager:授予管理和監(jiān)控集群的權(quán)限,A user with this role can access the config and local databases, which are used in sharding and replication, respectively.
clusterMonitor:授予監(jiān)控集群的權(quán)限汉操,對(duì)監(jiān)控工具具有readonly的權(quán)限
hostManager:管理Server
2再来,用戶自定義的角色(User-Defined Roles)
內(nèi)置角色只能控制User在DB級(jí)別上執(zhí)行的操作,管理員可以創(chuàng)建自定義角色磷瘤,控制用戶在集合級(jí)別(Collection-Level)上執(zhí)行的操作芒篷,即,控制User在當(dāng)前DB的特定集合上執(zhí)行特定的操作采缚。
在創(chuàng)建角色時(shí)针炉,必須明確Role的四個(gè)特性:
Scope:角色作用的范圍,創(chuàng)建在Admin中的角色扳抽,能夠在其他DB中使用糊识;在其他DB中創(chuàng)建的角色绩社,只能在當(dāng)前DB中使用;
Resource:角色控制的資源赂苗,表示授予在該資源上執(zhí)行特定操作的權(quán)限愉耙;
Privilege Actions:定義了User能夠在資源上執(zhí)行的操作,系統(tǒng)定義Action是:Privilege Actions拌滋;
Inherit:角色能夠繼承其他角色權(quán)限
2.1 角色作用的范圍(Scope)
在admin 數(shù)據(jù)庫(kù)中創(chuàng)建的角色朴沿,Scope是全局的,能夠在admin败砂,其他DB和集群中使用赌渣,并且能夠繼承其他DB的Role;而在非admin中創(chuàng)建的角色昌犹,Scope是當(dāng)前數(shù)據(jù)庫(kù)坚芜,只能在當(dāng)前DB中使用,只能繼承當(dāng)前數(shù)據(jù)庫(kù)的角色斜姥。
A role created in the admin database can include privileges that apply to the admin database, other databases or to the cluster resource, and can inherit from roles in other databases as well as the admin database.?Except for roles created in the admin database, a role can only include privileges that apply to its database and can only inherit from other roles in its database.?
2.2?權(quán)限的操作(Privilege actions)
MongoDB的權(quán)限包由:資源(Resource)和操作(Action)兩部分組成鸿竖,Privilege Actions 定義User能夠在資源上執(zhí)行的操作,例如:MongoDB在文檔級(jí)別(Document-Level)上執(zhí)行的讀寫(xiě)操作(Query and Write Actions)列表是:
find
insert
remove
update
3铸敏,創(chuàng)建角色
使用db.CreateRole()在當(dāng)前DB中創(chuàng)建角色缚忧,創(chuàng)建的語(yǔ)法示例如下:
use admin
db.createRole(
? {
? ? role: "new_role",
? ? privileges: [
? ? ? { resource: { cluster: true }, actions: [ "addShard" ] },
? ? ? { resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] },
? ? ? { resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] },
? ? ? { resource: { db: "", collection: "" }, actions: [ "find" ] }
? ? ],
? ? roles: [
? ? ? { role: "read", db: "admin" }
? ? ]
? },
? { w: "majority" , wtimeout: 5000 }
)
在roles數(shù)組中,指定被繼承的role杈笔,即闪水,新建的new_role從roles數(shù)組中繼承權(quán)限:
如果被繼承的role在當(dāng)前DB中,定義的格式是:roles:["role"]蒙具;
如果被繼承的role不在當(dāng)前DB中球榆,需要使用doc,指定該role所在的DB禁筏,定義的格式是:roles:[{role:"role_name", db:"db_name"}]持钉;
4,自定義角色管理函數(shù)
db.createRole():Creates a role and specifies its privileges.
db.updateRole():Updates a user-defined role.
db.dropRole():Deletes a user-defined role.
db.dropAllRoles():Deletes all user-defined roles associated with a database.
db.grantPrivilegesToRole():Assigns privileges to a user-defined role.
db.revokePrivilegesFromRole():Removes the specified privileges from a user-defined role.
db.grantRolesToRole():Specifies roles from which a user-defined role inherits privileges.
db.revokeRolesFromRole():Removes inherited roles from a role.
db.getRole():Returns information for the specified role.
db.getRoles():Returns information for all the user-defined roles in a database.
三融师,管理用戶和權(quán)限
1右钾,創(chuàng)建用戶
use db_name
db.createUser( {? ? user: "user_name",? ? pwd: "user_pwd",? ? roles: [? { role: "clusterAdmin", db: "admin" },? ? ? ? ? ? ? { role: "readAnyDatabase", db: "admin" },? ? ? ? ? ? ? "readWrite"
] })
為新建的User,授予一個(gè)或多個(gè)角色旱爆,通過(guò)roles數(shù)組來(lái)實(shí)現(xiàn):
如果role存在于當(dāng)前DB中舀射,roles的格式:roles:["role"];
如果role不存在于當(dāng)前DB中,roles的格式:roles:[Role:"role_name", db:"db_name"];
2腺兴,權(quán)限認(rèn)證(Authenticate)
mongo連接到mongod笙僚,有兩種權(quán)限認(rèn)證的方式:
在連接時(shí)認(rèn)證用戶訪問(wèn)的權(quán)限并徘,mongo 使用參數(shù)--authenticationDatabase? 指定認(rèn)證數(shù)據(jù)庫(kù)鸦采;
在連接后毙驯,認(rèn)證用戶訪問(wèn)的權(quán)限竟坛,mongo 沒(méi)有使用參數(shù)--authenticationDatabase?拜鹤,在連接到mongod之后框冀,切換到驗(yàn)證數(shù)據(jù)庫(kù)(authentication database)中,使用db.auth() 驗(yàn)證User是否有權(quán)限訪問(wèn)當(dāng)前數(shù)據(jù)庫(kù)敏簿;
use db_name
db.auth("user_name", "user_pwd" )
3明也,用戶管理函數(shù)
db.auth():Authenticates a user to a database.
db.createUser():Creates a new user.
db.updateUser():Updates user data.
db.changeUserPassword():Changes an existing user’s password.
db.dropAllUsers():Deletes all users associated with a database.
db.dropUser():Removes a single user.
db.grantRolesToUser():Grants a role and its privileges to a user.
db.revokeRolesFromUser():Removes a role from a user.
db.getUser():Returns information about the specified user.
db.getUsers():Returns information about all users associated with a database.