def test__entry_upsert_invalid_post_when_exists_throw_error(self): database_details_upsert = self._copy_database("temp_" + "".join( random.choices(string.ascii_uppercase + string.digits, k=16))) storage = KeepassDatabase(self._display, database_details_upsert) self.assertRaises(AttributeError, storage._entry_upsert, self._post_path_invalid.search, False) self._display.assert_has_calls([])
def test_execute_valid_get(self): storage = KeepassDatabase(self._display, self._database_details_valid) actual = storage.execute(self._search_path_valid.search, check_mode=False, fail_silently=True) self.assertFalse(actual["changed"]) self.assertFalse(actual["failed"]) self.assertDictEqual(self._search_path_valid.search.__dict__, actual["result"]["search"]) self.assertDictEqual(self._database_entry, actual["result"]["outcome"]) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % self._database_details_valid["location"]), call.vvv("Keepass: keyfile found - %s" % self._database_details_valid["keyfile"]), call.v("Keepass: database opened - %s" % self._database_details_valid["location"]), call.vvv('Keepass: execute - %s' % [{ 'search': to_native(self._search_path_valid.search) }, { 'check_mode': 'False' }, { 'fail_silently': 'True' }]), call.vv("KeePass: entry found - %s" % self._search_path_valid.search) ])
def test_execute_valid_delete_entry(self): database_details_delete = self._copy_database("temp_" + "".join( random.choices(string.ascii_uppercase + string.digits, k=16))) storage = KeepassDatabase(self._display, database_details_delete) actual = storage.execute(self._delete_entry.search, check_mode=False, fail_silently=False) self.assertTrue(actual["changed"]) self.assertFalse(actual["failed"]) self.assertDictEqual(self._delete_entry.search.__dict__, actual["result"]["search"]) self.assertEqual(None, actual["result"]["outcome"]) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % database_details_delete["location"]), call.vvv("Keepass: keyfile found - %s" % database_details_delete["keyfile"]), call.v("Keepass: database opened - %s" % database_details_delete["location"]), call.vvv('Keepass: execute - %s' % [{ 'search': to_native(self._delete_entry.search) }, { 'check_mode': 'False' }, { 'fail_silently': 'False' }]), call.vv("KeePass: entry found - %s" % self._delete_entry.search), call.v("Keepass: database saved - %s" % database_details_delete["location"]) ])
def test__get_binary_b64_encoded(self): message = "hello world".encode("utf16") base64_encoded = base64.b64encode(message) actual = KeepassDatabase( self._display, self._database_details_valid)._get_binary(base64_encoded) self.assertEqual((message, True), actual)
def test_execute_invalid_not_updatable_not_silently_throw(self): database_details_delete = dict(self._copy_database("temp_" + "".join( random.choices(string.ascii_uppercase + string.digits, k=16))), updatable=False) storage = KeepassDatabase(self._display, database_details_delete) self.assertRaises(AnsibleParserError, storage.execute, self._delete_entry.search, check_mode=False, fail_silently=False) self._display.assert_has_calls([])
def test__open_valid_details(self): storage = KeepassDatabase(self._display, self._database_details_valid) self.assertIsNotNone(storage._database) self.assertTrue(isinstance(storage._database, PyKeePass)) self._display.assert_has_calls([ call.v('Keepass: database found - %s' % self._database_details_valid["location"]), call.vvv('Keepass: keyfile found - %s' % self._database_details_valid["keyfile"]), call.v('Keepass: database opened - %s' % self._database_details_valid["location"]) ])
def test_get_invalid(self): storage = KeepassDatabase(self._display, self._database_details_valid) self.assertRaises(AttributeError, storage.get, self._query_invalid.search, False) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % self._database_details_valid["location"]), call.vvv("Keepass: keyfile found - %s" % self._database_details_valid["keyfile"]), call.v("Keepass: database opened - %s" % self._database_details_valid["location"]), call.vv("KeePass: entry found - %s" % self._query_invalid.search) ])
def run(self, terms, variables=None, **kwargs): self.set_options(var_options=variables, direct=kwargs) check_mode = self.get_option("check_mode") fail_silently = self.get_option("fail_silently") storage = KeepassDatabase(display, self.get_option("database")) display.vvv("keepass: terms %s" % terms) return list( map( lambda term: storage.execute(Query(display, True, term).search, check_mode=check_mode, fail_silently=fail_silently), terms))
def test__entry_find_invalid_path_raise_error(self): storage = KeepassDatabase(self._display, self._database_details_valid) self.assertRaises(AnsibleError, storage._entry_find, self._search_path_invalid.search, None, True) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % self._database_details_valid["location"]), call.vvv("Keepass: keyfile found - %s" % self._database_details_valid["keyfile"]), call.v("Keepass: database opened - %s" % self._database_details_valid["location"]), call.vv("KeePass: entry NOT found - %s" % self._search_path_invalid.search) ])
def test_execute_invalid_not_updatable_fail_silently(self): database_details_delete = dict(self._copy_database("temp_" + "".join( random.choices(string.ascii_uppercase + string.digits, k=16))), updatable=False) storage = KeepassDatabase(self._display, database_details_delete) actual = storage.execute(self._delete_entry.search, check_mode=False, fail_silently=True) self.assertFalse(actual["changed"]) self.assertTrue(actual["failed"]) self.assertDictEqual(self._delete_entry.search.__dict__, actual["result"]["search"]) self.assertTrue("Invalid query - database is not 'updatable'" in ( actual["result"]["outcome"]["error"])) self._display.assert_has_calls([])
def test__save_valid(self): database_details_save = self._copy_database("temp_" + "".join( random.choices(string.ascii_uppercase + string.digits, k=16))) storage = KeepassDatabase(self._display, database_details_save) self.assertTrue(isinstance(storage._database, PyKeePass)) storage._save() self._display.assert_has_calls([ call.v(u"Keepass: database found - %s" % database_details_save["location"]), call.vvv(u"Keepass: keyfile found - %s" % database_details_save["keyfile"]), call.v(u"Keepass: database opened - %s" % database_details_save["location"]), call.v(u"Keepass: database saved - %s" % database_details_save["location"]) ])
def test_delete_valid_no_property_exists(self): database_details_delete = self._copy_database("temp_" + "".join( random.choices(string.ascii_uppercase + string.digits, k=16))) storage = KeepassDatabase(self._display, database_details_delete) self.assertRaises(AttributeError, storage.delete, self._delete_invalid_property.search, False) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % database_details_delete["location"]), call.vvv("Keepass: keyfile found - %s" % database_details_delete["keyfile"]), call.v("Keepass: database opened - %s" % database_details_delete["location"]), call.vv("KeePass: entry found - %s" % self._delete_invalid_property.search) ])
def test__entry_find_invalid_by_path_no_error(self): storage = KeepassDatabase(self._display, self._database_details_valid) actual_entry = storage._entry_find(self._search_path_invalid.search, ref_uuid=None, not_found_throw=False) self.assertEqual(None, actual_entry) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % self._database_details_valid["location"]), call.vvv("Keepass: keyfile found - %s" % self._database_details_valid["keyfile"]), call.v("Keepass: database opened - %s" % self._database_details_valid["location"]), call.vv("KeePass: entry NOT found - %s" % self._search_path_invalid.search), ])
def test_get_valid_property(self): storage = KeepassDatabase(self._display, self._database_details_valid) has_changed, actual_entry = storage.get(self._query_password.search, check_mode=False) self.assertFalse(has_changed) self.assertEqual(self._database_entry["password"], actual_entry["password"]) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % self._database_details_valid["location"]), call.vvv("Keepass: keyfile found - %s" % self._database_details_valid["keyfile"]), call.v("Keepass: database opened - %s" % self._database_details_valid["location"]), call.vv("KeePass: entry found - %s" % self._query_password.search) ])
def test__entry_upsert_valid_noop(self): database_details_upsert = self._copy_database("temp_" + "".join( random.choices(string.ascii_uppercase + string.digits, k=16))) storage = KeepassDatabase(self._display, database_details_upsert) has_changed, updated_entry = storage._entry_upsert( self._noop_path_valid.search, check_mode=False) self.assertFalse(has_changed) self.assertDictEqual(self._clone_entry, updated_entry) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % database_details_upsert["location"]), call.vvv("Keepass: keyfile found - %s" % database_details_upsert["keyfile"]), call.v("Keepass: database opened - %s" % database_details_upsert["location"]), call.vv("KeePass: entry found - %s" % self._noop_path_valid.search) ])
def test__entry_find_valid_by_path(self): storage = KeepassDatabase(self._display, self._database_details_valid) actual_entry = storage._entry_find(self._search_path_valid.search, ref_uuid=None, not_found_throw=True) self.assertDictEqual(self._database_entry, EntryDump(actual_entry).__dict__) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % self._database_details_valid["location"]), call.vvv("Keepass: keyfile found - %s" % self._database_details_valid["keyfile"]), call.v("Keepass: database opened - %s" % self._database_details_valid["location"]), call.vv("KeePass: entry found - %s" % self._search_path_valid.search), ])
def test_delete_valid_entry(self): database_details_delete = self._copy_database("temp_" + "".join( random.choices(string.ascii_uppercase + string.digits, k=16))) storage = KeepassDatabase(self._display, database_details_delete) has_changed, deleted_entry = storage.delete(self._delete_entry.search, check_mode=False) self.assertTrue(has_changed) self.assertEqual(None, deleted_entry) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % database_details_delete["location"]), call.vvv("Keepass: keyfile found - %s" % database_details_delete["keyfile"]), call.v("Keepass: database opened - %s" % database_details_delete["location"]), call.vv("KeePass: entry found - %s" % self._delete_entry.search), call.v("Keepass: database saved - %s" % database_details_delete["location"]) ])
def do_lookup(value): try: if not isinstance(value, dict) or value.get( "database", None) is None or value.get("lookup", None) is None: raise AttributeError( "must be a dictionary providing the following elements database (must a valid database description) and lookup" ) display.vvv("keepass: lookup %s" % value["lookup"]) outcome = KeepassDatabase(display, value["database"]).execute( Query(display, True, value["lookup"]).search, check_mode=False, fail_silently=False)["result"]["outcome"] return next(enumerate( outcome.values()))[1] if "?" in value["lookup"] else outcome except Exception as error: raise AnsibleFilterError( AnsibleError(message=to_native(error), orig_exc=error))
def test_get_valid_file(self): storage = KeepassDatabase(self._display, self._database_details_valid) has_changed, actual_entry = storage.get(self._query_file.search, check_mode=False) with open(self._database_details_valid["keyfile"], mode="rb") as file: self.assertFalse(has_changed) self.assertEqual( base64.b64encode(file.read()), actual_entry[os.path.basename( self._database_details_valid["keyfile"])]) self._display.assert_has_calls([ call.v("Keepass: database found - %s" % self._database_details_valid["location"]), call.vvv("Keepass: keyfile found - %s" % self._database_details_valid["keyfile"]), call.v("Keepass: database opened - %s" % self._database_details_valid["location"]), call.vv("KeePass: entry found - %s" % self._query_file.search), call.vv("KeePass: found property/file on entry - %s" % self._query_file.search) ])
def run(self, tmp=None, task_vars=None): super(ActionModule, self).run(tmp, task_vars) display.vvv("keepass: args - %s" % list(({ key: value } for key, value in self._task.args.items() if key != "database"))) if self._task.args.get("term", None) is not None and len( set(self._search_args).intersection(set( self._task.args.keys()))) > 0: raise AnsibleParserError( AnsibleError(u"'term' is mutually exclusive with %s" % self._search_args)) search = Query(display, False, self._task.args["term"]).search if self._task.args.get("term", None) is not None else \ Search(display=display, read_only=False, action=self._task.args.get("action", None), path=self._task.args.get("path", None), field=self._task.args.get("field", None), value=self._task.args.get("value", None), value_was_provided=self._task.args.get("value", None) is not None) return KeepassDatabase(display, self._task.args.get("database", None)).\ execute(search, self._task.args.get("check_mode", False), self._task.args.get("fail_silently", False))
def test__get_binary_plain_text(self): message = "hello world".encode("utf16") actual = KeepassDatabase( self._display, self._database_details_valid)._get_binary(message) self.assertEqual((bytes(message), False), actual)