Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"__inputs": [
{
"name": "DS_NUL.DEV.NULLIFY.AI",
"label": "nul.dev.nullify.ai",
"name": "DS_NULLIFY",
"label": "Nullify",
"description": "",
"type": "datasource",
"pluginId": "nullify-grafana-datasource",
Expand All @@ -27,7 +27,7 @@
"type": "datasource",
"id": "nullify-grafana-datasource",
"name": "Nullify",
"version": "1.0.0"
"version": "0.0.0"
},
{
"type": "panel",
Expand Down Expand Up @@ -62,7 +62,7 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -232,10 +232,14 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"endpoint": "sast/summary",
"queryParameters": {},
"queryParameters": {
"githubRepositoryIdsOrQueries": [
"$repository"
]
},
"refId": "A"
}
],
Expand Down Expand Up @@ -316,7 +320,7 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -395,10 +399,14 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"endpoint": "secrets/events",
"queryParameters": {},
"queryParameters": {
"githubRepositoryIdsOrQueries": [
"$repository"
]
},
"refId": "A"
}
],
Expand Down Expand Up @@ -448,7 +456,7 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -526,10 +534,14 @@
"constant": 6.5,
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"endpoint": "sast/summary",
"queryParameters": {},
"queryParameters": {
"githubRepositoryIdsOrQueries": [
"$repository"
]
},
"refId": "A"
}
],
Expand Down Expand Up @@ -572,7 +584,7 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -651,10 +663,14 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"endpoint": "secrets/events",
"queryParameters": {},
"queryParameters": {
"githubRepositoryIdsOrQueries": [
"$repository"
]
},
"refId": "A"
}
],
Expand Down Expand Up @@ -704,7 +720,7 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -781,10 +797,14 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"endpoint": "sast/summary",
"queryParameters": {},
"queryParameters": {
"githubRepositoryIdsOrQueries": [
"$repository"
]
},
"refId": "A"
}
],
Expand Down Expand Up @@ -839,7 +859,7 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -915,10 +935,14 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"endpoint": "secrets/summary",
"queryParameters": {},
"queryParameters": {
"githubRepositoryIdsOrQueries": [
"$repository"
]
},
"refId": "A"
}
],
Expand Down Expand Up @@ -947,7 +971,7 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -1025,10 +1049,14 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"endpoint": "sast/events",
"queryParameters": {},
"queryParameters": {
"githubRepositoryIdsOrQueries": [
"$repository"
]
},
"refId": "A"
}
],
Expand All @@ -1038,7 +1066,7 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -1173,10 +1201,14 @@
{
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NUL.DEV.NULLIFY.AI}"
"uid": "${DS_NULLIFY}"
},
"endpoint": "sca/summary",
"queryParameters": {},
"queryParameters": {
"githubRepositoryIdsOrQueries": [
"$repository"
]
},
"refId": "A"
}
],
Expand Down Expand Up @@ -1245,7 +1277,31 @@
"style": "dark",
"tags": [],
"templating": {
"list": []
"list": [
{
"current": {},
"datasource": {
"type": "nullify-grafana-datasource",
"uid": "${DS_NULLIFY}"
},
"definition": "Nullify Repository Query",
"description": "Filter for the repositories for which to query data",
"hide": 0,
"includeAll": true,
"label": "Repository",
"multi": true,
"name": "repository",
"options": [],
"query": {
"queryType": "Repository"
},
"refresh": 1,
"regex": "",
"skipUrlSync": false,
"sort": 0,
"type": "query"
}
]
},
"time": {
"from": "now-7d",
Expand All @@ -1255,6 +1311,6 @@
"timezone": "",
"title": "Nullify Demo Dashboard",
"uid": "ecf0f5c8-bf81-479a-b72f-94b5c2855d04",
"version": 6,
"version": 2,
"weekStart": ""
}
2 changes: 1 addition & 1 deletion provisioning/datasources/datasources.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: 1

datasources:
- name: 'grafana'
- name: 'Nullify Datasource'
type: 'nullify-grafana-datasource'
access: proxy
isDefault: false
Expand Down
61 changes: 44 additions & 17 deletions src/api/sastEvents.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { z } from 'zod';
import { DataFrame, FieldType, TimeRange, createDataFrame } from '@grafana/data';
import { FetchResponse } from '@grafana/runtime';
import { FetchResponse, isFetchError } from '@grafana/runtime';
import { SastEventsQueryOptions } from 'types';
import { SastFinding } from './sastCommon';
import { unwrapRepositoryTemplateVariables } from 'utils/utils';

const MAX_API_REQUESTS = 10;

Expand Down Expand Up @@ -254,6 +255,16 @@ const SastEventsApiResponseSchema = z.object({

type SastEventsEvent = z.infer<typeof SastEventsEventSchema>;

interface SastEventsApiRequest {
githubRepositoryId?: number[];
branch?: string;
eventType?: string[];
fromTime?: string; // ISO string
fromEvent?: string;
numItems?: number; //max 100
sort?: 'asc' | 'desc';
}

export const processSastEvents = async (
queryOptions: SastEventsQueryOptions,
range: TimeRange,
Expand All @@ -262,33 +273,41 @@ export const processSastEvents = async (
let events: SastEventsEvent[] = [];
let prevEventId = null;
for (let i = 0; i < MAX_API_REQUESTS; ++i) {
const params = {
...(queryOptions.queryParameters.githubRepositoryId
? { githubRepositoryId: queryOptions.queryParameters.githubRepositoryId }
const params: SastEventsApiRequest = {
...(queryOptions.queryParameters.githubRepositoryIdsOrQueries
? {
githubRepositoryId: unwrapRepositoryTemplateVariables(
queryOptions.queryParameters.githubRepositoryIdsOrQueries
),
}
: {}),
...(queryOptions.queryParameters.branch ? { severity: queryOptions.queryParameters.branch } : {}),
...(queryOptions.queryParameters.branch ? { branch: queryOptions.queryParameters.branch } : {}),
...(queryOptions.queryParameters.eventTypes && queryOptions.queryParameters.eventTypes.length > 0
? { eventTypes: queryOptions.queryParameters.eventTypes.join(',') }
? { eventType: queryOptions.queryParameters.eventTypes }
: {}),
...(prevEventId ? { fromEvent: prevEventId } : { fromTime: range.from.toISOString() }),
sort: 'asc',
sort: 'desc',
};

const response: any = await request_fn('sast/events', params);
const endpointPath = 'sast/events';
console.log(`[${endpointPath}] starting request with params:`, params);
const response: any = await request_fn(endpointPath, params);

const parseResult = SastEventsApiResponseSchema.safeParse(response.data);
if (!parseResult.success) {
console.error('Error in data from sast events API', parseResult.error);
console.log('sast events request:', params);
console.log('sast events response:', response);
throw new Error(`Data from the API is misformed. See console log for more details.`);
throw {
message: `Data from the API is misformed. Contact Nullify with the data below for help`,
data: {
endpoint: endpointPath,
request_params: params,
response: response,
data_validation_error: parseResult.error,
},
};
}

if (parseResult.data.events) {
events.push(...parseResult.data.events);
}
// console.log('parseResult', parseResult);
// console.log('events', events);

if (!parseResult.data.events || parseResult.data.events.length === 0 || !parseResult.data.nextEventId) {
// No more events
Expand All @@ -304,13 +323,21 @@ export const processSastEvents = async (
return createDataFrame({
refId: queryOptions.refId,
fields: [
{ name: 'id', type: FieldType.string, values: events.map((event) => event.id) },
{
name: 'id',
type: FieldType.string,
values: events.map((event) => event.id),
},
{
name: 'time',
type: FieldType.time,
values: events.map((event) => new Date(event.timestampUnix * 1000)),
},
{ name: 'type', type: FieldType.string, values: events.map((event) => event.type) },
{
name: 'type',
type: FieldType.string,
values: events.map((event) => event.type),
},
{
name: 'numFindings',
type: FieldType.number,
Expand Down
Loading