Skip to content

Commit

Permalink
IE-356 + Added shorthand for specifying a 'since' based on duration
Browse files Browse the repository at this point in the history
  • Loading branch information
dspasojevic committed Dec 5, 2023
1 parent 8e1ff75 commit f78c8c2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
"@okta/okta-sdk-nodejs": "^6.5.0",
"@types/node-fetch": "^2.6.2",
"chalk-table": "^1.0.2",
"date-fns": "^2.30.0",
"fp-ts": "^2.16.1",
"generate-password": "^1.7.0",
"table": "^6.8.0",
"tinyduration": "^3.3.0",
"yargs": "^17.5.1",
"zod": "^3.22.4"
},
Expand Down
44 changes: 39 additions & 5 deletions src/scripts/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { retrieveLogs } from './services/okta-service';
import * as okta from '@okta/okta-sdk-nodejs';
import { ReadonlyDate, ReadonlyURL, readonlyDate } from 'readonly-types';
import { table } from 'table';
import * as duration from 'tinyduration';
import { sub } from 'date-fns';

// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
const logsTable = (logs: readonly okta.LogEvent[]): string => {
Expand Down Expand Up @@ -55,6 +57,16 @@ const displayLogs = (
TE.tapIO(Console.info)
);

const coercedDate = (s: string) => {
// eslint-disable-next-line functional/no-try-statement
try {
return readonlyDate(s);
} catch (error: unknown) {
// eslint-disable-next-line functional/no-throw-statement
throw new Error(`Invalid date [${s}].`, { cause: error });
}
};

export default (
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
rootCommand: RootCommand
Expand Down Expand Up @@ -103,14 +115,27 @@ export default (
'The filter by which the logs will be filtered (e.g. eventType eq "user.session.start").',
})
.option('since', {
type: 'string',
description: 'The start date of the logs to retrieve.',
coerce: (date: string) => readonlyDate(date),
coerce: (date: string) => coercedDate(date),
})
.option('until', {
type: 'string',
description: 'The end date of the logs to retrieve.',
coerce: (date: string) => readonlyDate(date),
coerce: (date: string) => coercedDate(date),
})
.option('within', {
description:
'The start date of the logs to retrieve, expressed as a duration to be applied to now (e.g. 1d/P1D for 1 day, 2w/P2W for two weeks).',
conflicts: ['since', 'until'],
coerce: (s: string) => {
const durationWithP = s.startsWith('P') ? s : `P${s}`;
// eslint-disable-next-line functional/no-try-statement
try {
return duration.parse(durationWithP.toUpperCase());
} catch (error: unknown) {
// eslint-disable-next-line functional/no-throw-statement
throw new Error(`Invalid duration [${s}].`, { cause: error });
}
},
});
};

Expand All @@ -129,6 +154,7 @@ export default (
readonly filter?: string;
readonly since?: ReadonlyDate;
readonly until?: ReadonlyDate;
readonly within?: duration.Duration;
}) => {
const { clientId, privateKey } = args;
const client = oktaReadOnlyClient(
Expand All @@ -140,13 +166,21 @@ export default (
['logs']
);

const effectiveSince =
args.since !== undefined
? args.since
: args.within !== undefined
? // eslint-disable-next-line no-restricted-globals
sub(new Date(), args.within)
: undefined;

const result = await displayLogs(
client,
args.outputFormat,
args.limit,
args.query,
args.filter,
args.since,
effectiveSince,
args.until
)();
// eslint-disable-next-line functional/no-conditional-statement
Expand Down

0 comments on commit f78c8c2

Please sign in to comment.