(2018-01-25 15:52:29)html
转载▼mongodb
标签: mongodbshard最小权限shardcollectionprivileges |
分类: 数据库 |
操做需求是为程序用户提供一个最小权限,知足如下操做需求:数据库
经过这样限制,最多只会影响到指定的数据库,
避免由于程序问题或者其余外部因素致使数据处理逻辑出问题而影响到整个集群。ide
以myDb.myCollection
为例,对collection开启shard,须要进行如下操做:测试
若是数据库没有开启分片功能,须要先开启:ui
sh.enableSharding("myDb")
若是collection里面已经有数据,须要先建立对应的shardkey索引:this
use myDb db.myCollection.createIndex( {"shard_key_field": "hashed"} )
对指定collection开启shard:spa
sh.shardCollection("myDb.myCollection", {"shard_key_field": "hashed"})
因此须要相应的权限来运行以上3个操做命令。
参考官网文档描述,须要给用户提供两个权限:code
https://docs.mongodb.com/manual/reference/privilege-actions/#enableShardinghtm
enableSharding
User can enable sharding on a database using theenableSharding
command and can shard a collection using theshardCollection
command. Apply this action to database or collection resources.
https://docs.mongodb.com/manual/reference/privilege-actions/#createIndex
createIndex
Provides access to thedb.collection.createIndex()
method and thecreateIndexes
command. Apply this action to database or collection resources.
由于原来项目用户关于业务数据库的角色就已是dbOwner
,
所以,再建立一个具备开启shard相关操做权限的角色,授予项目数据库用户便可实现最小权限控制。
首先建立新的角色,提供上述的两个操做权限。
这里命名为opShardRole
,能够根据实际需求更改:
use myDb; db.createRole({ role: "opShardRole", privileges:[ { resource:{db:"myDb", collection:""}, actions:["enableSharding, "createIndex"] } ], roles:[] } )
而后将opShardRole
角色grant给项目用户,这里假设用户名为project_user
:
db.grantRolesToUser("project_user",[{role:"opShardRole", db:"myDb"}])
至此,操做完成,能够经过项目用户进行测试开启shard相关操做有无问题。
切换到业务db,并检查相关的用户及角色:
查看角色相关操做权限:
mongos> db.getRole("opShardRole",{showPrivileges: true}) { "role" : "opShardRole", "db" : "myDb", "isBuiltin" : false, "roles" : [ ], "inheritedRoles" : [ ], "privileges" : [ { "resource" : { "db" : "myDb", "collection" : "" }, "actions" : [ "createIndex", "enableSharding", "shardCollection" ] } ], ... }
查看用户关联的角色:
mongos> show users; { "_id" : "myDb.project_user", "user" : "project_user", "db" : "myDb", "roles" : [ { "role" : "opShardRole", "db" : "myDb" }, { "role" : "dbOwner", "db" : "myDb" } ] }
能够看到,opShardRole
这个角色确实已经有了对业务数据库myDb
的所需shard操做权限,
同时,project_user
这个用户也已经有了opShardRole
角色受权。
分享: