-
Notifications
You must be signed in to change notification settings - Fork 14
feat(ssl-server-test): use durable Lambda function #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
35e32db
b12e24d
6e3f4bd
e768144
784eed3
a5ff21a
f0d8bf0
6fd4499
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,69 @@ | ||
| import { DurableContext, withDurableExecution } from '@aws/durable-execution-sdk-js'; | ||
| import { PublishCommand, SNSClient } from '@aws-sdk/client-sns'; | ||
| import got from 'got'; | ||
| import { getEnv } from '../utils'; | ||
| import { AnalyzeResponse, AnalyzeStatus, SslServerTestGrade } from './types'; | ||
|
|
||
| const sslLabsClient = got.extend({ | ||
| prefixUrl: 'https://api.ssllabs.com/api/v3', | ||
| prefixUrl: 'https://api.ssllabs.com/api/v4', | ||
| headers: { email: getEnv('REGISTRATION_EMAIL') }, | ||
| searchParams: { | ||
| host: getEnv('HOST'), | ||
| }, | ||
| }); | ||
|
|
||
| export async function handler(event: Record<string, string>) { | ||
| const response = await sslLabsClient('analyze', { | ||
| searchParams: event, | ||
| }).json(); | ||
| const snsClient = new SNSClient({}); | ||
|
|
||
| return response; | ||
| } | ||
| export const handler = withDurableExecution(async (_, context: DurableContext) => { | ||
| // Start analysis | ||
| await context.step('start-analysis', async () => { | ||
| const response = await sslLabsClient('analyze', { | ||
| searchParams: { startNew: 'on' }, | ||
| }).json(); | ||
| context.logger.info('Started SSL analysis', response); | ||
| }); | ||
|
|
||
| // Wait for analysis to complete | ||
| const analysis = await context.waitForCondition<AnalyzeResponse>('wait-for-completion', async (_state, waitContext) => { | ||
| const response = await sslLabsClient('analyze').json<AnalyzeResponse>(); | ||
| waitContext.logger.info('Current analysis status', response); | ||
| return response; | ||
| }, { | ||
| initialState: { status: AnalyzeStatus.DNS, endpoints: [] }, | ||
| waitStrategy: (state) => { | ||
| if (state.status === AnalyzeStatus.READY || state.status === AnalyzeStatus.ERROR) { | ||
| return { shouldContinue: false }; | ||
| } | ||
| return { shouldContinue: true, delay: { seconds: 30 } }; | ||
| }, | ||
| }); | ||
|
|
||
| if (analysis.status === AnalyzeStatus.ERROR) { | ||
| throw new Error(`Analysis failed: ${analysis.statusMessage}`); | ||
| } | ||
|
|
||
| const grades = Object.values(SslServerTestGrade).reverse(); | ||
|
|
||
| const gradeIndices = analysis.endpoints | ||
| .map(e => grades.indexOf(e.grade as SslServerTestGrade)) | ||
| .filter(index => index !== -1); | ||
|
|
||
| if (gradeIndices.length === 0) { | ||
| throw new Error('No valid grade found in analysis result'); | ||
| } | ||
|
|
||
| const bestGradeIndex = Math.max(...gradeIndices); | ||
| const bestGrade = grades[bestGradeIndex]; | ||
|
|
||
| if (bestGradeIndex < grades.indexOf(getEnv('MINIMUM_GRADE') as SslServerTestGrade)) { | ||
| await context.step('notify', async () => { | ||
| await snsClient.send(new PublishCommand({ | ||
| TopicArn: getEnv('ALARM_TOPIC_ARN'), | ||
| Message: JSON.stringify(analysis), | ||
| Subject: `SSL grade for ${getEnv('HOST')} is below minimum grade (${bestGrade} < ${getEnv('MINIMUM_GRADE')})`, | ||
| })); | ||
| }); | ||
| } | ||
|
|
||
| context.logger.info('SSL analysis completed successfully', analysis); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.