Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #36 from cisagov/bugfix/move-export-anonymize-to-d…
Browse files Browse the repository at this point in the history
…one-state-on-error

Small fix for anonymization & export error state
  • Loading branch information
GoldingAustin authored Nov 9, 2022
2 parents 6ef20f0 + 1d19e76 commit e401fc9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export const AnonymizeDialog = observer<AnonymizeDialogProps>(({ campaign, onClo
),
},
});
if (!fileName) {
this.error = 'Error anonymizing campaign';
return (this.isLoading = false);
}
const res: Response = yield store.auth.protectedFetch(
`${store.auth.serverUrl}/api/campaign/download/${fileName.anonymizeCampaign}`,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface AnonymizationMachineContext extends AnonymizationInput {
error?: string;
}

type AnonymizationMachineEvent = { type: 'ANONYMIZE' } | { type: 'SET_ERROR'; error: Error };
type AnonymizationMachineEvent = { type: 'ANONYMIZE' };

export const anonymizationMachine = createMachine(
{
Expand All @@ -67,13 +67,8 @@ export const anonymizationMachine = createMachine(
target: 'finished',
},
onError: {
target: 'idle',
actions: ['setError'],
},
},
on: {
SET_ERROR: {
actions: ['setError'],
actions: 'setError',
target: 'finished',
},
},
},
Expand All @@ -85,7 +80,7 @@ export const anonymizationMachine = createMachine(
{
actions: {
setError: assign((_, event) => ({
error: 'error' in event ? event.error.message : undefined,
error: 'data' in event ? (event.data as Error)?.message : undefined,
})),
},
services: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

export interface Typegen0 {
'@@xstate/typegen': true;
eventsCausingActions: {
setError: 'error.platform.ANONYMIZATION.anonymizing:invocation[0]' | 'SET_ERROR';
};
internalEvents: {
'error.platform.ANONYMIZATION.anonymizing:invocation[0]': {
type: 'error.platform.ANONYMIZATION.anonymizing:invocation[0]';
Expand All @@ -21,11 +18,14 @@ export interface Typegen0 {
guards: never;
delays: never;
};
eventsCausingActions: {
setError: 'error.platform.ANONYMIZATION.anonymizing:invocation[0]';
};
eventsCausingServices: {
anonymizeService: 'ANONYMIZE';
};
eventsCausingGuards: {};
eventsCausingDelays: {};
matchesStates: 'idle' | 'anonymizing' | 'finished';
matchesStates: 'anonymizing' | 'finished' | 'idle';
tags: never;
}
60 changes: 30 additions & 30 deletions applications/server/src/store/campaign-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,41 +96,41 @@ export class CampaignResolvers {

@Authorized()
@Mutation(() => String, { description: 'Anonymize campaign for export' })
anonymizeCampaign(
async anonymizeCampaign(
@Ctx() ctx: GraphQLContext,
@Arg('campaignId', () => String) campaignId: string,
@Arg('anonymizeOptions') anonymizeOptions: AnonymizationInput
): Promise<string | void> {
return new Promise(async (resolve, reject) => {
if (ctx.config.blueTeam) return reject(new AuthenticationError('Blue team cannot export'));
const tempCampaignFolder = `campaign-${randomUUID()}`;
const dbPath = path.join(getDbPath(ctx.config.databaseMode), 'campaign', campaignId);
const anonymizedDbPath = path.join(getDbPath(ctx.config.databaseMode), 'anonymized-campaigns', tempCampaignFolder);
const exists = existsSync(dbPath);
if (!exists) {
return reject(Error('Database not found'));
} else {
try {
const filePath = path.join(anonymizedDbPath, 'db.redeye');

await fs.copy(dbPath, anonymizedDbPath);
const orm = await MikroORM.init({ ...getProjectMikroOrmConfig(filePath) });
await orm.em.getDriver().execute('PRAGMA wal_checkpoint');
await orm.close();
if (ctx.config.blueTeam) throw new AuthenticationError('Blue team cannot export');
const tempCampaignFolder = `campaign-${randomUUID()}`;
const dbPath = path.join(getDbPath(ctx.config.databaseMode), 'campaign', campaignId);
const anonymizedDbPath = path.join(getDbPath(ctx.config.databaseMode), 'anonymized-campaigns', tempCampaignFolder);
const exists = existsSync(dbPath);
if (!exists) {
throw Error('Database not found');
} else {
try {
const filePath = path.join(anonymizedDbPath, 'db.redeye');

const machine = interpret(
anonymizationMachine.withContext({
database: filePath,
...anonymizeOptions,
})
).start();
machine.send('ANONYMIZE');
await fs.copy(dbPath, anonymizedDbPath);
const orm = await MikroORM.init({ ...getProjectMikroOrmConfig(filePath) });
await orm.em.getDriver().execute('PRAGMA wal_checkpoint');
await orm.close();

const machine = interpret(
anonymizationMachine.withContext({
database: filePath,
...anonymizeOptions,
})
).start();
machine.send('ANONYMIZE');
return new Promise(async (resolve, reject) => {
machine.onDone(() => {
// If there is an error during anonymization delete copy and return the error
if (machine.state.context.error) {
console.debug('Error during export & anonymization ', machine.state.context.error);
fs.rmSync(anonymizedDbPath, { recursive: true });
return reject(Error(machine.state.context.error));
return reject(new Error(machine.state.context.error));
} else {
// After a minute, delete the folder if it still exists
setTimeout(() => {
Expand All @@ -141,11 +141,11 @@ export class CampaignResolvers {
return resolve(tempCampaignFolder);
}
});
} catch (e) {
fs.rmSync(anonymizedDbPath, { recursive: true });
return reject(Error((e as Error).message));
}
});
} catch (e) {
fs.rmSync(anonymizedDbPath, { recursive: true });
throw Error((e as Error).message);
}
});
}
}
}

0 comments on commit e401fc9

Please sign in to comment.