2021-06-21 15:28:21 +00:00
|
|
|
# Copyright 2021 Google LLC
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
"""Implementation of a filestore using Github actions artifacts."""
|
|
|
|
import logging
|
2021-07-21 16:19:47 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tarfile
|
|
|
|
import tempfile
|
2021-06-21 15:28:21 +00:00
|
|
|
|
|
|
|
import http_utils
|
|
|
|
import filestore
|
|
|
|
from filestore.github_actions import github_api
|
|
|
|
from third_party.github_actions_toolkit.artifact import artifact_client
|
|
|
|
|
|
|
|
|
2021-07-21 16:19:47 +00:00
|
|
|
def tar_directory(directory, archive_path):
|
|
|
|
"""Tars a |directory| and stores archive at |archive_path|. |archive_path|
|
|
|
|
must end in .tar"""
|
|
|
|
assert archive_path.endswith('.tar')
|
|
|
|
# Do this because make_archive will append the extension to archive_path.
|
2021-07-21 17:19:32 +00:00
|
|
|
archive_path = os.path.splitext(archive_path)[0]
|
2021-07-21 16:19:47 +00:00
|
|
|
|
2021-07-21 17:19:32 +00:00
|
|
|
root_directory = os.path.abspath(directory)
|
2021-07-21 16:19:47 +00:00
|
|
|
shutil.make_archive(archive_path,
|
|
|
|
'tar',
|
2021-07-21 17:19:32 +00:00
|
|
|
root_dir=root_directory,
|
|
|
|
base_dir='./')
|
2021-07-21 16:19:47 +00:00
|
|
|
|
|
|
|
|
2021-06-21 15:28:21 +00:00
|
|
|
class GithubActionsFilestore(filestore.BaseFilestore):
|
|
|
|
"""Implementation of BaseFilestore using Github actions artifacts. Relies on
|
|
|
|
github_actions_toolkit for using the GitHub actions API and the github_api
|
|
|
|
module for using GitHub's standard API. We need to use both because the GitHub
|
|
|
|
actions API is the only way to upload an artifact but it does not support
|
|
|
|
downloading artifacts from other runs. The standard GitHub API does support
|
|
|
|
this however."""
|
|
|
|
|
2021-07-21 18:36:11 +00:00
|
|
|
ARTIFACT_PREFIX = 'cifuzz-'
|
|
|
|
|
2021-06-21 15:28:21 +00:00
|
|
|
def __init__(self, config):
|
|
|
|
super().__init__(config)
|
|
|
|
self.github_api_http_headers = github_api.get_http_auth_headers(config)
|
|
|
|
|
2021-07-21 18:36:11 +00:00
|
|
|
def _get_artifact_name(self, name):
|
2021-07-22 19:51:34 +00:00
|
|
|
"""Returns |name| prefixed with |self.ARITFACT_PREFIX| if it isn't already
|
|
|
|
prefixed. Otherwise returns |name|."""
|
2021-07-21 18:36:11 +00:00
|
|
|
if name.startswith(self.ARTIFACT_PREFIX):
|
|
|
|
return name
|
|
|
|
return f'{self.ARTIFACT_PREFIX}{name}'
|
|
|
|
|
2021-06-21 15:28:21 +00:00
|
|
|
def upload_directory(self, name, directory): # pylint: disable=no-self-use
|
|
|
|
"""Uploads |directory| as artifact with |name|."""
|
2021-07-22 05:02:00 +00:00
|
|
|
name = self._get_artifact_name(name)
|
2021-07-21 16:19:47 +00:00
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
archive_path = os.path.join(temp_dir, name + '.tar')
|
|
|
|
tar_directory(directory, archive_path)
|
2021-07-23 15:12:18 +00:00
|
|
|
_raw_upload_directory(name, temp_dir)
|
2021-06-21 15:28:21 +00:00
|
|
|
|
|
|
|
def download_corpus(self, name, dst_directory): # pylint: disable=unused-argument,no-self-use
|
|
|
|
"""Downloads the corpus located at |name| to |dst_directory|."""
|
|
|
|
return self._download_artifact(name, dst_directory)
|
|
|
|
|
|
|
|
def _find_artifact(self, name):
|
|
|
|
"""Finds an artifact using the GitHub API and returns it."""
|
2021-07-22 19:52:07 +00:00
|
|
|
logging.debug('Listing artifacts.')
|
2021-06-21 15:28:21 +00:00
|
|
|
artifacts = self._list_artifacts()
|
|
|
|
artifact = github_api.find_artifact(name, artifacts)
|
|
|
|
logging.debug('Artifact: %s.', artifact)
|
|
|
|
return artifact
|
|
|
|
|
|
|
|
def _download_artifact(self, name, dst_directory):
|
2021-07-23 15:00:35 +00:00
|
|
|
"""Downloads artifact with |name| to |dst_directory|. Returns True on
|
|
|
|
success."""
|
2021-07-21 18:36:11 +00:00
|
|
|
name = self._get_artifact_name(name)
|
2021-07-23 15:00:35 +00:00
|
|
|
|
2021-07-21 16:19:47 +00:00
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
2021-07-23 15:00:35 +00:00
|
|
|
if not self._raw_download_artifact(name, temp_dir):
|
|
|
|
logging.warning('Could not download artifact: %s.', name)
|
2021-07-21 16:19:47 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
artifact_tarfile_path = os.path.join(temp_dir, name + '.tar')
|
|
|
|
if not os.path.exists(artifact_tarfile_path):
|
|
|
|
logging.error('Artifact zip did not contain a tarfile.')
|
|
|
|
return False
|
|
|
|
|
|
|
|
# TODO(jonathanmetzman): Replace this with archive.unpack from
|
|
|
|
# libClusterFuzz so we can avoid path traversal issues.
|
|
|
|
with tarfile.TarFile(artifact_tarfile_path) as artifact_tarfile:
|
|
|
|
artifact_tarfile.extractall(dst_directory)
|
|
|
|
return True
|
2021-06-21 15:28:21 +00:00
|
|
|
|
2021-07-23 15:00:35 +00:00
|
|
|
def _raw_download_artifact(self, name, dst_directory):
|
|
|
|
"""Downloads the artifact with |name| to |dst_directory|. Returns True on
|
|
|
|
success. Does not do any untarring or adding prefix to |name|."""
|
|
|
|
artifact = self._find_artifact(name)
|
|
|
|
if not artifact:
|
|
|
|
logging.warning('Could not find artifact: %s.', name)
|
|
|
|
return False
|
|
|
|
download_url = artifact['archive_download_url']
|
|
|
|
return http_utils.download_and_unpack_zip(
|
|
|
|
download_url, dst_directory, headers=self.github_api_http_headers)
|
|
|
|
|
2021-06-21 15:28:21 +00:00
|
|
|
def _list_artifacts(self):
|
|
|
|
"""Returns a list of artifacts."""
|
|
|
|
return github_api.list_artifacts(self.config.project_repo_owner,
|
|
|
|
self.config.project_repo_name,
|
|
|
|
self.github_api_http_headers)
|
|
|
|
|
|
|
|
def download_latest_build(self, name, dst_directory):
|
|
|
|
"""Downloads latest build with name |name| to |dst_directory|."""
|
|
|
|
return self._download_artifact(name, dst_directory)
|
2021-07-20 17:04:57 +00:00
|
|
|
|
|
|
|
def download_coverage(self, name, dst_directory):
|
|
|
|
"""Downloads the latest project coverage report."""
|
|
|
|
return self._download_artifact(name, dst_directory)
|
2021-07-23 15:00:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _raw_upload_directory(name, directory):
|
|
|
|
"""Uploads the artifacts located in |directory| to |name|. Does not do any
|
|
|
|
tarring or adding prefixes to |name|."""
|
|
|
|
# Get file paths.
|
|
|
|
artifact_paths = []
|
|
|
|
for root, _, curr_file_paths in os.walk(directory):
|
|
|
|
for file_path in curr_file_paths:
|
|
|
|
artifact_paths.append(os.path.join(root, file_path))
|
|
|
|
logging.debug('Artifact paths: %s.', artifact_paths)
|
|
|
|
return artifact_client.upload_artifact(name, artifact_paths, directory)
|