欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

MongoDB的安全性配置

最编程 2024-08-14 12:23:19
...

MongoDB 安全配置

如果我们想自己的MongoDB在互联网上被远程访问,那么安全是最先考虑的因素,最好是不要轻易的在互联网上暴露数据库,但是如果非得这样做,我们可以使用一些技巧来让我们的数据库相对安全。

主要的技巧如下:

  1. 使用非默认的端口: 可以减少互联网上被端口扫描并定向爆破的概率
  2. 使用密码: 这个是必须的,密码强度越高越好
  3. 使用IP地址端限制: 可以显著的缩小可以远程访问数据库的IP范围,拒绝大部分国家和地区的访问.

修改端口和IP

对MongoDB监听的端口和IP做调整有下面2个目的

  • IP: 默认监听IP为127.0.0.1,我们可能需要开放特定的CIDR来让处于不同主机上的客户端可以连接. 最简单的就是直接允许任意IP的客户端连接,这时CIDR为0.0.0.0.但是这也是很危险的一个操作,意味着全球互联网上的任意一台电脑都可以连接上来,这虽然方便但是也是最危险的,建议指定具体的CIDR,这样可以排除互联网的大量肉鸡进行恶意的连接.
  • 端口: 默认的监听端口为27017,我们可以改为其他端口,躲开互联网上的恶意程序的扫描和连接.

修改/etc/mongod.conf

[thinktik@thinkdev ~]$ vim /etc/mongod.conf

在端口和IP监听部分我们改为如下设置

# network interfaces
net:
  port: 7017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

修改完成后重启服务

[root@thinkdev thinktik]# systemctl restart mongod

如果重启失败,可能是SELinux的安全策略限制了MongoDB使用新的端口,我们需要放行MongoDB使用新的端口

[root@localhost thinktik]# semanage port -a -t mongod_port_t -p tcp 7017
bash: semanage: command not found
# 如果出现上面的错误,可以这样这个解决:
[root@localhost thinktik]# dnf install policycoreutils-python-utils
Last metadata expiration check: 0:10:50 ago on Mon 27 Dec 2021 03:11:19 PM CST.
...

SELinux放行后,我们继续重启即可.

配置防火墙

如果你需要服务被互联网上的其他电脑访问,那么你需要配置防火墙,运行你的端口被访问

# 开放7017端口
[root@thinkvm01 thinktik]# firewall-cmd --zone=public --add-port=7017/tcp --permanent
# 重载防火墙
[root@thinkvm01 thinktik]# firewall-cmd --reload

强制密码认证

登录

我们需要先登录,然后再继续设置用户和密码.

[root@localhost thinktik]# mongosh --port 7017
Current Mongosh Log ID:	61c96c07d367601cc211040c
Connecting to:		mongodb://127.0.0.1:7017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:		5.0.5
Using Mongosh:		1.1.7

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting:
   2021-12-27T15:25:46.438+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2021-12-27T15:25:46.439+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
------

test> 

设置用户

管理员 可以对任意的库做管理

先切换到admin数据库

test> use admin
switched to db admin

添加管理员(管理员权限)

db.createUser({
    user:"admin",
    pwd:"Secure_123db$mon",
    roles:[
        {
            role:"userAdminAnyDatabase",
            db:"admin"
        }
    ]
})

普通用户 可以对某个库做读写

先切换到think_db数据库

test> use think_db
switched to db think_db

添加用户(读写权限)

db.createUser({
    user:"thinktik",
    pwd:"Secure_123@mon",
    roles:[
        {
            role:"readWrite",
            db:"think_db"
        }
    ]
})

设置为强制认证

修改/etc/mongod.conf

[thinktik@thinkdev ~]$ vim /etc/mongod.conf

打开认证

security:
  authorization: enabled

修改完成后重启服务

[root@thinkdev thinktik]# systemctl restart mongod

测试认证效果

[root@localhost thinktik]# mongosh --port 7017
Current Mongosh Log ID:	61c96d439916ef5e5dceb4b5
Connecting to:		mongodb://127.0.0.1:7017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:		5.0.5
Using Mongosh:		1.1.7

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> use think_db;
switched to db think_db
think_db> db.auth('thinktik','Secure_123@mon')
{ ok: 1 }
think_db> 

删除用户

[root@localhost thinktik]# mongosh --port 7017
Current Mongosh Log ID:	61c96e4ec324ba1753b9bbb1
Connecting to:		mongodb://127.0.0.1:7017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:		5.0.5
Using Mongosh:		1.1.7

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> use think_db;
switched to db think_db
think_db> db.system.users.find()
MongoServerError: command find requires authentication

think_db> use admin
admin> db.auth('admin',"Secure_123db$mon")
{ ok: 1 }
admin> db.system.users.find()
...

admin> use think_db
switched to db think_db
think_db> db.dropUser('thinktik')
true
think_db> 

参考:

  • MongoDB security
  • MongoDB 安全认证