[SCHEMA][DEPENDENCIES] Encryption of secrets (passwords, refresh_tokens) and blocks.

Summary:
We use PyNaCl's symmetric key encryption scheme, which implements
the Salsa20 stream cipher algorithm, with a unique 24-byte nonce that
is generated at each invocation and a static key that:
- is generated using the bin/create-encryption-keys script
- stored in the secrets config file specified in the config.
(defaults are: etc/secrets-dev.yml, etc/secrets-test.yml)

This implementation replaces our previous local/remote vault.

NOTE:
We currently use our own fork of pynacl, due to install issues with pyca/pynacl.
See dicussion here: https://github.com/pyca/pynacl/pull/106
Our fork does *not* change any functionality, merely fixes a typo in the name of a test.
TODO: Move to PyNaCl once that's resolved and we can actually install it.

Test Plan:
Added -
tests/general/test_secret.py
tests/general/test_storage.py

All tests pass.
Sync runs as before for an oauth and password test account.

Reviewers: spang, dlitz

Reviewed By: dlitz

Subscribers: dlitz, emfree

Differential Revision: https://review.inboxapp.com/D358
Fix spacing.
Rewrite migration 085 for performance.

We now make only 3 queries (one of which can be quite slow, but not TOO
slow) followed by O(n) inserts, rather than... what the ORM was doing.
Whatever that might have been.

We also no longer fail to run due to the addition of columns to Blocks,
which was added more recently.
Define polling greenlet vars outside try/finally block.

Fixes T388.
Syncback deletes of non-Inbox created drafts.
Fixes T345.
Helper script to re-encrypt secrets.
Check for None in migration 088.
[SCHEMA] Store Message.to_addr as a BigJSON (rather than a JSON).
We expand rather than truncate since this field may be used in sending
for e.g. if the message is a draft that was created on the remote.

Fixes T387.
Revert "[SCHEMA] Store Message.to_addr as a BigJSON (rather than a JSON)."
This reverts commit cb749b26e8484da0aea7eafb014284b791deb022; it's overkill.
[events/google/parse] Catch type error when trying to parse start / end times.

Summary:
Date parsing doesn't always pass. Catch the error for now and investiage the failed parsing in the future.

Fixes T386

Test Plan: review code.

Reviewers: spang

Reviewed By: spang

Maniphest Tasks: T386

Differential Revision: https://review.inboxapp.com/D408
[bin/test_db] Rename update_test_db.sh script. Add a downgrade_test_db.sh script.
[attachments] Initial checking of managing attachments without duping.

Summary:
Previously we had some issues with how we were handling blocks vs
parts. In particular, an uploaded file existed as a "block" while an attachment
was a "part". Thus when you created a draft you would specify and ID for the
block that you wanted to be an attachment. When the sendmail code created the
draft it would create a 'Part' out of the 'Block'. Since the previous
relationship had Blocks deriving from Parts, this meant that a new Part would
be created, thus doing two things: copying the data (unecessary, even though it
was duped on backend) as well as creating a new "ID". A concern with this is
that a client would like to attach a block to a message and the message would
end up with file_id's that do not correspond to the original block (and the
original block would remain in place).

To solve this issue, the 'Part' object is changed from deriving from 'Block'
into a relational table between blocks and messages. Therefore, a Part has
a 'block_id' and a 'message_id' in addition to a few other parameters (i.e.
the content_disposition and walk_index). Thus, when attachments are added to
drafts, the block is not duplicated and the original block's id can be used
for the attachment.

Corresponding code needed to be changed in the mailsync which was referring to
parts assuming they were blocks. Now it uses the "Part.block" relationship
(which should have eager loading on it). Further tests have also been added to
ensure that the id does not change when a file is attached to a draft.  This
means there is now a 1-to-many relationship between blocks and parts.

Additional 'rider' change (since I was looking at this code anyway) was to
provide an improved interface to files. Most notably, blocks that are not
attached to messages can be deleted.

Test Plan: Run the regression.

Reviewers: kav-ya

Reviewed By: kav-ya

Subscribers: spang

Differential Revision: https://review.inboxapp.com/D404
[migrations] Patch up conflicting migration.
Fix self-sending with namecheap

Summary: Charles, I looked into T334. It was simply a configuration issue. Also, I've added a couple commented lines to postel.py to enable debug logging. It may come handy someday.

Test Plan: tested a self send using the client.

Reviewers: charles

Reviewed By: charles

Projects: #inbox-sync

Maniphest Tasks: T334

Differential Revision: https://review.inboxapp.com/D400
Sendmail refactor.

Summary:
Code duplication increases the likelihood of bugs,
makes it hard to read/reason about and also makes
updating it not fun.

Gmail/generic sendmail code was identical, therefore
restructure to eliminate the duplication.

Test Plan:
Tests pass as before.
imap/network/test_send.py added too

Reviewers: charles

Reviewed By: charles

Differential Revision: https://review.inboxapp.com/D412
[events/encoding] Catch a utf-8 encoding error in event parsing.

Summary:
UTF-8 encoding error.

Fixes T389

Test Plan: Check the code to make sure utf-8 encoding is on the right string.

Reviewers: spang

Reviewed By: spang

Maniphest Tasks: T389

Differential Revision: https://review.inboxapp.com/D407
[SCHEMA][DEPENDENCIES] Encryption of secrets (passwords, refresh_tokens) and blocks.

Summary:
We use PyNaCl's symmetric key encryption scheme, which implements
the Salsa20 stream cipher algorithm, with a unique 24-byte nonce that
is generated at each invocation and a static key that:
- is generated using the bin/create-encryption-keys script
- stored in the secrets config file specified in the config.
(defaults are: etc/secrets-dev.yml, etc/secrets-test.yml)

This implementation replaces our previous local/remote vault.

NOTE:
We currently use our own fork of pynacl, due to install issues with pyca/pynacl.
See dicussion here: https://github.com/pyca/pynacl/pull/106
Our fork does *not* change any functionality, merely fixes a typo in the name of a test.
TODO: Move to PyNaCl once that's resolved and we can actually install it.

Test Plan:
Added -
tests/general/test_secret.py
tests/general/test_storage.py

All tests pass.
Sync runs as before for an oauth and password test account.

Reviewers: spang, dlitz

Reviewed By: dlitz

Subscribers: dlitz, emfree

Differential Revision: https://review.inboxapp.com/D358