-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudflare): add webhook support for custom hostname updates
Introduce a Cloudflare controller to handle webhook events for custom domain updates. This includes verifying secrets, validating input, and updating workspace configurations accordingly. Adjusted related services and entities for compatibility and added a new environment variable for webhook secret.
- Loading branch information
Showing
11 changed files
with
346 additions
and
19 deletions.
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
93 changes: 93 additions & 0 deletions
93
...twenty-server/src/engine/core-modules/domain-manager/controllers/cloudflare.controller.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,93 @@ | ||
import { Controller, Post, Req, Res, UseFilters } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import { Response } from 'express'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { AuthRestApiExceptionFilter } from 'src/engine/core-modules/auth/filters/auth-rest-api-exception.filter'; | ||
import { DomainManagerService } from 'src/engine/core-modules/domain-manager/services/domain-manager.service'; | ||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { | ||
DomainManagerException, | ||
DomainManagerExceptionCode, | ||
} from 'src/engine/core-modules/domain-manager/domain-manager.exception'; | ||
import { handleException } from 'src/engine/core-modules/exception-handler/http-exception-handler.service'; | ||
import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service'; | ||
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service'; | ||
|
||
@Controller('cloudflare') | ||
@UseFilters(AuthRestApiExceptionFilter) | ||
export class CloudflareController { | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
private readonly domainManagerService: DomainManagerService, | ||
private readonly exceptionHandlerService: ExceptionHandlerService, | ||
private readonly environmentService: EnvironmentService, | ||
) {} | ||
|
||
@Post('custom-hostname-webhooks') | ||
async customHostnameWebhooks(@Req() req: any, @Res() res: Response) { | ||
const cloudflareWebhookSecret = this.environmentService.get( | ||
'CLOUDFLARE_WEBHOOK_SECRET', | ||
); | ||
|
||
if ( | ||
cloudflareWebhookSecret && | ||
req.headers['cf-webhook-auth'] !== cloudflareWebhookSecret | ||
) { | ||
throw new DomainManagerException( | ||
'Invalid secret', | ||
DomainManagerExceptionCode.INVALID_INPUT_DATA, | ||
); | ||
} | ||
|
||
if (!req.body.data.data.hostname) { | ||
handleException( | ||
new DomainManagerException( | ||
'Hostname missing', | ||
DomainManagerExceptionCode.INVALID_INPUT_DATA, | ||
), | ||
this.exceptionHandlerService, | ||
); | ||
|
||
return res.status(200).send(); | ||
} | ||
|
||
const workspace = await this.workspaceRepository.findOneBy({ | ||
customDomain: req.body.data.data.hostname, | ||
}); | ||
|
||
if (!workspace) return; | ||
|
||
const customDomainDetails = | ||
await this.domainManagerService.getCustomDomainDetails( | ||
req.body.data.data.hostname, | ||
); | ||
|
||
const workspaceUpdated: Partial<Workspace> = { | ||
customDomain: workspace.customDomain, | ||
}; | ||
|
||
if (!customDomainDetails && workspace) { | ||
workspaceUpdated.customDomain = null; | ||
} | ||
|
||
workspaceUpdated.isCustomDomainEnabled = customDomainDetails | ||
? this.domainManagerService.isCustomDomainWorking(customDomainDetails) | ||
: false; | ||
|
||
if ( | ||
workspaceUpdated.isCustomDomainEnabled !== | ||
workspace.isCustomDomainEnabled || | ||
workspaceUpdated.customDomain !== workspace.customDomain | ||
) { | ||
await this.workspaceRepository.save({ | ||
...workspace, | ||
...workspaceUpdated, | ||
}); | ||
} | ||
|
||
return res.status(200).send(); | ||
} | ||
} |
217 changes: 217 additions & 0 deletions
217
packages/twenty-server/src/engine/core-modules/domain-manager/controllers/cloudflare.spec.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,217 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
|
||
import { Repository } from 'typeorm'; | ||
import { Request, Response } from 'express'; | ||
|
||
import { CloudflareController } from 'src/engine/core-modules/domain-manager/controllers/cloudflare.controller'; | ||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { DomainManagerService } from 'src/engine/core-modules/domain-manager/services/domain-manager.service'; | ||
import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service'; | ||
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service'; | ||
import { HttpExceptionHandlerService } from 'src/engine/core-modules/exception-handler/http-exception-handler.service'; | ||
import { CustomDomainDetails } from 'src/engine/core-modules/domain-manager/dtos/custom-domain-details'; | ||
|
||
describe('CloudflareController - customHostnameWebhooks', () => { | ||
let controller: CloudflareController; | ||
let WorkspaceRepository: Repository<Workspace>; | ||
let environmentService: EnvironmentService; | ||
let domainManagerService: DomainManagerService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [CloudflareController], | ||
providers: [ | ||
{ | ||
provide: getRepositoryToken(Workspace, 'core'), | ||
useValue: { | ||
findOneBy: jest.fn(), | ||
save: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: DomainManagerService, | ||
useValue: { | ||
getCustomDomainDetails: jest.fn(), | ||
isCustomDomainWorking: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: HttpExceptionHandlerService, | ||
useValue: { | ||
handleError: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: ExceptionHandlerService, | ||
useValue: { | ||
captureExceptions: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: EnvironmentService, | ||
useValue: { | ||
get: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get<CloudflareController>(CloudflareController); | ||
WorkspaceRepository = module.get(getRepositoryToken(Workspace, 'core')); | ||
environmentService = module.get<EnvironmentService>(EnvironmentService); | ||
domainManagerService = | ||
module.get<DomainManagerService>(DomainManagerService); | ||
}); | ||
|
||
it('should throw an error if the webhook secret does not match', async () => { | ||
const req = { | ||
headers: { 'cf-webhook-auth': 'wrong-secret' }, | ||
body: { data: { data: { hostname: 'example.com' } } }, | ||
} as unknown as Request; | ||
|
||
const res = {} as Response; | ||
|
||
jest.spyOn(environmentService, 'get').mockReturnValue('correct-secret'); | ||
|
||
await expect(controller.customHostnameWebhooks(req, res)).rejects.toThrow( | ||
'Invalid secret', | ||
); | ||
}); | ||
|
||
it('should handle exception and return status 200 if hostname is missing', async () => { | ||
const req = { | ||
headers: { 'cf-webhook-auth': 'correct-secret' }, | ||
body: { data: { data: {} } }, | ||
} as unknown as Request; | ||
const sendMock = jest.fn(); | ||
const res = { | ||
status: jest.fn().mockReturnThis(), | ||
send: sendMock, | ||
} as unknown as Response; | ||
|
||
jest.spyOn(environmentService, 'get').mockReturnValue('correct-secret'); | ||
|
||
await controller.customHostnameWebhooks(req, res); | ||
|
||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(sendMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should update workspace for a valid hostname and save changes', async () => { | ||
const req = { | ||
headers: { 'cf-webhook-auth': 'correct-secret' }, | ||
body: { data: { data: { hostname: 'example.com' } } }, | ||
} as unknown as Request; | ||
const sendMock = jest.fn(); | ||
const res = { | ||
status: jest.fn().mockReturnThis(), | ||
send: sendMock, | ||
} as unknown as Response; | ||
|
||
jest.spyOn(environmentService, 'get').mockReturnValue('correct-secret'); | ||
jest | ||
.spyOn(domainManagerService, 'getCustomDomainDetails') | ||
.mockResolvedValue({ | ||
records: [ | ||
{ | ||
success: true, | ||
}, | ||
], | ||
} as unknown as CustomDomainDetails); | ||
jest | ||
.spyOn(domainManagerService, 'isCustomDomainWorking') | ||
.mockReturnValue(true); | ||
jest.spyOn(WorkspaceRepository, 'findOneBy').mockResolvedValue({ | ||
customDomain: 'example.com', | ||
isCustomDomainEnabled: false, | ||
} as Workspace); | ||
|
||
await controller.customHostnameWebhooks(req, res); | ||
|
||
expect(WorkspaceRepository.findOneBy).toHaveBeenCalledWith({ | ||
customDomain: 'example.com', | ||
}); | ||
expect(domainManagerService.getCustomDomainDetails).toHaveBeenCalledWith( | ||
'example.com', | ||
); | ||
expect(WorkspaceRepository.save).toHaveBeenCalledWith({ | ||
customDomain: 'example.com', | ||
isCustomDomainEnabled: true, | ||
}); | ||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(sendMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should remove customDomain if no hostname found', async () => { | ||
const req = { | ||
headers: { 'cf-webhook-auth': 'correct-secret' }, | ||
body: { data: { data: { hostname: 'notfound.com' } } }, | ||
} as unknown as Request; | ||
const sendMock = jest.fn(); | ||
const res = { | ||
status: jest.fn().mockReturnThis(), | ||
send: sendMock, | ||
} as unknown as Response; | ||
|
||
jest.spyOn(environmentService, 'get').mockReturnValue('correct-secret'); | ||
jest.spyOn(WorkspaceRepository, 'findOneBy').mockResolvedValue({ | ||
customDomain: 'notfound.com', | ||
isCustomDomainEnabled: true, | ||
} as Workspace); | ||
|
||
jest | ||
.spyOn(domainManagerService, 'getCustomDomainDetails') | ||
.mockResolvedValue(undefined); | ||
|
||
await controller.customHostnameWebhooks(req, res); | ||
|
||
expect(WorkspaceRepository.findOneBy).toHaveBeenCalledWith({ | ||
customDomain: 'notfound.com', | ||
}); | ||
expect(WorkspaceRepository.save).toHaveBeenCalledWith({ | ||
customDomain: null, | ||
isCustomDomainEnabled: false, | ||
}); | ||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(sendMock).toHaveBeenCalled(); | ||
}); | ||
it('should do nothing if nothing change', async () => { | ||
const req = { | ||
headers: { 'cf-webhook-auth': 'correct-secret' }, | ||
body: { data: { data: { hostname: 'nothing-change.com' } } }, | ||
} as unknown as Request; | ||
const sendMock = jest.fn(); | ||
const res = { | ||
status: jest.fn().mockReturnThis(), | ||
send: sendMock, | ||
} as unknown as Response; | ||
|
||
jest.spyOn(environmentService, 'get').mockReturnValue('correct-secret'); | ||
jest.spyOn(WorkspaceRepository, 'findOneBy').mockResolvedValue({ | ||
customDomain: 'nothing-change.com', | ||
isCustomDomainEnabled: true, | ||
} as Workspace); | ||
jest | ||
.spyOn(domainManagerService, 'getCustomDomainDetails') | ||
.mockResolvedValue({ | ||
records: [ | ||
{ | ||
success: true, | ||
}, | ||
], | ||
} as unknown as CustomDomainDetails); | ||
jest | ||
.spyOn(domainManagerService, 'isCustomDomainWorking') | ||
.mockReturnValue(true); | ||
|
||
await controller.customHostnameWebhooks(req, res); | ||
|
||
expect(WorkspaceRepository.findOneBy).toHaveBeenCalledWith({ | ||
customDomain: 'nothing-change.com', | ||
}); | ||
expect(WorkspaceRepository.save).not.toHaveBeenCalled(); | ||
expect(res.status).toHaveBeenCalledWith(200); | ||
expect(sendMock).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
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
Oops, something went wrong.