Skip to content
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

Make CheckUrlExists work on Node18 #157

Merged
merged 30 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bdb3ae1
Upgrade the node-libcurl version
coverbeck Aug 9, 2023
de93919
See if this works, last one didn't.
coverbeck Aug 9, 2023
053024a
Revert "See if this works, last one didn't."
coverbeck Aug 15, 2023
7c6ff1a
See if a layer works
coverbeck Aug 15, 2023
fc90e60
See if a layer works, second try
coverbeck Aug 15, 2023
238e4eb
Specify the directory instead
coverbeck Aug 15, 2023
2c4f632
Try a newer libstdc
coverbeck Aug 15, 2023
be985ba
See if directory matters
coverbeck Aug 16, 2023
8c98c6b
Whoops
coverbeck Aug 16, 2023
7512599
Some debugging
coverbeck Aug 16, 2023
97ba06d
Better debugging?
coverbeck Aug 17, 2023
ca18a4c
Remove node-libcurl to get debugging working
coverbeck Aug 17, 2023
7b23190
Make tests pass
coverbeck Aug 17, 2023
78b2b93
Grr
coverbeck Aug 17, 2023
09e118e
Revert, try setting LD_LIBRARY_PATH
coverbeck Aug 17, 2023
a462862
See what this does
coverbeck Aug 17, 2023
4522218
Rename lib directory
coverbeck Aug 17, 2023
f6d5816
Here we go again
coverbeck Aug 17, 2023
e753134
See if updating serverless orb makes a difference
coverbeck Aug 18, 2023
17eea92
Try not using node-libcurl
coverbeck Aug 22, 2023
6f1e84a
Fix tests and whatnot
coverbeck Aug 23, 2023
ee26337
Update user-agent, go back to old SAM version
coverbeck Aug 23, 2023
50c9e13
Remove now used tls.
coverbeck Aug 23, 2023
9b10bb5
Upgrade runtime to Java 17
coverbeck Aug 23, 2023
2e4fbe8
Update to latest version of fmt-maven-plugin
coverbeck Aug 23, 2023
f986208
Remove unused layer
coverbeck Aug 23, 2023
482bb15
Revert back to develop for unused stuff.
coverbeck Aug 23, 2023
1980520
A little cleanup.
coverbeck Aug 23, 2023
df7ae20
PR feedback (my IDE prettier doesn't like it, but it is nicer).
coverbeck Aug 25, 2023
1035435
Make prettier happy! Build was failing.
coverbeck Aug 25, 2023
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
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ orbs:
aws-s3: circleci/aws-s3@3.0.0
node: circleci/node@4.7.0
sam: circleci/aws-sam-serverless@3.0
aws-cli: circleci/aws-cli@3.1.1 # perform openid connect
aws-cli: circleci/aws-cli@3.1.1 # perform openid connect
jobs:
build:
docker:
Expand All @@ -20,7 +20,7 @@ jobs:
- aws-cli/setup:
profile-name: WEB IDENTITY PROFILE
role-arn: $AWS_ROLE_ARN
role-session-name: "CircleCI-${CIRCLE_WORKFLOW_ID}-${CIRCLE_JOB}"
role-session-name: "CircleCI-${CIRCLE_WORKFLOW_ID}-${CIRCLE_JOB}"
- checkout
- node/install:
install-yarn: false
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
- aws-cli/setup:
profile-name: WEB IDENTITY PROFILE
role-arn: $AWS_ROLE_ARN
role-session-name: "CircleCI-${CIRCLE_WORKFLOW_ID}-${CIRCLE_JOB}"
role-session-name: "CircleCI-${CIRCLE_WORKFLOW_ID}-${CIRCLE_JOB}"
- checkout
- run:
name: Validate index.js
Expand Down
72 changes: 50 additions & 22 deletions checkUrlExists/lambda/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
const fs = require("fs");
const tls = require("tls");
const Url = require("url");
const ftp = require("basic-ftp");
const { http, https } = require("follow-redirects");

const { curly } = require("node-libcurl");

// important steps to get validation of https (as opposed to http) urls
// Get root certificates so https will work
//
// Write the certificates to a file
// https://stackoverflow.com/questions/63052127/protractor-node-libcurl-failed-ssl-peer-certificate-or-ssh-remote-key-was-not-o
// When doing sam build the file must be in /tmp because other wise it cannot be read
// due to ro file system in container
// https://stackoverflow.com/questions/53810516/getting-error-aws-lambda-erofs-read-only-file-system-open-var-task-assets
const certFilePath = "/tmp/cacert.pem";
// https://nodejs.org/api/tls.html#tls_tls_rootcertificates
// An immutable array of strings representing the root certificates (in PEM format) from the bundled Mozilla CA store as supplied by current Node.js version.
// The bundled CA store, as supplied by Node.js, is a snapshot of Mozilla CA store that is fixed at release time. It is identical on all supported platforms.
const tlsData = tls.rootCertificates.join("\n");
fs.writeFileSync(certFilePath, tlsData);
// The Node url.parse returns an object where the protocol is lower case and contains the colon at the end
const SECURE_FTP_PROTOCOL = "sftp:";
const FTP_PROTOCOL = "ftp:";
const HTTP_PROTOCOL = "http:";
const HTTPS_PROTOCOL = "https:";

/**
* TODO: Change to array of URLs to parse
Expand Down Expand Up @@ -50,10 +40,48 @@ async function checkUrl(url) {
}

async function run(url) {
const curlOpts = {
caInfo: certFilePath,
};
return curly.head(url, curlOpts);
const parsedUrl = Url.parse(url);
const protocol = parsedUrl.protocol; // Url.parse() lower cases the protocol
if (FTP_PROTOCOL === protocol || SECURE_FTP_PROTOCOL === protocol) {
const secure = SECURE_FTP_PROTOCOL === protocol;
const ftpClient = new ftp.Client();
try {
let options = {
host: parsedUrl.host,
secure: secure,
...(parsedUrl.port && { port: parsedUrl.port }),
};
await ftpClient.access(options);
const size = await ftpClient.size(parsedUrl.path);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this or any other await fails, it will throw an exception, which will be caught on line 46/36 (note to self and anybody else who isn't familiar with JS promises and await.

return size > 0
? Promise.resolve()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it plausible that a valid file will be empty? If so, >= here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I was thinking an empty file would never be valid from a workflow input perspective. But I could see it both ways. I'm going to leave it as is for now, but if you or anybody feels strongly, I can change it.

: Promise.reject("Could not get size for " + url);
} finally {
ftpClient.close();
}
} else if (HTTP_PROTOCOL === protocol) {
return httpOrHttpsRequest(url, http);
} else if (HTTPS_PROTOCOL === protocol) {
return httpOrHttpsRequest(url, https);
}
return Promise.reject("Unsupported protocol: " + protocol);
}

function httpOrHttpsRequest(url, httpOrHttps) {
return new Promise((resolve, reject) => {
const req = httpOrHttps.request(url, {
method: "HEAD",
headers: { "user-agent": "Dockstore/1.0" }, // User-agent must be set for tests to pass, AWS (WAF?) blocks requests with no user-agent
});
req.on("response", (res) => {
if (res.statusCode < 300) {
resolve(res.statusCode);
}
reject(res.statusCode);
});
req.on("error", (err) => reject(err));
req.end();
});
}

function returnResponse(fileFound) {
Expand Down
Loading
Loading