Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/vulnerabilities/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { noopUrlResolver } from '../common/index.js';
const { AUDIT_STEP_DESTINATIONS } = Audit;
const INTERVAL = 1; // days
const AUDIT_TYPE = Audit.AUDIT_TYPES.SECURITY_VULNERABILITIES;
const AUTOFIX_ALTERNATE_SITE_ID = 'd440f2b0-9820-4947-8c1e-f02112ae1676';
const AUTOFIX_ALTERNATE_QUEUE_URL = 'https://sqs.us-east-1.amazonaws.com/471112529073/mysticat-to-starfish-dev2';

/**
* Fetches vulnerability report for a given AEM Cloud Service site from the starfish API.
Expand Down Expand Up @@ -308,10 +310,15 @@ export const opportunityAndSuggestionsStep = async (context) => {
return { status: 'complete' };
}

if (!sqs || !env?.QUEUE_SPACECAT_TO_STARFISH_AUTO_CODE) {
const useAlternateQueue = site.getId() === AUTOFIX_ALTERNATE_SITE_ID;
const queueUrl = useAlternateQueue
? AUTOFIX_ALTERNATE_QUEUE_URL
: env?.QUEUE_SPACECAT_TO_STARFISH_AUTO_CODE;

if (!sqs || !queueUrl) {
log.warn(
`[${AUDIT_TYPE}] [Site: ${site.getId()}] skipping code generation with starfish-auto-code, because
QUEUE_SPACECAT_TO_STARFISH_AUTO_CODE is not configured.`,
${useAlternateQueue ? 'alternate queue' : 'QUEUE_SPACECAT_TO_STARFISH_AUTO_CODE'} is not configured.`,
);
return { status: 'complete' };
}
Expand All @@ -337,8 +344,8 @@ export const opportunityAndSuggestionsStep = async (context) => {
},
};

log.debug(`[${AUDIT_TYPE}] [Site: ${site.getId()}] sending message to starfish-auto-code for code fix generation: ${JSON.stringify(message)}`);
await sqs.sendMessage(env.QUEUE_SPACECAT_TO_STARFISH_AUTO_CODE, message);
log.debug(`[${AUDIT_TYPE}] [Site: ${site.getId()}] sending message to ${useAlternateQueue ? 'alternate autofix queue' : 'starfish-auto-code'} for code fix generation: ${JSON.stringify(message)}`);
await sqs.sendMessage(queueUrl, message);
return { status: 'complete' };
};

Expand Down
90 changes: 90 additions & 0 deletions test/audits/vulnerabilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,96 @@ describe('Vulnerabilities Handler Integration Tests', () => {
);
});

it('should send to alternate queue when site matches alternate site ID', async () => {
const mockOpportunity = {
getId: () => 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
addSuggestions: sandbox.stub().resolves({ errorItems: [], createdItems: [] }),
getSuggestions: sandbox.stub()
.onCall(0)
.resolves([])
.onCall(1)
.resolves([
{ getId: () => 'suggestion-new', getStatus: () => 'NEW' },
]),
};

context.dataAccess.Opportunity.create.resolves(mockOpportunity);
context.dataAccess.Opportunity.findById.resolves(null);

const configuration = {
isHandlerEnabledForSite: sandbox.stub(),
};
context.dataAccess.Configuration.findLatest.resolves(configuration);

configuration.isHandlerEnabledForSite.withArgs('security-vulnerabilities').returns(true);
configuration.isHandlerEnabledForSite.withArgs('security-vulnerabilities-auto-suggest').returns(true);

// Set site ID to the alternate site ID
context.site.getId = () => 'd440f2b0-9820-4947-8c1e-f02112ae1676';

context.audit = {
getAuditResult: () => ({
vulnerabilityReport: VULNERABILITY_REPORT_WITH_VULNERABILITIES,
success: true,
}),
getId: () => 'test-audit-id',
};

context.data = {
importResults: [{
result: [{
codeBucket: 'spacecat-importer-bucket',
codePath: 'code/test/repository.zip',
}],
}],
};

const result = await opportunityAndSuggestionsStep(context);
expect(result).to.deep.equal({ status: 'complete' });

expect(context.sqs.sendMessage).to.have.been.calledOnce;
const messageCall = context.sqs.sendMessage.getCall(0);
expect(messageCall.args[0]).to.equal('https://sqs.us-east-1.amazonaws.com/471112529073/mysticat-to-starfish-dev2');
expect(messageCall.args[1]).to.have.property('type', 'codefix:security-vulnerabilities');
expect(messageCall.args[1]).to.have.property('siteId', 'd440f2b0-9820-4947-8c1e-f02112ae1676');
});

it('should skip when alternate site has no sqs', async () => {
const configuration = {
isHandlerEnabledForSite: sandbox.stub(),
};
context.dataAccess.Configuration.findLatest.resolves(configuration);

configuration.isHandlerEnabledForSite.withArgs('security-vulnerabilities').returns(true);
configuration.isHandlerEnabledForSite.withArgs('security-vulnerabilities-auto-suggest').returns(true);

context.site.getId = () => 'd440f2b0-9820-4947-8c1e-f02112ae1676';

context.audit = {
getAuditResult: () => ({
vulnerabilityReport: VULNERABILITY_REPORT_WITH_VULNERABILITIES,
success: true,
}),
getId: () => 'test-audit-id',
};

context.data = {
importResults: [{
result: [{
codeBucket: 'spacecat-importer-bucket',
codePath: 'code/test/repository.zip',
}],
}],
};

context.sqs = null;

const result = await opportunityAndSuggestionsStep(context);

expect(result).to.deep.equal({ status: 'complete' });
expect(context.log.warn).to.have.been.calledWithMatch(/alternate queue/);
});

it('should skip starfish-auto-code when queue env var is not configured', async () => {
const configuration = {
isHandlerEnabledForSite: sandbox.stub(),
Expand Down
Loading