Skip to content

Commit

Permalink
Migrate audit logs to team device authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikescops committed Jul 19, 2023
1 parent ceef1a1 commit 54f6c0b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
36 changes: 18 additions & 18 deletions src/endpoints/getAuditLogs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { requestUserApi } from '../requestApi';
import { Secrets } from '../types';
import { requestTeamApi } from '../requestApi';
import { TeamDeviceCredentials } from '../types';

export interface StartAuditLogsQueryParams {
secrets: Secrets;
teamDeviceCredentials: TeamDeviceCredentials;

/**
* The start of the date range to query audit logs by. The format is unix timestamp in seconds. Only the date is used, not the time.
Expand Down Expand Up @@ -49,20 +49,20 @@ export interface StartAuditLogsQueryOutput {
}

export const startAuditLogsQuery = (params: StartAuditLogsQueryParams) => {
const { secrets, ...payload } = params;
return requestUserApi<StartAuditLogsQueryOutput>({
path: 'teams/StartAuditLogsQuery',
login: secrets.login,
deviceKeys: {
accessKey: secrets.accessKey,
secretKey: secrets.secretKey,
const { teamDeviceCredentials, ...payload } = params;
return requestTeamApi<StartAuditLogsQueryOutput>({
path: 'auditlogs-teamdevice/StartAuditLogsQuery',
teamUuid: teamDeviceCredentials.uuid,
teamDeviceKeys: {
accessKey: teamDeviceCredentials.accessKey,
secretKey: teamDeviceCredentials.secretKey,
},
payload,
});
};

export interface GetAuditLogQueryResultsParams {
secrets: Secrets;
teamDeviceCredentials: TeamDeviceCredentials;

/**
* The ID associated with the query executed by the RequestAuditLogs endpoint.
Expand Down Expand Up @@ -94,13 +94,13 @@ export interface GetAuditLogQueryResultsOutput {
}

export const getAuditLogQueryResults = (params: GetAuditLogQueryResultsParams) => {
const { secrets, ...payload } = params;
return requestUserApi<GetAuditLogQueryResultsOutput>({
path: 'teams/GetAuditLogQueryResults',
login: secrets.login,
deviceKeys: {
accessKey: secrets.accessKey,
secretKey: secrets.secretKey,
const { teamDeviceCredentials, ...payload } = params;
return requestTeamApi<GetAuditLogQueryResultsOutput>({
path: 'auditlogs-teamdevice/GetAuditLogQueryResults',
teamUuid: teamDeviceCredentials.uuid,
teamDeviceKeys: {
accessKey: teamDeviceCredentials.accessKey,
secretKey: teamDeviceCredentials.secretKey,
},
payload,
});
Expand Down
34 changes: 19 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,6 @@ Use generate-credentials to generate some team credentials (requires to be a tea
);
}

teamGroup
.command('members')
.alias('m')
.description('List team members')
.argument('[page]', 'Page number', '0')
.argument('[limit]', 'Limit of members per page', '0')
.action(async (page: string, limit: string) => {
if (!teamDeviceCredentials) {
throw new Error('Could not find team credentials');
}
await getTeamMembers({ teamDeviceCredentials, page: parseInt(page), limit: parseInt(limit) });
});

teamGroup
.command('generate-credentials')
.option('--json', 'Output in JSON format')
Expand Down Expand Up @@ -187,6 +174,19 @@ teamGroup
console.log('The credentials have been revoked');
});

teamGroup
.command('members')
.alias('m')
.description('List team members')
.argument('[page]', 'Page number', '0')
.argument('[limit]', 'Limit of members per page', '0')
.action(async (page: string, limit: string) => {
if (!teamDeviceCredentials) {
throw new Error('Could not find team credentials');
}
await getTeamMembers({ teamDeviceCredentials, page: parseInt(page), limit: parseInt(limit) });
});

teamGroup
.command('logs')
.alias('l')
Expand All @@ -196,12 +196,16 @@ teamGroup
.option('--type <type>', 'log type')
.option('--category <category>', 'log category')
.action(async (options: { start: string; end: string; type: string; category: string }) => {
if (!teamDeviceCredentials) {
throw new Error('Could not find team credentials');
}

const { start, type, category } = options;
const end = options.end === 'now' ? Math.floor(Date.now() / 1000).toString() : options.end;

const { db, secrets } = await connectAndPrepare({ autoSync: false });
const { db } = await connectAndPrepare({ autoSync: false });
await getAuditLogs({
secrets,
teamDeviceCredentials,
startDateRangeUnix: parseInt(start),
endDateRangeUnix: parseInt(end),
logType: type,
Expand Down
8 changes: 4 additions & 4 deletions src/middleware/getAuditLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { getAuditLogQueryResults, startAuditLogsQuery, StartAuditLogsQueryParams
const MAX_RESULT = 1000;

export const getAuditLogs = async (params: StartAuditLogsQueryParams) => {
const { secrets } = params;
const { teamDeviceCredentials } = params;

const { queryExecutionId } = await startAuditLogsQuery(params);

let result = await getAuditLogQueryResults({ secrets, queryExecutionId, maxResults: MAX_RESULT });
let result = await getAuditLogQueryResults({ teamDeviceCredentials, queryExecutionId, maxResults: MAX_RESULT });
winston.debug(`Query state: ${result.state}`);

while (['QUEUED', 'RUNNING'].includes(result.state)) {
await new Promise((resolve) => setTimeout(resolve, 2000));
result = await getAuditLogQueryResults({ secrets, queryExecutionId, maxResults: MAX_RESULT });
result = await getAuditLogQueryResults({ teamDeviceCredentials, queryExecutionId, maxResults: MAX_RESULT });
winston.debug(`Query state: ${result.state}`);
}

Expand All @@ -24,7 +24,7 @@ export const getAuditLogs = async (params: StartAuditLogsQueryParams) => {
let logs = result.results;
while (result.nextToken) {
result = await getAuditLogQueryResults({
secrets,
teamDeviceCredentials,
queryExecutionId,
maxResults: MAX_RESULT,
nextToken: result.nextToken,
Expand Down

0 comments on commit 54f6c0b

Please sign in to comment.