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(core): Refactor how base urls are defined to make urls consistent across the backend #6713

Closed
wants to merge 1 commit into from

Conversation

netroy
Copy link
Member

@netroy netroy commented Jul 20, 2023

replaces #6245

@github-actions
Copy link
Contributor

Great PR! Please pay attention to the following items before merging:

Files matching packages/**:

  • If fixing bug, added test to cover scenario.
  • If addressing forum or Github issue, added link to description.

Files matching packages/**/*.ts:

  • Added unit tests to cover new or updated functionality.

Files matching packages/nodes-base/nodes/**:

  • Added workflow tests for nodes if possible.

Make sure to check off this list before asking for review.

@codecov
Copy link

codecov bot commented Jul 20, 2023

Codecov Report

Attention: 61 lines in your changes are missing coverage. Please review.

Comparison is base (05586a9) 33.59% compared to head (c4b38e9) 33.60%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6713   +/-   ##
=======================================
  Coverage   33.59%   33.60%           
=======================================
  Files        3401     3402    +1     
  Lines      207699   207722   +23     
  Branches    22467    22469    +2     
=======================================
+ Hits        69781    69802   +21     
- Misses     136771   136772    +1     
- Partials     1147     1148    +1     
Files Coverage Δ
packages/cli/src/GenericHelpers.ts 79.48% <ø> (+0.32%) ⬆️
packages/cli/src/PublicApi/index.ts 86.20% <100.00%> (+0.24%) ⬆️
...ges/cli/src/UserManagement/UserManagementHelper.ts 83.63% <ø> (-0.24%) ⬇️
...i/src/UserManagement/email/UserManagementMailer.ts 48.57% <100.00%> (+1.51%) ⬆️
packages/cli/src/WebhookHelpers.ts 32.03% <ø> (-0.42%) ⬇️
packages/cli/src/config/schema.ts 83.33% <ø> (ø)
...es/cli/src/controllers/passwordReset.controller.ts 73.68% <100.00%> (-0.88%) ⬇️
packages/cli/src/services/user.service.ts 88.09% <100.00%> (+12.65%) ⬆️
packages/cli/src/sso/saml/constants.ts 100.00% <100.00%> (ø)
packages/cli/src/sso/saml/serviceProvider.ee.ts 100.00% <100.00%> (ø)
... and 14 more

... and 4 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

workflowData.id
}/executions/${executionId}`;
const { editorBaseUrl } = Container.get(URLService);
pastExecutionUrl = `${editorBaseUrl}workflow/${workflowData.id}/executions/${executionId}`;
Copy link
Member Author

Choose a reason for hiding this comment

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

changed this to use editor url instead of webhook url, as i believe that is the correct url.

packages/cli/src/commands/start.ts Outdated Show resolved Hide resolved
baseUrl: {
format: String,
default: '',
env: 'N8N_BASE_URL',
Copy link
Member Author

Choose a reason for hiding this comment

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

added a new env variable, as WEBHOOK_URL was never meant to be used for non webhook urls (even though in some please it incorrectly is used that way).

Copy link
Contributor

Choose a reason for hiding this comment

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

The problem with this new variable is that users were supposed to set up n8n using N8N_HOST, N8N_PATH and N8N_PORT

@@ -1,15 +1,15 @@
export class SamlUrls {
static readonly samlRESTRoot = '/rest/sso/saml';
Copy link
Member Author

Choose a reason for hiding this comment

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

a hardcoded /rest somehow managed to sneak past reviews.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch

@@ -138,10 +138,10 @@ export class SamlController {
if (isSamlLicensedAndEnabled()) {
await issueCookie(res, loginResult.authenticatedUser);
if (loginResult.onboardingRequired) {
return res.redirect(getInstanceBaseUrl() + SamlUrls.samlOnboarding);
return res.redirect(this.urlService.editorBaseUrl + SamlUrls.samlOnboarding);
Copy link
Member Author

@netroy netroy Jul 20, 2023

Choose a reason for hiding this comment

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

this should be using editorBaseUrl so that these urls work with --tunnel, and also when someone is overriding the base url.

@@ -138,14 +141,14 @@ export class SamlService {

private getRedirectLoginRequestUrl(relayState?: string): BindingContext {
const sp = this.getServiceProviderInstance();
sp.entitySetting.relayState = relayState ?? getInstanceBaseUrl();
sp.entitySetting.relayState = relayState ?? this.urlService.editorBaseUrl;
Copy link
Member Author

Choose a reason for hiding this comment

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

same here. I'm wondering if we should add createBackendUrl and createFrontendUrl methods in URLService, so that people are forced to declare if the generated url is meant to be handled by the frontend, or the backend.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a fair point, maybe in the future we have these services running in separate places (rest API and FE). I'm afraid this would make the PR larger, useless at this time and harder to test. For now I think it's ok to assume that editor and rest api are always on the same host (as they actually are)

packages/cli/src/user/user.service.ts Outdated Show resolved Hide resolved
packages/cli/src/services/url.service.ts Outdated Show resolved Hide resolved
@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team node/improvement New feature or request labels Jul 20, 2023
Copy link
Contributor

@ivov ivov 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 taking this on!

From a quick test, running packages/cli/bin/n8n start --tunnel fails to open in browser, after a while ends up with 504 Gateway Time-out

packages/cli/src/services/url.service.ts Outdated Show resolved Hide resolved
packages/cli/src/services/url.service.ts Outdated Show resolved Hide resolved
packages/cli/src/services/url.service.ts Outdated Show resolved Hide resolved
packages/cli/src/services/url.service.ts Outdated Show resolved Hide resolved
packages/cli/src/services/url.service.ts Outdated Show resolved Hide resolved
format: String,
default: '',
env: 'WEBHOOK_URL',
doc: 'Used to manually provide the Webhook URL when running n8n behind a reverse proxy.',
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's specify in the description what these default to?

docker/compose/subfolderWithSSL/docker-compose.yml Outdated Show resolved Hide resolved
webhook: string;
oauth1Callback: string;
oauth2Callback: string;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add N8N_VERSION_NOTIFICATIONS_ENDPOINT?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add N8N_LICENSE_SERVER_URL?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add EXTERNAL_FRONTEND_HOOKS_URLS?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add N8N_VERSION_NOTIFICATIONS_INFO_URL?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add N8N_PUBLIC_API_ENDPOINT?

packages/cli/src/services/url.service.ts Outdated Show resolved Hide resolved
return this.urls.editor;
}

/** This is the base url for webhooks */
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if the comment is adding much.

@netroy
Copy link
Member Author

netroy commented Jul 21, 2023

Thanks for taking this on!

From a quick test, running packages/cli/bin/n8n start --tunnel fails to open in browser, after a while ends up with 504 Gateway Time-out

That's because the hooks server is acting up again.

Comment on lines +1157 to +1042
webhookBaseUrl: webhookBaseUrl + config.getEnv('endpoints.webhook'),
webhookWaitingBaseUrl: webhookBaseUrl + config.getEnv('endpoints.webhookWaiting'),
webhookTestBaseUrl: webhookBaseUrl + config.getEnv('endpoints.webhookTest'),
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe expose those 3 URLs as part of the urlService so it's always centralized. Right now multiple places of the code can generate this URL, possibly leading to inconsistencies.

Comment on lines 60 to 61
// TODO: should this use `webhookBaseUrl` ?
const restBaseUrl = this.stripSlash(`${baseUrl}${config.getEnv('endpoints.rest')}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is correct, the backend (rest API) always resides with the editor.
The webhooks URL can (if set up like this) point to another pool of servers.

let's say you deploy n8n using https://n8n.omar.cloud pointing to editor + rest APIs and then another subdomain such as https://webhook.omar.cloud that points to another pool of servers only running webhook-type servers for queue mode.

@@ -1,15 +1,15 @@
export class SamlUrls {
static readonly samlRESTRoot = '/rest/sso/saml';
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch

webhookBaseUrl: {
format: String,
default: '',
env: 'WEBHOOK_URL',
Copy link
Contributor

Choose a reason for hiding this comment

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

Renaming environment variables breaks so many things, I advise against making this change now

baseUrl: {
format: String,
default: '',
env: 'N8N_BASE_URL',
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem with this new variable is that users were supposed to set up n8n using N8N_HOST, N8N_PATH and N8N_PORT

@@ -138,14 +141,14 @@ export class SamlService {

private getRedirectLoginRequestUrl(relayState?: string): BindingContext {
const sp = this.getServiceProviderInstance();
sp.entitySetting.relayState = relayState ?? getInstanceBaseUrl();
sp.entitySetting.relayState = relayState ?? this.urlService.editorBaseUrl;
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a fair point, maybe in the future we have these services running in separate places (rest API and FE). I'm afraid this would make the PR larger, useless at this time and harder to test. For now I think it's ok to assume that editor and rest api are always on the same host (as they actually are)

@ivov
Copy link
Contributor

ivov commented Sep 15, 2023

I'd love to eventually merge this in - if any of my comments take long, please feel free to disregard!

@netroy netroy marked this pull request as draft October 25, 2023 20:26
@netroy
Copy link
Member Author

netroy commented Oct 25, 2023

rebased. but going to break this out into 2 PRs, one introducing the URLService and it's tests, and other with any potential breaking changes.

netroy added a commit that referenced this pull request Dec 22, 2023
This change kept coming up in #6713, #7773, and #8135. 
So this PR moves the existing code without actually changing anything,
to help get rid of some of the circular dependencies.


## Review / Merge checklist
- [x] PR title and summary are descriptive.
@netroy
Copy link
Member Author

netroy commented Jun 20, 2024

closing this PR since quite a bit of this was already merged, and the remaining bits will need be done from scratch again.

@netroy netroy closed this Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team node/improvement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants