Skip to content

Commit

Permalink
fix: use 'ca' and 'cafile' npm config options for binary download (#8877
Browse files Browse the repository at this point in the history
)

Co-authored-by: Zach Bloomquist <github@chary.us>
  • Loading branch information
minijus and flotwig authored May 10, 2021
1 parent ee69541 commit 6bc7e7d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
41 changes: 37 additions & 4 deletions cli/lib/tasks/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ const getBaseUrl = () => {
return defaultBaseUrl
}

const getCA = () => {
return new Promise((resolve) => {
if (!util.getEnv('CYPRESS_DOWNLOAD_USE_CA')) {
resolve()
}

if (process.env.npm_config_ca) {
resolve(process.env.npm_config_ca)
} else if (process.env.npm_config_cafile) {
fs.readFile(process.env.npm_config_cafile, 'utf8')
.then((cafileContent) => {
resolve(cafileContent)
})
.catch(() => {
resolve()
})
} else {
resolve()
}
})
}

const prepend = (urlPath) => {
const endpoint = url.resolve(getBaseUrl(), urlPath)
const platform = os.platform()
Expand Down Expand Up @@ -181,7 +203,7 @@ const verifyDownloadedFile = (filename, expectedSize, expectedChecksum) => {
// downloads from given url
// return an object with
// {filename: ..., downloaded: true}
const downloadFromUrl = ({ url, downloadDestination, progress }) => {
const downloadFromUrl = ({ url, downloadDestination, progress, ca }) => {
return new Promise((resolve, reject) => {
const proxy = getProxyUrl()

Expand All @@ -193,7 +215,7 @@ const downloadFromUrl = ({ url, downloadDestination, progress }) => {

let redirectVersion

const req = request({
const reqOptions = {
url,
proxy,
followRedirect (response) {
Expand All @@ -210,7 +232,14 @@ const downloadFromUrl = ({ url, downloadDestination, progress }) => {
// yes redirect
return true
},
})
}

if (ca) {
debug('using custom CA details from npm config')
reqOptions.agentOptions = { ca }
}

const req = request(reqOptions)

// closure
let started = null
Expand Down Expand Up @@ -315,7 +344,10 @@ const start = (opts) => {
// ensure download dir exists
return fs.ensureDirAsync(path.dirname(downloadDestination))
.then(() => {
return downloadFromUrl({ url, downloadDestination, progress })
return getCA()
})
.then((ca) => {
return downloadFromUrl({ url, downloadDestination, progress, ca })
})
.catch((err) => {
return prettyDownloadErr(err, version)
Expand All @@ -326,4 +358,5 @@ module.exports = {
start,
getUrl,
getProxyUrl,
getCA,
}
1 change: 1 addition & 0 deletions cli/test/fixture/cafile.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
43 changes: 43 additions & 0 deletions cli/test/lib/tasks/download_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,47 @@ describe('lib/tasks/download', function () {
expect(download.getProxyUrl()).to.eq('baz')
})
})

context('with CA and CAFILE env vars', () => {
beforeEach(function () {
this.env = _.clone(process.env)
})

afterEach(function () {
process.env = this.env
})

it('returns undefined if not set', () => {
return download.getCA().then((ca) => {
expect(ca).to.be.undefined
})
})

it('returns CA from npm_config_ca', () => {
process.env.CYPRESS_DOWNLOAD_USE_CA = 'true'
process.env.npm_config_ca = 'foo'

return download.getCA().then((ca) => {
expect(ca).to.eqls('foo')
})
})

it('returns CA from npm_config_cafile', () => {
process.env.CYPRESS_DOWNLOAD_USE_CA = 'true'
process.env.npm_config_cafile = 'test/fixture/cafile.pem'

return download.getCA().then((ca) => {
expect(ca).to.eqls('bar\n')
})
})

it('returns undefined if failed reading npm_config_cafile', () => {
process.env.CYPRESS_DOWNLOAD_USE_CA = 'true'
process.env.npm_config_cafile = 'test/fixture/not-exists.pem'

return download.getCA().then((ca) => {
expect(ca).to.be.undefined
})
})
})
})

4 comments on commit 6bc7e7d

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 6bc7e7d May 10, 2021

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.3.0/circle-develop-6bc7e7da0f5110533cd055397a111a997612245a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 6bc7e7d May 10, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.3.0/appveyor-develop-6bc7e7da0f5110533cd055397a111a997612245a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 6bc7e7d May 10, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.3.0/appveyor-develop-6bc7e7da0f5110533cd055397a111a997612245a/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 6bc7e7d May 10, 2021

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/7.3.0/circle-develop-6bc7e7da0f5110533cd055397a111a997612245a/cypress.tgz

Please sign in to comment.