Skip to content

Commit 5be2126

Browse files
committed
@W-17233341 - Updating SFAP endpoint, removal of csrf, removal of credential file
1 parent 2cceecc commit 5be2126

File tree

5 files changed

+63
-62
lines changed

5 files changed

+63
-62
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
"@salesforce/kit": "^3.2.2",
1212
"@salesforce/sf-plugins-core": "^11.3.12",
1313
"form-data": "^4.0.1",
14-
"got": "^14.4.3",
15-
"tough-cookie": "^4.1.4"
14+
"got": "^14.4.3"
1615
},
1716
"devDependencies": {
1817
"@oclif/plugin-command-snapshot": "^5.2.21",
1918
"@salesforce/cli-plugins-testkit": "^5.3.35",
2019
"@salesforce/dev-scripts": "^10.2.9",
21-
"@types/tough-cookie": "^4.0.5",
2220
"@salesforce/plugin-command-reference": "^3.1.32",
21+
"@types/tough-cookie": "^4.0.5",
2322
"eslint-plugin-sf-plugin": "^1.20.11",
2423
"oclif": "^4.15.14",
2524
"ts-node": "^10.9.2",

src/commands/data-seeding/generate/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ export default class DataSeedingGenerate extends SfCommand<DataSeedingGenerateRe
6767

6868
// Fetch Valid JWT with Data Seed Org Perm
6969
const { jwt: jwtValue } = await initiateJWTMint(srcOrgInstUrl, srcAccessToken, tgtOrgInstUrl, tgtAccessToken);
70-
const { request_id: jobId } = await initiateDataSeed(configFile, 'data-generation', jwtValue);
70+
const { request_id: jobId } = await initiateDataSeed(
71+
configFile,
72+
'data-generation',
73+
jwtValue,
74+
srcOrgInstUrl,
75+
srcAccessToken,
76+
tgtOrgInstUrl,
77+
tgtAccessToken
78+
);
7179
const reportMessage = messages.getMessage('report.suggestion', [jobId]);
7280

7381
if (!jobId) throw new Error('Failed to receive job id');

src/commands/data-seeding/migrate/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ export default class DataSeedingMigrate extends SfCommand<DataSeedingMigrateResu
6767

6868
// Fetch Valid JWT with Data Seed Org Perm
6969
const { jwt: jwtValue } = await initiateJWTMint(srcOrgInstUrl, srcAccessToken, tgtOrgInstUrl, tgtAccessToken);
70-
71-
const { request_id: jobId } = await initiateDataSeed(configFile, 'data-copy', jwtValue);
70+
const { request_id: jobId } = await initiateDataSeed(
71+
configFile,
72+
'data-copy',
73+
jwtValue,
74+
srcOrgInstUrl,
75+
srcAccessToken,
76+
tgtOrgInstUrl,
77+
tgtAccessToken
78+
);
7279

7380
if (!jobId) throw new Error('Failed to receive job id');
7481

src/utils/api.ts

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import fs from 'node:fs';
99
import got from 'got';
10-
import { CookieJar } from 'tough-cookie';
1110
import FormData from 'form-data';
1211
import { SfError, Logger } from '@salesforce/core';
1312

@@ -17,6 +16,10 @@ export type SeedResponse = {
1716
export type ServletResponse = {
1817
jwt: string;
1918
};
19+
export type AuthServletResponse = {
20+
statusCode: string;
21+
body: string;
22+
};
2023

2124
export type PollSeedResponse = {
2225
execution_end_time: string;
@@ -29,46 +32,33 @@ export type PollSeedResponse = {
2932

3033
export type DataSeedingOperation = 'data-generation' | 'data-copy';
3134

32-
// TODO Change to SFAP Endpoint
33-
const baseUrl = process.env.SF_DATA_SEEDING_URL ?? 'https://data-seed-gid.sfdc-yfeipo.svc.sfdcfc.net';
34-
const csrfUrl = `${baseUrl}/get-csrf-token`;
35+
const baseUrl = 'https://api.salesforce.com/platform/data-seed/v1';
3536
const seedUrl = `${baseUrl}/data-seed`;
3637
const pollUrl = `${baseUrl}/status`;
3738

38-
export const getCookieJar = async (): Promise<CookieJar> => {
39-
const cookieJar = new CookieJar();
40-
await got(csrfUrl, { cookieJar });
41-
return cookieJar;
42-
};
43-
44-
export const getCsrfToken = (cookieJar: CookieJar): string => {
45-
const csrfToken = cookieJar.getCookiesSync(csrfUrl).find((cookie) => cookie.key === 'csrf_token')?.value;
46-
if (!csrfToken) throw new SfError('Failed to obtain CSRF token');
47-
48-
return csrfToken;
49-
};
50-
5139
export const initiateDataSeed = async (
5240
config: string,
5341
operation: DataSeedingOperation,
54-
jwt: string
42+
jwt: string,
43+
srcOrgUrl: string,
44+
srcAccessToken: string,
45+
tgtOrgUrl: string,
46+
tgtAccessToken: string
5547
): Promise<SeedResponse> => {
56-
// const cookieJar = await getCookieJar();
57-
// const csrf = getCsrfToken(cookieJar);
5848
const form = new FormData();
5949
form.append('config_file', fs.createReadStream(config));
60-
// TODO : Remove credential file once SFAP is active and dataseed endpoint accepts orgurl and token
61-
form.append('credentials_file', fs.createReadStream('ignore/credentials.txt'));
6250
form.append('operation', operation);
51+
form.append('source_access_token', srcAccessToken);
52+
form.append('source_instance_url', srcOrgUrl);
53+
form.append('target_access_token', tgtAccessToken);
54+
form.append('target_instance_url', tgtOrgUrl);
6355
// TODO: Update to use .json() instead of JSON.parse once the Error response is changed to be JSON
6456
// Update the return type as well
6557
const response = await got.post(seedUrl, {
6658
throwHttpErrors: false,
67-
// cookieJar,
6859
headers: {
6960
...form.getHeaders(),
70-
// 'X-CSRFToken': csrf,
71-
Authorization: 'Bearer ' + jwt,
61+
Authorization: `Bearer ${jwt}`,
7262
},
7363
body: form,
7464
});
@@ -86,31 +76,38 @@ export const initiateJWTMint = async (
8676
tgtOrgUrl: string,
8777
tgtAccessToken: string
8878
): Promise<ServletResponse> => {
89-
const srcServletUrl = srcOrgUrl + '/dataseed/auth';
90-
const responseSrc = await got.post(srcServletUrl, {
91-
throwHttpErrors: false,
92-
headers: {
93-
Authorization: 'Bearer ' + srcAccessToken,
94-
},
95-
});
79+
const srcServletUrl = `${srcOrgUrl}/dataseed/auth`;
80+
const tgtServletUrl = `${tgtOrgUrl}/dataseed/auth`;
81+
82+
const [responseSrc, responseTgt] = await Promise.all([
83+
callAuthServlet(srcServletUrl, srcAccessToken),
84+
callAuthServlet(tgtServletUrl, tgtAccessToken),
85+
]);
9686

97-
if (responseSrc.statusCode !== 200) {
98-
const tgtServletUrl = tgtOrgUrl + '/dataseed/auth';
99-
const responseTgt = await got.post(tgtServletUrl, {
100-
throwHttpErrors: false,
101-
headers: {
102-
Authorization: 'Bearer ' + tgtAccessToken,
103-
},
104-
});
105-
if (responseTgt.statusCode !== 200) {
106-
throw new SfError(
107-
`Org permission for data seed not found in source & target org.\nSource Response: Error Code : ${responseSrc.statusCode} - ${responseSrc.body}. \nTarget Response: Error Code : ${responseTgt.statusCode} - ${responseTgt.body}`
108-
);
109-
}
87+
if (responseSrc.statusCode === '200') {
88+
return JSON.parse(responseSrc.body) as ServletResponse;
89+
}
90+
91+
if (responseTgt.statusCode === '200') {
11092
return JSON.parse(responseTgt.body) as ServletResponse;
11193
}
11294

113-
return JSON.parse(responseSrc.body) as ServletResponse;
95+
throw new SfError(
96+
`Org permission for data seed not found in either the source or target org.\nSource Response: Error Code : ${responseSrc.statusCode} - ${responseSrc.body}. \nTarget Response: Error Code : ${responseTgt.statusCode} - ${responseTgt.body}`
97+
);
98+
};
99+
100+
const callAuthServlet = async (url: string, accessToken: string): Promise<AuthServletResponse> => {
101+
const response = await got.post(url, {
102+
throwHttpErrors: false,
103+
headers: {
104+
Authorization: `Bearer ${accessToken}`,
105+
},
106+
});
107+
return {
108+
statusCode: response.statusCode.toString(), // Convert to string
109+
body: response.body,
110+
};
114111
};
115112

116113
export const pollSeedStatus = async (jobId: string): Promise<PollSeedResponse> => {

yarn.lock

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7171,16 +7171,6 @@ tough-cookie@*:
71717171
universalify "^0.2.0"
71727172
url-parse "^1.5.3"
71737173

7174-
tough-cookie@^4.1.4:
7175-
version "4.1.4"
7176-
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-4.1.4.tgz#945f1461b45b5a8c76821c33ea49c3ac192c1b36"
7177-
integrity sha512-Loo5UUvLD9ScZ6jh8beX1T6sO1w2/MpCRpEP7V280GKMVUQ0Jzar2U3UJPsrdbziLEMMhu3Ujnq//rhiFuIeag==
7178-
dependencies:
7179-
psl "^1.1.33"
7180-
punycode "^2.1.1"
7181-
universalify "^0.2.0"
7182-
url-parse "^1.5.3"
7183-
71847174
tr46@~0.0.3:
71857175
version "0.0.3"
71867176
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"

0 commit comments

Comments
 (0)