-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathLoggingPage.tsx
More file actions
301 lines (270 loc) · 10.3 KB
/
LoggingPage.tsx
File metadata and controls
301 lines (270 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import React, { useEffect, useState, useMemo, useCallback } from 'react'
import { Form, FormGroup, Card, CardBody, Col, CustomInput } from 'Components'
import GluuLabel from 'Routes/Apps/Gluu/GluuLabel'
import GluuLoader from 'Routes/Apps/Gluu/GluuLoader'
import GluuViewWrapper from 'Routes/Apps/Gluu/GluuViewWrapper'
import GluuCommitDialog from 'Routes/Apps/Gluu/GluuCommitDialog'
import GluuFormFooter from 'Routes/Apps/Gluu/GluuFormFooter'
import { JSON_CONFIG } from 'Utils/ApiResources'
import { loggingValidationSchema } from './validations'
import {
LOG_LEVELS,
LOG_LAYOUTS,
getLoggingInitialValues,
getMergedValues,
getChangedFields,
} from './utils'
import type { LoggingFormValues } from './utils'
import applicationStyle from 'Routes/Apps/Gluu/styles/applicationstyle'
import { Formik } from 'formik'
import { useNavigate } from 'react-router-dom'
import { useGetConfigLogging, usePutConfigLogging, type Logging } from 'JansConfigApi'
import { LOGGING_READ, LOGGING_WRITE } from 'Utils/PermChecker'
import { useCedarling } from '@/cedarling'
import { useTranslation } from 'react-i18next'
import SetTitle from 'Utils/SetTitle'
import GluuToogleRow from 'Routes/Apps/Gluu/GluuToogleRow'
import { useLoggingActions, type ModifiedFields } from './hooks/useLoggingActions'
import { toast } from 'react-toastify'
interface PendingValues {
mergedValues: Logging
changedFields: ModifiedFields
}
function LoggingPage(): React.ReactElement {
const { t } = useTranslation()
const navigate = useNavigate()
const { hasCedarPermission, authorize } = useCedarling()
const { logLoggingUpdate } = useLoggingActions()
const [showCommitDialog, setShowCommitDialog] = useState(false)
const [pendingValues, setPendingValues] = useState<PendingValues | null>(null)
const [localLogging, setLocalLogging] = useState<Logging | null>(null)
const [permissionsInitialized, setPermissionsInitialized] = useState(false)
const [permissionError, setPermissionError] = useState(false)
const { data: logging, isLoading: isLoadingData } = useGetConfigLogging({
query: {
enabled: permissionsInitialized && hasCedarPermission(LOGGING_READ),
},
})
const updateLogging = usePutConfigLogging()
useEffect(() => {
let isMounted = true
const initPermissions = async (): Promise<void> => {
try {
await Promise.all([authorize([LOGGING_READ]), authorize([LOGGING_WRITE])])
if (isMounted) {
setPermissionsInitialized(true)
setPermissionError(false)
}
} catch (error) {
if (isMounted) {
console.error('Failed to authorize permissions:', error)
setPermissionError(true)
setPermissionsInitialized(true)
}
}
}
initPermissions()
return () => {
isMounted = false
}
}, [authorize])
useEffect(() => {
if (logging) {
setLocalLogging(logging)
}
}, [logging])
const initialValues: LoggingFormValues = useMemo(
() => getLoggingInitialValues(localLogging),
[localLogging],
)
const levels = LOG_LEVELS
const logLayouts = LOG_LAYOUTS
SetTitle('Logging')
const handleSubmit = useCallback(
(values: LoggingFormValues): void => {
if (!localLogging) {
console.error('Cannot submit: logging data not loaded')
return
}
const mergedValues = getMergedValues(localLogging, values)
const changedFields = getChangedFields(localLogging, mergedValues)
if (Object.keys(changedFields).length === 0) {
return
}
setPendingValues({ mergedValues, changedFields })
setShowCommitDialog(true)
},
[localLogging],
)
const handleAccept = useCallback(
async (userMessage: string): Promise<void> => {
if (!pendingValues) return
const { mergedValues, changedFields } = pendingValues
try {
const result = await updateLogging.mutateAsync({ data: mergedValues })
setLocalLogging(result)
if (Object.keys(changedFields).length > 0) {
logLoggingUpdate(userMessage, changedFields).catch((error) =>
console.error('Audit logging failed:', error),
)
}
toast.success(t('messages.success_in_saving'))
setShowCommitDialog(false)
setPendingValues(null)
} catch (error) {
console.error('Failed to update logging configuration:', error)
toast.error(t('messages.error_in_saving'))
}
},
[pendingValues, updateLogging, logLoggingUpdate, t],
)
const isLoading = !permissionsInitialized || isLoadingData || updateLogging.isPending
if (permissionError) {
return (
<GluuLoader blocking={false}>
<Card style={applicationStyle.mainCard}>
<CardBody style={{ minHeight: 500 }}>
<div className="alert alert-danger" role="alert">
{t('messages.permission_error')}
</div>
</CardBody>
</Card>
</GluuLoader>
)
}
return (
<GluuLoader blocking={isLoading}>
<Card style={applicationStyle.mainCard}>
<CardBody style={{ minHeight: 500 }}>
<GluuViewWrapper canShow={hasCedarPermission(LOGGING_READ)}>
<Formik
initialValues={initialValues}
validationSchema={loggingValidationSchema}
enableReinitialize
onSubmit={handleSubmit}
>
{(formik) => (
<Form onSubmit={formik.handleSubmit}>
<FormGroup row>
<GluuLabel
label="fields.log_level"
size={4}
doc_category={JSON_CONFIG}
doc_entry="loggingLevel"
/>
<Col sm={8}>
<CustomInput
type="select"
id="loggingLevel"
name="loggingLevel"
data-testid="loggingLevel"
value={formik.values.loggingLevel}
onChange={(e: React.ChangeEvent<HTMLSelectElement>) =>
formik.setFieldValue('loggingLevel', e.target.value)
}
>
<option value="">{t('actions.choose')}...</option>
{levels.map((item, key) => (
<option value={item} key={key}>
{item}
</option>
))}
</CustomInput>
</Col>
</FormGroup>
<FormGroup row>
<GluuLabel
label="fields.log_layout"
size={4}
doc_category={JSON_CONFIG}
doc_entry="loggingLayout"
/>
<Col sm={8}>
<CustomInput
type="select"
id="loggingLayout"
name="loggingLayout"
data-testid="loggingLayout"
value={formik.values.loggingLayout}
onChange={(e: React.ChangeEvent<HTMLSelectElement>) =>
formik.setFieldValue('loggingLayout', e.target.value)
}
>
<option value="">{t('actions.choose')}...</option>
{logLayouts.map((item, key) => (
<option value={item} key={key}>
{item}
</option>
))}
</CustomInput>
</Col>
</FormGroup>
<GluuToogleRow
label="fields.http_logging_enabled"
name="httpLoggingEnabled"
handler={(e: React.ChangeEvent<HTMLInputElement>) =>
formik.setFieldValue('httpLoggingEnabled', e.target.checked)
}
lsize={5}
rsize={7}
value={formik.values.httpLoggingEnabled}
doc_category={JSON_CONFIG}
/>
<GluuToogleRow
label="fields.disable_jdk_logger"
name="disableJdkLogger"
handler={(e: React.ChangeEvent<HTMLInputElement>) =>
formik.setFieldValue('disableJdkLogger', e.target.checked)
}
lsize={5}
rsize={7}
doc_category={JSON_CONFIG}
value={formik.values.disableJdkLogger}
/>
<GluuToogleRow
label="fields.enabled_oAuth_audit_logging"
name="enabledOAuthAuditLogging"
handler={(e: React.ChangeEvent<HTMLInputElement>) =>
formik.setFieldValue('enabledOAuthAuditLogging', e.target.checked)
}
lsize={5}
rsize={7}
doc_category={JSON_CONFIG}
value={formik.values.enabledOAuthAuditLogging}
/>
{hasCedarPermission(LOGGING_WRITE) && (
<GluuFormFooter
showBack={true}
onBack={() => {
if (window.history.length > 1) {
navigate(-1)
} else {
navigate('/auth-server/config/logging')
}
}}
showCancel={true}
onCancel={() => formik.resetForm()}
disableCancel={!formik.dirty}
showApply={true}
onApply={formik.handleSubmit}
disableApply={!formik.isValid || !formik.dirty}
applyButtonType="button"
isLoading={isLoading}
/>
)}
</Form>
)}
</Formik>
<GluuCommitDialog
handler={() => setShowCommitDialog(false)}
modal={showCommitDialog}
onAccept={handleAccept}
isLicenseLabel={false}
/>
</GluuViewWrapper>
</CardBody>
</Card>
</GluuLoader>
)
}
export default LoggingPage