How to ease CouchDB read/write restrictions on _us

2019-08-06 15:14发布

问题:

In my couchapp two databases are being used

1 Is for application data 2 Is "_users" database.

In my application In one form I'm trying to implement autocomplete where data source is a "view" created in "_users" database.

Now when I login with normal user id other than admin. While trying to access the view inside "_users" database I'm getting the error 403 which is :

{"error":"forbidden","reason":"Only admins can access design document actions for system databases."}

Is it possible for me to allow and limit the access for non admin users to that view only ? So I can get the list of users from _users database into my application.

回答1:

I've never been able to do many tasks that require much custom with CouchDB by itself. I've always needed a script somewhere else that gives me the info that I need.

What works for me is this setup:

  • A gatekeeper Sinatra app that has admin access to my CouchDB
  • Using CouchDB's config to proxy to my Sinatra app. httpd_global_handlers _my_service {couch_httpd_proxy, handle_proxy_req, <<"http://127.0.0.1:9999">>}

The reason for the proxy is because any request that comes through to your gatekeeper will have the AuthSession token set. Inside your gatekeeper, you can GET localhost:5984/_session passing the AuthSession cookie along, it will tell you who is making the request, allowing you to look them up and see if they have access, or just give everyone access to whatever you like. Another reason for the proxy is to avoid any CORS nonsense since you're making the request to yourserver:5984/_my_service.

Update

A purely client-side/javascript solution means that it will be fundamentally insecure at some point, since well, everything is on the client-side. But perhaps your application, doesn't need to be that secure. That's up to you.

One workaround could be to make your application authenticate as a predefined admin, and then create more admin users that way. You could authenticate once when your application boots or on an as needed basis.

The "problem" is that CouchDB sees the _users database as fundamentally special, and doesn't give you the opportunity to change the credential requirements like other databases. Normally you would be able to use the _security document to give role based or user based access. But that's not possible with _users.

An alternative implementation might be to keep track of your own users and forgo the _users database altogether. In that case you could set your own cookies and have your own login and logout methods that don't depend on CouchDB's authentication scheme. You could query your own _view/users because it would be in your main database. Things wouldn't be locked down tight but they would work fine as long as no one was interested in hacking your system. :)