简体   繁体   English

MongoDB 自定义用户角色 - “不允许用户(...)”

[英]MongoDB custom user roles - "user is not allowed(...)"

I created a free tier cluster on MongoDB Atlas (it has 3 shards) and I want my Node.js app to connect with a database I created there, using a specific user, that will be restricted from using any other database than the one inteded for this app.我在 MongoDB Atlas 上创建了一个免费层集群(它有 3 个分片),我希望我的 Node.js 应用程序连接到我在那里创建的数据库,使用特定用户,该用户将被限制使用除指定数据库之外的任何其他数据库对于这个应用程序。

So step by step.就这样一步步来。

I create a database called, let's say, test .我创建了一个名为test的数据库。

I create a role here - I go to Security -> MongoDB Roles -> Add New Custom Role and I give it all Collection actions and all Database actions and roles to test在这里创建一个角色 - 我去Security -> MongoDB Roles -> Add New Custom Role ,我给它所有的Collection actions和所有的Database actions and rolestest

Time for a user, so again Security -> MongoDB Users -> Add New User and I assign a previously created role to it so it has access only to test database.用户的时间,所以再次Security -> MongoDB Users -> Add New User ,我为其分配一个先前创建的角色,因此它只能访问test数据库。 So now I have 2 users - atlasAdmin and my created user.所以现在我有 2 个用户 - atlasAdmin 和我创建的用户。

That's where the problem occurs, when I use admin user in my app to connect, .find() or .create() it works fine all the time.这就是问题所在,当我在我的应用程序中使用管理员用户进行连接时, .find().create()它始终可以正常工作。 With a user with custom role, it works for like 10mins/1 connection (until I shut down the local server I have my node app on) and the next time I get an error that "user is not allowed to perform action (...)".对于具有自定义角色的用户,它的工作时间约为 10 分钟/1 个连接(直到我关闭了我的节点应用程序所在的本地服务器),下次我收到错误消息“不允许用户执行操作(.. .)”。

I tried everything, tinkering with a string I use to connect, updating mongoose (I use it in my app), creating user and custom role using mongodb shell but nothing seems to work.我尝试了一切,修改我用来连接的字符串,更新mongoose (我在我的应用程序中使用它),使用 mongodb shell 创建用户和自定义角色,但似乎没有任何效果。

HOWEVER:然而:

  • if I have this custom user, my app connects with it to the database and it works, then on the next connection it stops working AND I go here and just click UPDATE USER without changing anything there (I just click edit next to the user and then update ) then wait for the cluster to make changes, it will work again for like +/- one connection.如果我有这个自定义用户,我的应用程序将它连接到数据库并且它可以工作,然后在下一个连接上它停止工作我去这里点击UPDATE USER而不改变那里的任何东西(我只点击用户旁边的edit和然后update ) 然后等待集群进行更改,它将再次为类似 +/- 一个连接工作。

  • everything works just fine if my app uses admin account如果我的应用程序使用管理员帐户,一切正常

Anyone had similar problem?任何人有类似的问题? Screenshot of the error I was also thinking that it might be because of how many times I try to connect with mongo from the app (I use nodemon so everytime I save a file with changes, server restarts, thus connecting to database again) but I think that's not the case - if it was, why would I be able to make it work with admin user?错误的屏幕截图我也在想这可能是因为我尝试从应用程序连接 mongo 的次数(我使用nodemon所以每次我保存一个有更改的文件时,服务器重新启动,从而再次连接到数据库)但是我认为情况并非如此 - 如果是这样,为什么我能够让它与管理员用户一起使用?

The string I use to connect with mongo:我用来连接mongo的字符串:

// DATABASE SETUP
var dbURL = 'mongodb://[cluster0:port],[cluster1:port],[cluster2:port]/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true';
var options = {
    useNewUrlParser: true,
    dbName: "test"
    user: [login],
    pass: [pass]
};
mongoose.connect(dbURL, options);

I have also encountered this problem on Atlas Free tier, not just on NodeJS but Java as well我在 Atlas Free tier 上也遇到过这个问题,不仅在 NodeJS 上,在 Java 上也是如此

For now, you can try mitigating this problem by using a default role instead of having a custom one现在,您可以尝试使用默认角色而不是自定义角色来缓解此问题

On the MongoDB Users tab, click "Edit" on your user => Add Default Privileges在 MongoDB 用户选项卡上,单击用户的“编辑”=> 添加默认权限

Picture 1图片1

Then select "readWrite" and type your database name on the first field, then save the user然后选择“readWrite”并在第一个字段中键入您的数据库名称,然后保存用户

Picture 2图二

Or, if you want database administration, add another field with "dbAdmin" role或者,如果您想要数据库管理,请添加另一个具有“dbAdmin”角色的字段

Picture 3图3

At least that's how I solved it.至少我是这样解决的。 I hope this helps.我希望这有帮助。

Side note: You can also use the shorter connection string (MongoDB+SRV) and it would still work.旁注:您也可以使用较短的连接字符串 (MongoDB+SRV),它仍然可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM