Skip to content

Commit

Permalink
Check if DNS records exist before posting comment with subdomains (#1…
Browse files Browse the repository at this point in the history
…2319)

* Check if DNS records exist before posting comment with subdomains

* Avoid invalid string name for session

* Convert to ES Module

* Add debugging for subdomains

* Move subdomains var outside of try/catch block to fix scope issue

* Change max retries to 8 for DNS check

* Use octokit from @actions/github and use loop for subdomains

* Simplify conditional

Co-authored-by: HenryNguyen5 <[email protected]>

* Address PR feedback

* Break out variable into object instance

* Create separate paginate function

* Switch to pnpm

* Shuffle order of steps

* Adjust info log

* Add empty commit to fix SonarQube CI check not reporting

---------

Co-authored-by: HenryNguyen5 <[email protected]>
  • Loading branch information
chainchad and HenryNguyen5 authored Mar 13, 2024
1 parent 310e936 commit 945a9d9
Show file tree
Hide file tree
Showing 6 changed files with 1,279 additions and 467 deletions.
91 changes: 91 additions & 0 deletions .github/scripts/crib/lib/check-route53-records.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { setTimeout } from "node:timers/promises";
import {
Route53Client,
ListResourceRecordSetsCommand,
} from "@aws-sdk/client-route-53";

// us-east-1 is the global region used by Route 53.
const route53Client = new Route53Client({ region: "us-east-1" });

async function paginateListResourceRecordSets(route53Client, params) {
let isTruncated = true;
let nextRecordName, nextRecordType;
let allRecordSets = [];

while (isTruncated) {
const response = await route53Client.send(
new ListResourceRecordSetsCommand({
...params,
...(nextRecordName && { StartRecordName: nextRecordName }),
...(nextRecordType && { StartRecordType: nextRecordType }),
})
);

allRecordSets = allRecordSets.concat(response.ResourceRecordSets);
isTruncated = response.IsTruncated;
if (isTruncated) {
nextRecordName = response.NextRecordName;
nextRecordType = response.NextRecordType;
}
}

return allRecordSets;
}

/**
* Check if Route 53 records exist for a given Route 53 zone.
*
* @param {string} hostedZoneId The ID of the hosted zone.
* @param {string[]} recordNames An array of record names to check.
* @param {number} maxRetries The maximum number of retries.
* @param {number} initialBackoffMs The initial backoff time in milliseconds.
* @returns {Promise<boolean>} True if records exist, false otherwise.
*/
export async function route53RecordsExist(
hostedZoneId,
recordNames,
maxRetries = 8,
initialBackoffMs = 2000
) {
let attempts = 0;

// We try to gather all records within a specified time limit.
// We issue retries due to an indeterminate amount of time required
// for record propagation.
console.info("Checking DNS records in Route 53...");
while (attempts < maxRetries) {
try {
const allRecordSets = await paginateListResourceRecordSets(
route53Client,
{
HostedZoneId: hostedZoneId,
MaxItems: "300",
}
);

const recordExists = recordNames.every((name) =>
allRecordSets.some((r) => r.Name.includes(name))
);

if (recordExists) {
console.info("All records found in Route 53.");
return true;
}

// If any record is not found, throw an error to trigger a retry
throw new Error(
"One or more DNS records not found in Route 53, retrying..."
);
} catch (error) {
console.error(`Attempt ${attempts + 1}:`, error.message);
if (attempts === maxRetries - 1) {
return false; // Return false after the last attempt
}
// Exponential backoff
await setTimeout(initialBackoffMs * 2 ** attempts);
attempts++;
}
}
// Should not reach here if retries are exhausted
return false;
}
Loading

0 comments on commit 945a9d9

Please sign in to comment.