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

feat: Rotate WebRTC Direct certificates #2997

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Aryakoste
Copy link

Title

Rotating web RTC certificate when expiring

Description

Fixes #2989

Notes & open questions

I know this PR is not be perfect. Please provide me suggestions or changes that needed to be done.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@Aryakoste Aryakoste requested a review from a team as a code owner February 23, 2025 18:56
Copy link
Member

@achingbrain achingbrain left a comment

Choose a reason for hiding this comment

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

Thanks for opening this, comments inline.

@@ -154,20 +164,19 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
server: Promise.resolve()
.then(async (): Promise<StunServer> => {
// ensure we have a certificate
if (this.certificate == null) {
if (this.certificate == null || this.isCertificateExpiring()) {
Copy link
Member

Choose a reason for hiding this comment

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

This check will only run when the node is started.

If the certificate expires while the node is running it will continue using it and connections will start to fail.

Instead a timeout should be set that fires a short(ish) time before the certificate expiry that creates a new certificate, restarts the server with the new cert and emits the "listening" event.

@@ -280,6 +280,7 @@ export interface TransportCertificate {
* The hash of the certificate
*/
certhash: string
notAfter: string
Copy link
Member

Choose a reason for hiding this comment

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

This would be better as a number so we don't have to convert the string back to a date in order to see if the cert is about to expire, instead it can just be compared to Date.now() - threshold .

const expiryDate = new Date(this.certificate.notAfter)
const now = new Date()
const timeToExpiry = expiryDate.getTime() - now.getTime()
const threshold = 30 * 24 * 60 * 60 * 1000
Copy link
Member

Choose a reason for hiding this comment

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

threshold should be configurable.

Comment on lines +18 to +25
peerId: { toB58String: () => 'QmPeerId' } as any,
privateKey: {} as any,
logger: {
forComponent: () => ({
trace: () => {},
error: () => {}
})
} as any,
Copy link
Member

Choose a reason for hiding this comment

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

These fields can be filled using actual values, see other tests here for an example.

error: () => {}
})
} as any,
transportManager: {} as any
Copy link
Member

Choose a reason for hiding this comment

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

You can use stubInterface<TransportManager>() from sinon-ts here to avoid casting to any.

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.

Rotate WebRTC Direct certificates
2 participants