1
- import { requestTeamApi } from '../requestApi.js' ;
1
+ import { apiConnect } from '../modules/tunnel-api-connect/apiconnect.js' ;
2
+ import { logger } from '../logger.js' ;
2
3
import { TeamDeviceCredentials } from '../types.js' ;
4
+ import { GenericLog } from '../types/logs.js' ;
3
5
4
6
export interface StartAuditLogsQueryParams {
5
- teamDeviceCredentials : TeamDeviceCredentials ;
6
-
7
7
/**
8
8
* 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.
9
9
*/
10
- startDateRangeUnix : number ;
10
+ startDateRangeUnixMs : number ;
11
11
/**
12
12
* The end of the date range of to query audit logs by. The format is unix timestamp in seconds. Only the date is used, not the time.
13
13
*/
14
- endDateRangeUnix : number ;
15
- /**
16
- * The user ID of the author of the audit log.
17
- */
18
- authorUserId ?: number ;
19
- /**
20
- * The ID of the user targeted by the audit log action.
21
- */
22
- targetUserId ?: number ;
23
- /**
24
- * The ID of the sharing group targeted by the audit log action.
25
- */
26
- sharingGroupId ?: number ;
27
- /**
28
- * The types of audit logs to filter by.
29
- */
30
- logType ?: string ;
31
- /**
32
- * The categories audit logs to filter by.
33
- */
34
- category ?: string ;
35
- /**
36
- * Additional properties to filter by. Refer to the specific audit log schema for property details.
37
- */
38
- properties ?: {
39
- propName : string ;
40
- value : string ;
41
- } [ ] ;
14
+ endDateRangeUnixMs : number ;
42
15
}
43
16
44
17
export interface StartAuditLogsQueryOutput {
@@ -48,22 +21,13 @@ export interface StartAuditLogsQueryOutput {
48
21
queryExecutionId : string ;
49
22
}
50
23
51
- export const startAuditLogsQuery = ( params : StartAuditLogsQueryParams ) => {
52
- const { teamDeviceCredentials, ...payload } = params ;
53
- return requestTeamApi < StartAuditLogsQueryOutput > ( {
54
- path : 'auditlogs-teamdevice/StartAuditLogsQuery' ,
55
- teamUuid : teamDeviceCredentials . uuid ,
56
- teamDeviceKeys : {
57
- accessKey : teamDeviceCredentials . accessKey ,
58
- secretKey : teamDeviceCredentials . secretKey ,
59
- } ,
60
- payload,
61
- } ) ;
62
- } ;
24
+ export interface StartAuditLogsQueryRequest {
25
+ path : 'logs-teamdevice/StartAuditLogsQuery' ;
26
+ input : StartAuditLogsQueryParams ;
27
+ output : StartAuditLogsQueryOutput ;
28
+ }
63
29
64
30
export interface GetAuditLogQueryResultsParams {
65
- teamDeviceCredentials : TeamDeviceCredentials ;
66
-
67
31
/**
68
32
* The ID associated with the query executed by the RequestAuditLogs endpoint.
69
33
*/
@@ -93,15 +57,59 @@ export interface GetAuditLogQueryResultsOutput {
93
57
nextToken ?: string ;
94
58
}
95
59
96
- export const getAuditLogQueryResults = ( params : GetAuditLogQueryResultsParams ) => {
97
- const { teamDeviceCredentials, ...payload } = params ;
98
- return requestTeamApi < GetAuditLogQueryResultsOutput > ( {
99
- path : 'auditlogs-teamdevice/GetAuditLogQueryResults' ,
100
- teamUuid : teamDeviceCredentials . uuid ,
101
- teamDeviceKeys : {
102
- accessKey : teamDeviceCredentials . accessKey ,
103
- secretKey : teamDeviceCredentials . secretKey ,
60
+ export interface GetAuditLogQueryResultsRequest {
61
+ path : 'logs-teamdevice/GetAuditLogQueryResults' ;
62
+ input : GetAuditLogQueryResultsParams ;
63
+ output : GetAuditLogQueryResultsOutput ;
64
+ }
65
+
66
+ const MAX_RESULT = 1000 ;
67
+
68
+ export const getAuditLogs = async ( params : {
69
+ queryParams : StartAuditLogsQueryParams ;
70
+ teamDeviceCredentials : TeamDeviceCredentials ;
71
+ } ) : Promise < GenericLog [ ] > => {
72
+ const { teamDeviceCredentials, queryParams } = params ;
73
+
74
+ const api = await apiConnect ( {
75
+ useProductionCertificate : true ,
76
+ } ) ;
77
+
78
+ const { queryExecutionId } = await api . sendSecureContent < StartAuditLogsQueryRequest > ( {
79
+ ...api ,
80
+ path : 'logs-teamdevice/StartAuditLogsQuery' ,
81
+ payload : queryParams ,
82
+ authentication : {
83
+ type : 'teamDevice' ,
84
+ teamDeviceKeys : teamDeviceCredentials ,
85
+ teamUuid : teamDeviceCredentials . uuid ,
104
86
} ,
105
- payload,
106
87
} ) ;
88
+
89
+ let result : GetAuditLogQueryResultsOutput | undefined ;
90
+ let logs : string [ ] = [ ] ;
91
+
92
+ do {
93
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
94
+ result = await api . sendSecureContent < GetAuditLogQueryResultsRequest > ( {
95
+ ...api ,
96
+ path : 'logs-teamdevice/GetAuditLogQueryResults' ,
97
+ payload : { queryExecutionId, maxResults : MAX_RESULT , nextToken : result ?. nextToken } ,
98
+ authentication : {
99
+ type : 'teamDevice' ,
100
+ teamDeviceKeys : teamDeviceCredentials ,
101
+ teamUuid : teamDeviceCredentials . uuid ,
102
+ } ,
103
+ } ) ;
104
+ logger . debug ( `Query state: ${ result . state } ` ) ;
105
+ if ( result . state === 'SUCCEEDED' ) {
106
+ logs = logs . concat ( result . results ) ;
107
+ } else if ( [ 'QUEUED' , 'RUNNING' ] . includes ( result . state ) ) {
108
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
109
+ } else {
110
+ throw new Error ( `Query execution did not succeed: ${ result . state } ` ) ;
111
+ }
112
+ } while ( result . state !== 'SUCCEEDED' || result . nextToken ) ;
113
+
114
+ return logs . map ( ( log ) => JSON . parse ( log ) as GenericLog ) ;
107
115
} ;
0 commit comments