MongoDB 安全和访问权限控制

MongoDB的访问控制能够有效保证数据库的安全,访问控制是指绑定Application监听的IP地址,设置监听端口,使用账户和密码登录

一,访问控制的参数

1,绑定IP地址

mongod 参数:--bind_ip <ip address>

默认值是所有的IP地址都能访问,该参数指定MongoDB对外提供服务的绑定IP地址,用于监听客户端 Application的连接,客户端只能使用绑定的IP地址才能访问mongod,其他IP地址是无法访问的。

2,设置监听端口

mongod 参数:--port <port>

MongoDB 默认监听的端口是27017,该参数显式指定MongoDB实例监听的TCP 端口,只有当客户端Application连接的端口和MongoDB实例监听的端口一致时,才能连接到MongoDB实例。

3,启用用户验证

mongod 参数:--auth 

默认值是不需要验证,即 --noauth,该参数启用用户访问权限控制;当mongod 使用该参数启动时,MongoDB会验证客户端连接的账户和密码,以确定其是否有访问的权限。如果认证不通过,那么客户端不能访问MongoDB的数据库。

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,权限认证

mongo 参数:--username <username>, -u <username>
mongo 参数:--password <password>, -p <password>
mongo 参数:--authenticationDatabase <dbname>  指定创建User的数据库;在特定的数据库中创建User,该DB就是User的authentication database。

在连接mongo时,使用参数 --authenticationDatabase,会认证 -u 和 -p 参数指定的账户和密码。如果没有指定验证数据库,mongo使用连接字符串中指定的DB作为验证数据块。

二,基于角色的访问控制(Role-Based Access Control)

角色是授予User在指定资源上执行指定操作的权限,MongoDB官方手册对角色的定义是:

A role grants privileges to perform the specified actions on resource.

MongoDB为了方便管理员管理权限,在DB级别上预先定义了内置角色;如果用户需要对权限进行更为细致的管理,MongoDB允许用户创建自定义的角色,能够在集合级别上控制User能够执行的操作。
MongoDB使用角色(Role)授予User访问资源的权限,Role决定User能够访问的数据库资源和执行的操作。一个User能够被授予一个或多个Role,如果User没有被授予Role,那么就没有访问MongoDB系统的权限。

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,内置角色(Built-In Roles)

内置角色是MongoDB预定义的角色,操作的资源是在DB级别上。MongoDB拥有一个SuperUser的角色:root,拥有最大权限,能够在系统的所有资源上执行任意操作。

数据库用户角色(Database User Roles):

  • read:授予User只读数据的权限
  • readWrite:授予User读写数据的权限

数据库管理角色(Database Administration Roles):

  • dbAdmin:在当前dB中执行管理操作
  • dbOwner:在当前DB中执行任意操作
  • userAdmin:在当前DB中管理User

备份和还原角色(Backup and Restoration Roles):

  • backup
  • restore

跨库角色(All-Database Roles):

  • readAnyDatabase:授予在所有数据库上读取数据的权限
  • readWriteAnyDatabase:授予在所有数据库上读写数据的权限
  • 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.

 

参考文档:

Role-Based Access Control

Built-In Roles

Collection-Level Access Control

db.createRole()

db.createUser()

Enable Auth

Manage Users and Roles

mongod

posted @ 2016-10-28 18:05  悦光阴  阅读(13722)  评论(2编辑  收藏  举报