MongoDB 安全和访问权限控制

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

一,访问控制的参数数据库

1,绑定IP地址数组

mongod 参数:--bind_ip <ip address>安全

默认值是全部的IP地址都能访问,该参数指定MongoDB对外提供服务的绑定IP地址,用于监听客户端 Application的链接,客户端只能使用绑定的IP地址才能访问mongod,其余IP地址是没法访问的。app

2,设置监听端口ide

mongod 参数:--port <port>函数

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

3,启用用户验证ui

mongod 参数:--auth this

默认值是不须要验证,即 --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

相关文章
相关标签/搜索