chore: resolve raised changes
Signed-off-by: John Redwood <john.r.k.redwood@gmail.com>
This commit is contained in:
+1
-1
@@ -201,7 +201,7 @@ catalog:
|
||||
|
||||
scaffolder:
|
||||
auditor:
|
||||
maxLength: 256
|
||||
taskParameterMaxLength: 256
|
||||
# Use to customize default commit author info used when new components are created
|
||||
defaultAuthor:
|
||||
name: Scaffolder
|
||||
|
||||
+15
@@ -93,5 +93,20 @@ export interface Config {
|
||||
* Default value is 24 hours.
|
||||
*/
|
||||
taskTimeout?: HumanDuration | string;
|
||||
|
||||
/**
|
||||
* Sets the maximum length for task parameters recorded by the auditor.
|
||||
*
|
||||
* If set to -1, the limit is disabled and parameters are not truncated.
|
||||
* Defaults to 256 character length.
|
||||
*
|
||||
* @example
|
||||
* scaffolder:
|
||||
* auditor:
|
||||
* taskParameterMaxLength: 512
|
||||
*/
|
||||
auditor?: {
|
||||
taskParameterMaxLength?: number;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import os from 'os';
|
||||
import { DatabaseManager } from '@backstage/backend-defaults/database';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { Config, ConfigReader } from '@backstage/config';
|
||||
import { DatabaseTaskStore } from './DatabaseTaskStore';
|
||||
import { StorageTaskBroker } from './StorageTaskBroker';
|
||||
import { TaskWorker, TaskWorkerOptions } from './TaskWorker';
|
||||
@@ -343,3 +343,58 @@ describe('TaskWorker internals', () => {
|
||||
expect(inflightTasks.length).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('TaskWorker.truncateParameters', () => {
|
||||
let worker: TaskWorker;
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.resetAllMocks();
|
||||
|
||||
const logger = { debug: jest.fn() } as any;
|
||||
|
||||
const config = {
|
||||
getOptionalNumber: jest.fn().mockReturnValue(5),
|
||||
} as unknown as Config;
|
||||
|
||||
worker = await TaskWorker.create({
|
||||
logger,
|
||||
workingDirectory: '/tmp',
|
||||
integrations: {} as ScmIntegrations,
|
||||
taskBroker: {} as TaskBroker,
|
||||
actionRegistry: {} as TemplateActionRegistry,
|
||||
config,
|
||||
});
|
||||
});
|
||||
|
||||
it('successfully does nothing', async () => {
|
||||
const testParams = {};
|
||||
|
||||
// @ts-expect-error (truncateParameters is private, but for test we can access)
|
||||
const result = worker.truncateParameters(testParams);
|
||||
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
it('truncates long strings in nested objects and arrays', async () => {
|
||||
const params = {
|
||||
test: 'short',
|
||||
test2: 'thisisaverylongstring',
|
||||
nested: {
|
||||
test3: 'anotherlongstringhere',
|
||||
test4: ['ok', 'toolongstring'],
|
||||
},
|
||||
};
|
||||
|
||||
// @ts-expect-error (truncateParameters is private, but for test we can access)
|
||||
const result = worker.truncateParameters(params);
|
||||
|
||||
expect(result).toEqual({
|
||||
test: 'short',
|
||||
test2: 'thisi...<truncated>',
|
||||
nested: {
|
||||
test3: 'anoth...<truncated>',
|
||||
test4: ['ok', 'toolo...<truncated>'],
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -190,34 +190,44 @@ export class TaskWorker {
|
||||
});
|
||||
}
|
||||
|
||||
protected truncateParameters(parameters: JsonObject) {
|
||||
const auditMaxLength =
|
||||
this.config?.getOptionalNumber('scaffolder.auditor.maxLength') ?? 256;
|
||||
private truncateParameters(parameters: JsonObject) {
|
||||
const taskParameterMaxLength =
|
||||
this.config?.getOptionalNumber(
|
||||
'scaffolder.auditor.taskParameterMaxLength',
|
||||
) ?? 256;
|
||||
|
||||
if (auditMaxLength === -1) {
|
||||
if (taskParameterMaxLength === -1) {
|
||||
this.logger?.debug(
|
||||
`scaffolder.auditor.maxLength manually disabled via configuration, no task parameter length limit set.`,
|
||||
`scaffolder.auditor.taskParameterMaxLength manually disabled via configuration, no task parameter length limit set.`,
|
||||
);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
const truncatedParameters: JsonObject = {};
|
||||
|
||||
for (const key in parameters) {
|
||||
if (Object.prototype.hasOwnProperty.call(parameters, key)) {
|
||||
const rawValue = parameters[key];
|
||||
const value = rawValue?.toString();
|
||||
if (value && value.length > auditMaxLength) {
|
||||
truncatedParameters[key] = value
|
||||
.slice(0, auditMaxLength)
|
||||
function truncate(value: unknown): unknown {
|
||||
if (typeof value === 'string') {
|
||||
if (value.length > taskParameterMaxLength) {
|
||||
return value
|
||||
.slice(0, taskParameterMaxLength)
|
||||
.concat('...<truncated>');
|
||||
} else {
|
||||
truncatedParameters[key] = rawValue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(truncate);
|
||||
}
|
||||
if (value && typeof value === 'object') {
|
||||
const result: Record<string, unknown> = {};
|
||||
for (const k in value as object) {
|
||||
if (Object.hasOwn(value, k)) {
|
||||
result[k] = truncate((value as any)[k]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
return truncatedParameters;
|
||||
return truncate(parameters) as JsonObject;
|
||||
}
|
||||
|
||||
async runOneTask(task: TaskContext) {
|
||||
|
||||
Reference in New Issue
Block a user