mongo数据库用户权限(二)

mongo数据库中存在两种角色,一种是内置角色,另一种是用户自定义的角色。

1:内置角色有数据库上读写数据的权限

  • userAdminAnyDatabase:授予在所有数据库上管理User的权限
  • dbAdminAnyDatabase:授予管理所有数据库的权限

集群管理角色(Cluster Administration Roles):

  • clusterAdmin:授予管理集群的最高权限
  • clusterManager:授予管理和监控集群的权限,A user with this role can access the config and local databases, which are used in sharding and replication, respectively.
  • clusterMonitor:授予监控集群的权限,对监控工具具有readonly的权限
  • hostManager:管理Server

2,用户自定义的角色(User-Defined Roles)

内置角色只能控制User在DB级别上执行的操作,管理员可以创建自定义角色,控制用户在集合级别(Collection-Level)上执行的操作,即,控制User在当前DB的特定集合上执行特定的操作。

在创建角色时,必须明确Role的四个特性:

  • Scope:角色作用的范围,创建在Admin中的角色,能够在其他DB中使用;在其他DB中创建的角色,只能在当前DB中使用;
  • Resource:角色控制的资源,表示授予在该资源上执行特定操作的权限;
  • Privilege Actions:定义了User能够在资源上执行的操作,系统定义Action是:Privilege Actions
  • Inherit:角色能够继承其他角色权限

2.1 角色作用的范围(Scope)

在admin 数据库中创建的角色,Scope是全局的,能够在admin,其他DB和集群中使用,并且能够继承其他DB的Role;而在非admin中创建的角色,Scope是当前数据库,只能在当前DB中使用,只能继承当前数据库的角色。

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 权限的操作(Privilege actions)

MongoDB的权限包由:资源(Resource)和操作(Action)两部分组成,Privilege Actions 定义User能够在资源上执行的操作,例如:MongoDB在文档级别(Document-Level)上执行的读写操作(Query and Write Actions)列表是

  • find
  • insert
  • remove
  • update

3,创建角色

使用db.CreateRole()在当前DB中创建角色,创建的语法示例如下:

复制代码
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数组中,指定被继承的role,即,新建的new_role从roles数组中继承权限:

  • 如果被继承的role在当前DB中,定义的格式是:roles:["role"];
  • 如果被继承的role不在当前DB中,需要使用doc,指定该role所在的DB,定义的格式是:roles:[{role:"role_name", db:"db_name"}];

4,自定义角色管理函数

  • 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.

三,管理用户和权限

1,创建用户

复制代码
use db_name
db.createUser( { user:
"user_name", pwd: "user_pwd", roles: [ { role: "clusterAdmin", db: "admin" }, { role: "readAnyDatabase", db: "admin" }, "readWrite"
] } )
复制代码

为新建的User,授予一个或多个角色,通过roles数组来实现:

  • 如果role存在于当前DB中,roles的格式:roles:["role"];
  • 如果role不存在于当前DB中,roles的格式:roles:[Role:"role_name", db:"db_name"];

2,权限认证(Authenticate)

mongo连接到mongod,有两种权限认证的方式:

  • 在连接时认证用户访问的权限,mongo 使用参数 --authenticationDatabase <dbname> 指定认证数据库;
  • 在连接后,认证用户访问的权限,mongo 没有使用参数 --authenticationDatabase <dbname>,在连接到mongod之后,切换到验证数据库(authentication database)中,使用db.auth() 验证User是否有权限访问当前数据库;
use db_name
db.auth("user_name", "user_pwd" )

3,用户管理函数

  • 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.


猜你喜欢

转载自blog.csdn.net/qq_32367137/article/details/78667142