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

Fix progress reporting for key import #4131

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

BillCarsonFr
Copy link
Member

@BillCarsonFr BillCarsonFr commented Mar 25, 2024

Fixes element-hq/element-web#27235

Checklist

  • Tests written for new code (and old code if feasible).
  • New or updated public/exported symbols have accurate TSDoc documentation.
  • Linter and other CI checks pass.
  • Sign-off given on the changes (see CONTRIBUTING.md).

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

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

A few requests for cleanups... and hopefully a better test?

const backupVersion = backupInfo.version!;
let success = 0;
let failures = 0;
const partialProgress = (stage: ImportRoomKeyProgressData): void => {
Copy link
Member

Choose a reason for hiding this comment

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

seems like stage is a confusing name, when ImportRoomKeyProgressData also has a property called stage ?

Comment on lines +3925 to +3926
let success = 0;
let failures = 0;
Copy link
Member

Choose a reason for hiding this comment

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

could we give these clearer names, like thisChunkSuccesses / thisChunkFailures ?

let success = 0;
let failures = 0;
const partialProgress = (stage: ImportRoomKeyProgressData): void => {
success = stage.successes ?? 0;
Copy link
Member

Choose a reason for hiding this comment

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

I am led to wonder if we couldn't just change the type definition for ImportRoomKeyProgressData to make successes and failures mandatory.

});
totalImported += chunk.length;
totalImported += success;
totalFailures += failures;
} catch (e) {
totalFailures += chunk.length;
Copy link
Member

Choose a reason for hiding this comment

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

this looks kinda wrong now: we could import some keys and then throw an exception, so we would end up with success + failure > total.

Maybe:

Suggested change
totalFailures += chunk.length;
totalFailures += (chunk.length - success);

const backupVersion = backupInfo.version!;
let success = 0;
let failures = 0;
const partialProgress = (stage: ImportRoomKeyProgressData): void => {
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be clearer to inline this function? YMMV

@@ -265,6 +265,17 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
},
backupVersion,
);
// call the progress callback one last time with the final state
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// call the progress callback one last time with the final state
// Call the progress callback one last time with the final state.

Copy link
Member

Choose a reason for hiding this comment

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

Could you also extend this comment to explain why this is required? It seems like OlmMachine.importBackedUpRoomKeys runs the callback after each key so it's not obvious.

}
});
// @ts-ignore - mock a private method for testing purpose
aliceCrypto.importBackedUpRoomKeys = importMockImpl;
Copy link
Member

Choose a reason for hiding this comment

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

we should definitely not do this. Doing so turns all of these tests from integration tests (where we test the whole stack, including the rust bits) into unit tests (where we are just testing the behaviour of MatrixClient).

To be honest, it is a problem that we have existing tests that mock out CryptoApi.importBackedUpRoomKeys but which claim to be integration tests, but that's a problem for another day.

@@ -540,6 +551,85 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm-keys backup (%s)", (backe
expect(result.imported).toStrictEqual(expectedTotal - decryptionFailureCount);
});

it("Should report failures when decryption works but import fails", async function () {
// @ts-ignore - mock a private method for testing purpose
aliceCrypto.importBackedUpRoomKeys = jest
Copy link
Member

Choose a reason for hiding this comment

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

Again: by mocking out importBackedUpRoomKeys, these are unit tests rather than integration tests. We have no real evidence that the actual RustCrypto implementation behaves the same way that these mocks do.

Could we not make up some data which causes RustCrypto.importBackedUpRoomKeys to report a failure, without having to mock this out?

@richvdh richvdh changed the title Fix report keys import progress Fix progress reporting for key import Jun 3, 2024
@richvdh richvdh added T-Defect and removed T-Task Tasks for the team like planning labels Jun 3, 2024
@richvdh
Copy link
Member

richvdh commented Jun 11, 2024

@BillCarsonFr I'm going to close this for now as it seems to be bitrotting.

@richvdh richvdh closed this Jun 11, 2024
@richvdh
Copy link
Member

richvdh commented Jun 11, 2024

wait, sorry, it was updated last week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Backup import | When keys can be decrypted but not imported (malformed), web doesn't properly report failures
2 participants