cloudant / python-cloudant

A Python library for Cloudant and CouchDB
Apache License 2.0
163 stars 55 forks source link

Cannot update _security document using Cloudant python #52

Closed mikerhodes closed 8 years ago

mikerhodes commented 8 years ago

You cannot update a _security document using python-cloudant. If you run the following script:

from cloudant.account import Cloudant

client = Cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME)
client.connect()
session = client.session()
database = client[DATABASE_NAME]
security_document = database[DOC_NAME]
print "Got security_document: ", security_document

# Update the security_document with the API key permissions
security_document[u'cloudant'][u'bouninamendouldnimendepa'] = [u'_reader']

security_document.save()

client.disconnect()

You get a problem because _security documents do not have _rev fields (they are not versioned in the same way):

Traceback (most recent call last):
  File "main.py", line 23, in <module>
    security_document.save()
  File "/Users/mike/scratch/python-cloudant/issue-49/lib/python2.7/site-packages/cloudant/document.py", line 172, in save
    super(Document, self).__setitem__('_rev', data['rev'])
KeyError: 'rev'

I should've spotted this issue; it's probably an issue elsewhere too.

alfinkel commented 8 years ago

@mikerhodes we are not wrapping _security documents as Document for that very reason. For now we are using the share_database and unshare_database methods to manage permissions for users and API keys. So the _security document would be updated that way. I suppose that I see your point in that you want to be able to handle the _security document as a Document so we can let this issue track that enhancement. As for the "rev" finding it should not be an issue other than in this case but again the _security document was not meant to be wrapped as a Document when this library was initially written.

alfinkel commented 8 years ago

@mikerhodes - I spoke to @evansde77 about setting permissions and the sharing databases. I've summarized the major points of our conversation below:

mikerhodes commented 8 years ago

@alfinkel Seems clearer. For me, the naming of share/unshare is very use-case specific (to sharing databases) -- therefore editing the _security doc to add permissions is a separate thing.

alfinkel commented 8 years ago

I've opened #98 to split out the work involving the share_database missing roles. So this Issue is just for the direct management of the security document.