@optimizely-opal/opal-tool-ocp-sdk 1.0.0-OCP-1441.4 → 1.0.0-OCP-1442.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,175 @@
1
+ import { logger, LogVisibility } from '@zaiusinc/app-sdk';
2
+ import * as App from '@zaiusinc/app-sdk';
3
+
4
+ /**
5
+ * Utility class for logging Opal tool requests and responses with security considerations
6
+ */
7
+ export class ToolLogger {
8
+ private static readonly SENSITIVE_FIELDS = [
9
+ // Authentication / secrets
10
+ 'password',
11
+ 'pass',
12
+ 'secret',
13
+ 'key',
14
+ 'token',
15
+ 'auth',
16
+ 'credentials',
17
+ 'access_token',
18
+ 'refresh_token',
19
+ 'api_key',
20
+ 'private_key',
21
+ 'client_secret',
22
+ 'session_token',
23
+ 'authorization',
24
+
25
+ // Payment-related
26
+ 'card_number',
27
+ 'credit_card',
28
+ 'cvv',
29
+ 'expiry_date',
30
+
31
+ // Personal info
32
+ 'ssn', // social security number
33
+ 'nid', // national ID
34
+ 'passport',
35
+ 'dob', // date of birth
36
+ 'email',
37
+ 'phone',
38
+ 'address',
39
+
40
+ // Misc / environment
41
+ 'otp',
42
+ 'pin',
43
+ 'security_answer',
44
+ 'security_question',
45
+ 'signing_key',
46
+ 'encryption_key',
47
+ 'jwt',
48
+ 'bearer_token'
49
+ ];
50
+
51
+ private static readonly MAX_PARAM_LENGTH = 100;
52
+ private static readonly MAX_ARRAY_ITEMS = 10;
53
+
54
+ /**
55
+ * Redacts sensitive data from an object
56
+ */
57
+ private static redactSensitiveData(data: any, maxDepth = 5): any {
58
+ if (maxDepth <= 0 || data === null || data === undefined) {
59
+ return data;
60
+ }
61
+
62
+ if (typeof data === 'string') {
63
+ return data.length > this.MAX_PARAM_LENGTH
64
+ ? `${data.substring(0, this.MAX_PARAM_LENGTH)}... (truncated, ${data.length} chars total)`
65
+ : data;
66
+ }
67
+
68
+ if (typeof data === 'number' || typeof data === 'boolean') {
69
+ return data;
70
+ }
71
+
72
+ if (Array.isArray(data)) {
73
+ const truncated = data.slice(0, this.MAX_ARRAY_ITEMS);
74
+ const result = truncated.map((item) => this.redactSensitiveData(item, maxDepth - 1));
75
+ if (data.length > this.MAX_ARRAY_ITEMS) {
76
+ result.push(`... (${data.length - this.MAX_ARRAY_ITEMS} more items truncated)`);
77
+ }
78
+ return result;
79
+ }
80
+
81
+ if (typeof data === 'object') {
82
+ const result: any = {};
83
+ for (const [key, value] of Object.entries(data)) {
84
+ // Check if this field contains sensitive data
85
+ const isSensitive = this.isSensitiveField(key);
86
+
87
+ if (isSensitive) {
88
+ result[key] = '[REDACTED]';
89
+ } else {
90
+ result[key] = this.redactSensitiveData(value, maxDepth - 1);
91
+ }
92
+ }
93
+ return result;
94
+ }
95
+
96
+ return data;
97
+ }
98
+
99
+ /**
100
+ * Checks if a field name is considered sensitive
101
+ */
102
+ private static isSensitiveField(fieldName: string): boolean {
103
+ const lowerKey = fieldName.toLowerCase();
104
+ return this.SENSITIVE_FIELDS.some((sensitiveField) =>
105
+ lowerKey.includes(sensitiveField)
106
+ );
107
+ }
108
+
109
+ /**
110
+ * Creates a summary of request parameters
111
+ */
112
+ private static createParameterSummary(params: any): any {
113
+ if (!params) {
114
+ return null;
115
+ }
116
+
117
+ return this.redactSensitiveData(params);
118
+ }
119
+
120
+ /**
121
+ * Calculates content length of response data
122
+ */
123
+ private static calculateContentLength(responseData?: any): number | string {
124
+ if (!responseData) {
125
+ return 0;
126
+ }
127
+
128
+ try {
129
+ const serialized = JSON.stringify(responseData);
130
+ return serialized.length;
131
+ } catch {
132
+ return 'unknown';
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Logs an incoming request
138
+ */
139
+ public static logRequest(
140
+ req: App.Request,
141
+ ): void {
142
+ const params = req.bodyJSON && req.bodyJSON.parameters ? req.bodyJSON.parameters : req.bodyJSON;
143
+ const requestLog = {
144
+ event: 'opal_tool_request',
145
+ path: req.path,
146
+ parameters: this.createParameterSummary(params)
147
+ };
148
+
149
+ // Log with Zaius audience so developers only see requests for accounts they have access to
150
+ logger.info(LogVisibility.Zaius, JSON.stringify(requestLog));
151
+ }
152
+
153
+ /**
154
+ * Logs a successful response
155
+ */
156
+ public static logResponse(
157
+ req: App.Request,
158
+ response: App.Response,
159
+ processingTimeMs?: number
160
+ ): void {
161
+
162
+ const responseLog = {
163
+ event: 'opal_tool_response',
164
+ path: req.path,
165
+ duration: processingTimeMs ? `${processingTimeMs}ms` : undefined,
166
+ status: response.status,
167
+ contentType: response.headers?.get('content-type') || 'unknown',
168
+ contentLength: this.calculateContentLength(response.bodyJSON),
169
+ success: response.status >= 200 && response.status < 300
170
+ };
171
+
172
+ // Log with Zaius audience so developers only see requests for accounts they have access to
173
+ logger.info(LogVisibility.Zaius, JSON.stringify(responseLog));
174
+ }
175
+ }