mongodb.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2015 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. from pwman.data.database import Database, __DB_FORMAT__
  20. from pwman.util.crypto_engine import CryptoEngine
  21. import pymongo
  22. class MongoDB(Database):
  23. @classmethod
  24. def check_db_version(cls, dburi):
  25. return __DB_FORMAT__
  26. def __init__(self, mongodb_uri, dbformat=__DB_FORMAT__):
  27. self.uri = mongodb_uri.geturl()
  28. def _open(self):
  29. self._con = pymongo.Connection(self.uri)
  30. self._db = self._con.get_default_database()
  31. counters = self._db.counters.find()
  32. if not counters.count():
  33. self._db.counters.insert({'_id': 'nodeid', 'seq': 0})
  34. def _get_next_node_id(self):
  35. # for newer pymongo versions ...
  36. # return_document=ReturnDocument.AFTER
  37. nodeid = self._db.counters.find_and_modify(
  38. {'_id': 'nodeid'}, {'$inc': {'seq': 1}}, new=True,
  39. fields={'seq': 1, '_id': 0})
  40. return nodeid['seq']
  41. def getnodes(self, ids):
  42. if ids:
  43. ids = list(map(int, ids))
  44. node_dicts = self._db.nodes.find({'_id': {'$in': ids}})
  45. else:
  46. node_dicts = self._db.nodes.find({})
  47. nodes = []
  48. for node in node_dicts:
  49. n = [node['_id'],
  50. node['user'],
  51. node['password'],
  52. node['url'],
  53. node['notes']]
  54. [n.append(t) for t in node['tags']]
  55. nodes.append(n)
  56. return nodes
  57. def listnodes(self, filter_=None):
  58. if not filter_:
  59. nodes = self._db.nodes.find({}, {'_id': 1})
  60. return [node["_id"] for node in nodes]
  61. else:
  62. matching = []
  63. ce = CryptoEngine.get()
  64. nodes = list(self._db.nodes.find({}, {'_id': 1, 'tags': 1}))
  65. for node in nodes:
  66. node['tags'] = [ce.decrypt(t) for t in node['tags']]
  67. if filter_ in node['tags']:
  68. matching.append(node)
  69. return [node["_id"] for node in matching]
  70. def add_node(self, node):
  71. nid = self._get_next_node_id()
  72. node = node.to_encdict()
  73. node['_id'] = nid
  74. self._db.nodes.insert(node)
  75. return nid
  76. def listtags(self):
  77. tags = self._db.nodes.distinct('tags')
  78. return tags
  79. def editnode(self, nid, **kwargs):
  80. self._db.nodes.find_and_modify({'_id': nid}, kwargs)
  81. def removenodes(self, nid):
  82. nid = list(map(int, nid))
  83. self._db.nodes.remove({'_id': {'$in': nid}})
  84. def fetch_crypto_info(self):
  85. pass
  86. def savekey(self, key):
  87. coll = self._db['crypto']
  88. salt, digest = key.split('$6$')
  89. coll.insert({'salt': salt, 'key': digest})
  90. def loadkey(self):
  91. coll = self._db['crypto']
  92. try:
  93. key = coll.find_one({}, {'_id': 0})
  94. key = key['salt'] + '$6$' + key['key']
  95. except TypeError:
  96. key = None
  97. return key
  98. def close(self):
  99. self._con.close()