-
Notifications
You must be signed in to change notification settings - Fork 158
feat: add TLS support to API/UI service #1414
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
Open
fabiovincenzi
wants to merge
12
commits into
finos:main
Choose a base branch
from
fabiovincenzi:feat/tls-ui-api-server
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2725132
feat(config): add GIT_PROXY_HTTPS_UI_PORT env var with default 8444
fabiovincenzi 06e75f1
feat(service): add HTTPS server with TLS support on port 8444
fabiovincenzi 4942237
chore(docker): expose port 8444 for HTTPS UI/API service
fabiovincenzi 6e591f7
test(service): add TLS unit tests for service HTTPS server
fabiovincenzi f3bd5c2
Merge branch 'main' into feat/tls-ui-api-server
fabiovincenzi f7703a9
Merge branch 'main' into feat/tls-ui-api-server
fabiovincenzi fdd8573
Merge branch 'main' into feat/tls-ui-api-server
fabiovincenzi c7e6cfc
Merge branch 'main' into feat/tls-ui-api-server
fabiovincenzi 604b939
Merge branch 'main' into feat/tls-ui-api-server
fabiovincenzi 8320bb9
Merge branch 'main' into feat/tls-ui-api-server
fabiovincenzi 8cea3ab
Merge branch 'main' into feat/tls-ui-api-server
kriswest 60bbee5
Merge branch 'main' into feat/tls-ui-api-server
fabiovincenzi 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,152 @@ | ||
| import http from 'http'; | ||
| import https from 'https'; | ||
| import { describe, it, beforeEach, afterEach, expect, vi } from 'vitest'; | ||
| import fs from 'fs'; | ||
|
|
||
| describe('Service Module TLS', () => { | ||
| let serviceModule: any; | ||
| let mockConfig: any; | ||
| let mockHttpServer: any; | ||
| let mockHttpsServer: any; | ||
| let mockProxy: any; | ||
|
|
||
| beforeEach(async () => { | ||
| vi.resetModules(); | ||
|
|
||
| mockConfig = { | ||
| getTLSEnabled: vi.fn(), | ||
| getTLSKeyPemPath: vi.fn(), | ||
| getTLSCertPemPath: vi.fn(), | ||
| getRateLimit: vi.fn().mockReturnValue({ windowMs: 15 * 60 * 1000, max: 100 }), | ||
| getCookieSecret: vi.fn().mockReturnValue('test-secret'), | ||
| getSessionMaxAgeHours: vi.fn().mockReturnValue(12), | ||
| getCSRFProtection: vi.fn().mockReturnValue(false), | ||
| }; | ||
|
|
||
| mockHttpServer = { | ||
| listen: vi.fn().mockReturnThis(), | ||
| close: vi.fn().mockImplementation((cb) => { | ||
| if (cb) cb(); | ||
| }), | ||
| on: vi.fn().mockReturnThis(), | ||
| }; | ||
|
|
||
| mockHttpsServer = { | ||
| listen: vi.fn().mockImplementation((_port: any, cb: any) => { | ||
| if (cb) cb(); | ||
| return mockHttpsServer; | ||
| }), | ||
| close: vi.fn().mockImplementation((cb: any) => { | ||
| if (cb) cb(); | ||
| }), | ||
| on: vi.fn().mockReturnThis(), | ||
| }; | ||
|
|
||
| mockProxy = {}; | ||
|
|
||
| vi.doMock('../src/config', async (importOriginal) => { | ||
| const actual: any = await importOriginal(); | ||
| return { | ||
| ...actual, | ||
| getTLSEnabled: mockConfig.getTLSEnabled, | ||
| getTLSKeyPemPath: mockConfig.getTLSKeyPemPath, | ||
| getTLSCertPemPath: mockConfig.getTLSCertPemPath, | ||
| getRateLimit: mockConfig.getRateLimit, | ||
| getCookieSecret: mockConfig.getCookieSecret, | ||
| getSessionMaxAgeHours: mockConfig.getSessionMaxAgeHours, | ||
| getCSRFProtection: mockConfig.getCSRFProtection, | ||
| }; | ||
| }); | ||
|
|
||
| vi.doMock('../src/db', async (importOriginal) => { | ||
| const actual: any = await importOriginal(); | ||
| return { | ||
| ...actual, | ||
| getSessionStore: vi.fn().mockReturnValue(undefined), | ||
| }; | ||
| }); | ||
|
|
||
| vi.doMock('../src/service/passport', () => ({ | ||
| configure: vi.fn().mockResolvedValue({ | ||
| initialize: vi.fn().mockReturnValue((_req: any, _res: any, next: any) => next()), | ||
| session: vi.fn().mockReturnValue((_req: any, _res: any, next: any) => next()), | ||
| }), | ||
| })); | ||
|
|
||
| vi.doMock('../src/service/routes', () => ({ | ||
| default: vi.fn().mockReturnValue((_req: any, _res: any, next: any) => next()), | ||
| })); | ||
|
|
||
| vi.spyOn(http, 'createServer').mockReturnValue(mockHttpServer as any); | ||
| vi.spyOn(https, 'createServer').mockReturnValue(mockHttpsServer as any); | ||
|
|
||
| serviceModule = await import('../src/service/index'); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| try { | ||
| await serviceModule.Service.stop(); | ||
| } catch (err) { | ||
| console.error('Error occurred when stopping the service: ', err); | ||
| } | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('TLS certificate file reading', () => { | ||
| it('should start HTTPS server and read TLS files when TLS is enabled and paths are provided', async () => { | ||
| const mockKeyContent = Buffer.from('mock-key-content'); | ||
| const mockCertContent = Buffer.from('mock-cert-content'); | ||
|
|
||
| mockConfig.getTLSEnabled.mockReturnValue(true); | ||
| mockConfig.getTLSKeyPemPath.mockReturnValue('/path/to/key.pem'); | ||
| mockConfig.getTLSCertPemPath.mockReturnValue('/path/to/cert.pem'); | ||
|
|
||
| const fsStub = vi.spyOn(fs, 'readFileSync'); | ||
| fsStub.mockImplementation((path: any) => { | ||
| if (path === '/path/to/key.pem') return mockKeyContent; | ||
| if (path === '/path/to/cert.pem') return mockCertContent; | ||
| return Buffer.from('default'); | ||
| }); | ||
|
|
||
| await serviceModule.Service.start(mockProxy); | ||
|
|
||
| expect(https.createServer).toHaveBeenCalled(); | ||
| expect(fsStub).toHaveBeenCalledWith('/path/to/key.pem'); | ||
| expect(fsStub).toHaveBeenCalledWith('/path/to/cert.pem'); | ||
| }); | ||
|
|
||
| it('should not start HTTPS server when TLS is disabled', async () => { | ||
| mockConfig.getTLSEnabled.mockReturnValue(false); | ||
|
|
||
| await serviceModule.Service.start(mockProxy); | ||
|
|
||
| expect(https.createServer).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not read TLS files when paths are not provided', async () => { | ||
| mockConfig.getTLSEnabled.mockReturnValue(true); | ||
| mockConfig.getTLSKeyPemPath.mockReturnValue(null); | ||
| mockConfig.getTLSCertPemPath.mockReturnValue(null); | ||
|
|
||
| const fsStub = vi.spyOn(fs, 'readFileSync'); | ||
|
|
||
| await serviceModule.Service.start(mockProxy); | ||
|
|
||
| expect(fsStub).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should close both HTTP and HTTPS servers on stop() when TLS is enabled', async () => { | ||
| mockConfig.getTLSEnabled.mockReturnValue(true); | ||
| mockConfig.getTLSKeyPemPath.mockReturnValue('/path/to/key.pem'); | ||
| mockConfig.getTLSCertPemPath.mockReturnValue('/path/to/cert.pem'); | ||
|
|
||
| vi.spyOn(fs, 'readFileSync').mockReturnValue(Buffer.from('mock-content')); | ||
|
|
||
| await serviceModule.Service.start(mockProxy); | ||
| await serviceModule.Service.stop(); | ||
|
|
||
| expect(mockHttpServer.close).toHaveBeenCalled(); | ||
| expect(mockHttpsServer.close).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
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.
Is it okay to reuse the proxy TLS credentials for the service and UI or should these be separate configurable parameters 🤔