Skip to content

Commit

Permalink
Refactor TrustedApp type + code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Aug 26, 2020
1 parent 6a7fbd6 commit b97524b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,47 @@

import { TypeOf } from '@kbn/config-schema';
import { GetTrustedAppsRequestSchema } from '../schema/trusted_apps';
import { ExceptionListItemSchema } from '../../../../lists/common';
import { FoundExceptionListItemSchema } from '../../../../lists/common/schemas/response';

/** API request params for retrieving a list of Trusted Apps */
export type GetTrustedAppsListRequest = TypeOf<typeof GetTrustedAppsRequestSchema.query>;
export type GetTrustedListAppsResponse = Pick<
FoundExceptionListItemSchema,
'per_page' | 'page' | 'total'
> & {
export interface GetTrustedListAppsResponse {
per_page: number;
page: number;
total: number;
data: TrustedApp[];
};
}

interface MacosLinuxConditionEntry {
field: 'hash' | 'path';
type: 'match';
operator: 'included';
value: string;
}

type WindowsConditionEntry =
| MacosLinuxConditionEntry
| (Omit<MacosLinuxConditionEntry, 'field'> & {
field: 'signer';
});

/** Type for a new Trusted App Entry */
export type NewTrustedApp = Pick<ExceptionListItemSchema, 'name' | 'description' | 'entries'> & {
os: string;
};
export type NewTrustedApp = {
name: string;
description?: string;
} & (
| {
os: 'linux' | 'macos';
entries: MacosLinuxConditionEntry[];
}
| {
os: 'windows';
entries: WindowsConditionEntry[];
}
);

/** A trusted app entry */
export type TrustedApp = NewTrustedApp &
Pick<ExceptionListItemSchema, 'created_at' | 'created_by'> & {
id: string;
};
export type TrustedApp = NewTrustedApp & {
id: string;
created_at: string;
created_by: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const getTrustedAppsListRouteHandler = (
data: results?.data.map(exceptionItemToTrustedAppItem) ?? [],
total: results?.total ?? 0,
page: results?.page ?? 1,
per_page: perPage!,
per_page: results?.per_page ?? perPage!,
};
return res.ok({ body });
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ export const exceptionItemToTrustedAppItem = (
name,
os,
id,
};
} as TrustedApp;
};

/**
* Retrieves the OS entry from a list of tags (property returned with ExcptionListItem).
* For Trusted Apps each entry must have at MOST 1 OS.
* */
const osFromTagsList = (tags: string[]): string => {
const osFromTagsList = (tags: string[]): TrustedApp['os'] | 'unknown' => {
for (const tag of tags) {
if (tag.startsWith('os:')) {
return tag;
return tag.substr(3) as TrustedApp['os'];
}
}
return '';
return 'unknown';
};

0 comments on commit b97524b

Please sign in to comment.