|
| 1 | +""" |
| 2 | +Functions needed to synchronise data with the SecureDrop server (via the |
| 3 | +securedrop_sdk). Each function requires but two arguments: an authenticated |
| 4 | +instance of the securedrop_sdk API class, and a SQLAlchemy session to the local |
| 5 | +database. |
| 6 | +
|
| 7 | +Copyright (C) 2018 The Freedom of the Press Foundation. |
| 8 | +
|
| 9 | +This program is free software: you can redistribute it and/or modify |
| 10 | +it under the terms of the GNU Affero General Public License as published |
| 11 | +by the Free Software Foundation, either version 3 of the License, or |
| 12 | +(at your option) any later version. |
| 13 | +
|
| 14 | +This program is distributed in the hope that it will be useful, |
| 15 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +GNU Affero General Public License for more details. |
| 18 | +
|
| 19 | +You should have received a copy of the GNU Affero General Public License |
| 20 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +""" |
| 22 | +import logging |
| 23 | +from dateutil.parser import parse |
| 24 | +from securedrop_client.models import Source, Submission, Reply, User |
| 25 | + |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +def sync_with_api(api, session): |
| 31 | + """ |
| 32 | + Synchronises sources and submissions from the remote server's API. |
| 33 | + """ |
| 34 | + remote_submissions = [] |
| 35 | + try: |
| 36 | + remote_sources = api.get_sources() |
| 37 | + for source in remote_sources: |
| 38 | + remote_submissions.extend(api.get_submissions(source)) |
| 39 | + remote_replies = api.get_all_replies() |
| 40 | + except Exception as ex: |
| 41 | + # Log any errors but allow the caller to handle the exception. |
| 42 | + logger.error(ex) |
| 43 | + raise(ex) |
| 44 | + logger.info('Fetched {} remote sources.'.format(len(remote_sources))) |
| 45 | + logger.info('Fetched {} remote submissions.'.format( |
| 46 | + len(remote_submissions))) |
| 47 | + logger.info('Fetched {} remote replies.'.format(len(remote_replies))) |
| 48 | + local_sources = session.query(Source) |
| 49 | + local_submissions = session.query(Submission) |
| 50 | + local_replies = session.query(Reply) |
| 51 | + update_sources(remote_sources, local_sources, session) |
| 52 | + update_submissions(remote_submissions, local_submissions, session) |
| 53 | + update_replies(remote_replies, local_replies, session) |
| 54 | + |
| 55 | + |
| 56 | +def update_sources(remote_sources, local_sources, session): |
| 57 | + """ |
| 58 | + Given collections of remote sources, the current local sources and a |
| 59 | + session to the local database, ensure the state of the local database |
| 60 | + matches that of the remote sources: |
| 61 | +
|
| 62 | + * Existing items are updated in the local database. |
| 63 | + * New items are created in the local database. |
| 64 | + * Local items not returned in the remote sources are deleted from the |
| 65 | + local database. |
| 66 | + """ |
| 67 | + local_uuids = {source.uuid for source in local_sources} |
| 68 | + for source in remote_sources: |
| 69 | + if source.uuid in local_uuids: |
| 70 | + # Update an existing record. |
| 71 | + local_source = [s for s in local_sources |
| 72 | + if s.uuid == source.uuid][0] |
| 73 | + local_source.journalist_designation = source.journalist_designation |
| 74 | + local_source.is_flagged = source.is_flagged |
| 75 | + local_source.public_key = source.key |
| 76 | + local_source.interaction_count = source.interaction_count |
| 77 | + local_source.is_starred = source.is_starred |
| 78 | + local_source.last_updated = parse(source.last_updated) |
| 79 | + # Removing the UUID from local_uuids ensures this record won't be |
| 80 | + # deleted at the end of this function. |
| 81 | + local_uuids.remove(source.uuid) |
| 82 | + logger.info('Updated source {}'.format(source.uuid)) |
| 83 | + else: |
| 84 | + # A new source to be added to the database. |
| 85 | + ns = Source(uuid=source.uuid, |
| 86 | + journalist_designation=source.journalist_designation, |
| 87 | + is_flagged=source.is_flagged, |
| 88 | + public_key=source.key, |
| 89 | + interaction_count=source.interaction_count, |
| 90 | + is_starred=source.is_starred, |
| 91 | + last_updated=parse(source.last_updated)) |
| 92 | + session.add(ns) |
| 93 | + logger.info('Added new source {}'.format(source.uuid)) |
| 94 | + # The uuids remaining in local_uuids do not exist on the remote server, so |
| 95 | + # delete the related records. |
| 96 | + for deleted_source in [s for s in local_sources if s.uuid in local_uuids]: |
| 97 | + session.delete(deleted_source) |
| 98 | + logger.info('Deleted source {}'.format(deleted_source.uuid)) |
| 99 | + session.commit() |
| 100 | + |
| 101 | + |
| 102 | +def update_submissions(remote_submissions, local_submissions, session): |
| 103 | + """ |
| 104 | + * Existing submissions are updated in the local database. |
| 105 | + * New submissions have an entry created in the local database. |
| 106 | + * Local submissions not returned in the remote submissions are deleted |
| 107 | + from the local database. |
| 108 | + """ |
| 109 | + local_uuids = {submission.uuid for submission in local_submissions} |
| 110 | + for submission in remote_submissions: |
| 111 | + if submission.uuid in local_uuids: |
| 112 | + # Update an existing record. |
| 113 | + local_submission = [s for s in local_submissions |
| 114 | + if s.uuid == submission.uuid][0] |
| 115 | + local_submission.filename = submission.filename |
| 116 | + local_submission.size = submission.size |
| 117 | + local_submission.is_read = submission.is_read |
| 118 | + # Removing the UUID from local_uuids ensures this record won't be |
| 119 | + # deleted at the end of this function. |
| 120 | + local_uuids.remove(submission.uuid) |
| 121 | + logger.info('Updated submission {}'.format(submission.uuid)) |
| 122 | + else: |
| 123 | + # A new submission to be added to the database. |
| 124 | + _, source_uuid = submission.source_url.rsplit('/', 1) |
| 125 | + source = session.query(Source).filter_by(uuid=source_uuid)[0] |
| 126 | + ns = Submission(source=source, uuid=submission.uuid, |
| 127 | + filename=submission.filename) |
| 128 | + session.add(ns) |
| 129 | + logger.info('Added new submission {}'.format(submission.uuid)) |
| 130 | + # The uuids remaining in local_uuids do not exist on the remote server, so |
| 131 | + # delete the related records. |
| 132 | + for deleted_submission in [s for s in local_submissions |
| 133 | + if s.uuid in local_uuids]: |
| 134 | + session.delete(deleted_submission) |
| 135 | + logger.info('Deleted submission {}'.format(deleted_submission.uuid)) |
| 136 | + session.commit() |
| 137 | + |
| 138 | + |
| 139 | +def update_replies(remote_replies, local_replies, session): |
| 140 | + """ |
| 141 | + * Existing replies are updated in the local database. |
| 142 | + * New replies have an entry created in the local database. |
| 143 | + * Local replies not returned in the remote replies are deleted from the |
| 144 | + local database. |
| 145 | +
|
| 146 | + If a reply references a new journalist username, add them to the database |
| 147 | + as a new user. |
| 148 | + """ |
| 149 | + local_uuids = {reply.uuid for reply in local_replies} |
| 150 | + for reply in remote_replies: |
| 151 | + if reply.uuid in local_uuids: |
| 152 | + # Update an existing record. |
| 153 | + local_reply = [r for r in local_replies if r.uuid == reply.uuid][0] |
| 154 | + user = find_or_create_user(reply.journalist_username, session) |
| 155 | + local_reply.journalist_id = user.id |
| 156 | + local_reply.filename = reply.filename |
| 157 | + local_reply.size = reply.size |
| 158 | + local_uuids.remove(reply.uuid) |
| 159 | + logger.info('Updated reply {}'.format(reply.uuid)) |
| 160 | + else: |
| 161 | + # A new reply to be added to the database. |
| 162 | + source_uuid = reply.source_uuid |
| 163 | + source = session.query(Source).filter_by(uuid=source_uuid)[0] |
| 164 | + user = find_or_create_user(reply.journalist_username, session) |
| 165 | + nr = Reply(reply.uuid, user, source, reply.filename, reply.size) |
| 166 | + session.add(nr) |
| 167 | + logger.info('Added new reply {}'.format(reply.uuid)) |
| 168 | + # The uuids remaining in local_uuids do not exist on the remote server, so |
| 169 | + # delete the related records. |
| 170 | + for deleted_reply in [r for r in local_replies if r.uuid in local_uuids]: |
| 171 | + session.delete(deleted_reply) |
| 172 | + logger.info('Deleted reply {}'.format(deleted_reply.uuid)) |
| 173 | + session.commit() |
| 174 | + |
| 175 | + |
| 176 | +def find_or_create_user(username, session): |
| 177 | + """ |
| 178 | + Returns a user object representing the referenced username. If the username |
| 179 | + does not already exist in the data, a new instance is created. |
| 180 | + """ |
| 181 | + user = session.query(User).filter_by(username=username) |
| 182 | + if user: |
| 183 | + return user[0] |
| 184 | + new_user = User(username) |
| 185 | + session.add(new_user) |
| 186 | + session.commit() |
| 187 | + return new_user |
0 commit comments