centos 切换用户_MongoDB运维与开发(四)用户权限管理2

d23f3c9f28a0b7cf3c66eb4a3ac9cbbd.png

d017a1e3c0bc5d4261fe152192828146.gif//

MongoDB运维与开发(四)---用户权限管理

//

    上次的文章中我们说到了MongoDB中的用户初始化,重点说了启用访问控制的方法、角色与用户的关系、用户的创建、修改、删除、查询方法、而且举了几个小的例子来说明上面的过程,今天我们来看用户的授权和权限回收。

ff2816e9f428df9132bc60735f9803f5.pngMongoDB授权与回收权限

如何授予或者删除用户权限? 

想要在已有的用户上添加角色或者权限,有下面该两种方法:

1、db.grantRolesToUser()

该方法将指定角色授予给自定义的用户,它的语法格式为:

db.grantRolesToUser("<username>",[<roles>],{<writeConcern>})

其中,writeConcern参数可选,后续我们会分析这个参数,这里不做赘述。

2、db.revokeRolesFromUser()

该方法将回收自定义角色的某些权限,它的语法格式为:

db.revokeRolesFromUser("<username>",[<roles>],{<writeConcern>})

下面我们来看他们的具体用法,先来看角色授权:

> db.system.users.find().pretty()
{
        "_id" : "admin.root",
        "user" : "root",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
{
        "_id" : "test.yeyz",
        "user" : "yeyz",
        "db" : "test",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "test"
                }
        ],
        "customData" : {
                "desc" : "this is user yeyz"
        }
}

可以看到,yeyz用户具有test库下面的read权限。我们来验证一下这个权限:

[root@VM-0-14-centos ~]# mongo -u "yeyz" -p "123456" --authenticationDatabase "test"
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?authSource=test&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9d4929be-e7f0-46bf-9fa6-4d37e5a84ad1") }
MongoDB server version: 4.0.6

> show dbs
test  0.000GB
> show collections
yeyz

> db.yeyz.find()
{ "_id" : ObjectId("5fa172c86e2f58da10d28aa3"), "name" : "yeyz" }
> db.yeyz.insert({"name":"zhangsan"})
WriteCommandError({
        "ok" : 0,
        "errmsg" : "not authorized on test to execute command { insert: \"yeyz\", ordered: true, lsid: { id: UUID(\"9d4929be-e7f0-46bf-9fa6-4d37e5a84ad1\") }, $db: \"test\" }",
        "code" : 13,
        "codeName" : "Unauthorized"
})
> exit
bye

可以看到,这个yeyz的账户,仅仅对yeyz这个集合有read的权限,当我们执行insert操作的时候,报错了,认为没有权限。

此时我们切换到root用户,使用grantRolesToUser来给他添加权限:

> db.grantRolesToUser("yeyz",[{role:"readWrite",db:"test"}])

权限角色变为readWrite,这样重新使用账号yeyz进行登录并对yeyz这个集合进行insert操作,发现执行成功,如下:

[root@VM-0-14-centos ~]# mongo -u "yeyz" -p "123456" --authenticationDatabase "test"
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?authSource=test&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bc2f783f-7807-41cd-82fc-e19cf9a822e7") }
MongoDB server version: 4.0.6
> db
test
> db.yeyz.insert({"name":"zhangsan"})
WriteResult({ "nInserted" : 1 })

> db.yeyz.find()
{ "_id" : ObjectId("5fa172c86e2f58da10d28aa3"), "name" : "yeyz" }
{ "_id" : ObjectId("5fa1743ac69e2dfa962884d7"), "name" : "zhangsan" }

接下来我们演示回收权限的过程:

在上面的yeyz的权限基础上,我们回收yeyz这个账号的readWrite权限,切换到root用户,执行下面的命令:

> use test
switched to db test
> db.revokeRolesFromUser("yeyz",[{role:"readWrite",db:"test"}])

再次使用yeyz这个用户去执行插入操作:

[root@VM-0-14-centos ~]# mongo -u "yeyz" -p "123456" --authenticationDatabase "test"
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?authSource=test&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a09a5449-28f6-4479-9b1a-861b0f8418ac") }
MongoDB server version: 4.0.6
> db.yeyz.insert({"name":"wangwu"})
WriteCommandError({
        "ok" : 0,
        "errmsg" : "not authorized on test to execute command { insert: \"yeyz\", ordered: true, lsid: { id: UUID(\"a09a5449-28f6-4479-9b1a-861b0f8418ac\") }, $db: \"test\" }",
        "code" : 13,
        "codeName" : "Unauthorized"
})

可以发现,插入操作已经报错了,再来看看现在yeyz的权限:

> db.system.users.find().pretty()
{
        "_id" : "admin.root",
        "user" : "root",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
{
        "_id" : "test.yeyz",
        "user" : "yeyz",
        "db" : "test",
        "roles" : [ ],
        "customData" : {
                "desc" : "this is user yeyz"
        }
}

可以发现,yeyz这个用户已经没有任何的权限了,包括读的权限都已经没有了,我们现在试下这个用户还能不能登录:

[root@VM-0-14-centos ~]# mongo -u "yeyz" -p "123456" --authenticationDatabase "test"
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?authSource=test&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1e968648-44c5-46c1-8dfc-47915b89be28") }
MongoDB server version: 4.0.6
> show collections
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> db
test
> use test
switched to db test
> db.yeyz.find()
Error: error: {
        "ok" : 0,
        "errmsg" : "not authorized on test to execute command { find: \"yeyz\", filter: {}, lsid: { id: UUID(\"1e968648-44c5-46c1-8dfc-47915b89be28\") }, $db: \"test\" }",
        "code" : 13,
        "codeName" : "Unauthorized"
}

可以看到,用户可以登录,但是不能执行任何操作,test下面的show collections操作都不可以了。

这里我们简单总结一下:

1、grantRolesToUser和revokeRolesFromUser这两个方法可以用来给用户分配角色和回收用户的角色。

2、grantRolesToUser将某个角色分配给用户之后,会覆盖用户原来的角色,也就失去了原来的角色权限。

3、revokeRolesFromUser回收用户的角色权限之后,用户的角色会变成空,只有访问权限,没有读写权限。

90aa32c6f7b31ed72aa5c91a895e6925.png 90b1137238274f68e8188b040cf26481.png有帮助的话还希望点下再看哈
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值