diff --git a/bootstrap_sdk b/bootstrap_sdk index 0c636beda2b..24c940e02e8 100755 --- a/bootstrap_sdk +++ b/bootstrap_sdk @@ -54,11 +54,6 @@ EOF catalyst_stage_default 4 } -# Switch to HTTP because early boostrap stages do not have SSL support. -GENTOO_MIRRORS=$(portageq envvar GENTOO_MIRRORS) -GENTOO_MIRRORS="${GENTOO_MIRRORS//https:\/\//http://}" -export GENTOO_MIRRORS - catalyst_init "$@" ROOT_OVERLAY=${TEMPDIR}/stage4_overlay diff --git a/bootstrap_sdk_container b/bootstrap_sdk_container index 49834cbf40f..72c0bee26fd 100755 --- a/bootstrap_sdk_container +++ b/bootstrap_sdk_container @@ -81,7 +81,7 @@ fi # bootstrap_sdk needs FLATCAR_SDK_VERSION set to the seed version failed='' -./run_sdk_container "${cleanup[@]}" -V "$seed_version" -v "$target_version" \ +./run_sdk_container "${cleanup[@]}" -V "$seed_version" -v "$target_version" -e "${!RCLONE_S3_*}" \ sudo -E ./bootstrap_sdk "${bootstrap_args[@]}" || failed=x # Update versionfile to the actual SDK version diff --git a/build_library/catalyst.sh b/build_library/catalyst.sh index 475486913f1..821d4b394e0 100644 --- a/build_library/catalyst.sh +++ b/build_library/catalyst.sh @@ -62,6 +62,7 @@ sharedir="/usr/share/catalyst" storedir="$CATALYST_ROOT" distdir="$DISTDIR" envscript="$TEMPDIR/catalystrc" +envvars=[$(printf '"%s", ' $(< sdk_lib/env_rclone.txt))] port_logdir="$CATALYST_ROOT/log" repo_basedir="/mnt/host/source/src/third_party" repo_name="gentoo" @@ -69,14 +70,14 @@ EOF } catalystrc() { -local load=$((NUM_JOBS * 2)) -cat <&2 + exit +fi + +while [[ 0 -lt $# ]]; do + case $1 in + --output-document=*) + file=${1#*=}; shift ;; + --output-document|-O) + file=${2-}; shift 2 ;; + -*) + shift ;; + *) + url=$1; shift ;; + esac +done + +if [[ ${file-} != /*.__download__ || ${url-} != *://* ]]; then + echo "* Could not parse wget arguments. Cannot mirror." >&2 + exit +fi + +if [[ ${url} == */layout.conf ]]; then + # distfiles layout configuration shouldn't be mirrored. + exit +fi + +# GENTOO_MIRRORS is filtered by Portage. We cannot get the real value if it has +# been overridden, so don't do that when the S3 credentials are set. +GENTOO_MIRRORS=$(unset PORTAGE_REPOSITORIES; portageq envvar GENTOO_MIRRORS) +GENTOO_MIRRORS=${GENTOO_MIRRORS%% *} + +# This shouldn't happen, but if it does, it would cost money, so double check. +if [[ ${GENTOO_MIRRORS} != https://mirror.release.flatcar-linux.net ]]; then + echo "* GENTOO_MIRRORS has been modified. Cannot mirror." >&2 + exit +fi + +if [[ ${url} == "${GENTOO_MIRRORS}"/distfiles/* ]]; then + # Already mirrored. + exit +fi + +if ! [[ + ${RCLONE_S3_PROVIDER-} == Cloudflare && \ + ${RCLONE_S3_ACCESS_KEY_ID-} && \ + ${RCLONE_S3_SECRET_ACCESS_KEY-} && \ + ${RCLONE_S3_ENDPOINT-} \ +]]; then + echo "* Cloudflare credentials not present. Cannot mirror." >&2 + exit +fi + +basename=${file##*/} +basename=${basename%.__download__} + +# Upstream Gentoo uses the filename-hash layout, but this has little benefit +# over the flat layout when hosted on Cloudflare R2. However, the content-hash +# layout handles different versions of files with the same filename, which could +# be useful when maintaining several Flatcar release channels. +path=$(python3 - "${EBUILD%/*}" "${basename}" <<-'EOF' + import sys + from portage.manifest import Manifest + from portage.package.ebuild.fetch import ContentHashLayout, DistfileName + + dir, file = sys.argv[1:] + digests = Manifest(dir).getTypeDigests("DIST")[file] + filename = DistfileName(file, digests=digests) + print(ContentHashLayout("BLAKE2B", "8").get_path(filename)) +EOF +) || : + +if [[ -z ${path} ]]; then + echo "* Could not determine content-hash path for ${basename}. Cannot mirror." >&2 + exit +fi + +# The last part of the path should be the BLAKE2B digest. +if ! b2sum --check --status <<< "${path##*/} *${file}"; then + echo "* Checksum mismatch for ${basename}. Refusing to mirror." >&2 + exit +fi + +echo "* Attempting to mirror ${basename} ..." >&2 + +curl \ + --location \ + --fail-with-body \ + --aws-sigv4 "aws:amz:auto:s3" \ + --variable %RCLONE_S3_ACCESS_KEY_ID \ + --variable %RCLONE_S3_SECRET_ACCESS_KEY \ + --expand-user "{{RCLONE_S3_ACCESS_KEY_ID}}:{{RCLONE_S3_SECRET_ACCESS_KEY}}" \ + --upload-file "${file}" \ + "${RCLONE_S3_ENDPOINT}/flatcar/mirror/distfiles/${path}" >&2 || : + +echo >&2 diff --git a/ci-automation/packages.sh b/ci-automation/packages.sh index 137ec48857b..0bf03320d61 100644 --- a/ci-automation/packages.sh +++ b/ci-automation/packages.sh @@ -38,6 +38,8 @@ # # 4. A file ../scripts.patch to apply with "git am -3" for the scripts repo. # +# 5. RCLONE_S3_*. Environment variables. Credentials for mirroring Portage distfiles. See rclone's documentation. +# # OUTPUT: # # 1. Exported container image "flatcar-packages-[ARCH]-[VERSION].tar.gz" with binary packages @@ -91,7 +93,7 @@ function _packages_build_impl() { # Build packages; store packages in container failed='' ./run_sdk_container -x ./ci-cleanup.sh -n "${packages_container}" -v "${vernum}" \ - -C "${sdk_image}" \ + -C "${sdk_image}" -e "${!RCLONE_S3_*}" \ ./build_packages --board="${arch}-usr" || failed=x # Copy the build logs to a directory accessible for us. diff --git a/ci-automation/release.sh b/ci-automation/release.sh index 678fde80341..86b9a37763f 100644 --- a/ci-automation/release.sh +++ b/ci-automation/release.sh @@ -156,41 +156,26 @@ function copy_from_bincache_to_bucket() { local arch="${2}" local version="${3}" - rclone_configuration_file="$(mktemp)" - chmod 600 "${rclone_configuration_file}" - - ( - trap "rm -f ${rclone_configuration_file}" EXIT - echo "${RCLONE_CONFIGURATION_FILE}" | base64 --decode > "${rclone_configuration_file}" - - echo "Copying the images from bincache to CloudFlare bucket" - docker run --rm --net host \ - -v "${rclone_configuration_file}:/opt/rclone.conf:ro" \ - docker.io/rclone/rclone:1.71.1 \ - --config "/opt/rclone.conf" \ + echo "Copying the images from bincache to Cloudflare bucket" + docker run --rm --net host --env-file="sdk_lib/env_rclone.txt" \ + docker.io/rclone/rclone:1.76.0 \ sync \ - --http-url "https://${BUILDCACHE_SERVER}/images/${arch}/${version}" :http: "r2:flatcar/${channel}/${arch}-usr/${version}" || { echo "ERROR: Skipping images copy to bucket due to failure" ; } + --http-url "https://${BUILDCACHE_SERVER}/images/${arch}/${version}" :http: ":s3:flatcar/${channel}/${arch}-usr/${version}" || { echo "ERROR: Skipping images copy to bucket due to failure" ; } - echo "Copying the binary packages from bincache to CloudFlare bucket" - docker run --rm --net host \ - -v "${rclone_configuration_file}:/opt/rclone.conf:ro" \ - docker.io/rclone/rclone:1.71.1 \ - --config "/opt/rclone.conf" \ + echo "Copying the binary packages from bincache to Cloudflare bucket" + docker run --rm --net host --env-file="sdk_lib/env_rclone.txt" \ + docker.io/rclone/rclone:1.76.0 \ sync \ - --http-url "https://${BUILDCACHE_SERVER}/boards/${arch}-usr/${version}" :http: "r2:flatcar/mirror/boards/${arch}-usr/${version}" || { echo "ERROR: Skipping binary packages copy to bucket due to failure" ; } + --http-url "https://${BUILDCACHE_SERVER}/boards/${arch}-usr/${version}" :http: ":s3:flatcar/mirror/boards/${arch}-usr/${version}" || { echo "ERROR: Skipping binary packages copy to bucket due to failure" ; } # Only copy once the 'containers' artifacts if [ "${arch}" = "amd64" ]; then - echo "Copying SDK and packages containers from bincache to CloudFlare bucket" - docker run --rm --net host \ - -v "${rclone_configuration_file}:/opt/rclone.conf:ro" \ - docker.io/rclone/rclone:1.71.1 \ - --config "/opt/rclone.conf" \ + echo "Copying SDK and packages containers from bincache to Cloudflare bucket" + docker run --rm --net host --env-file="sdk_lib/env_rclone.txt" \ + docker.io/rclone/rclone:1.76.0 \ sync \ - --http-url "https://${BUILDCACHE_SERVER}/containers/${version}" :http: "r2:flatcar/mirror/containers/${version}" || { echo "ERROR: Skipping containers copy (SDK / packages) to bucket due to failure" ; } + --http-url "https://${BUILDCACHE_SERVER}/containers/${version}" :http: ":s3:flatcar/mirror/containers/${version}" || { echo "ERROR: Skipping containers copy (SDK / packages) to bucket due to failure" ; } fi - - ) } function publish_sdk() { @@ -215,21 +200,11 @@ function publish_sdk() { docker push "${sdk_container_common_registry}/flatcar-sdk-${a}:${docker_sdk_vernum}" done - rclone_configuration_file="$(mktemp)" - chmod 600 "${rclone_configuration_file}" - - ( - trap "rm -f ${rclone_configuration_file}" EXIT - echo "${RCLONE_CONFIGURATION_FILE}" | base64 --decode > "${rclone_configuration_file}" - - echo "Copying the SDK from bincache to CloudFlare bucket" - docker run --rm --net host \ - -v "${rclone_configuration_file}:/opt/rclone.conf:ro" \ - docker.io/rclone/rclone:1.71.1 \ - --config "/opt/rclone.conf" \ + echo "Copying the SDK from bincache to Cloudflare bucket" + docker run --rm --net host --env-file="sdk_lib/env_rclone.txt" \ + docker.io/rclone/rclone:1.76.0 \ sync \ - --http-url "https://${BUILDCACHE_SERVER}/sdk/amd64/${docker_sdk_vernum}" :http: "r2:flatcar/mirror/sdk/amd64/${docker_sdk_vernum}" || { echo "ERROR: Skipping SDK copy to bucket due to failure" ; } - ) + --http-url "https://${BUILDCACHE_SERVER}/sdk/amd64/${docker_sdk_vernum}" :http: ":s3:flatcar/mirror/sdk/amd64/${docker_sdk_vernum}" || { echo "ERROR: Skipping SDK copy to bucket due to failure" ; } } function _release_build_impl() { diff --git a/ci-automation/sdk_bootstrap.sh b/ci-automation/sdk_bootstrap.sh index 6465b0e1f41..d96ff9845d7 100644 --- a/ci-automation/sdk_bootstrap.sh +++ b/ci-automation/sdk_bootstrap.sh @@ -45,6 +45,8 @@ # 8. SEED_TARBALL. Environment variable. Overrides the default seed tarball # from the given seed SDK. A Gentoo stage3 tarball URL can be used here. # +# 9. RCLONE_S3_*. Environment variables. Credentials for mirroring Portage distfiles. See rclone's documentation. +# # OUTPUT: # # 1. SDK tarball (gentoo catalyst output) of the new SDK, pushed to buildcache. diff --git a/sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/make.defaults b/sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/make.defaults index 2c5df4df477..583ff45de27 100644 --- a/sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/make.defaults +++ b/sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/make.defaults @@ -71,11 +71,13 @@ USE="${USE} bindist" ACCEPT_LICENSE="${ACCEPT_LICENSE} no-source-code linux-fw-redistributable freedist intel-ucode NVIDIA-2023 NVIDIA-2025" -# Favor our own mirrors over Gentoo's -GENTOO_MIRRORS=" - https://mirror.release.flatcar-linux.net/coreos/ - http://distfiles.gentoo.org/ -" +# Flatcar's mirror has sources from all recent CI builds. Ideally, we wouldn't +# include Gentoo's mirror because we don't want to spam them with requests for +# Flatcar-only sources, but their own gitweb instance is the upstream source for +# many packages and is currently very unreliable. Other mirrors presumably +# manage to download these after several tries. *DO NOT* add extra whitespace +# here. If this is modified, you *MUST* update the fetch-wrapper script. +GENTOO_MIRRORS="https://mirror.release.flatcar-linux.net https://distfiles.gentoo.org" # The following files are modified by make_chroot, and we do not want to # clobber them. All other config files should not be protected, so we diff --git a/sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/profile.bashrc b/sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/profile.bashrc index 3f0583dbc53..9c5b19a5053 100644 --- a/sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/profile.bashrc +++ b/sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/profile.bashrc @@ -3,6 +3,10 @@ CROS_BUILD_BOARD_TREE="${SYSROOT}/build" CROS_ADDONS_TREE="/mnt/host/source/src/third_party/coreos-overlay/coreos" +# Ensure sensitive credentials used for mirroring don't make it into the build +# environment where they could be stolen. fetch-wrapper is unaffected by this. +unset "${!RCLONE_S3_@}" + # Are we merging for the board sysroot, or for the SDK, or for # the images? Returns a string in a passed variable: # diff --git a/sdk_lib/Dockerfile.sdk-import b/sdk_lib/Dockerfile.sdk-import index 7f3445b25e8..e95147b14a5 100644 --- a/sdk_lib/Dockerfile.sdk-import +++ b/sdk_lib/Dockerfile.sdk-import @@ -29,7 +29,7 @@ COPY --chown=sdk:sdk . /mnt/host/source/src/scripts RUN chown sdk:sdk /mnt/host/source RUN --mount=type=bind,target=/mnt/host/source/src/scripts \ - cat /mnt/host/source/src/scripts/sdk_lib/env_{pass,keep}.txt | \ + cat /mnt/host/source/src/scripts/sdk_lib/env_{pass,keep,rclone}.txt | \ sed -e '1i Defaults env_keep += " \\' -e 's:$: \\:' -e '$a "' | \ install -m0440 /dev/stdin /etc/sudoers.d/90_env_keep diff --git a/sdk_lib/env_rclone.txt b/sdk_lib/env_rclone.txt new file mode 100644 index 00000000000..dcbb4da1db0 --- /dev/null +++ b/sdk_lib/env_rclone.txt @@ -0,0 +1,4 @@ +RCLONE_S3_ACCESS_KEY_ID +RCLONE_S3_ENDPOINT +RCLONE_S3_PROVIDER +RCLONE_S3_SECRET_ACCESS_KEY diff --git a/setup_board b/setup_board index 561ec016401..6d35d491b75 100755 --- a/setup_board +++ b/setup_board @@ -295,7 +295,7 @@ sudo_clobber "${BOARD_ETC}/portage/make.conf" <