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

Refactor encryption code to use bot-sdk #369

Merged
merged 31 commits into from
Nov 18, 2021
Merged

Conversation

Half-Shot
Copy link
Contributor

@Half-Shot Half-Shot commented Nov 3, 2021

This code refactors our encryption broker component and intents to generally handle encrypted events better. The changes are:

  • Use the bot-sdk for encryption, rather than the js-sdk
  • Drop support for ephemeral events via /sync, preferring the AS route.
  • Refactors the session creation code within Intent, hopefully reducing the chance of a race.
  • Improves the example encryption app.

@Half-Shot Half-Shot requested a review from a team November 3, 2021 12:02
@Half-Shot Half-Shot changed the title Refactor encryption code to use pantaliamon Refactor encryption code to use bot-sdk Nov 3, 2021
if (!session) {
// No session in the store, attempt a login.
log.debug("ensureRegistered: Attempting encrypted login");
// Login as the user
Copy link
Contributor

Choose a reason for hiding this comment

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

Login is the noun.

Suggested change
// Login as the user
// Log in as the user

private readyPromise?: Promise<unknown>;
private encryptionReadyPromise?: Promise<void>;

// A client that talks directly to the homeserver, bypassing pan.
Copy link
Contributor

Choose a reason for hiding this comment

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

To reduce the knowledge one needs to have to understand this comment, I'd recommend not abbreviating pantalaimon.

Suggested change
// A client that talks directly to the homeserver, bypassing pan.
// A client that talks directly to the homeserver, bypassing pantalaimon.

@@ -1180,51 +1185,81 @@ export class Intent {
}
}

// NOTES:
// Still seeing errors about invalid access tokens.
// Still seeing the server attempt to send stuff to pan without sync being enabled.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Still seeing the server attempt to send stuff to pan without sync being enabled.
// Still seeing the server attempt to send stuff to pantalaimon without sync being enabled.

if (!event.event.decrypted) {
private onSyncEvent(roomId: string, event: PanWeakEvent): void {
log.info("BLARGH", roomId, event);
if (!event.decrypted) {
// We only care about encrypted events, and pan appends a decrypted key to each event.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// We only care about encrypted events, and pan appends a decrypted key to each event.
// We only care about encrypted events, and pantalaimon appends a decrypted key to each event.

private onSyncEvent(event: any) {
if (!event.event.decrypted) {
private onSyncEvent(roomId: string, event: PanWeakEvent): void {
log.info("BLARGH", roomId, event);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
log.info("BLARGH", roomId, event);

or

Suggested change
log.info("BLARGH", roomId, event);
log.debug("onSyncEvent", roomId, event);

// We only care about encrypted events, and pan appends a decrypted key to each event.
return;
}
if (!this.eventsPendingSync.has(event.getId())) {
// TODO: Do we need this?
Copy link
Contributor

Choose a reason for hiding this comment

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

Just pointing this out: You've left a TODO here.

if (!this.encryption) {
throw Error('Cannot call getEncryptedSession without enabling encryption');
}
// We've not got a session so let's see if the store has one.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// We've not got a session so let's see if the store has one.
// We've got no session so let's see if the store has one.

log.debug("ensureRegistered: failed to ready", ex);
// Failed to ready up - fall through and try again.
}
// Past this point, we're an encryption enabled Intent.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Past this point, we're an encryption enabled Intent.
// Past this point, we're an encryption-enabled Intent.

src/bridge.ts Outdated
};
clientIntentOpts.registered = this.membershipCache.isUserRegistered(userId);
const encryptionOpts = this.opts.bridgeEncryption;
if (encryptionOpts) {
clientIntentOpts.encryption = {
sessionPromise: encryptionOpts.store.getStoredSession(userId),
origianlHomeserver: this.opts.homeserverUrl,
Copy link
Contributor

Choose a reason for hiding this comment

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

typo that hopefully still can be fixed in the entire repo.

Suggested change
origianlHomeserver: this.opts.homeserverUrl,
originalHomeserver: this.opts.homeserverUrl,

@Half-Shot
Copy link
Contributor Author

I had an epiphany and decided to refactor the encryption code into a separate subclass so it's easier to understand (and less if/else-ing)

src/components/membership-cache.ts Outdated Show resolved Hide resolved
src/components/encrypted-intent.ts Outdated Show resolved Hide resolved
};
}

public async ensureRegistered(forceRegister = false): Promise<"registered=true"|undefined> {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reason for it returning this awkward string rather than a true (or just void, since it rejects on failure anyway?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a thing that the old old sdk did and we've just carried it forward. I'm not sure if anyone actually expects it..

src/components/encryption.ts Outdated Show resolved Hide resolved
src/components/encryption.ts Show resolved Hide resolved
src/components/encrypted-intent.ts Outdated Show resolved Hide resolved
@Half-Shot Half-Shot merged commit 91f709e into develop Nov 18, 2021
@Half-Shot Half-Shot deleted the hs/encryption-refactore branch May 2, 2023 16:06
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.

3 participants