-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create cli-pull-ot command to sync assessments from OneTrust to disk (#…
…375) * create cli-pull-ot file * modify cli-pull-ot * create createOneTrustGotInstance helper * create helpers * create getAssessment helper * create index.ts * update enums * update package.json * fix bug in write * fix compilation * update package version * update README.md * add default debug argument * update parseCliPullOtArguments comment * fix readme * nit * improveOneTrust types based on responses * improve types * more types improvements
- Loading branch information
1 parent
fc042ea
commit df0943d
Showing
12 changed files
with
852 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env node | ||
import { logger } from './logger'; | ||
import colors from 'colors'; | ||
import { | ||
getListOfAssessments, | ||
getAssessment, | ||
writeOneTrustAssessment, | ||
parseCliPullOtArguments, | ||
createOneTrustGotInstance, | ||
} from './oneTrust'; | ||
import { OneTrustPullResource } from './enums'; | ||
import { mapSeries } from 'bluebird'; | ||
|
||
/** | ||
* Pull configuration from OneTrust down locally to disk | ||
* | ||
* Dev Usage: | ||
* yarn ts-node ./src/cli-pull-ot.ts --hostname=customer.my.onetrust.com --auth=$ONE_TRUST_OAUTH_TOKEN --file=./oneTrustAssessment.json | ||
* | ||
* Standard usage | ||
* yarn cli-pull-ot --hostname=customer.my.onetrust.com --auth=$ONE_TRUST_OAUTH_TOKEN --file=./oneTrustAssessment.json | ||
*/ | ||
async function main(): Promise<void> { | ||
const { file, fileFormat, hostname, auth, resource, debug } = | ||
parseCliPullOtArguments(); | ||
|
||
try { | ||
if (resource === OneTrustPullResource.Assessments) { | ||
// use the hostname and auth token to instantiate a client to talk to OneTrust | ||
const oneTrust = createOneTrustGotInstance({ hostname, auth }); | ||
|
||
// fetch the list of all assessments in the OneTrust organization | ||
const assessments = await getListOfAssessments({ oneTrust }); | ||
|
||
// fetch details about one assessment at a time and sync to disk right away to avoid running out of memory | ||
await mapSeries(assessments, async (assessment, index) => { | ||
logger.info( | ||
`Fetching details about assessment ${index + 1} of ${ | ||
assessments.length | ||
}...`, | ||
); | ||
const assessmentDetails = await getAssessment({ | ||
oneTrust, | ||
assessmentId: assessment.assessmentId, | ||
}); | ||
|
||
writeOneTrustAssessment({ | ||
assessment, | ||
assessmentDetails, | ||
index, | ||
total: assessments.length, | ||
file, | ||
fileFormat, | ||
}); | ||
}); | ||
} | ||
} catch (err) { | ||
logger.error( | ||
colors.red( | ||
`An error occurred pulling the resource ${resource} from OneTrust: ${ | ||
debug ? err.stack : err.message | ||
}`, | ||
), | ||
); | ||
process.exit(1); | ||
} | ||
|
||
// Indicate success | ||
logger.info( | ||
colors.green( | ||
`Successfully synced OneTrust ${resource} to disk at "${file}"!`, | ||
), | ||
); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import got, { Got } from 'got'; | ||
|
||
/** | ||
* Instantiate an instance of got that is capable of making requests to OneTrust | ||
* | ||
* @param param - information about the OneTrust URL | ||
* @returns The instance of got that is capable of making requests to the customer ingress | ||
*/ | ||
export const createOneTrustGotInstance = ({ | ||
hostname, | ||
auth, | ||
}: { | ||
/** Hostname of the OneTrust API */ | ||
hostname: string; | ||
/** The OAuth access token */ | ||
auth: string; | ||
}): Got => | ||
got.extend({ | ||
prefixUrl: `https://${hostname}`, | ||
headers: { | ||
accept: 'application/json', | ||
'content-type': 'application/json', | ||
authorization: `Bearer ${auth}`, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Got } from 'got'; | ||
import { OneTrustGetAssessmentResponse } from './types'; | ||
|
||
/** | ||
* Retrieve details about a particular assessment. | ||
* | ||
* @param param - the information about the OneTrust client and assessment to retrieve | ||
* @returns details about the assessment | ||
*/ | ||
export const getAssessment = async ({ | ||
oneTrust, | ||
assessmentId, | ||
}: { | ||
/** The OneTrust client instance */ | ||
oneTrust: Got; | ||
/** The ID of the assessment to retrieve */ | ||
assessmentId: string; | ||
}): Promise<OneTrustGetAssessmentResponse> => { | ||
const { body } = await oneTrust.get( | ||
`api/assessment/v2/assessments/${assessmentId}/export?ExcludeSkippedQuestions=false`, | ||
); | ||
|
||
return JSON.parse(body) as OneTrustGetAssessmentResponse; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Got } from 'got'; | ||
import { logger } from '../logger'; | ||
import { | ||
OneTrustAssessment, | ||
OneTrustGetListOfAssessmentsResponse, | ||
} from './types'; | ||
|
||
/** | ||
* Fetch a list of all assessments from the OneTrust client. | ||
* | ||
* @param param - the information about the OneTrust client | ||
* @returns a list of OneTrustAssessment | ||
*/ | ||
export const getListOfAssessments = async ({ | ||
oneTrust, | ||
}: { | ||
/** The OneTrust client instance */ | ||
oneTrust: Got; | ||
}): Promise<OneTrustAssessment[]> => { | ||
let currentPage = 0; | ||
let totalPages = 1; | ||
let totalElements = 0; | ||
|
||
const allAssessments: OneTrustAssessment[] = []; | ||
|
||
logger.info('Getting list of all assessments from OneTrust...'); | ||
while (currentPage < totalPages) { | ||
// eslint-disable-next-line no-await-in-loop | ||
const { body } = await oneTrust.get( | ||
`api/assessment/v2/assessments?page=${currentPage}&size=2000`, | ||
); | ||
const { page, content } = JSON.parse( | ||
body, | ||
) as OneTrustGetListOfAssessmentsResponse; | ||
allAssessments.push(...(content ?? [])); | ||
if (currentPage === 0) { | ||
totalPages = page?.totalPages ?? 0; | ||
totalElements = page?.totalElements ?? 0; | ||
} | ||
currentPage += 1; | ||
|
||
// log progress | ||
logger.info( | ||
`Fetched ${allAssessments.length} of ${totalElements} assessments.`, | ||
); | ||
} | ||
|
||
return allAssessments; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './getListOfAssessments'; | ||
export * from './createOneTrustGotInstance'; | ||
export * from './getAssessment'; | ||
export * from './writeOneTrustAssessment'; | ||
export * from './parseCliPullOtArguments'; |
Oops, something went wrong.