Skip to content

Commit

Permalink
call syncOneTrustAssessments from cli-sync-ot
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 14, 2025
1 parent 13e80e9 commit d11609e
Showing 1 changed file with 5 additions and 112 deletions.
117 changes: 5 additions & 112 deletions src/cli-sync-ot.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
#!/usr/bin/env node
import { logger } from './logger';
import keyBy from 'lodash/keyBy';

import colors from 'colors';
import {
getListOfOneTrustAssessments,
getOneTrustAssessment,
writeOneTrustAssessment,
parseCliSyncOtArguments,
createOneTrustGotInstance,
getOneTrustRisk,
} from './oneTrust';
import { parseCliSyncOtArguments, createOneTrustGotInstance } from './oneTrust';
import { OneTrustPullResource } from './enums';
import { mapSeries, map } from 'bluebird';
import uniq from 'lodash/uniq';
import {
OneTrustAssessmentQuestion,
OneTrustAssessmentSection,
OneTrustGetRiskResponse,
} from '@transcend-io/privacy-types';
import { syncOneTrustAssessments } from './oneTrust/helpers/syncOneTrustAssessments';

/**
* Pull configuration from OneTrust down locally to disk
Expand All @@ -30,108 +16,15 @@ import {
* yarn cli-sync-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 } =
const { file, fileFormat, hostname, auth, resource, debug, dryRun } =
parseCliSyncOtArguments();

// use the hostname and auth token to instantiate a client to talk to OneTrust
const oneTrust = createOneTrustGotInstance({ hostname, auth });

try {
// TODO: move to helper function
if (resource === OneTrustPullResource.Assessments) {
// fetch the list of all assessments in the OneTrust organization
const assessments = await getListOfOneTrustAssessments({ oneTrust });

/**
* fetch details about one assessment in series and push to transcend or write to disk
* (depending on the dryRun argument) 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 getOneTrustAssessment({
oneTrust,
assessmentId: assessment.assessmentId,
});

// enrich assessments with risk information
let riskDetails: OneTrustGetRiskResponse[] = [];
const riskIds = uniq(
assessmentDetails.sections.flatMap((s: OneTrustAssessmentSection) =>
s.questions.flatMap((q: OneTrustAssessmentQuestion) =>
(q.risks ?? []).flatMap((r) => r.riskId),
),
),
);
if (riskIds.length > 0) {
logger.info(
`Fetching details about ${riskIds.length} risks for assessment ${
index + 1
} of ${assessments.length}...`,
);
riskDetails = await map(
riskIds,
(riskId) => getOneTrustRisk({ oneTrust, riskId: riskId as string }),
{
concurrency: 5,
},
);
}

// TODO: create a helper for this
// enrich the sections with risk details
const riskDetailsById = keyBy(riskDetails, 'id');
const { sections, ...restAssessmentDetails } = assessmentDetails;
const sectionsWithEnrichedRisk = sections.map((section) => {
const { questions, ...restSection } = section;
const enrichedQuestions = questions.map((question) => {
const { risks, ...restQuestion } = question;
const enrichedRisks = (risks ?? []).map((risk) => {
const details = riskDetailsById[risk.riskId];
// TODO: missing the risk meta data and links to the assessment
return {
...risk,
description: details.description,
name: details.name,
treatment: details.treatment,
treatmentStatus: details.treatmentStatus,
type: details.type,
state: details.state,
stage: details.stage,
result: details.result,
categories: details.categories,
};
});
return {
...restQuestion,
risks: enrichedRisks,
};
});
return {
...restSection,
questions: enrichedQuestions,
};
});

// combine the two assessments into a single enriched result
const assessmentWithEnrichedRisk = {
...restAssessmentDetails,
sections: sectionsWithEnrichedRisk,
};

writeOneTrustAssessment({
assessment: {
...assessment,
...assessmentWithEnrichedRisk,
},
index,
total: assessments.length,
file,
fileFormat,
});
});
await syncOneTrustAssessments({ oneTrust, file, fileFormat, dryRun });
}
} catch (err) {
logger.error(
Expand Down

0 comments on commit d11609e

Please sign in to comment.