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: saving logbook token in memory #725

Merged

Conversation

Junjiequan
Copy link
Contributor

@Junjiequan Junjiequan commented Sep 11, 2023

Description

This PR is correlated with SciCatProject/scichat-loopback#320
In this PR, we're optimizing token handling. Instead of triggering the Synapse login endpoint for every logbook fetch request in scichat-loopback, we now save the token in memory and reuse it until the loopback service restarts. This reduces the need for frequent Synapse login calls and improves efficiency.

Motivation

Before, when the loopback service restarted, access to logbook services from scicat backend was lost. To regain access, it was a must to remove the expired token from MongoDB and generate a new one.

Tests included/Docs Updated?

  • Included for each change/fix?
  • Passing? (Merge will not be approved unless this is checked)
  • Docs updated?
  • New packages used/requires npm install?
  • Toggle added for new features?

Copy link
Contributor

@bpedersen2 bpedersen2 left a comment

Choose a reason for hiding this comment

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

Looks to me, but as we are not using the logbook service in Munich, I can not give a vote.

Copy link

@enthusiastio enthusiastio left a comment

Choose a reason for hiding this comment

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

Left a small question, overall looks good and clean to me.

@@ -132,7 +144,10 @@ export class LogbooksService {
if (this.logbookEnabled) {
if (this.username && this.password) {
try {
const accessToken = await this.login(this.username, this.password);
const shouldRenewToken = await this.shouldRenewToken();
if (!this.accessToken || shouldRenewToken) {

Choose a reason for hiding this comment

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

Question: Do you think checking for if this.accessToken is falsy could be done in the shouldRenewToken() instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes code much cleaner, I have changed them in the new commit. Thanks!

Copy link
Contributor

@nitrosx nitrosx left a comment

Choose a reason for hiding this comment

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

I wonder why the token has been removed from all the calls to the loopback service

this.httpService.get<Logbook[]>(this.baseUrl + "/Logbooks", {
headers: { Authorization: `Bearer ${accessToken.token}` },
}),
this.httpService.get<Logbook[]>(this.baseUrl + "/Logbooks"),
Copy link
Contributor

Choose a reason for hiding this comment

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

why the token was removed?
Does this mean that I can retrieve the logbook without authentication?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the loopback repo we don't use headers to retrieve token. The token used for loopback is created and stored on the first success login to the synapse service, and then will re-use the token until loopback service is restarted.

Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean that there is no authentication between SciCat BE and loopback connector?
If so, is that what we want to do?

Copy link
Contributor Author

@Junjiequan Junjiequan Sep 15, 2023

Choose a reason for hiding this comment

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

we need to provide synapse credential to the loopback on the first time login, which is saved in scicat backend .env file. then loopback service will generates a token with given credential and stores the token in loopback application scope. Thereafter, we just send request from scicat BE to loopback without further authentication.

One of the main reason we should try to use token exclusively and minimize login attempt is because, synapse service is very sensitive to login Rate limit with same account. If we somehow triggeres login more than 2 times within short period time(less than 1 minute or so), synapse will throw rate limit excedeed error and we will need to wait for 1~5 minutes login restriction untill next login attempt.

const shouldRenewToken = await this.shouldRenewToken();
if (shouldRenewToken) {
await this.login(this.username, this.password);
}
Logger.log("Fetching logbook " + name, "LogbooksService.findByName");
Logger.log(filters, "LogbooksService.findByName");
const res = await firstValueFrom(
this.httpService.get<Logbook>(
this.baseUrl + `/Logbooks/${name}?filter=${filters}`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as my previous comment

@@ -141,7 +153,6 @@ export class LogbooksService {
this.httpService.post<{ event_id: string }>(
this.baseUrl + `/Logbooks/${name}/message`,
data,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

Copy link
Contributor

@nitrosx nitrosx left a comment

Choose a reason for hiding this comment

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

Does this mean that there is no authentication between SciCat BE and loopback connector?
If so, is that what we want to do?

this.httpService.get<Logbook[]>(this.baseUrl + "/Logbooks", {
headers: { Authorization: `Bearer ${accessToken.token}` },
}),
this.httpService.get<Logbook[]>(this.baseUrl + "/Logbooks"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean that there is no authentication between SciCat BE and loopback connector?
If so, is that what we want to do?

@Junjiequan Junjiequan merged commit 5cb4425 into master Sep 20, 2023
5 checks passed
@Junjiequan Junjiequan deleted the SWAP-3487-scicat-fix-test-fail-for-the-loopback-recent-pr-a branch September 20, 2023 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants