-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
253 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright 2019 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* @fileoverview | ||
* This script performs quick checks on the source code | ||
* prior to running unit and integration tests. | ||
* This is run during the CI stage = build; job = checks. | ||
*/ | ||
|
||
const { | ||
timedExecOrDieWithDnsMonitor: timedExecOrDieWithDnsMonitorBase, | ||
} = require('./utils'); | ||
|
||
const FILENAME = 'checks.js'; | ||
const timedExecOrDieWithDnsMonitor = (cmd, unusedFileName) => | ||
timedExecOrDieWithDnsMonitorBase(cmd, FILENAME); | ||
|
||
async function main() { | ||
await timedExecOrDieWithDnsMonitor('gulp update-packages'); | ||
|
||
await timedExecOrDieWithDnsMonitor('gulp check-exact-versions'); | ||
await timedExecOrDieWithDnsMonitor('gulp lint'); | ||
await timedExecOrDieWithDnsMonitor('gulp prettify'); | ||
await timedExecOrDieWithDnsMonitor('gulp presubmit'); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const argv = require('minimist')(process.argv.slice(2)); | ||
const colors = require('ansi-colors'); | ||
const log = require('fancy-log'); | ||
const readline = require('readline'); | ||
const {execScriptAsync} = require('../common/exec'); | ||
|
||
const PERMITTED_DOMAIN_REGEXES = [ | ||
/\.internal$/, // Travis use | ||
/^(amp-test-status-bot)\.appspot\.com$/, // Infra team use | ||
/\.test$/, // .test TLD is safe to use for tests | ||
]; | ||
|
||
const TCPDUMP_DNS_REGEX = /^(\d{2}:\d{2}:\d{2})\.\d{6} IP6? [^ ]+ > [^ ]+: \d+\+ (A|AAAA)\? ([^ ]+)\. \(\d+\)$/; | ||
|
||
/** | ||
* Monitor DNS requests | ||
* @return {!Promise} | ||
*/ | ||
function dnsMonitor() { | ||
let resolver; | ||
const deferred = new Promise(resolverIn => { | ||
resolver = resolverIn; | ||
}); | ||
|
||
const monProcess = execScriptAsync('sudo tcpdump -l port 53'); | ||
console.log(monProcess.pid); | ||
monProcess.unref(); | ||
console.log(monProcess.pid); | ||
const domainAccesses = new Map(); | ||
|
||
const onFinish = () => { | ||
if (domainAccesses.size) { | ||
log( | ||
'Tests that send requests to external domains might slow', | ||
'down the tests and cause flakiness.' | ||
); | ||
log( | ||
'If possible, please change them to a *.test domain since', | ||
'they never resolve.' | ||
); | ||
log( | ||
'If these requests are indeed necessary to the test, go to', | ||
colors.cyan('build-system/tasks/dns-monitor.js'), | ||
'to permit it.' | ||
); | ||
if (!argv.nonblocking) { | ||
process.exitCode = 1; | ||
} | ||
} else { | ||
log(colors.green('No impermissible external requests found')); | ||
} | ||
for (const [domain, ts] of domainAccesses.entries()) { | ||
log(ts, 'Request(s) to', colors.red(domain), 'recorded.'); | ||
} | ||
|
||
execScriptAsync('sudo ps -ef').stdout.pipe(process.stdout); | ||
const killAll = execScriptAsync('sudo killall tcpdump'); | ||
killAll.stdout.pipe(process.stdout); | ||
killAll.stderr.pipe(process.stderr); | ||
resolver(); | ||
}; | ||
process.on('SIGTERM', () => { | ||
log(colors.red('SIGTERM')); | ||
onFinish(); | ||
}); | ||
process.on('SIGINT', onFinish); | ||
|
||
console.error('DNS Ready'); // To signal that the monitor is up. | ||
readline | ||
.createInterface({ | ||
input: monProcess.stdout, | ||
}) | ||
.on('line', line => { | ||
handleLog_(line, domainAccesses); | ||
}); | ||
return deferred; | ||
} | ||
|
||
function handleLog_(line, domainAccesses) { | ||
const match = TCPDUMP_DNS_REGEX.exec(line); | ||
if ( | ||
match && | ||
!domainAccesses.has(match[3]) && | ||
!matchOneOfRegexes(match[3], PERMITTED_DOMAIN_REGEXES) | ||
) { | ||
domainAccesses.set(match[3], match[1]); | ||
} | ||
} | ||
|
||
function matchOneOfRegexes(testStr, regexes) { | ||
for (let i = 0; i < regexes.length; i++) { | ||
if (regexes[i].exec(testStr)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
module.exports = { | ||
dnsMonitor, | ||
}; | ||
|
||
dnsMonitor.description = | ||
'Check if there are unpermitted DNS requests (requires sudo)'; | ||
dnsMonitor.flags = { | ||
'nonblocking': 'Do not block the test even if the test fails', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters