diff --git a/backend/model/south-connector.model.ts b/backend/model/south-connector.model.ts new file mode 100644 index 0000000000..fb489ab541 --- /dev/null +++ b/backend/model/south-connector.model.ts @@ -0,0 +1,43 @@ +/** + * DTO for South connectors + */ +export interface SouthConnectorDTO { + id: string; + name: string; + type: string; + description: string; + enabled: boolean; + settings: object; +} + +/** + * Command DTO for South connector + */ +export interface SouthConnectorCommandDTO { + name: string; + type: string; + description: string; + enabled: boolean; + settings: object; +} + +/** + * DTO used for South scan (a point/query/regexp and its settings linked to a scan mode) + */ +export interface SouthScanDTO { + id: string; + name: string; + southId: string; + settings: object; + scanModeId: string; +} + +/** + * Command DTO used for South scan (a point/query/folder and its settings linked to a scan mode) + */ +export interface SouthScanCommandDTO { + southId: string; + name: string; + settings: object; + scanModeId: string; +} diff --git a/backend/repository/engine.repository.ts b/backend/repository/engine.repository.ts index 7a9e5457ae..36352f3b3c 100644 --- a/backend/repository/engine.repository.ts +++ b/backend/repository/engine.repository.ts @@ -3,6 +3,7 @@ import { EngineSettingsDTO, } from "../model/engine.model"; import { generateRandomId } from "./utils"; +import { PROXY_TABLE } from "./proxy.repository"; const ENGINE_TABLE = "engine"; @@ -68,8 +69,8 @@ export default class EngineRepository { "health_signal_http_interval INTEGER, health_signal_http_verbose INTEGER, health_signal_http_address TEXT, " + "health_signal_http_proxy_id TEXT, health_signal_http_authentication_type TEXT, health_signal_http_authentication_key TEXT, " + "health_signal_http_authentication_secret TEXT, " + - "FOREIGN KEY(log_loki_proxy_id) REFERENCES proxy(id), " + - "FOREIGN KEY(health_signal_http_proxy_id) REFERENCES proxy(id));"; + `FOREIGN KEY(log_loki_proxy_id) REFERENCES ${PROXY_TABLE}(id), ` + + `FOREIGN KEY(health_signal_http_proxy_id) REFERENCES ${PROXY_TABLE}(id));`; this.database.prepare(query).run(); this.createEngineSettings(defaultEngineSettings); diff --git a/backend/repository/proxy.repository.ts b/backend/repository/proxy.repository.ts index 6816dc5299..2fb5edcab2 100644 --- a/backend/repository/proxy.repository.ts +++ b/backend/repository/proxy.repository.ts @@ -1,7 +1,7 @@ import { generateRandomId } from "./utils"; import { ProxyCommandDTO, ProxyDTO } from "../model/proxy.model"; -const PROXY_TABLE = "proxy"; +export const PROXY_TABLE = "proxy"; /** * Repository used for proxies diff --git a/backend/repository/scan-mode.repository.ts b/backend/repository/scan-mode.repository.ts index e32f81cd66..6101fcbb72 100644 --- a/backend/repository/scan-mode.repository.ts +++ b/backend/repository/scan-mode.repository.ts @@ -1,7 +1,7 @@ import { ScanModeCommandDTO, ScanModeDTO } from "../model/scan-mode.model"; import { generateRandomId } from "./utils"; -const SCAN_MODE_TABLE = "scan_mode"; +export const SCAN_MODE_TABLE = "scan_mode"; /** * Repository used for scan modes (cron definitions) diff --git a/backend/repository/south-connector.repository.spec.ts b/backend/repository/south-connector.repository.spec.ts new file mode 100644 index 0000000000..3096b27667 --- /dev/null +++ b/backend/repository/south-connector.repository.spec.ts @@ -0,0 +1,155 @@ +import SqliteDatabaseMock from "../tests/__mocks__/database.mock"; +import { generateRandomId } from "./utils"; +import { + SouthConnectorCommandDTO, + SouthConnectorDTO, +} from "../model/south-connector.model"; +import SouthConnectorRepository from "./south-connector.repository"; + +jest.mock("../tests/__mocks__/database.mock"); +jest.mock("./utils", () => ({ + generateRandomId: jest.fn(() => "123456"), +})); + +let database; +let repository: SouthConnectorRepository; +describe("South connector repository", () => { + beforeEach(() => { + jest.clearAllMocks(); + database = new SqliteDatabaseMock(); + repository = new SouthConnectorRepository(database); + }); + + it("should properly init south connector table", () => { + expect(database.prepare).toHaveBeenCalledWith( + "CREATE TABLE IF NOT EXISTS south_connector (id TEXT PRIMARY KEY, name TEXT, type TEXT, description TEXT, " + + "enabled INTEGER, settings TEXT);" + ); + expect(database.run).toHaveBeenCalledTimes(1); + }); + + it("should properly get south connectors", () => { + const expectedValue: Array = [ + { + id: "id1", + name: "south1", + type: "SouthConnector", + description: "My south connector", + enabled: true, + settings: {}, + }, + { + id: "id2", + name: "south2", + type: "SouthConnector", + description: "My second south connector", + enabled: true, + settings: {}, + }, + ]; + database.all.mockReturnValueOnce([ + { + id: "id1", + name: "south1", + type: "SouthConnector", + description: "My south connector", + enabled: true, + settings: JSON.stringify({}), + }, + { + id: "id2", + name: "south2", + type: "SouthConnector", + description: "My second south connector", + enabled: true, + settings: JSON.stringify({}), + }, + ]); + const southConnectors = repository.getSouthConnectors(); + expect(database.prepare).toHaveBeenCalledWith( + "SELECT id, name, type, description, enabled, settings FROM south_connector;" + ); + expect(southConnectors).toEqual(expectedValue); + }); + + it("should properly get a south connector", () => { + const expectedValue: SouthConnectorDTO = { + id: "id1", + name: "south1", + type: "SouthConnector", + description: "My south connector", + enabled: true, + settings: {}, + }; + database.get.mockReturnValueOnce({ + id: "id1", + name: "south1", + type: "SouthConnector", + description: "My south connector", + enabled: true, + settings: JSON.stringify({}), + }); + const southConnector = repository.getSouthConnector("id1"); + expect(database.prepare).toHaveBeenCalledWith( + "SELECT id, name, type, description, enabled, settings FROM south_connector WHERE id = ?;" + ); + expect(database.get).toHaveBeenCalledWith("id1"); + expect(southConnector).toEqual(expectedValue); + }); + + it("should create a south connector", () => { + const command: SouthConnectorCommandDTO = { + name: "south1", + type: "SouthConnector", + description: "My south connector", + enabled: true, + settings: {}, + }; + repository.createSouthConnector(command); + expect(generateRandomId).toHaveBeenCalledWith(6); + expect(database.prepare).toHaveBeenCalledWith( + "INSERT INTO south_connector (id, name, type, description, enabled, settings) VALUES (?, ?, ?, ?, ?, ?);" + ); + expect(database.run).toHaveBeenCalledWith( + "123456", + command.name, + command.type, + command.description, + +command.enabled, + JSON.stringify(command.settings) + ); + + expect(database.prepare).toHaveBeenCalledWith( + "SELECT id, name, type, description, enabled, settings FROM south_connector WHERE ROWID = ?;" + ); + }); + + it("should update a south connector", () => { + const command: SouthConnectorCommandDTO = { + name: "south1", + type: "SouthConnector", + description: "My south connector", + enabled: true, + settings: {}, + }; + repository.updateSouthConnector("id1", command); + expect(database.prepare).toHaveBeenCalledWith( + "UPDATE south_connector SET name = ?, description = ?, enabled = ?, settings = ? WHERE id = ?;" + ); + expect(database.run).toHaveBeenCalledWith( + command.name, + command.description, + +command.enabled, + JSON.stringify(command.settings), + "id1" + ); + }); + + it("should delete a south connector", () => { + repository.deleteSouthConnector("id1"); + expect(database.prepare).toHaveBeenCalledWith( + "DELETE FROM south_connector WHERE id = ?;" + ); + expect(database.run).toHaveBeenCalledWith("id1"); + }); +}); diff --git a/backend/repository/south-connector.repository.ts b/backend/repository/south-connector.repository.ts new file mode 100644 index 0000000000..d4aee975c3 --- /dev/null +++ b/backend/repository/south-connector.repository.ts @@ -0,0 +1,112 @@ +import { generateRandomId } from "./utils"; +import { + SouthConnectorCommandDTO, + SouthConnectorDTO, +} from "../model/south-connector.model"; + +export const SOUTH_CONNECTOR_TABLE = "south_connector"; + +/** + * Repository used for South connectors (Data sources) + */ +export default class SouthConnectorRepository { + private readonly database; + constructor(database) { + this.database = database; + const query = + `CREATE TABLE IF NOT EXISTS ${SOUTH_CONNECTOR_TABLE} (id TEXT PRIMARY KEY, name TEXT, type TEXT, description TEXT, ` + + `enabled INTEGER, settings TEXT);`; + this.database.prepare(query).run(); + } + + /** + * Retrieve all South connectors + */ + getSouthConnectors(): Array { + const query = `SELECT id, name, type, description, enabled, settings FROM ${SOUTH_CONNECTOR_TABLE};`; + return this.database + .prepare(query) + .all() + .map((result) => ({ + id: result.id, + name: result.name, + type: result.type, + description: result.description, + enabled: result.enabled, + settings: JSON.parse(result.settings), + })); + } + + /** + * Retrieve a South connector by its ID + */ + getSouthConnector(id: string): SouthConnectorDTO { + const query = `SELECT id, name, type, description, enabled, settings FROM ${SOUTH_CONNECTOR_TABLE} WHERE id = ?;`; + const result = this.database.prepare(query).get(id); + return { + id: result.id, + name: result.name, + type: result.type, + description: result.description, + enabled: result.enabled, + settings: JSON.parse(result.settings), + }; + } + + /** + * Create a South connector with a random generated ID + */ + createSouthConnector(command: SouthConnectorCommandDTO): SouthConnectorDTO { + const id = generateRandomId(6); + const insertQuery = + `INSERT INTO ${SOUTH_CONNECTOR_TABLE} (id, name, type, description, enabled, settings) ` + + `VALUES (?, ?, ?, ?, ?, ?);`; + const insertResult = this.database + .prepare(insertQuery) + .run( + id, + command.name, + command.type, + command.description, + +command.enabled, + JSON.stringify(command.settings) + ); + + const query = `SELECT id, name, type, description, enabled, settings FROM ${SOUTH_CONNECTOR_TABLE} WHERE ROWID = ?;`; + const result = this.database + .prepare(query) + .get(insertResult.lastInsertRowid); + return { + id: result.id, + name: result.name, + type: result.type, + description: result.description, + enabled: result.enabled, + settings: JSON.parse(result.settings), + }; + } + + /** + * Update a South connector by its ID + */ + updateSouthConnector(id: string, command: SouthConnectorCommandDTO): void { + const query = `UPDATE ${SOUTH_CONNECTOR_TABLE} SET name = ?, description = ?, enabled = ?, settings = ? WHERE id = ?;`; + return this.database + .prepare(query) + .run( + command.name, + command.description, + +command.enabled, + JSON.stringify(command.settings), + id + ); + } + + /** + * Delete a South Connector by its ID + */ + deleteSouthConnector(id: string): void { + const query = `DELETE FROM ${SOUTH_CONNECTOR_TABLE} WHERE id = ?;`; + return this.database.prepare(query).run(id); + } +} diff --git a/backend/repository/south-scan.repository.spec.ts b/backend/repository/south-scan.repository.spec.ts new file mode 100644 index 0000000000..8f8d215672 --- /dev/null +++ b/backend/repository/south-scan.repository.spec.ts @@ -0,0 +1,146 @@ +import SqliteDatabaseMock from "../tests/__mocks__/database.mock"; +import { generateRandomId } from "./utils"; +import SouthScanRepository from "./south-scan.repository"; +import { + SouthScanCommandDTO, + SouthScanDTO, +} from "../model/south-connector.model"; + +jest.mock("../tests/__mocks__/database.mock"); +jest.mock("./utils", () => ({ + generateRandomId: jest.fn(() => "123456"), +})); + +let database; +let repository: SouthScanRepository; +describe("South scan repository", () => { + beforeEach(() => { + jest.clearAllMocks(); + database = new SqliteDatabaseMock(); + repository = new SouthScanRepository(database); + }); + + it("should properly init south scan table", () => { + expect(database.prepare).toHaveBeenCalledWith( + "CREATE TABLE IF NOT EXISTS south_scan (id TEXT PRIMARY KEY, south_id TEXT, scan_mode_id TEXT, name TEXT, " + + "settings TEXT, FOREIGN KEY(south_id) REFERENCES south_connector(id), " + + "FOREIGN KEY(scan_mode_id) REFERENCES scan_mode(id));" + ); + expect(database.run).toHaveBeenCalledTimes(1); + }); + + it("should properly get south scans by South ID", () => { + const expectedValue: Array = [ + { + id: "id1", + name: "my south scan", + southId: "south1", + scanModeId: "scanMode1", + settings: {}, + }, + { + id: "id2", + name: "my second south scan", + southId: "south1", + scanModeId: "scan1", + settings: {}, + }, + ]; + database.all.mockReturnValueOnce([ + { + id: "id1", + name: "my south scan", + southId: "south1", + scanModeId: "scanMode1", + settings: JSON.stringify({}), + }, + { + id: "id2", + name: "my second south scan", + southId: "south1", + scanModeId: "scan1", + settings: JSON.stringify({}), + }, + ]); + const southScans = repository.getSouthScans("south1"); + expect(database.prepare).toHaveBeenCalledWith( + "SELECT id, name, south_id AS southId, scan_mode_id AS scanModeId, settings FROM south_scan WHERE south_id = ?;" + ); + expect(southScans).toEqual(expectedValue); + }); + + it("should properly get a south scan", () => { + const expectedValue: SouthScanDTO = { + id: "id1", + name: "southScan1", + southId: "southId", + scanModeId: "scanModeId", + settings: {}, + }; + database.get.mockReturnValueOnce({ + id: "id1", + name: "southScan1", + southId: "southId", + scanModeId: "scanModeId", + settings: JSON.stringify({}), + }); + const southScan = repository.getSouthScan("id1"); + expect(database.prepare).toHaveBeenCalledWith( + "SELECT id, name, south_id AS southId, scan_mode_id AS scanModeId, settings FROM south_scan WHERE id = ?;" + ); + expect(database.get).toHaveBeenCalledWith("id1"); + expect(southScan).toEqual(expectedValue); + }); + + it("should create a south scan", () => { + const command: SouthScanCommandDTO = { + name: "southScan1", + southId: "southId", + scanModeId: "scanModeId", + settings: {}, + }; + repository.createSouthScan(command); + expect(generateRandomId).toHaveBeenCalledWith(6); + expect(database.prepare).toHaveBeenCalledWith( + "INSERT INTO south_scan (id, name, south_id, scan_mode_id, settings) VALUES (?, ?, ?, ?, ?);" + ); + expect(database.run).toHaveBeenCalledWith( + "123456", + command.name, + command.southId, + command.scanModeId, + JSON.stringify(command.settings) + ); + + expect(database.prepare).toHaveBeenCalledWith( + "SELECT id, name, south_id AS southId, scan_mode_id AS scanModeId, settings FROM south_scan WHERE ROWID = ?;" + ); + }); + + it("should update a south scan", () => { + const command: SouthScanCommandDTO = { + name: "southScan1", + southId: "southId", + scanModeId: "scanModeId", + settings: {}, + }; + repository.updateSouthScan("id1", command); + expect(database.prepare).toHaveBeenCalledWith( + "UPDATE south_scan SET name = ?, scan_mode_id = ?, settings = ? WHERE id = ?;" + ); + expect(database.run).toHaveBeenCalledWith( + command.name, + command.scanModeId, + JSON.stringify(command.settings), + "id1" + ); + }); + + it("should delete a south scan", () => { + repository.deleteSouthScan("id1"); + expect(database.prepare).toHaveBeenCalledWith( + "DELETE FROM south_scan WHERE id = ?;" + ); + expect(database.run).toHaveBeenCalledWith("id1"); + }); +}); diff --git a/backend/repository/south-scan.repository.ts b/backend/repository/south-scan.repository.ts new file mode 100644 index 0000000000..7d187eb586 --- /dev/null +++ b/backend/repository/south-scan.repository.ts @@ -0,0 +1,110 @@ +import { generateRandomId } from "./utils"; +import { + SouthScanCommandDTO, + SouthScanDTO, +} from "../model/south-connector.model"; +import { SOUTH_CONNECTOR_TABLE } from "./south-connector.repository"; +import { SCAN_MODE_TABLE } from "./scan-mode.repository"; + +const SOUTH_SCAN_TABLE = "south_scan"; + +/** + * Repository used for South connectors (Data sources) + */ +export default class SouthScanRepository { + private readonly database; + constructor(database) { + this.database = database; + const query = + `CREATE TABLE IF NOT EXISTS ${SOUTH_SCAN_TABLE} (id TEXT PRIMARY KEY, south_id TEXT, scan_mode_id TEXT, name TEXT, ` + + `settings TEXT, FOREIGN KEY(south_id) REFERENCES ${SOUTH_CONNECTOR_TABLE}(id), ` + + `FOREIGN KEY(scan_mode_id) REFERENCES ${SCAN_MODE_TABLE}(id));`; + this.database.prepare(query).run(); + } + + /** + * Retrieve all South scan (point, query, folder...) associated to a South connector + */ + getSouthScans(southId: string): Array { + const query = `SELECT id, name, south_id AS southId, scan_mode_id AS scanModeId, settings FROM ${SOUTH_SCAN_TABLE} WHERE south_id = ?;`; + return this.database + .prepare(query) + .all(southId) + .map((result) => ({ + id: result.id, + name: result.name, + southId: result.southId, + scanModeId: result.scanModeId, + settings: JSON.parse(result.settings), + })); + } + + /** + * Retrieve a South scan by its ID + */ + getSouthScan(id: string): SouthScanDTO { + const query = `SELECT id, name, south_id AS southId, scan_mode_id AS scanModeId, settings FROM ${SOUTH_SCAN_TABLE} WHERE id = ?;`; + const result = this.database.prepare(query).get(id); + return { + id: result.id, + name: result.name, + southId: result.southId, + scanModeId: result.scanModeId, + settings: JSON.parse(result.settings), + }; + } + + /** + * Create a South scan with a random generated ID + */ + createSouthScan(command: SouthScanCommandDTO): SouthScanDTO { + const id = generateRandomId(6); + const insertQuery = + `INSERT INTO ${SOUTH_SCAN_TABLE} (id, name, south_id, scan_mode_id, settings) ` + + `VALUES (?, ?, ?, ?, ?);`; + const insertResult = this.database + .prepare(insertQuery) + .run( + id, + command.name, + command.southId, + command.scanModeId, + JSON.stringify(command.settings) + ); + + const query = `SELECT id, name, south_id AS southId, scan_mode_id AS scanModeId, settings FROM ${SOUTH_SCAN_TABLE} WHERE ROWID = ?;`; + const result = this.database + .prepare(query) + .get(insertResult.lastInsertRowid); + return { + id: result.id, + name: result.name, + southId: result.southId, + scanModeId: result.scanModeId, + settings: JSON.parse(result.settings), + }; + } + + /** + * Update a South scan by its ID + */ + updateSouthScan(id: string, command: SouthScanCommandDTO): void { + const query = `UPDATE ${SOUTH_SCAN_TABLE} SET name = ?, scan_mode_id = ?, settings = ? WHERE id = ?;`; + return this.database + .prepare(query) + .run( + command.name, + command.scanModeId, + JSON.stringify(command.settings), + id + ); + } + + /** + * Delete a South scan by its ID + */ + deleteSouthScan(id: string): void { + const query = `DELETE FROM ${SOUTH_SCAN_TABLE} WHERE id = ?;`; + return this.database.prepare(query).run(id); + } +} diff --git a/backend/service/repository.service.spec.ts b/backend/service/repository.service.spec.ts index e043ccb525..09e3825afc 100644 --- a/backend/service/repository.service.spec.ts +++ b/backend/service/repository.service.spec.ts @@ -7,6 +7,8 @@ import ExternalSourceRepository from "../repository/external-source.repository"; import IpFilterRepository from "../repository/ip-filter.repository"; import ProxyRepository from "../repository/proxy.repository"; import ScanModeRepository from "../repository/scan-mode.repository"; +import SouthConnectorRepository from "../repository/south-connector.repository"; +import SouthScanRepository from "../repository/south-scan.repository"; jest.mock("better-sqlite3", () => jest.fn(() => "sqlite database")); jest.mock("../repository/external-source.repository"); @@ -14,6 +16,8 @@ jest.mock("../repository/ip-filter.repository"); jest.mock("../repository/proxy.repository"); jest.mock("../repository/scan-mode.repository"); jest.mock("../repository/engine.repository"); +jest.mock("../repository/south-connector.repository"); +jest.mock("../repository/south-scan.repository"); describe("Repository service", () => { it("should properly initialize service", () => { @@ -24,11 +28,15 @@ describe("Repository service", () => { expect(IpFilterRepository).toHaveBeenCalledWith("sqlite database"); expect(ProxyRepository).toHaveBeenCalledWith("sqlite database"); expect(ScanModeRepository).toHaveBeenCalledWith("sqlite database"); + expect(SouthConnectorRepository).toHaveBeenCalledWith("sqlite database"); + expect(SouthScanRepository).toHaveBeenCalledWith("sqlite database"); expect(repositoryService.engineRepository).toBeDefined(); expect(repositoryService.externalSourceRepository).toBeDefined(); expect(repositoryService.ipFilterRepository).toBeDefined(); expect(repositoryService.proxyRepository).toBeDefined(); expect(repositoryService.scanModeRepository).toBeDefined(); + expect(repositoryService.southConnectorRepository).toBeDefined(); + expect(repositoryService.southScanRepository).toBeDefined(); }); }); diff --git a/backend/service/repository.service.ts b/backend/service/repository.service.ts index 7c1d2e9f24..b6b4f61f55 100644 --- a/backend/service/repository.service.ts +++ b/backend/service/repository.service.ts @@ -5,6 +5,8 @@ import ExternalSourceRepository from "../repository/external-source.repository"; import IpFilterRepository from "../repository/ip-filter.repository"; import ProxyRepository from "../repository/proxy.repository"; import ScanModeRepository from "../repository/scan-mode.repository"; +import SouthConnectorRepository from "../repository/south-connector.repository"; +import SouthScanRepository from "../repository/south-scan.repository"; export default class RepositoryService { private readonly _engineRepository: EngineRepository; @@ -12,6 +14,8 @@ export default class RepositoryService { private readonly _ipFilterRepository: IpFilterRepository; private readonly _proxyRepository: ProxyRepository; private readonly _scanModeRepository: ScanModeRepository; + private readonly _southConnectorRepository: SouthConnectorRepository; + private readonly _southScanRepository: SouthScanRepository; constructor(databasePath: string) { const database = db(databasePath); @@ -20,6 +24,8 @@ export default class RepositoryService { this._proxyRepository = new ProxyRepository(database); this._scanModeRepository = new ScanModeRepository(database); this._engineRepository = new EngineRepository(database); + this._southConnectorRepository = new SouthConnectorRepository(database); + this._southScanRepository = new SouthScanRepository(database); } get engineRepository(): EngineRepository { @@ -41,4 +47,12 @@ export default class RepositoryService { get scanModeRepository(): ScanModeRepository { return this._scanModeRepository; } + + get southConnectorRepository(): SouthConnectorRepository { + return this._southConnectorRepository; + } + + get southScanRepository(): SouthScanRepository { + return this._southScanRepository; + } } diff --git a/backend/tests/__mocks__/database.mock.ts b/backend/tests/__mocks__/database.mock.ts index cc492c288a..40e75e745a 100644 --- a/backend/tests/__mocks__/database.mock.ts +++ b/backend/tests/__mocks__/database.mock.ts @@ -4,7 +4,7 @@ export default jest.fn().mockImplementation(() => { const execution = { run: jest.fn(() => ({ lastInsertRowId: 1 })), - get: jest.fn(), + get: jest.fn(() => ({ id: "id1", settings: JSON.stringify({}) })), all: jest.fn(), }; return { diff --git a/backend/web-server/controllers/api.controller.ts b/backend/web-server/controllers/api.controller.ts index 6501cbcd2e..f1fe2c4e40 100644 --- a/backend/web-server/controllers/api.controller.ts +++ b/backend/web-server/controllers/api.controller.ts @@ -3,13 +3,6 @@ import { EngineSettingsCommandDTO, EngineSettingsDTO, } from "../../model/engine.model"; -import { ProxyCommandDTO, ProxyDTO } from "../../model/proxy.model"; -import { ScanModeCommandDTO, ScanModeDTO } from "../../model/scan-mode.model"; -import { IpFilterCommandDTO, IpFilterDTO } from "../../model/ip-filter.model"; -import { - ExternalSourceCommandDTO, - ExternalSourceDTO, -} from "../../model/external-sources.model"; const getEngineSettings = async (ctx: KoaContext) => { const settings = @@ -26,167 +19,7 @@ const updateEngineSettings = async ( ctx.ok("Engine settings updated!"); }; -const getProxies = async (ctx: KoaContext>) => { - const proxies = ctx.app.repositoryService.proxyRepository.getProxies(); - ctx.ok(proxies); -}; - -const getProxy = async (ctx: KoaContext) => { - const proxy = ctx.app.repositoryService.proxyRepository.getProxy( - ctx.params.id - ); - ctx.ok(proxy); -}; - -const createProxy = async (ctx: KoaContext) => { - const proxy = ctx.app.repositoryService.proxyRepository.createProxy( - ctx.request.body - ); - ctx.ok(proxy); -}; - -const updateProxy = async (ctx: KoaContext) => { - ctx.app.repositoryService.proxyRepository.updateProxy( - ctx.params.id, - ctx.request.body - ); - - ctx.ok(); -}; - -const deleteProxy = async (ctx: KoaContext) => { - ctx.app.repositoryService.proxyRepository.deleteProxy(ctx.params.id); - ctx.ok(); -}; - -const getScanModes = async (ctx: KoaContext>) => { - const scanModes = ctx.app.repositoryService.scanModeRepository.getScanModes(); - ctx.ok(scanModes); -}; - -const getScanMode = async (ctx: KoaContext) => { - const scanMode = ctx.app.repositoryService.scanModeRepository.getScanMode( - ctx.params.id - ); - ctx.ok(scanMode); -}; - -const createScanMode = async (ctx: KoaContext) => { - const scanMode = ctx.app.repositoryService.scanModeRepository.createScanMode( - ctx.request.body - ); - ctx.ok(scanMode); -}; - -const updateScanMode = async (ctx: KoaContext) => { - ctx.app.repositoryService.scanModeRepository.updateScanMode( - ctx.params.id, - ctx.request.body - ); - ctx.ok(); -}; - -const deleteScanMode = async (ctx: KoaContext) => { - ctx.app.repositoryService.scanModeRepository.deleteScanMode(ctx.params.id); - ctx.ok(); -}; - -const getIpFilters = async (ctx: KoaContext>) => { - const ipFilters = ctx.app.repositoryService.ipFilterRepository.getIpFilters(); - ctx.ok(ipFilters); -}; - -const getIpFilter = async (ctx: KoaContext) => { - const ipFilter = ctx.app.repositoryService.ipFilterRepository.getIpFilter( - ctx.params.id - ); - ctx.ok(ipFilter); -}; - -const createIpFilter = async (ctx: KoaContext) => { - const ipFilter = ctx.app.repositoryService.ipFilterRepository.createIpFilter( - ctx.request.body - ); - ctx.ok(ipFilter); -}; - -const updateIpFilter = async (ctx: KoaContext) => { - ctx.app.repositoryService.ipFilterRepository.updateIpFilter( - ctx.params.id, - ctx.request.body - ); - ctx.ok(); -}; - -const deleteIpFilter = async (ctx: KoaContext) => { - ctx.app.repositoryService.ipFilterRepository.deleteIpFilter(ctx.params.id); - ctx.ok(); -}; - -const getExternalSources = async ( - ctx: KoaContext> -) => { - const externalSources = - ctx.app.repositoryService.externalSourceRepository.getExternalSources(); - ctx.ok(externalSources); -}; - -const getExternalSource = async (ctx: KoaContext) => { - const externalSource = - ctx.app.repositoryService.externalSourceRepository.getExternalSource( - ctx.params.id - ); - ctx.ok(externalSource); -}; - -const createExternalSource = async ( - ctx: KoaContext -) => { - const externalSource = - ctx.app.repositoryService.externalSourceRepository.createExternalSource( - ctx.request.body - ); - ctx.ok(externalSource); -}; - -const updateExternalSource = async ( - ctx: KoaContext -) => { - ctx.app.repositoryService.externalSourceRepository.updateExternalSource( - ctx.params.id, - ctx.request.body - ); - ctx.ok(); -}; - -const deleteExternalSource = async (ctx: KoaContext) => { - ctx.app.repositoryService.externalSourceRepository.deleteExternalSource( - ctx.params.id - ); - ctx.ok(); -}; - export default { getEngineSettings, updateEngineSettings, - getProxies, - getProxy, - createProxy, - updateProxy, - deleteProxy, - getScanModes, - getScanMode, - createScanMode, - updateScanMode, - deleteScanMode, - getIpFilters, - getIpFilter, - createIpFilter, - updateIpFilter, - deleteIpFilter, - getExternalSources, - getExternalSource, - createExternalSource, - updateExternalSource, - deleteExternalSource, }; diff --git a/backend/web-server/controllers/external-source.controller.ts b/backend/web-server/controllers/external-source.controller.ts new file mode 100644 index 0000000000..9f9d131f92 --- /dev/null +++ b/backend/web-server/controllers/external-source.controller.ts @@ -0,0 +1,56 @@ +import { KoaContext } from "../koa"; +import { + ExternalSourceCommandDTO, + ExternalSourceDTO, +} from "../../model/external-sources.model"; + +const getExternalSources = async ( + ctx: KoaContext> +) => { + const externalSources = + ctx.app.repositoryService.externalSourceRepository.getExternalSources(); + ctx.ok(externalSources); +}; + +const getExternalSource = async (ctx: KoaContext) => { + const externalSource = + ctx.app.repositoryService.externalSourceRepository.getExternalSource( + ctx.params.id + ); + ctx.ok(externalSource); +}; + +const createExternalSource = async ( + ctx: KoaContext +) => { + const externalSource = + ctx.app.repositoryService.externalSourceRepository.createExternalSource( + ctx.request.body + ); + ctx.ok(externalSource); +}; + +const updateExternalSource = async ( + ctx: KoaContext +) => { + ctx.app.repositoryService.externalSourceRepository.updateExternalSource( + ctx.params.id, + ctx.request.body + ); + ctx.ok(); +}; + +const deleteExternalSource = async (ctx: KoaContext) => { + ctx.app.repositoryService.externalSourceRepository.deleteExternalSource( + ctx.params.id + ); + ctx.ok(); +}; + +export default { + getExternalSources, + getExternalSource, + createExternalSource, + updateExternalSource, + deleteExternalSource, +}; diff --git a/backend/web-server/controllers/ip-filter.controller.ts b/backend/web-server/controllers/ip-filter.controller.ts new file mode 100644 index 0000000000..95cf18bac3 --- /dev/null +++ b/backend/web-server/controllers/ip-filter.controller.ts @@ -0,0 +1,42 @@ +import { KoaContext } from "../koa"; +import { IpFilterCommandDTO, IpFilterDTO } from "../../model/ip-filter.model"; + +const getIpFilters = async (ctx: KoaContext>) => { + const ipFilters = ctx.app.repositoryService.ipFilterRepository.getIpFilters(); + ctx.ok(ipFilters); +}; + +const getIpFilter = async (ctx: KoaContext) => { + const ipFilter = ctx.app.repositoryService.ipFilterRepository.getIpFilter( + ctx.params.id + ); + ctx.ok(ipFilter); +}; + +const createIpFilter = async (ctx: KoaContext) => { + const ipFilter = ctx.app.repositoryService.ipFilterRepository.createIpFilter( + ctx.request.body + ); + ctx.ok(ipFilter); +}; + +const updateIpFilter = async (ctx: KoaContext) => { + ctx.app.repositoryService.ipFilterRepository.updateIpFilter( + ctx.params.id, + ctx.request.body + ); + ctx.ok(); +}; + +const deleteIpFilter = async (ctx: KoaContext) => { + ctx.app.repositoryService.ipFilterRepository.deleteIpFilter(ctx.params.id); + ctx.ok(); +}; + +export default { + getIpFilters, + getIpFilter, + createIpFilter, + updateIpFilter, + deleteIpFilter, +}; diff --git a/backend/web-server/controllers/proxy.controller.ts b/backend/web-server/controllers/proxy.controller.ts new file mode 100644 index 0000000000..9088ccae86 --- /dev/null +++ b/backend/web-server/controllers/proxy.controller.ts @@ -0,0 +1,43 @@ +import { KoaContext } from "../koa"; +import { ProxyCommandDTO, ProxyDTO } from "../../model/proxy.model"; + +const getProxies = async (ctx: KoaContext>) => { + const proxies = ctx.app.repositoryService.proxyRepository.getProxies(); + ctx.ok(proxies); +}; + +const getProxy = async (ctx: KoaContext) => { + const proxy = ctx.app.repositoryService.proxyRepository.getProxy( + ctx.params.id + ); + ctx.ok(proxy); +}; + +const createProxy = async (ctx: KoaContext) => { + const proxy = ctx.app.repositoryService.proxyRepository.createProxy( + ctx.request.body + ); + ctx.ok(proxy); +}; + +const updateProxy = async (ctx: KoaContext) => { + ctx.app.repositoryService.proxyRepository.updateProxy( + ctx.params.id, + ctx.request.body + ); + + ctx.ok(); +}; + +const deleteProxy = async (ctx: KoaContext) => { + ctx.app.repositoryService.proxyRepository.deleteProxy(ctx.params.id); + ctx.ok(); +}; + +export default { + getProxies, + getProxy, + createProxy, + updateProxy, + deleteProxy, +}; diff --git a/backend/web-server/controllers/scan-mode.controller.ts b/backend/web-server/controllers/scan-mode.controller.ts new file mode 100644 index 0000000000..1ca9c3e129 --- /dev/null +++ b/backend/web-server/controllers/scan-mode.controller.ts @@ -0,0 +1,42 @@ +import { KoaContext } from "../koa"; +import { ScanModeCommandDTO, ScanModeDTO } from "../../model/scan-mode.model"; + +const getScanModes = async (ctx: KoaContext>) => { + const scanModes = ctx.app.repositoryService.scanModeRepository.getScanModes(); + ctx.ok(scanModes); +}; + +const getScanMode = async (ctx: KoaContext) => { + const scanMode = ctx.app.repositoryService.scanModeRepository.getScanMode( + ctx.params.id + ); + ctx.ok(scanMode); +}; + +const createScanMode = async (ctx: KoaContext) => { + const scanMode = ctx.app.repositoryService.scanModeRepository.createScanMode( + ctx.request.body + ); + ctx.ok(scanMode); +}; + +const updateScanMode = async (ctx: KoaContext) => { + ctx.app.repositoryService.scanModeRepository.updateScanMode( + ctx.params.id, + ctx.request.body + ); + ctx.ok(); +}; + +const deleteScanMode = async (ctx: KoaContext) => { + ctx.app.repositoryService.scanModeRepository.deleteScanMode(ctx.params.id); + ctx.ok(); +}; + +export default { + getScanModes, + getScanMode, + createScanMode, + updateScanMode, + deleteScanMode, +}; diff --git a/backend/web-server/controllers/south-connector.controller.ts b/backend/web-server/controllers/south-connector.controller.ts new file mode 100644 index 0000000000..49a852b6e3 --- /dev/null +++ b/backend/web-server/controllers/south-connector.controller.ts @@ -0,0 +1,99 @@ +import { KoaContext } from "../koa"; +import { + SouthConnectorCommandDTO, + SouthConnectorDTO, + SouthScanCommandDTO, + SouthScanDTO, +} from "../../model/south-connector.model"; + +const getSouthConnectors = async ( + ctx: KoaContext> +) => { + const southConnectors = + ctx.app.repositoryService.southConnectorRepository.getSouthConnectors(); + ctx.ok(southConnectors); +}; + +const getSouthConnector = async (ctx: KoaContext) => { + const southConnector = + ctx.app.repositoryService.southConnectorRepository.getSouthConnector( + ctx.params.id + ); + ctx.ok(southConnector); +}; + +const createSouthConnector = async ( + ctx: KoaContext +) => { + const southConnector = + ctx.app.repositoryService.southConnectorRepository.createSouthConnector( + ctx.request.body + ); + ctx.ok(southConnector); +}; + +const updateSouthConnector = async ( + ctx: KoaContext +) => { + ctx.app.repositoryService.southConnectorRepository.updateSouthConnector( + ctx.params.id, + ctx.request.body + ); + ctx.ok(); +}; + +const deleteSouthConnector = async (ctx: KoaContext) => { + ctx.app.repositoryService.southConnectorRepository.deleteSouthConnector( + ctx.params.id + ); + ctx.ok(); +}; + +const getSouthScans = async (ctx: KoaContext>) => { + const southScans = + ctx.app.repositoryService.southScanRepository.getSouthScans( + ctx.params.southId + ); + ctx.ok(southScans); +}; + +const getSouthScan = async (ctx: KoaContext) => { + const southScan = ctx.app.repositoryService.southScanRepository.getSouthScan( + ctx.params.id + ); + ctx.ok(southScan); +}; + +const createSouthScan = async (ctx: KoaContext) => { + const southScan = + ctx.app.repositoryService.southScanRepository.createSouthScan( + ctx.request.body + ); + ctx.ok(southScan); +}; + +const updateSouthScan = async (ctx: KoaContext) => { + ctx.app.repositoryService.southScanRepository.updateSouthScan( + ctx.params.id, + ctx.request.body + ); + ctx.ok(); +}; + +const deleteSouthScan = async (ctx: KoaContext) => { + ctx.app.repositoryService.southScanRepository.deleteSouthScan(ctx.params.id); + ctx.ok(); +}; + +export default { + getSouthConnectors, + getSouthConnector, + createSouthConnector, + updateSouthConnector, + deleteSouthConnector, + getSouthScans, + getSouthScan, + createSouthScan, + updateSouthScan, + deleteSouthScan, +}; diff --git a/backend/web-server/routes/index.js b/backend/web-server/routes/index.js index 15bdd09778..c51460920f 100644 --- a/backend/web-server/routes/index.js +++ b/backend/web-server/routes/index.js @@ -8,6 +8,11 @@ import historyQueryController from '../controllers/history-query.controller.js' import oibusController from '../controllers/oibus.controller.js' import fileCacheController from '../controllers/file-cache.controller.js' import apiController from '../controllers/api.controller' +import proxyController from '../controllers/proxy.controller' +import externalSourceController from '../controllers/external-source.controller' +import ipFilterController from '../controllers/ip-filter.controller' +import scanModeController from '../controllers/scan-mode.controller' +import southConnectorController from '../controllers/south-connector.controller' const router = new Router() @@ -52,25 +57,35 @@ router.post('/north/:id/cache/file-errors/retry-all', fileCacheController.retryA router.get('/api/engine', apiController.getEngineSettings) router.put('/api/engine', apiController.updateEngineSettings) -router.get('/api/proxies', apiController.getProxies) -router.get('/api/proxies/:id', apiController.getProxy) -router.post('/api/proxies', apiController.createProxy) -router.put('/api/proxies/:id', apiController.updateProxy) -router.delete('/api/proxies/:id', apiController.deleteProxy) -router.get('/api/scan-modes', apiController.getScanModes) -router.get('/api/scan-modes/:id', apiController.getScanMode) -router.post('/api/scan-modes', apiController.createScanMode) -router.put('/api/scan-modes/:id', apiController.updateScanMode) -router.delete('/api/scan-modes/:id', apiController.deleteScanMode) -router.get('/api/ip-filters', apiController.getIpFilters) -router.get('/api/ip-filters/:id', apiController.getIpFilter) -router.post('/api/ip-filters', apiController.createIpFilter) -router.put('/api/ip-filters/:id', apiController.updateIpFilter) -router.delete('/api/ip-filters/:id', apiController.deleteIpFilter) -router.get('/api/external-sources', apiController.getExternalSources) -router.get('/api/external-sources/:id', apiController.getExternalSource) -router.post('/api/external-sources', apiController.createExternalSource) -router.put('/api/external-sources/:id', apiController.updateExternalSource) -router.delete('/api/external-sources/:id', apiController.deleteExternalSource) +router.get('/api/proxies', proxyController.getProxies) +router.get('/api/proxies/:id', proxyController.getProxy) +router.post('/api/proxies', proxyController.createProxy) +router.put('/api/proxies/:id', proxyController.updateProxy) +router.delete('/api/proxies/:id', proxyController.deleteProxy) +router.get('/api/scan-modes', scanModeController.getScanModes) +router.get('/api/scan-modes/:id', scanModeController.getScanMode) +router.post('/api/scan-modes', scanModeController.createScanMode) +router.put('/api/scan-modes/:id', scanModeController.updateScanMode) +router.delete('/api/scan-modes/:id', scanModeController.deleteScanMode) +router.get('/api/ip-filters', ipFilterController.getIpFilters) +router.get('/api/ip-filters/:id', ipFilterController.getIpFilter) +router.post('/api/ip-filters', ipFilterController.createIpFilter) +router.put('/api/ip-filters/:id', ipFilterController.updateIpFilter) +router.delete('/api/ip-filters/:id', ipFilterController.deleteIpFilter) +router.get('/api/external-sources', externalSourceController.getExternalSources) +router.get('/api/external-sources/:id', externalSourceController.getExternalSource) +router.post('/api/external-sources', externalSourceController.createExternalSource) +router.put('/api/external-sources/:id', externalSourceController.updateExternalSource) +router.delete('/api/external-sources/:id', externalSourceController.deleteExternalSource) +router.get('/api/south-connector', southConnectorController.getSouthConnectors) +router.get('/api/south-connector/:id', southConnectorController.getSouthConnector) +router.post('/api/south-connector', southConnectorController.createSouthConnector) +router.put('/api/south-connector/:id', southConnectorController.updateSouthConnector) +router.delete('/api/south-connector/:id', southConnectorController.deleteSouthConnector) +router.get('/api/south-connector/:southId/scan', southConnectorController.getSouthScans) +router.get('/api/south-connector/:southId/scan/:id', southConnectorController.getSouthScan) +router.post('/api/south-connector/:southId/scan', southConnectorController.createSouthScan) +router.put('/api/south-connector/:southId/scan/:id', southConnectorController.updateSouthScan) +router.delete('/api/south-connector/:southId/scan/:id', southConnectorController.deleteSouthScan) export default router