Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions api/src/routers/processings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import { reqOrigin, session } from '@data-fair/lib-express/index.js'
import { httpError } from '@data-fair/lib-utils/http-errors.js'
import { createNext } from '@data-fair/processings-shared/runs.ts'
import { applyProcessing, deleteProcessing } from '../utils/runs.ts'
import { cipher, decipher } from '@data-fair/processings-shared/cipher.ts'
import mongo from '#mongo'
import config from '#config'
import locks from '#locks'
import { resolvedSchema as processingSchema } from '#types/processing/index.ts'
import getApiDoc from '../utils/api-docs.ts'
import findUtils from '../utils/find.ts'
import permissions from '../utils/permissions.ts'
import { cipher } from '../utils/cipher.ts'

const router = Router()
export default router
Expand Down Expand Up @@ -52,12 +52,21 @@ const validateFullProcessing = async (processing: Processing) => {
// Get the plugin file and execute the prepare function if it exists
const plugin = await import(path.resolve(process.cwd(), pluginsDir, processing.plugin, 'index.js'))
if (plugin.prepare && typeof plugin.prepare === 'function') {
const res = await (plugin.prepare as PrepareFunction)({ processingConfig: processing.config })
// Decipher the actuals secrets if they are present
const secrets: Record<string, string> = {}
if (processing.secrets) {
Object.keys(processing.secrets).forEach(key => {
secrets[key] = decipher(processing.secrets![key], config.cipherPassword)
})
}

// Call the prepare function
const res = await (plugin.prepare as PrepareFunction)({ processingConfig: processing.config, secrets })
if (res.processingConfig) processing.config = res.processingConfig
if (res.secrets) {
processing.secrets = {}
Object.keys(res.secrets).forEach(key => {
processing.secrets![key] = cipher(res.secrets![key])
processing.secrets![key] = cipher(res.secrets![key], config.cipherPassword)
})
}
}
Expand Down
22 changes: 0 additions & 22 deletions api/src/utils/cipher.ts

This file was deleted.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@data-fair/lib-types-builder": "^1.7.0"
},
"devDependencies": {
"@data-fair/lib-common-types": "^1.10.0",
"@data-fair/lib-common-types": "^1.10.1",
"@commitlint/cli": "^19.7.1",
"@commitlint/config-conventional": "^19.7.1",
"@data-fair/lib-node": "^2.4.0",
Expand Down
32 changes: 32 additions & 0 deletions shared/cipher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { CipheredContent } from '#api/type/processing/index.js'
import { createHash, createCipheriv, createDecipheriv, randomBytes } from 'node:crypto'

const getSecurityKey = (password: string): Buffer => {
return createHash('sha256').update(password).digest()
}

export const cipher = (content: CipheredContent | string, cipherPassword: string): CipheredContent => {
const securityKey = getSecurityKey(cipherPassword)
if (typeof content !== 'string') return content

const initVector = randomBytes(16)
const algo = 'aes256'
const cipher = createCipheriv(algo, securityKey, initVector)
let encryptedData = cipher.update(content, 'utf-8', 'hex')
encryptedData += cipher.final('hex')
return {
iv: initVector.toString('hex'),
alg: algo,
data: encryptedData
}
}

export const decipher = (cipheredContent: CipheredContent | string, cipherPassword: string): string => {
const securityKey = getSecurityKey(cipherPassword)

if (typeof cipheredContent === 'string') return cipheredContent
const decipher = createDecipheriv(cipheredContent.alg, securityKey, Buffer.from(cipheredContent.iv, 'hex'))
let content = decipher.update(cipheredContent.data, 'hex', 'utf-8')
content += decipher.final('utf8')
return content
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"skipLibCheck": true,
"paths": {
"#api/types": ["./api/types/index.ts"],
"#api/type/*": ["./api/types/*"]
}
},
"exclude": ["node_modules", ".type", "ui", "dev", "data", "test-it"]
Expand Down
4 changes: 2 additions & 2 deletions worker/src/task/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import tmp from 'tmp-promise'
import { DataFairWsClient } from '@data-fair/lib-node/ws-client.js'
import { httpAgent, httpsAgent } from '@data-fair/lib-node/http-agents.js'
import * as wsEmitter from '@data-fair/lib-node/ws-emitter.js'
import { decipher } from '@data-fair/processings-shared/cipher.ts'
import { running } from '../utils/runs.ts'
import { decipher } from '../utils/cipher.ts'
import config from '#config'

fs.ensureDirSync(config.dataDir)
Expand Down Expand Up @@ -188,7 +188,7 @@ export const run = async (db: Db, mailTransport: any) => {
const secrets: Record<string, string> = {}
if (processing.secrets) {
Object.keys(processing.secrets).forEach(key => {
secrets[key] = decipher(processing.secrets![key])
secrets[key] = decipher(processing.secrets![key], config.cipherPassword)
})
}

Expand Down
17 changes: 0 additions & 17 deletions worker/src/utils/cipher.ts

This file was deleted.