diff --git a/src/util/customTransformer-faas.js b/src/util/customTransformer-faas.js index faa6a6ffa2..9ac9804097 100644 --- a/src/util/customTransformer-faas.js +++ b/src/util/customTransformer-faas.js @@ -11,8 +11,7 @@ const { } = require('./openfaas'); const { getLibraryCodeV1 } = require('./customTransforrmationsStore-v1'); -const HASH_SECRET = process.env.HASH_SECRET || ''; - +const HASH_SECRET = process.env.OPENFAAS_FN_HASH_SECRET || ''; const libVersionIdsCache = new NodeCache(); function generateFunctionName(userTransformation, libraryVersionIds, testMode, hashSecret = '') { @@ -28,7 +27,7 @@ function generateFunctionName(userTransformation, libraryVersionIds, testMode, h ); if (hashSecret !== '') { - ids = ids.concat([hashSecret]).sort(); + ids = ids.concat([hashSecret]); } // FIXME: Why the id's are sorted ?! @@ -98,7 +97,12 @@ async function setOpenFaasUserTransform( }; const functionName = pregeneratedFnName || - generateFunctionName(userTransformation, libraryVersionIds, testMode, HASH_SECRET); + generateFunctionName( + userTransformation, + libraryVersionIds, + testMode, + process.env.OPENFAAS_FN_HASH_SECRET, + ); const setupTime = new Date(); await setupFaasFunction( @@ -142,8 +146,9 @@ async function runOpenFaasUserTransform( userTransformation, libraryVersionIds, testMode, - HASH_SECRET, + process.env.OPENFAAS_FN_HASH_SECRET, ); + if (testMode) { await setOpenFaasUserTransform( userTransformation, diff --git a/test/__tests__/user_transformation.test.js b/test/__tests__/user_transformation.test.js index 77c7eaa257..c4f4a8792a 100644 --- a/test/__tests__/user_transformation.test.js +++ b/test/__tests__/user_transformation.test.js @@ -11,6 +11,7 @@ jest.mock("axios", () => ({ put: jest.fn() })); +const { generateFunctionName } = require('../../src/util/customTransformer-faas.js'); const { Response, Headers } = jest.requireActual("node-fetch"); const lodashCore = require("lodash/core"); const _ = require("lodash"); @@ -90,8 +91,12 @@ const pyLibCode = (name, versionId) => { } } -const pyfaasFuncName = (workspaceId, versionId, libraryVersionIds=[]) => { - const ids = [workspaceId, versionId].concat(libraryVersionIds.sort()); +const pyfaasFuncName = (workspaceId, versionId, libraryVersionIds=[], hashSecret="") => { + let ids = [workspaceId, versionId].concat(libraryVersionIds.sort()); + if (hashSecret !== "") { + ids = ids.concat([hashSecret]); + } + const hash = crypto.createHash('md5').update(`${ids}`).digest('hex'); return `fn-${workspaceId}-${hash}` @@ -107,6 +112,19 @@ const getfetchResponse = (resp, url) => let importNameLibraryVersionIdsMap; +describe("User transformation utils", () => { + + it("generates the openfaas-fn name correctly", () => { + const fnName = generateFunctionName( + {workspaceId: 'workspaceId', transformationId: 'transformationId'}, + [], + false, + 'hash-secret'); + expect(fnName).toEqual('fn-workspaceid-34a32ade07ebbc7bc5ea795b8200de9f'); + }); + +}); + describe("User transformation", () => { beforeEach(() => { jest.resetAllMocks();