-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathCIPPDeploymentStep.js
More file actions
380 lines (370 loc) · 13.7 KB
/
CIPPDeploymentStep.js
File metadata and controls
380 lines (370 loc) · 13.7 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { useEffect, useState } from "react";
import {
Alert,
Button,
Grid,
Link,
Stack,
Typography,
Skeleton,
Box,
CircularProgress,
SvgIcon,
} from "@mui/material";
import CippFormComponent from "../CippComponents/CippFormComponent";
import { CippWizardStepButtons } from "./CippWizardStepButtons";
import { ApiGetCall } from "../../api/ApiCall";
import CippButtonCard from "../CippCards/CippButtonCard";
import { CippCopyToClipBoard } from "../CippComponents/CippCopyToClipboard";
import { CheckCircle, OpenInNew, Sync } from "@mui/icons-material";
import CippPermissionCheck from "../CippSettings/CippPermissionCheck";
import { useQueryClient } from "@tanstack/react-query";
import { CippApiResults } from "../CippComponents/CippApiResults";
export const CippDeploymentStep = (props) => {
const queryClient = useQueryClient();
const { formControl, onPreviousStep, onNextStep, currentStep } = props;
const values = formControl.getValues();
const [currentStepState, setCurrentStepState] = useState(1);
const [pollingStep, setPollingStep] = useState(1);
const [approvalUrl, setApprovalUrl] = useState(true);
const startSetupApi = ApiGetCall({
url: "/api/ExecSAMSetup?CreateSAM=true&partnersetup=true",
queryKey: "startSAMSetup",
});
const checkSetupStatusApi = ApiGetCall({
url: `/api/ExecSAMSetup?CheckSetupProcess=true&step=${pollingStep}`,
queryKey: `checkSetupStep${pollingStep}`,
waiting: !pollingStep,
});
const appId = ApiGetCall({
url: `/api/ExecListAppId`,
queryKey: `ExecListAppId`,
waiting: true,
});
useEffect(() => {
if (
startSetupApi.data &&
startSetupApi.data.step === 1 &&
values.selectedOption === "CreateApp"
) {
formControl.register("wizardStatus", {
required: true,
});
formControl.setValue("noSubmitButton", true);
setPollingStep(1);
setCurrentStepState(1);
}
}, [startSetupApi.data]);
useEffect(() => {
if (pollingStep && values.selectedOption === "CreateApp") {
const intervalId = setInterval(() => {
if (!checkSetupStatusApi.isFetching) {
checkSetupStatusApi.refetch();
}
}, 5000);
return () => clearInterval(intervalId);
}
}, [pollingStep, checkSetupStatusApi]);
useEffect(() => {
if (checkSetupStatusApi.data) {
const { step, message, url, code } = checkSetupStatusApi.data;
if (url) {
setApprovalUrl(url);
}
if (step === 2) {
setCurrentStepState(2);
setPollingStep(2);
} else if (step >= 3) {
setCurrentStepState(4);
setPollingStep(null);
formControl.setValue(
"wizardStatus",
"You've executed the Setup Wizard. You may now navigate away from this wizard."
);
formControl.trigger();
}
}
}, [checkSetupStatusApi.data, currentStepState]);
const openPopup = (url) => {
const width = 500;
const height = 500;
const left = window.screen.width / 2 - width / 2;
const top = window.screen.height / 2 - height / 2;
window.open(url, "_blank", `width=${width},height=${height},left=${left},top=${top}`);
};
return (
<Stack spacing={3}>
<Stack spacing={2}>
{values.selectedOption === "CreateApp" && (
<>
<Alert severity="info">
To run this setup you will need the following prerequisites:
<li>
A CIPP Service Account. For more information on how to create a service account,
click{" "}
<Link
href="https://docs.cipp.app/setup/installation/samwizard"
rel="noreferrer"
target="_blank"
>
here
</Link>
</li>
<li>(Temporary) Global Administrator permissions for the CIPP Service Account</li>
<li>
Multi-factor authentication enabled for the CIPP Service Account, with no trusted
locations or other exclusions.
</li>
</Alert>
{currentStepState >= 1 && (
<CippButtonCard
title={
<Stack direction="row" justifyContent={"space-between"}>
<Box>Step 1: Create Application</Box>
<Stack direction="row" spacing={2}>
{currentStepState <= 1 ? (
<CircularProgress size="1rem" />
) : (
<SvgIcon>
<CheckCircle />
</SvgIcon>
)}
</Stack>
</Stack>
}
variant="outlined"
isFetching={startSetupApi.isLoading}
CardButton={
<Button
disabled={currentStepState >= 2}
variant="contained"
color="primary"
onClick={() => openPopup("https://microsoft.com/devicelogin")}
>
Login to Microsoft
</Button>
}
>
<Typography variant="body2" gutterBottom>
Click the button below and enter the provided code. This creates the CIPP
Application Registration in your tenant that allows you to access the Graph API.
Login using your CIPP Service Account.
</Typography>
{startSetupApi.isLoading ? (
<Skeleton variant="rectangular" height={60} />
) : (
<CippCopyToClipBoard text={startSetupApi.data?.code} type="chip" />
)}
</CippButtonCard>
)}
{currentStepState >= 2 && (
<CippButtonCard
variant="outlined"
isFetching={checkSetupStatusApi.isLoading && pollingStep === 2}
title={
<Stack direction="row" justifyContent={"space-between"}>
<Box>Step 2: Approve Permissions</Box>
<Stack direction="row" spacing={2}>
{currentStepState <= 2 ? (
<CircularProgress size="1rem" />
) : (
<SvgIcon>
<CheckCircle />
</SvgIcon>
)}
</Stack>
</Stack>
}
CardButton={
<Button
variant="contained"
color="primary"
disabled={
currentStepState >= 4 ||
!approvalUrl ||
!approvalUrl.startsWith("https://login") ||
typeof approvalUrl !== "string"
}
onClick={() => openPopup(approvalUrl)}
>
Open Approval Link
</Button>
}
>
<Typography variant="h6" gutterBottom>
Step 2: Approvals Required
</Typography>
<Typography variant="body2" gutterBottom>
Please open the link below and provide the required approval, this allows the app
specific permissions shown in the next screen. Login using your CIPP Service
Account.
</Typography>
</CippButtonCard>
)}
{/* Final Step 4 Card */}
{currentStepState >= 4 && <CippPermissionCheck variant="outlined" type="Permissions" />}
<Grid item xs={12}>
<Button
variant="contained"
color="primary"
onClick={() => {
setPollingStep(1);
setCurrentStepState(1);
setApprovalUrl(null);
queryClient.removeQueries("startSAMSetup");
queryClient.removeQueries("checkSetupStep1");
setTimeout(() => {
startSetupApi.refetch();
}, 200);
}}
>
Start Over
</Button>
<Grid item xs={12}>
<CippApiResults apiObject={startSetupApi} errorsOnly={true} />
<CippApiResults apiObject={checkSetupStatusApi} errorsOnly={true} />
</Grid>
</Grid>
</>
)}
{values.selectedOption === "UpdateTokens" && (
<CippButtonCard
variant="outlined"
title={
<Stack direction="row" justifyContent={"space-between"}>
<Box>Update Tokens</Box>
<Stack direction="row" spacing={2}>
{appId.isLoading ? (
<CircularProgress size="1rem" />
) : (
<SvgIcon>
<CheckCircle />
</SvgIcon>
)}
</Stack>
</Stack>
}
CardButton={
<>
<Button
variant="contained"
disabled={
appId.isLoading ||
!/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(
appId?.data?.applicationId
)
}
onClick={() => openPopup(appId?.data?.refreshUrl)}
color="primary"
startIcon={
<OpenInNew />
}
>
Refresh Graph Token
</Button>
<Button
onClick={() => appId.refetch()}
variant="outlined"
color="primary"
startIcon={<Sync />}
disabled={appId.isFetching}
>
Check Application ID
</Button>
{!/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(
appId?.data?.applicationId
) && (
<Alert severity="warning">
The Application ID is not valid. Please return to the first page of the SAM
wizard and use the Manual .
</Alert>
)}
</>
}
>
<Typography variant="body2" gutterBottom>
Click the button below to refresh your token.
</Typography>
{formControl.setValue("noSubmitButton", true)}
<CippApiResults apiObject={appId} errorsOnly />
</CippButtonCard>
)}
{values.selectedOption === "Manual" && (
<>
{formControl.setValue("setKeys", true)}
<Typography variant="body1">
You may enter your secrets below. Leave fields blank to retain existing values.
</Typography>
<CippFormComponent
formControl={formControl}
type="textField"
name="TenantID"
label="Tenant ID"
placeholder="Enter the Tenant ID. Leave blank to retain previous key."
validators={{
validate: (value) => {
const guidRegex =
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
return value === "" || guidRegex.test(value) || "Invalid Tenant ID";
},
}}
/>
<CippFormComponent
formControl={formControl}
type="textField"
name="ApplicationID"
label="Application ID"
placeholder="Enter the Application ID. Leave blank to retain previous key."
validators={{
validate: (value) => {
const guidRegex =
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
return value === "" || guidRegex.test(value) || "Invalid Application ID";
},
}}
/>
<CippFormComponent
formControl={formControl}
type="password"
name="ApplicationSecret"
label="Application Secret"
placeholder="Enter the application secret. Leave blank to retain previous key."
validators={{
validate: (value) => {
const secretRegex =
/^(?![0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)[0-9a-zA-Z]{40}$/;
return (
value === "" ||
secretRegex.test(value) ||
"This should be the secret value, not the secret ID"
);
},
}}
/>
<CippFormComponent
formControl={formControl}
type="password"
name="RefreshToken"
label="Refresh Token"
placeholder="Enter the refresh token. Leave blank to retain previous key."
validators={{
validate: (value) => {
const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/;
return value === "" || jwtRegex.test(value) || "Invalid Refresh Token";
},
}}
/>
</>
)}
</Stack>
<CippWizardStepButtons
currentStep={currentStep}
onPreviousStep={onPreviousStep}
onNextStep={onNextStep}
noNextButton={values.selectedOption === "UpdateTokens"}
formControl={formControl}
noSubmitButton={true}
/>
</Stack>
);
};