1 | |
2 |
|
3 | import * as wmill from "windmill-client" |
4 |
|
5 | |
6 | * New System Log Event |
7 | * Emits Okta System Log events published since the last poll, tracked via Windmill state. Pass an optional `filter` expression (e.g. eventType eq "user.session.start") to scope the events watched. The first run sets the watermark to now and emits nothing. |
8 | */ |
9 | export async function main(auth: RT.Okta, filter: string | undefined) { |
10 | const lastChecked: string | undefined = await wmill.getState() |
11 |
|
12 | |
13 | if (!lastChecked) { |
14 | await wmill.setState(new Date().toISOString()) |
15 | return [] |
16 | } |
17 |
|
18 | const url = new URL(`${auth.org_url}/api/v1/logs`) |
19 | url.searchParams.append("since", lastChecked) |
20 | url.searchParams.append("sortOrder", "ASCENDING") |
21 | url.searchParams.append("limit", "1000") |
22 | if (filter !== undefined && filter !== "") |
23 | url.searchParams.append("filter", filter) |
24 |
|
25 | const response = await fetch(url, { |
26 | method: "GET", |
27 | headers: { |
28 | Authorization: `SSWS ${auth.token}`, |
29 | Accept: "application/json", |
30 | }, |
31 | }) |
32 |
|
33 | if (!response.ok) { |
34 | throw new Error(`${response.status} ${await response.text()}`) |
35 | } |
36 |
|
37 | const events: Array<{ uuid: string; published: string }> = |
38 | await response.json() |
39 |
|
40 | |
41 | const fresh = events.filter((e) => e.published > lastChecked) |
42 |
|
43 | if (fresh.length > 0) { |
44 | await wmill.setState(fresh[fresh.length - 1].published) |
45 | } |
46 |
|
47 | return fresh |
48 | } |
49 |
|