-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
284 lines (275 loc) · 8.6 KB
/
setup.js
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
const { Permit } = require("permitio");
const STEP = process.env.SETUP_STEP || "0";
const permit = new Permit({
token: process.env.PERMIT_API_KEY,
pdp: process.env.PDP_URL,
});
const ELEMENTS_CONFIG = {
"wire-transfer-approval-management": {
name: "Wire Transfer Approval Management",
elements_type: "approval_management",
settings: {
name: "Wire Transfer Approval Management",
title: "Approval Management",
isDarkMode: 0,
isShowDate: 1,
isShowReason: 1,
primaryColor: "#067A6F",
secondaryColor: "#E5484D",
backgroundColor: "#FFFFFF",
isShowUserEmail: 1,
darkPrimaryColor: "#0AC5B3",
darkSecondaryColor: "#E5484D",
darkBackgroundColor: "#282826",
},
email_notifications: false,
roles_to_levels: {},
webhook: null,
},
"wire-transfer-approval-request": {
name: "Wire Transfer Approval Request",
elements_type: "operation_approval",
settings: {
name: "Wire Transfer Approval Request",
title: "Approval Required",
resource: "Wire_Transfer",
isDarkMode: 0,
webhookUrl: `https://${process.env.NGROK_DOMAIN}/account/webhook`,
messageText:
"Performing this transfer requires approval from the account owner",
primaryColor: "#067A6F",
webhookSecret: process.env.WEBHOOK_SECRET,
backgroundColor: "#FFFFFF",
actionButtonText: "Request Transfer Approval",
darkPrimaryColor: "#0AC5B3",
emailNotifications: 0,
darkBackgroundColor: "#282826",
webhookNotification: 1,
isRequireJustification: 1,
justificationPlaceholder: "I need this wire for...",
},
email_notifications: false,
roles_to_levels: {},
webhook: {
type: "elements",
url: `https://${process.env.NGROK_DOMAIN}/account/webhook`,
bearer_token: process.env.WEBHOOK_SECRET,
},
},
"wire-transfer-request": {
name: "Wire Transfer Request",
elements_type: "approval_flow",
settings: {
name: "Wire Transfer Request",
title: "Access Restricted to Perform a Wire Transfer",
messageText:
"Hit the request access button and we’ll let the Account Owner know that you want to perform a Wire Transfer.",
primaryColor: "#33a2e9",
backgroundColor: "#FFFFFF",
actionButtonText: "Request Access",
darkPrimaryColor: "#0AC5B3",
darkBackgroundColor: "#282826",
justificationMessage: "",
isAccessJustification: 1,
isRequireJustification: 0,
justificationPlaceholder: "Reason",
selectedUserManagementElement: "account-members-management",
},
email_notifications: false,
roles_to_levels: {},
webhook: null,
},
"account-members-management": {
name: "Account Members Management",
elements_type: "user_management",
settings: {
name: "Account Members Management",
title: "Members",
isDarkMode: 0,
webhookUrl: "",
isShowTitle: 1,
isShowReason: 1,
primaryColor: "#067A6F",
configureType: "RBAC",
webhookSecret: "",
backgroundColor: "#FFFFFF",
isShowUserEmail: 1,
actionButtonText: "Send Invite",
darkPrimaryColor: "#0AC5B3",
multiRoleSupport: "single",
emailNotifications: 0,
isShowUserFullName: 1,
approvalFlowElement: "wire-transfer-request",
darkBackgroundColor: "#282826",
defaultRoleSelected: "",
webhookNotification: 0,
resourceTypeSelected: "",
requestedAccessTimeWindow: 1,
approvalFlowIsShowUserEmail: 1,
approvalFlowIsShowUserFullName: 1,
},
email_notifications: false,
roles_to_levels: {
LEVEL_1: ["AccountOwner"],
LEVEL_2: ["AccountBeneficiary"],
LEVEL_3: ["AccountMember"],
},
webhook: null,
},
};
const USER_ATTRIBUTES_CONFIG = [
{
key: "location",
type: "string",
description: "ISO 3166 country code of the user location",
},
{
key: "country",
type: "string",
description: "ISO 3166 country code of the country the user is from",
},
{
key: "strongAuth",
type: "bool",
description: "Context attribute if a user entered TOTP code",
},
];
const createUserAttribute = async (attribute) => {
const { project, environment } = permit.config.apiContext;
const response = await fetch(
`https://api.permit.io/v2/schema/${project}/${environment}/users/attributes`,
{
method: "POST",
body: JSON.stringify(attribute),
headers: {
Authorization: `Bearer ${process.env.PERMIT_API_KEY}`,
"Content-Type": "application/json",
},
},
);
return response.json();
};
const createElement = async (element) => {
const { project, environment } = permit.config.apiContext;
const response = await fetch(
`https://api.permit.io/v2/elements/${project}/${environment}/config`,
{
method: "POST",
body: JSON.stringify(element),
headers: {
Authorization: `Bearer ${process.env.PERMIT_API_KEY}`,
"Content-Type": "application/json",
},
},
);
return response.json();
};
const updateElement = async (key, element) => {
const { project, environment } = permit.config.apiContext;
const response = await fetch(
`https://api.permit.io/v2/elements/${project}/${environment}/config/${key}`,
{
method: "PATCH",
body: JSON.stringify(element),
headers: {
Authorization: `Bearer ${process.env.PERMIT_API_KEY}`,
"Content-Type": "application/json",
},
},
);
return response.json();
};
const createUserAttributes = async () => {
const { project, environment } = permit.config.apiContext;
const response = await fetch(
`https://api.permit.io/v2/schema/${project}/${environment}/users/attributes`,
{
headers: {
Authorization: `Bearer ${process.env.PERMIT_API_KEY}`,
},
},
);
const schema = await response.json();
console.log(`Found ${schema.length - 3} custom user attributes`);
const missingAttributes = USER_ATTRIBUTES_CONFIG.filter(
(attr) => !schema.find((s) => s.key === attr.key),
);
console.log(`Creating ${missingAttributes.length} missing attributes`);
await Promise.all(missingAttributes.map((attr) => createUserAttribute(attr)));
console.log("User attributes created");
};
const createElementsConfig = async () => {
const { project, environment } = permit.config.apiContext;
const response = await fetch(
`https://api.permit.io/v2/elements/${project}/${environment}/config`,
{
headers: {
Authorization: `Bearer ${process.env.PERMIT_API_KEY}`,
},
},
);
const elements = await response.json();
console.log(`Found ${elements.data.length} elements`);
const missingElements = Object.keys(ELEMENTS_CONFIG).filter(
(element) => !elements.data.find((e) => e.key === element),
);
console.log(`Creating ${missingElements.length} missing elements`);
const elementsResponse = await Promise.all(
missingElements.map((key) =>
createElement({
key,
...ELEMENTS_CONFIG[key],
}),
),
);
const userManagementElement = elementsResponse.find(
(element) => element.key === "account-members-management",
);
const wireTransferRequestElement = elementsResponse.find(
(element) => element.key === "wire-transfer-request",
);
if (userManagementElement && wireTransferRequestElement) {
await updateElement("account-members-management", {
...ELEMENTS_CONFIG["account-members-management"],
settings: {
...ELEMENTS_CONFIG["account-members-management"].settings,
approvalFlowElement: wireTransferRequestElement.id,
},
});
console.log("Updated user management element");
await updateElement("wire-transfer-request", {
...ELEMENTS_CONFIG["wire-transfer-request"],
settings: {
...ELEMENTS_CONFIG["wire-transfer-request"].settings,
selectedUserManagementElement: userManagementElement.id,
},
});
console.log("Updated wire transfer request element");
}
};
(async () => {
// a small hack to set the project and environment
await permit.api.projects.list();
console.log('Permit Environment ID:', permit.config.apiContext.environment);
switch (STEP) {
case "0":
console.log("Creating user attributes");
try {
await createUserAttributes();
} catch (error) {
console.error(`Failed to create user attributes: ${error}`);
}
break;
case "1":
console.log("Creating elements config");
try {
await createElementsConfig();
} catch (error) {
console.error(`Failed to create elements config: ${error}`);
}
break;
default:
console.log("Unknown step");
break;
}
})();