-
Notifications
You must be signed in to change notification settings - Fork 16.6k
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(core): Improve UX on permission errors (no-changelog) #13795
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
78450e9
improve the errors on permission checker
netroy a688e95
render subworkflow permission-checking errors
netroy 4e1ec19
Merge remote-tracking branch 'origin/master' into improve-permission-…
netroy 40d9c33
address PR feedback
netroy 0668160
Update packages/cli/src/executions/__tests__/execution.service.test.ts
netroy 0300696
Merge remote-tracking branch 'origin/master' into improve-permission-…
netroy 9a9462b
move generateFailedExecutionFromError to ExecutionDataService
netroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/cli/src/executions/__tests__/execution-data.service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { mock } from 'jest-mock-extended'; | ||
import { NodeOperationError } from 'n8n-workflow'; | ||
import type { INode, WorkflowExecuteMode } from 'n8n-workflow'; | ||
|
||
import { ExecutionDataService } from '../execution-data.service'; | ||
|
||
describe('ExecutionDataService', () => { | ||
const service = new ExecutionDataService(); | ||
|
||
describe('generateFailedExecutionFromError', () => { | ||
const mode: WorkflowExecuteMode = 'manual'; | ||
const node = mock<INode>({ name: 'Test Node' }); | ||
const error = new NodeOperationError(node, 'Test error message'); | ||
|
||
it('should generate a failed execution with error details', () => { | ||
const startTime = Date.now(); | ||
|
||
const result = service.generateFailedExecutionFromError(mode, error, node, startTime); | ||
|
||
expect(result.mode).toBe(mode); | ||
expect(result.status).toBe('error'); | ||
expect(result.startedAt).toBeInstanceOf(Date); | ||
expect(result.stoppedAt).toBeInstanceOf(Date); | ||
expect(result.data.resultData.error?.message).toBe(error.message); | ||
|
||
const taskData = result.data.resultData.runData[node.name][0]; | ||
expect(taskData.error?.message).toBe(error.message); | ||
expect(taskData.startTime).toBe(startTime); | ||
expect(taskData.executionStatus).toBe('error'); | ||
expect(result.data.resultData.lastNodeExecuted).toBe(node.name); | ||
expect(result.data.executionData?.nodeExecutionStack[0].node).toEqual(node); | ||
}); | ||
|
||
it('should generate a failed execution without node details if node is undefined', () => { | ||
const result = service.generateFailedExecutionFromError(mode, error, undefined); | ||
|
||
expect(result.mode).toBe(mode); | ||
expect(result.status).toBe('error'); | ||
expect(result.startedAt).toBeInstanceOf(Date); | ||
expect(result.stoppedAt).toBeInstanceOf(Date); | ||
expect(result.data.resultData.error?.message).toBe(error.message); | ||
expect(result.data.resultData.runData).toEqual({}); | ||
expect(result.data.resultData.lastNodeExecuted).toBeUndefined(); | ||
expect(result.data.executionData).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Service } from '@n8n/di'; | ||
import type { ExecutionError, INode, IRun, WorkflowExecuteMode } from 'n8n-workflow'; | ||
|
||
@Service() | ||
export class ExecutionDataService { | ||
generateFailedExecutionFromError( | ||
mode: WorkflowExecuteMode, | ||
error: ExecutionError, | ||
node: INode | undefined, | ||
startTime = Date.now(), | ||
): IRun { | ||
const executionError = { | ||
...error, | ||
message: error.message, | ||
stack: error.stack, | ||
}; | ||
const returnData: IRun = { | ||
data: { | ||
resultData: { | ||
error: executionError, | ||
runData: {}, | ||
}, | ||
}, | ||
finished: false, | ||
mode, | ||
startedAt: new Date(), | ||
stoppedAt: new Date(), | ||
status: 'error', | ||
}; | ||
|
||
if (node) { | ||
returnData.data.startData = { | ||
destinationNode: node.name, | ||
runNodeFilter: [node.name], | ||
}; | ||
returnData.data.resultData.lastNodeExecuted = node.name; | ||
returnData.data.resultData.runData[node.name] = [ | ||
{ | ||
startTime, | ||
executionTime: 0, | ||
executionStatus: 'error', | ||
error: executionError, | ||
source: [], | ||
}, | ||
]; | ||
returnData.data.executionData = { | ||
contextData: {}, | ||
metadata: {}, | ||
waitingExecution: {}, | ||
waitingExecutionSource: {}, | ||
nodeExecutionStack: [ | ||
{ | ||
node, | ||
data: {}, | ||
source: null, | ||
}, | ||
], | ||
}; | ||
} | ||
return returnData; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
.../cli/src/executions/pre-execution-checks/__tests__/credentials-permission-checker.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { mock } from 'jest-mock-extended'; | ||
import type { INode } from 'n8n-workflow'; | ||
|
||
import type { Project } from '@/databases/entities/project'; | ||
import type { User } from '@/databases/entities/user'; | ||
import type { SharedCredentialsRepository } from '@/databases/repositories/shared-credentials.repository'; | ||
import type { OwnershipService } from '@/services/ownership.service'; | ||
import type { ProjectService } from '@/services/project.service.ee'; | ||
|
||
import { CredentialsPermissionChecker } from '../credentials-permission-checker'; | ||
|
||
describe('CredentialsPermissionChecker', () => { | ||
const sharedCredentialsRepository = mock<SharedCredentialsRepository>(); | ||
const ownershipService = mock<OwnershipService>(); | ||
const projectService = mock<ProjectService>(); | ||
const permissionChecker = new CredentialsPermissionChecker( | ||
sharedCredentialsRepository, | ||
ownershipService, | ||
projectService, | ||
); | ||
|
||
const workflowId = 'workflow123'; | ||
const credentialId = 'cred123'; | ||
const personalProject = mock<Project>({ | ||
id: 'personal-project', | ||
name: 'Personal Project', | ||
type: 'personal', | ||
}); | ||
|
||
const node = mock<INode>({ | ||
name: 'Test Node', | ||
credentials: { | ||
someCredential: { | ||
id: credentialId, | ||
name: 'Test Credential', | ||
}, | ||
}, | ||
disabled: false, | ||
}); | ||
|
||
beforeEach(async () => { | ||
jest.resetAllMocks(); | ||
|
||
node.credentials!.someCredential.id = credentialId; | ||
ownershipService.getWorkflowProjectCached.mockResolvedValueOnce(personalProject); | ||
projectService.findProjectsWorkflowIsIn.mockResolvedValueOnce([personalProject.id]); | ||
}); | ||
|
||
it('should throw if a node has a credential without an id', async () => { | ||
node.credentials!.someCredential.id = null; | ||
|
||
await expect(permissionChecker.check(workflowId, [node])).rejects.toThrow( | ||
'Node "Test Node" uses invalid credential', | ||
); | ||
|
||
expect(projectService.findProjectsWorkflowIsIn).toHaveBeenCalledWith(workflowId); | ||
expect(sharedCredentialsRepository.getFilteredAccessibleCredentials).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw if a credential is not accessible', async () => { | ||
ownershipService.getPersonalProjectOwnerCached.mockResolvedValueOnce(null); | ||
sharedCredentialsRepository.getFilteredAccessibleCredentials.mockResolvedValueOnce([]); | ||
|
||
await expect(permissionChecker.check(workflowId, [node])).rejects.toThrow( | ||
'Node "Test Node" does not have access to the credential', | ||
); | ||
|
||
expect(projectService.findProjectsWorkflowIsIn).toHaveBeenCalledWith(workflowId); | ||
expect(sharedCredentialsRepository.getFilteredAccessibleCredentials).toHaveBeenCalledWith( | ||
[personalProject.id], | ||
[credentialId], | ||
); | ||
}); | ||
|
||
it('should not throw an error if the workflow has no credentials', async () => { | ||
await expect(permissionChecker.check(workflowId, [])).resolves.not.toThrow(); | ||
|
||
expect(projectService.findProjectsWorkflowIsIn).toHaveBeenCalledWith(workflowId); | ||
expect(sharedCredentialsRepository.getFilteredAccessibleCredentials).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not throw an error if all credentials are accessible', async () => { | ||
ownershipService.getPersonalProjectOwnerCached.mockResolvedValueOnce(null); | ||
sharedCredentialsRepository.getFilteredAccessibleCredentials.mockResolvedValueOnce([ | ||
credentialId, | ||
]); | ||
|
||
await expect(permissionChecker.check(workflowId, [node])).resolves.not.toThrow(); | ||
|
||
expect(projectService.findProjectsWorkflowIsIn).toHaveBeenCalledWith(workflowId); | ||
expect(sharedCredentialsRepository.getFilteredAccessibleCredentials).toHaveBeenCalledWith( | ||
[personalProject.id], | ||
[credentialId], | ||
); | ||
}); | ||
|
||
it('should skip credential checks if the home project owner has global scope', async () => { | ||
const projectOwner = mock<User>({ | ||
hasGlobalScope: (scope) => scope === 'credential:list', | ||
}); | ||
ownershipService.getPersonalProjectOwnerCached.mockResolvedValueOnce(projectOwner); | ||
|
||
await expect(permissionChecker.check(workflowId, [node])).resolves.not.toThrow(); | ||
|
||
expect(projectService.findProjectsWorkflowIsIn).not.toHaveBeenCalled(); | ||
expect(sharedCredentialsRepository.getFilteredAccessibleCredentials).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { CredentialsPermissionChecker } from './credentials-permission-checker'; | ||
export { SubworkflowPolicyChecker } from './subworkflow-policy-checker'; |
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this name better 👍🏻