Repository navigation
fix: decrypt a submission once however many decryption tasks run #3289
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
Open
kelvin-muchiri
wants to merge
20
commits into
main
Choose a base branch
from
fix/decrypt-submission-once
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+385
−35
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9ec54e9
fix: decrypt a submission once however many decryption tasks run
kelvin-muchiri e6ba77d
fix: retry submissions that failed decryption for a temporary reason
kelvin-muchiri 3a3059d
refactor: set pending decryption where submissions are created or edited
kelvin-muchiri 6e35d54
perf: find submissions to retry decrypting without searching json
kelvin-muchiri 0f54092
refactor: retry failed decryptions the way earlier data migrations do
kelvin-muchiri df69143
test: drop the retry failed decryptions migration test
kelvin-muchiri 901dff2
fix: keep an edit saved while its submission is being decrypted
kelvin-muchiri 60c9620
chore: show progress while retrying failed decryptions
kelvin-muchiri d9c4169
chore: show progress without counting submissions first
kelvin-muchiri 13dbb3d
fix: don't record a decryption error for a form without managed keys
kelvin-muchiri 32f88bc
docs: explain why decryption discards its result
kelvin-muchiri 3046420
test: clarify what decryption waits for
kelvin-muchiri adb0bce
test: clarify that decryption waits for KMS
kelvin-muchiri 3314585
fix: decrypt submissions retried after a temporary decryption failure
kelvin-muchiri 65a6cfd
docs: stop listing an unmanaged form as a decryption failure
kelvin-muchiri 6b260c4
fix: leave an edit to its own decryption when an older copy is decrypted
kelvin-muchiri e5d36d7
refactor: check the checksum decryption started from directly
kelvin-muchiri c0d4234
fix: keep an edit when decryption of the older copy fails
kelvin-muchiri 88f3da0
docs: note that decryption must claim a submission first
kelvin-muchiri 01a0525
docs: say when a pending submission is decrypted again
kelvin-muchiri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
onadata/apps/logger/migrations/0048_alter_instance_decryption_status.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Generated by Django 5.2.17 on 2026-10-12 09:41 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('logger', '0047_instance_user_date_created_idx'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name='instance', | ||
| name='decryption_status', | ||
| field=models.CharField(choices=[('unmanaged', 'Does not use managed keys'), ('pending', 'Pending decryption'), ('in_progress', 'Decryption in progress'), ('success', 'Decryption successful'), ('failed', 'Decryption failed')], default='unmanaged', max_length=20), | ||
| ), | ||
| ] |
71 changes: 71 additions & 0 deletions
71
onadata/apps/logger/migrations/0049_retry_failed_decryptions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """Decrypt submissions that failed decryption for a temporary reason.""" | ||
|
|
||
| from django.db import migrations | ||
|
|
||
| # Live models are used for decrypt_instance. This is needed because | ||
| # apps.get_model("logger", "Instance") returns a frozen version of | ||
| # Instance that has only the fields known at this migration | ||
| from onadata.apps.logger.models import Instance as LiveInstance | ||
| from onadata.libs.kms.tools import decrypt_instance | ||
|
|
||
|
|
||
| # pylint: disable=unused-argument | ||
| def retry_failed_decryptions(apps, schema_editor): | ||
| """Decrypt submissions that failed decryption for a temporary reason. | ||
|
|
||
| Decryption only picks up pending submissions, so these are set to pending | ||
| first, otherwise they would never be decrypted. | ||
| """ | ||
| # pylint: disable=invalid-name | ||
| Instance = apps.get_model("logger", "Instance") | ||
| candidates = Instance.objects.filter( | ||
| deleted_at__isnull=True, | ||
| is_encrypted=True, | ||
| decryption_status="failed", | ||
| ).only("json") | ||
| processed_count = 0 | ||
| decrypted_count = 0 | ||
| failed_count = 0 | ||
| skipped_count = 0 | ||
|
|
||
| for instance in candidates.iterator(chunk_size=100): | ||
| processed_count += 1 | ||
| print(f"Processed {processed_count} submissions") | ||
|
|
||
| if (instance.json or {}).get("_decryption_error") not in ( | ||
| "NOT_ALL_MEDIA_RECEIVED", | ||
| "MAX_RETRIES_EXCEEDED", | ||
| ): | ||
| skipped_count += 1 | ||
| continue | ||
|
|
||
| Instance.objects.filter(pk=instance.pk).update(decryption_status="pending") | ||
|
|
||
| try: | ||
| decrypt_instance(LiveInstance.objects.get(pk=instance.pk)) | ||
| decrypted_count += 1 | ||
| print(f"Decrypted Instance {instance.id}") | ||
|
|
||
| # pylint: disable=broad-exception-caught | ||
| except Exception as e: | ||
| # Best-effort; decryption records why the submission failed | ||
| failed_count += 1 | ||
| print(f"Decrypting Instance {instance.id} failed: {e}") | ||
|
|
||
| print( | ||
| f"Decrypted {decrypted_count} submissions," | ||
| f" {failed_count} failed, {skipped_count} skipped" | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("logger", "0048_alter_instance_decryption_status"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| retry_failed_decryptions, | ||
| reverse_code=migrations.RunPython.noop, | ||
| ), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.