Skip to content

Commit

Permalink
Merge in backend changes from osints/dev
Browse files Browse the repository at this point in the history
Signed-off-by: Simeon Widdis <sawiddis@amazon.com>
  • Loading branch information
Swiddis committed Jul 6, 2023
1 parent 11893b7 commit 53ca987
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
{
"annotation": "ELB Dashboard",
"path": "dashboard1.png"
},
{
"annotation": "ELB Logo",
"path": "logo.png"
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions server/adaptors/integrations/integrations_adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ export interface IntegrationsAdaptor {
) => Promise<{ mappings: { [key: string]: unknown }; schemas: { [key: string]: unknown } }>;

getAssets: (templateName: string) => Promise<{ savedObjects?: unknown }>;

getSampleData: (templateName: string) => Promise<{ sampleData: object[] | null }>;
}
11 changes: 11 additions & 0 deletions server/adaptors/integrations/integrations_kibana_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,15 @@ export class IntegrationsKibanaBackend implements IntegrationsAdaptor {
}
return Promise.resolve(integration.getAssets());
};

getSampleData = async (templateName: string): Promise<{ sampleData: object[] | null }> => {
const integration = await this.repository.getIntegration(templateName);
if (integration === null) {
return Promise.reject({
message: `Template ${templateName} not found`,
statusCode: 404,
});
}
return Promise.resolve(integration.getSampleData());
};
}
44 changes: 44 additions & 0 deletions server/adaptors/integrations/repository/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,50 @@ export class Integration {
return result;
}

/**
* Retrieve sample data associated with the integration.
* If the version is invalid, an error is thrown.
* If the sample data is invalid, null will be returned
*
* @param version The version of the integration to retrieve assets for.
* @returns An object containing a list of sample data with adjusted timestamps.
*/
async getSampleData(
version?: string
): Promise<{
sampleData: object[] | null;
}> {
const config = await this.getConfig(version);
if (config === null) {
return Promise.reject(new Error('Attempted to get assets of invalid config'));
}
const result: { sampleData: object[] | null } = { sampleData: null };
if (config.sampleData) {
const sobjPath = path.join(this.directory, 'data', config.sampleData?.path);
try {
const jsonContent = await fs.readFile(sobjPath, { encoding: 'utf-8' });
const parsed = JSON.parse(jsonContent) as object[];
for (const value of parsed) {
if (!('@timestamp' in value)) {
continue;
}
// Randomly scatter timestamps across last 10 minutes
// Assume for now that the ordering of events isn't important, can change to a sequence if needed
// Also doesn't handle fields like `observedTimestamp` if present
Object.assign(value, {
'@timestamp': new Date(
Date.now() - Math.floor(Math.random() * 1000 * 60 * 10)
).toISOString(),
});
}
result.sampleData = parsed;
} catch (err: any) {
console.error("Failed to load saved object assets, proceeding as if it's absent", err);
}
}
return result;
}

/**
* Retrieve schema data associated with the integration.
* This method greedily retrieves all mappings and schemas.
Expand Down
17 changes: 17 additions & 0 deletions server/routes/integrations/integrations_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ export function registerIntegrationsRoute(router: IRouter) {
}
);

router.get(
{
path: `${INTEGRATIONS_BASE}/repository/{id}/data`,
validate: {
params: schema.object({
id: schema.string(),
}),
},
},
async (context, request, response): Promise<any> => {
const adaptor = getAdaptor(context, request);
return handleWithCallback(adaptor, response, async (a: IntegrationsAdaptor) =>
a.getSampleData(request.params.id)
);
}
);

router.get(
{
path: `${INTEGRATIONS_BASE}/store`,
Expand Down

0 comments on commit 53ca987

Please sign in to comment.