Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move CryptoUtil.display_id() out of CryptoUtil to fix type-checking #6087

Merged

Conversation

nabla-c0d3
Copy link
Contributor

@nabla-c0d3 nabla-c0d3 commented Aug 29, 2021

Status

Ready.

Description of Changes

This small PR refactors CryptoUtil.display_id() and moves it out of CryptoUtil in order to:

I tested this in the dev environment and checked that journalist designations are properly generated.

@nabla-c0d3 nabla-c0d3 force-pushed the refactor-display-id branch 2 times, most recently from 5afe5fc to 1531a59 Compare September 1, 2021 02:43
@@ -116,7 +116,7 @@ def set_source_count(s: str) -> int:


def add_journalist(
username: str = "",
username: str,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

username is now a required argument to simplify the logic here (and improve typing).

@@ -84,8 +71,6 @@ class CryptoUtil:

def __init__(self,
securedrop_root: str,
nouns_file: str,
adjectives_file: str,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved to the new _DesignationGenerator class.

new_designation = ' '.join([random.choice(self.adjectives),
random.choice(self.nouns)])

collisions = Source.query.filter(Source.journalist_designation == new_designation)
Copy link
Contributor Author

@nabla-c0d3 nabla-c0d3 Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bit problematic because it pulls a database session "out of thin air" (by doing Source.query()) instead of database_session.query()), so display_id() could only work when called from the correct context, which can get complicated (for example in unit tests).

except ValueError:
# Create a unique journalist designation for the source
# TODO: Add unique=True to models.Source.journalist_designation to enforce uniqueness
# as the logic below has a race condition (time we check VS time when we add to the DB)
Copy link
Contributor Author

@nabla-c0d3 nabla-c0d3 Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a TODO here to not have a to do a DB migration. The race condition already existed in the previous code base. However, it may also not be a "real" problem.

def test_encrypt_then_decrypt_gives_same_result(
source_app,
test_source,
name,
secret,
Copy link
Contributor Author

@nabla-c0d3 nabla-c0d3 Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used by the test.

@nabla-c0d3 nabla-c0d3 changed the title Refactor CryptoUtil.display_id() to simplify source creation logic Move CryptoUtil.display_id() out of CryptoUtil to fix type-checking Sep 25, 2021
@nabla-c0d3 nabla-c0d3 marked this pull request as ready for review September 25, 2021 17:32
@nabla-c0d3 nabla-c0d3 requested a review from a team as a code owner September 25, 2021 17:32
@zenmonkeykstop zenmonkeykstop added this to the 2.2.0 milestone Nov 4, 2021
@zenmonkeykstop zenmonkeykstop self-assigned this Nov 16, 2021
Copy link
Contributor

@zenmonkeykstop zenmonkeykstop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - also ran test suite against staging locally, ran loaddata.py adding ~1000 sources - no issues found with gnerated journalist designations.

@zenmonkeykstop zenmonkeykstop merged commit 611efd1 into freedomofpress:develop Nov 19, 2021
@nabla-c0d3 nabla-c0d3 deleted the refactor-display-id branch February 27, 2022 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants