Skip to content

Commit 2858fe5

Browse files
fix: avoid sending empty runtime images payloads
1 parent a38f8b5 commit 2858fe5

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

src/data-scraper/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ export async function scrapeData(): Promise<void> {
101101
2,
102102
);
103103

104-
logger.info({}, 'sending runtime data upstream');
105-
await sendRuntimeData(runtimeDataPayload);
104+
if (runtimeDataPayload) {
105+
logger.info({}, 'sending runtime data upstream');
106+
await sendRuntimeData(runtimeDataPayload);
107+
}
106108

107109
cursor = responseBody?.page.next || '';
108110
if (!cursor) {

src/data-scraper/scraping-v1.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ export async function scrapeDataV1(): Promise<void> {
9191
1,
9292
);
9393

94-
logger.info({}, 'sending runtime data upstream');
95-
await sendRuntimeData(runtimeDataPayload);
94+
if (runtimeDataPayload) {
95+
logger.info({}, 'sending runtime data upstream');
96+
await sendRuntimeData(runtimeDataPayload);
97+
}
9698

9799
cursor = responseBody?.page.next || '';
98100
if (!cursor) {

src/transmitter/payload.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ const workloadKindMap = {
152152
pod: 'Pod',
153153
rollout: 'Rollout',
154154
};
155+
155156
export function constructRuntimeData(
156157
runtimeResults: IRuntimeImage[],
157158
sysdigVersion: number,
158-
): IRuntimeDataPayload {
159+
): IRuntimeDataPayload | undefined {
159160
const filteredRuntimeResults = runtimeResults.reduce((acc, runtimeResult) => {
160161
if (!isExcludedNamespace(runtimeResult.namespace)) {
161162
const mappedWorkloadKind =
@@ -178,6 +179,10 @@ export function constructRuntimeData(
178179
return acc;
179180
}, [] as IRuntimeImage[]);
180181

182+
if (filteredRuntimeResults.length === 0) {
183+
return;
184+
}
185+
181186
const dataFact: IRuntimeDataFact = {
182187
type: 'loadedPackages',
183188
data: filteredRuntimeResults,

test/unit/data-scraper/scrape-data.spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,78 @@ describe('dataScraper()', () => {
132132
throw err;
133133
}
134134
});
135+
136+
it('correctly skips pages of data without relevant runtime images', async () => {
137+
const runtimeImageTemplate = {
138+
imageID: 'something',
139+
namespace: 'sysdig',
140+
workloadName: 'workload',
141+
workloadKind: 'Deployment',
142+
container: 'box',
143+
packages: [],
144+
};
145+
const page1 = {
146+
data: Array(10).fill({
147+
...runtimeImageTemplate,
148+
namespace: 'kube-system',
149+
}),
150+
page: {
151+
returned: 10,
152+
next: 'xxx',
153+
},
154+
};
155+
const page2 = {
156+
data: [runtimeImageTemplate],
157+
page: {
158+
returned: 1,
159+
},
160+
};
161+
nock('https://sysdig')
162+
.get(
163+
'/api/scanning/eveintegration/v2/runtimeimages?clusterName=test-sysdig-cluster&limit=10',
164+
)
165+
.times(1)
166+
.reply(200, page1)
167+
.get(
168+
'/api/scanning/eveintegration/v2/runtimeimages?clusterName=test-sysdig-cluster&limit=10&cursor=xxx',
169+
)
170+
.times(1)
171+
.reply(200, page2);
172+
173+
nock('https://api.snyk.io')
174+
.post(
175+
'/v2/kubernetes-upstream/api/v1/runtime-results?version=2023-02-10',
176+
)
177+
.times(1)
178+
.reply(200, (_, requestBody: transmitterTypes.IRuntimeDataPayload) => {
179+
expect(requestBody).toEqual<transmitterTypes.IRuntimeDataPayload>({
180+
identity: {
181+
type: 'sysdig',
182+
sysdigVersion: 2,
183+
},
184+
target: {
185+
userLocator: expect.any(String),
186+
cluster: expect.any(String),
187+
agentId: expect.any(String),
188+
},
189+
facts: [
190+
{
191+
type: 'loadedPackages',
192+
data: page2.data,
193+
},
194+
],
195+
});
196+
});
197+
198+
await scrapeData();
199+
200+
try {
201+
expect(nock.isDone()).toBeTruthy();
202+
} catch (err) {
203+
console.error(`nock pending mocks: ${nock.pendingMocks()}`);
204+
throw err;
205+
}
206+
});
135207
});
136208
describe('when sysdig v1 and v2 env vars configured, should use v2', () => {
137209
beforeAll(() => {

0 commit comments

Comments
 (0)