def send_request(url, data): """Get a clusterfuzz url that requires authentication. Attempts to authenticate and is guaranteed to either return a valid, authorized response or throw an exception.""" header = common.get_stored_auth_header() or get_verification_header() response = None for _ in range(2): response = common.post(url=url, headers={ 'Authorization': header, 'User-Agent': 'clusterfuzz-tools' }, allow_redirects=True, data=data) if response.status_code == 401: # The access token expired. header = get_verification_header() else: # Other errors or success break if response.status_code != 200: raise common.ClusterfuzzAuthError(response.text) common.store_auth_header(response.headers[CLUSTERFUZZ_AUTH_HEADER]) return response
def send_request(url, data): """Get a clusterfuzz url that requires authentication. Attempts to authenticate and is guaranteed to either return a valid, authorized response or throw an exception.""" header = common.get_stored_auth_header() or get_verification_header() response = None for _ in range(RETRY_COUNT): response = common.post( url=url, headers={ 'Authorization': header, 'User-Agent': 'clusterfuzz-tools' }, data=data, allow_redirects=True) # The access token expired. if response.status_code == 401: header = get_verification_header() # Internal server error (e.g. due to deployment) elif response.status_code == 500: time.sleep(common.RETRY_SLEEP_TIME) continue else: # Other errors or success break if response.status_code != 200: raise error.ClusterFuzzError( response.status_code, response.text, str(response.headers.get(CLUSTERFUZZ_AUTH_IDENTITY, ''))) common.store_auth_header(response.headers[CLUSTERFUZZ_AUTH_HEADER]) return response
def get_testcase_path(self): """Downloads & returns the location of the testcase file.""" testcase_dir = self.testcase_dir_name() filename = os.path.join(testcase_dir, 'testcase%s' % self.file_extension) common.delete_if_exists(testcase_dir) os.makedirs(testcase_dir) logger.info('Downloading testcase data...') auth_header = common.get_stored_auth_header() # Do not use curl because curl doesn't support downloading an empty file. # See: https://github.com/google/clusterfuzz-tools/issues/326 args = ( '--no-verbose --waitretry=%s --retry-connrefused --content-disposition ' '--header="Authorization: %s" "%s"' % (DOWNLOAD_TIMEOUT, auth_header, CLUSTERFUZZ_TESTCASE_URL % self.id)) common.execute('wget', args, testcase_dir) downloaded_filename = os.listdir(testcase_dir)[0] filename = self.get_true_testcase_path(downloaded_filename) return filename
def test_file_valid(self): """Tests when file is accessible and auth key is returned.""" self.fs.CreateFile(common.AUTH_HEADER_FILE, contents='Bearer 1234') os.chmod(common.AUTH_HEADER_FILE, stat.S_IWUSR | stat.S_IRUSR) result = common.get_stored_auth_header() self.assertEqual(result, 'Bearer 1234')
def test_permissions_incorrect(self): """Tests functionality when file exists but permissions wrong.""" self.fs.CreateFile(common.AUTH_HEADER_FILE) os.chmod(common.AUTH_HEADER_FILE, stat.S_IWGRP) with self.assertRaises(error.PermissionsTooPermissiveError) as ex: result = common.get_stored_auth_header() self.assertEqual(result, None) self.assertIn('File permissions too permissive to open', ex.exception.message)
def download_testcase(url): """Download the testcase into dest_dir.""" tmp_dir_path = tempfile.mkdtemp(dir=common.CLUSTERFUZZ_TMP_DIR) logger.info('Downloading testcase files...') auth_header = common.get_stored_auth_header() # Do not use curl because curl doesn't support downloading an empty file. # See: https://github.com/google/clusterfuzz-tools/issues/326 args = ( '--no-verbose --waitretry=%s --retry-connrefused --content-disposition ' '--header="Authorization: %s" "%s"' % (DOWNLOAD_TIMEOUT, auth_header, url)) common.execute('wget', args, tmp_dir_path) return os.path.join(tmp_dir_path, os.listdir(tmp_dir_path)[0])
def send_request(url): """Get a clusterfuzz url that requires authentication. Attempts to authenticate and is guaranteed to either return a valid, authorized response or throw an exception.""" header = common.get_stored_auth_header() response = None for _ in range(2): if not header or (response and response.status == 401): header = get_verification_header() response = urlfetch.fetch(url=url, headers={'Authorization': header}) if response.status == 200: break if response.status != 200: raise common.ClusterfuzzAuthError(response.body) common.store_auth_header(response.headers[CLUSTERFUZZ_AUTH_HEADER]) return response
def get_testcase_path(self): """Downloads & returns the location of the testcase file.""" testcase_dir = self.testcase_dir_name() #TODO: Filename testcase.js is d8-specific filename = os.path.join(testcase_dir, 'testcase.js') if os.path.isfile(filename): return filename print 'Downloading testcase data...' if not os.path.exists(CLUSTERFUZZ_TESTCASES_DIR): os.makedirs(CLUSTERFUZZ_TESTCASES_DIR) os.makedirs(testcase_dir) auth_header = common.get_stored_auth_header() command = 'wget --header="Authorization: %s" "%s" -O ./testcase.js' % ( auth_header, CLUSTERFUZZ_TESTCASE_URL % self.id) common.execute(command, testcase_dir) return filename
def get_testcase_path(self): """Downloads & returns the location of the testcase file.""" testcase_dir = self.testcase_dir_name() filename = os.path.join(testcase_dir, 'testcase%s' % self.file_extension) common.delete_if_exists(testcase_dir) os.makedirs(testcase_dir) logger.info('Downloading testcase data...') auth_header = common.get_stored_auth_header() args = ( '--remote-name --remote-header-name --retry-max-time 30 --retry 5 ' '--silent --show-error --fail --location ' '-H "Authorization: %s" %s' % (auth_header, CLUSTERFUZZ_TESTCASE_URL % self.id)) common.execute('curl', args, testcase_dir) downloaded_filename = os.listdir(testcase_dir)[0] filename = self.get_true_testcase_path(downloaded_filename) return filename
def get_testcase_path(self): """Downloads & returns the location of the testcase file.""" testcase_dir = self.testcase_dir_name() filename = os.path.join(testcase_dir, 'testcase%s' % self.file_extension) common.delete_if_exists(testcase_dir) logger.info('Downloading testcase data...') if not os.path.exists(common.CLUSTERFUZZ_TESTCASES_DIR): os.makedirs(common.CLUSTERFUZZ_TESTCASES_DIR) os.makedirs(testcase_dir) auth_header = common.get_stored_auth_header() args = '--content-disposition --header="Authorization: %s" "%s"' % ( auth_header, CLUSTERFUZZ_TESTCASE_URL % self.id) common.execute('wget', args, testcase_dir) downloaded_filename = os.listdir(testcase_dir)[0] filename = self.get_true_testcase_path(downloaded_filename) return filename
def test_file_missing(self): """Tests functionality when auth key file does not exist.""" result = common.get_stored_auth_header() self.assertEqual(result, None)