Skip to content
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

Logs: move logs validation to the point of ingest rather than publish #1915

Merged
merged 1 commit into from
Dec 24, 2024
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
20 changes: 0 additions & 20 deletions src/features/device-logs/lib/backends/loki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,25 +353,6 @@ export class LokiBackend implements DeviceLogsBackend {
return `{fleet_id="${ctx.appId}"}`;
}

private validateLog(log: DeviceLog): asserts log is DeviceLog {
if (typeof log.message !== 'string') {
throw new BadRequestError('DeviceLog message must be string');
} else if (typeof log.timestamp !== 'number') {
throw new BadRequestError('DeviceLog timestamp must be number');
} else if (typeof log.isSystem !== 'boolean') {
throw new BadRequestError('DeviceLog isSystem must be boolean');
} else if (typeof log.isStdErr !== 'boolean') {
throw new BadRequestError('DeviceLog isStdErr must be boolean');
} else if (
typeof log.serviceId !== 'number' &&
log.serviceId !== undefined
) {
throw new BadRequestError(
'DeviceLog serviceId must be number or undefined',
);
}
}

private fromStreamToDeviceLogs(stream: loki.StreamAdapter): DeviceLog[] {
try {
return stream.getEntriesList().map((entry) => {
Expand Down Expand Up @@ -399,7 +380,6 @@ export class LokiBackend implements DeviceLogsBackend {
logs: Array<DeviceLog & { version?: number }>,
) {
return logs.map((log) => {
this.validateLog(log);
const timestamp = new loki.Timestamp();
timestamp.setSeconds(Math.floor(Number(log.nanoTimestamp / 1000000000n)));
timestamp.setNanos(Number(log.nanoTimestamp % 1000000000n));
Expand Down
7 changes: 6 additions & 1 deletion src/features/device-logs/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ function handleStreamingWrite(
}

parser.on('error', close).on('data', (sLog: SupervisorLog) => {
const log = supervisor.convertLog(sLog);
let log;
try {
log = supervisor.convertLog(sLog);
} catch {
return;
}
if (!log) {
return;
}
Expand Down
22 changes: 19 additions & 3 deletions src/features/device-logs/lib/supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,36 @@ export class Supervisor {
return this.convertLog(log);
}

public convertLog(log: SupervisorLog): DeviceLog | undefined {
public convertLog(log: {
[key in keyof SupervisorLog]: unknown;
}): DeviceLog | undefined {
// see struct.ts for explanation on this
if (log.uuid) {
return;
}
return {
if (typeof log.message !== 'string') {
throw new errors.BadRequestError('DeviceLog message must be string');
}
if (typeof log.timestamp !== 'number') {
throw new errors.BadRequestError('DeviceLog timestamp must be number');
}
const validatedLog: DeviceLog = {
nanoTimestamp: getNanoTimestamp(),
createdAt: Date.now(),
timestamp: log.timestamp,
isSystem: log.isSystem === true,
isStdErr: log.isStdErr === true,
message: log.message,
serviceId: log.serviceId,
};
if ('serviceId' in log) {
if (typeof log.serviceId !== 'number') {
throw new errors.BadRequestError(
'DeviceLog serviceId must be number or undefined',
);
}
validatedLog.serviceId = log.serviceId;
}
return validatedLog;
}

private isOldLog(log: any): log is OldSupervisorLog {
Expand Down
Loading