-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsso.utils.js
More file actions
402 lines (355 loc) · 13.4 KB
/
Copy pathsso.utils.js
File metadata and controls
402 lines (355 loc) · 13.4 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const contentstack = require("@contentstack/marketplace-sdk");
const readline = require("readline");
const { execSync } = require("child_process");
const fs = require("fs");
const crypto = require("crypto");
const rawManifest = require("./manifest.json");
const { default: axios } = require("axios");
const dotenv = require("dotenv");
dotenv.config();
const ENCRYPT_KEY = process.env?.MANIFEST_ENCRYPT_KEY;
const ENCRYPT_SALT = process.env?.MANIFEST_ENCRYPT_SALT;
const ALGORITHM = "aes-256-gcm";
const ENC_PREFIX = "enc:";
function encrypt(plaintext) {
if (!plaintext || plaintext?.startsWith(ENC_PREFIX)) return plaintext;
if (!ENCRYPT_KEY) throw new Error("MANIFEST_ENCRYPT_KEY env variable is required to encrypt credentials");
const key = crypto?.scryptSync(ENCRYPT_KEY, ENCRYPT_SALT, 32);
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
let encrypted = cipher?.update(plaintext, "utf8", "hex");
encrypted += cipher?.final("hex");
const authTag = cipher?.getAuthTag()?.toString("hex");
return `${ENC_PREFIX}${iv?.toString("hex")}:${authTag}:${encrypted}`;
}
function decrypt(encryptedValue) {
if (!encryptedValue || !encryptedValue?.startsWith(ENC_PREFIX)) return encryptedValue;
if (!ENCRYPT_KEY) throw new Error("MANIFEST_ENCRYPT_KEY env variable is required to decrypt manifest credentials");
const parts = encryptedValue?.slice(ENC_PREFIX?.length)?.split(":");
if (parts.length !== 3) throw new Error("Invalid encrypted value format");
const [ivHex, authTagHex, cipherHex] = parts;
const key = crypto?.scryptSync(ENCRYPT_KEY, ENCRYPT_SALT, 32);
const decipher = crypto?.createDecipheriv(ALGORITHM, key, Buffer?.from(ivHex, "hex"));
decipher?.setAuthTag(Buffer?.from(authTagHex, "hex"));
let decrypted = decipher?.update(cipherHex, "hex", "utf8");
decrypted += decipher?.final("utf8");
return decrypted;
}
function decryptManifest(m) {
const decrypted = JSON.parse(JSON.stringify(m));
if (decrypted.uid?.startsWith(ENC_PREFIX)) decrypted.uid = decrypt(decrypted.uid);
if (decrypted.oauth?.client_id?.startsWith(ENC_PREFIX)) decrypted.oauth.client_id = decrypt(decrypted.oauth.client_id);
if (decrypted.oauth?.client_secret?.startsWith(ENC_PREFIX)) decrypted.oauth.client_secret = decrypt(decrypted.oauth.client_secret);
return decrypted;
}
const manifest = decryptManifest(rawManifest);
// Region configuration
const REGION_CONFIG = {
NA: {
name: "North America",
cma: "https://api.contentstack.io",
cda: "https://cdn.contentstack.io",
app: "https://app.contentstack.com",
developerHub: "https://developerhub-api.contentstack.com",
personalize: "https://personalize-api.contentstack.com",
launch: "https://launch-api.contentstack.com",
},
EU: {
name: "Europe",
cma: "https://eu-api.contentstack.com",
cda: "https://eu-cdn.contentstack.com",
app: "https://eu-app.contentstack.com",
developerHub: "https://eu-developerhub-api.contentstack.com",
personalize: "https://eu-personalize-api.contentstack.com",
launch: "https://eu-launch-api.contentstack.com",
},
"AZURE-NA": {
name: "Azure North America",
cma: "https://azure-na-api.contentstack.com",
cda: "https://azure-na-cdn.contentstack.com",
app: "https://azure-na-app.contentstack.com",
developerHub: "https://azure-na-developerhub-api.contentstack.com",
personalize: "https://azure-na-personalize-api.contentstack.com",
launch: "https://azure-na-launch-api.contentstack.com",
},
"AZURE-EU": {
name: "Azure Europe",
cma: "https://azure-eu-api.contentstack.com",
cda: "https://azure-eu-cdn.contentstack.com",
app: "https://azure-eu-app.contentstack.com",
developerHub: "https://azure-eu-developerhub-api.contentstack.com",
personalize: "https://azure-eu-personalize-api.contentstack.com",
launch: "https://azure-eu-launch-api.contentstack.com",
},
"GCP-NA": {
name: "GCP North America",
cma: "https://gcp-na-api.contentstack.com",
cda: "https://gcp-na-cdn.contentstack.com",
app: "https://gcp-na-app.contentstack.com",
developerHub: "https://gcp-na-developerhub-api.contentstack.com",
personalize: "https://gcp-na-personalize-api.contentstack.com",
launch: "https://gcp-na-launch-api.contentstack.com",
},
"GCP-EU": {
name: "GCP Europe",
cma: "https://gcp-eu-api.contentstack.com",
cda: "https://gcp-eu-cdn.contentstack.com",
app: "https://gcp-eu-app.contentstack.com",
developerHub: "https://gcp-eu-developerhub-api.contentstack.com",
personalize: "https://gcp-eu-personalize-api.contentstack.com",
launch: "https://gcp-eu-launch-api.contentstack.com",
},
"AU": {
name: "Australia",
cma: "https://au-api.contentstack.com",
cda: "https://au-cdn.contentstack.com",
app: "https://au-app.contentstack.com",
developerHub: "https://au-developerhub-api.contentstack.com",
personalize: "https://au-personalize-api.contentstack.com",
launch: "https://au-launch-api.contentstack.com",
},
};
/**
* Gets the current region from the CSDX config.
* @returns The current region.
*/
function getCurrentRegion() {
try {
const regionOutput = execSync("csdx config:get:region", {
encoding: "utf8",
}).trim();
console.log("Raw region from CSDX config:", regionOutput);
const regionMatch = regionOutput.match(
/\b(NA|EU|AZURE-NA|AZURE-EU|GCP-NA)\b/
);
if (regionMatch) {
const regionKey = regionMatch[1];
console.log("Extracted region key:", regionKey);
return regionKey;
}
console.warn("Could not extract region from:", regionOutput);
return "NA";
} catch (error) {
console.warn("Could not get region from CSDX:", error.message);
return "NA";
}
}
/**
* Sets the OAuth configuration for the CLI.
* @param migration - The migration object.
* @param stackSDKInstance - The stack SDK instance.
* @param managementAPIClient - The management API client.
*/
module.exports = async ({
migration,
stackSDKInstance,
managementAPIClient,
}) => {
const axiosInstance = managementAPIClient.axiosInstance;
const regionKey = getCurrentRegion();
const regionConfig = REGION_CONFIG[regionKey];
console.log(`\n=== USING REGION: ${regionConfig.name} (${regionKey}) ===`);
console.log(`CMA: ${regionConfig.cma}`);
console.log(`CDA: ${regionConfig.cda}`);
console.log(`App: ${regionConfig.app}`);
console.log("=".repeat(50));
try {
const user = await managementAPIClient.getUser();
console.log(`✓ User: ${user?.email} (${user?.uid})`);
if (!user?.organizations || user?.organizations?.length === 0) {
console.log("No organizations found");
return;
}
console.log(`\n=== YOUR ORGANIZATIONS ===`);
user?.organizations?.forEach((org, index) => {
console.log(`${index + 1}. ${org.name} (${org.uid})`);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const selectedOrg = await new Promise((resolve) => {
rl.question(`\nSelect organization number: `, (answer) => {
rl.close();
const index = parseInt(answer) - 1;
if (index >= 0 && index < user?.organizations?.length) {
resolve(user?.organizations?.[index]);
} else {
console.log("Invalid selection");
resolve(null);
}
});
});
if (!selectedOrg) {
console.log("No organization selected. Exiting...");
return;
}
const headers = managementAPIClient.axiosInstance.defaults.headers;
const authtoken = headers.authtoken || headers.authorization;
console.log(`\n✓ Selected: ${selectedOrg?.name} (${selectedOrg?.uid})`);
console.log(
`Auth token: ${
authtoken ? authtoken.substring(0, 20) + "..." : "Not found"
}`
);
const orgDetails = await managementAPIClient
.organization(selectedOrg?.uid)
.fetch();
console.log(`✓ Organization details fetched: ${orgDetails.name}`);
const regionMapping = {
NA: "NA",
EU: "EU",
"AZURE-NA": "AZURE_NA",
"AZURE-EU": "AZURE_EU",
"GCP-NA": "GCP_NA",
"GCP-EU": "GCP_EU",
};
const sdkRegion = regionMapping[regionKey];
let clientConfig = {
authorization: authtoken,
};
if (regionKey !== "NA" && sdkRegion) {
clientConfig.region = contentstack.Region[sdkRegion];
console.log(`Setting SDK region to: ${sdkRegion}`);
}
const client = contentstack.client(clientConfig);
console.log(`Contentstack client configured for ${regionKey} region`);
// Find or create app
let existingApp = null;
try {
console.log("Searching for existing app...");
const allApps = await client.marketplace(selectedOrg.uid).findAllApps();
existingApp = allApps?.items?.find((app) => app?.name === manifest?.name);
if (!existingApp) {
console.log("Creating new app...");
existingApp = await client
.marketplace(selectedOrg.uid)
.app()
.create(manifest);
console.log(`App created: ${existingApp.name} (${existingApp.uid})`);
} else {
console.log(
`Found existing app: ${existingApp.name} (${existingApp.uid})`
);
console.log("Updating existing app with manifest...");
// Update the existing app with the current manifest
const oauthUpdatePayload = {
redirect_uri: manifest?.oauth?.redirect_uri,
app_token_config: manifest?.oauth?.app_token_config || {
enabled: false,
scopes: [],
},
user_token_config: manifest?.oauth?.user_token_config || {
enabled: true,
scopes: manifest?.oauth?.user_token_config?.scopes || [],
allow_pkce: true,
},
};
const updatedApp = await axios.put(
`${regionConfig.app}/apps-api/manifests/${existingApp?.uid}/oauth`,
oauthUpdatePayload,
{
headers: {
authorization: authtoken,
"Content-Type": "application/json",
organization_uid: selectedOrg.uid,
},
}
);
console.log(`App updated: ${existingApp.name} (${existingApp.uid})`);
}
} catch (error) {
console.error("Error with app operations:", error.message);
if (error.status === 401) {
console.error(`\nAuthentication Error - This usually means:`);
console.error(` • Your auth token is from a different region`);
console.error(
` • Please logout and login again in the ${regionKey} region`
);
console.error(` • Commands: csdx auth:logout → csdx auth:login`);
}
throw error;
}
console.log("Fetching OAuth configuration...");
const oauthData = await client
?.marketplace(selectedOrg?.uid)
?.app(existingApp?.uid)
?.oauth()
?.fetch();
console.log("Generating PKCE credentials...");
const code_verifier = crypto?.randomBytes(32).toString("hex");
const code_challenge = crypto
?.createHash("sha256")
?.update(code_verifier)
?.digest("base64")
?.replace(/\+/g, "-")
?.replace(/\//g, "_")
?.replace(/=+$/, "");
// Generates the authorization URL for the app
const authUrl = `${regionConfig.app}/#!/apps/${
existingApp?.uid
}/authorize?response_type=code&client_id=${
oauthData?.client_id
}&redirect_uri=${encodeURIComponent(
oauthData?.redirect_uri
)}&code_challenge=${code_challenge}&code_challenge_method=S256`;
console.log(`\nAuthorization URL for ${regionConfig.name}:`);
console.log(authUrl);
// Formats the app data for the app.json file
const appData = {
timestamp: new Date().toISOString(),
region: {
key: regionKey,
name: regionConfig.name,
endpoints: regionConfig,
},
user: {
email: user?.email,
uid: user?.uid,
},
organization: {
name: selectedOrg?.name,
uid: selectedOrg?.uid,
},
app: {
name: existingApp?.name,
uid: existingApp?.uid,
manifest: manifest?.name,
},
oauthData: oauthData,
pkce: {
code_verifier: code_verifier,
code_challenge: code_challenge,
},
authUrl: authUrl,
isDefault: false,
};
if (ENCRYPT_KEY) {
if (appData?.oauthData) {
appData?.oauthData?.client_id = encrypt(appData?.oauthData?.client_id);
appData?.oauthData?.client_secret = encrypt(appData?.oauthData?.client_secret);
}
if (appData?.pkce) {
appData?.pkce?.code_verifier = encrypt(appData?.pkce?.code_verifier);
appData?.pkce?.code_challenge = encrypt(appData?.pkce?.code_challenge);
}
} else {
console.warn("WARNING: MANIFEST_ENCRYPT_KEY not set — app.json will contain plaintext credentials");
}
fs.writeFileSync("app.json", JSON.stringify(appData, null, 2));
console.log("OAuth data & Auth URL logged to app.json");
} catch (error) {
console.error("Setup failed:");
console.error("Error:", error?.message);
if (error?.errorMessage) {
console.error("Details:", error?.errorMessage);
}
console.error(`\nDebug Info:`);
console.error(`Region: ${regionKey} (${regionConfig?.name || "Unknown"})`);
console.error(`Expected CMA: ${regionConfig?.cma || "Unknown"}`);
console.error(
`Management API URL: ${managementAPIClient.axiosInstance.defaults.baseURL}`
);
throw error;
}
};