[ 
https://issues.apache.org/jira/browse/HADOOP-20091?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148442#comment-18148442
 ] 

ASF GitHub Bot commented on HADOOP-20091:
-----------------------------------------

slfan1989 commented on code in PR #9014:
URL: https://github.com/apache/hadoop/pull/9014#discussion_r4649285283


##########
dev-support/bin/mvnw-bootstrap:
##########
@@ -0,0 +1,146 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+# Downloads what ./mvnw needs before its first real use, with retries.
+#
+# mvnw fetches maven-wrapper.jar once and without retry. When that download
+# fails, it deletes the jar and then reports the misleading
+# "Failed to validate Maven wrapper SHA-256" error. maven-wrapper.jar in turn
+# downloads the Maven distribution, also without retry. This script does both
+# downloads up front and retries each of them, so that a transient failure to
+# reach the Maven repository does not fail the build.
+#
+# TODO: This is a temporary workaround. Remove this script, and the
+# "Download Maven wrapper" steps that call it in .github/workflows/, once
+# mvnw retries failed downloads itself and Hadoop has upgraded to that
+# maven-wrapper release. See https://github.com/apache/maven-wrapper/pull/445
+#
+# Environment variables:
+#   MVNW_BOOTSTRAP_ATTEMPTS  number of attempts per download (default 5)
+#   MVNW_BOOTSTRAP_DELAY     seconds to wait after the first failed attempt
+#                            (default 10); the wait doubles after each attempt
+#   MVNW_BOOTSTRAP_MIRROR    mirror of Maven Central to download
+#                            maven-wrapper.jar from when Maven Central fails;
+#                            set to an empty value to disable
+
+attempts=${MVNW_BOOTSTRAP_ATTEMPTS:-5}
+delay=${MVNW_BOOTSTRAP_DELAY:-10}
+central="https://repo.maven.apache.org/maven2/";
+mirror=${MVNW_BOOTSTRAP_MIRROR-https://maven-central.storage-download.googleapis.com/maven2}
+
+basedir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P) || exit 1
+props="${basedir}/.mvn/wrapper/maven-wrapper.properties"
+jar="${basedir}/.mvn/wrapper/maven-wrapper.jar"
+
+function property
+{
+  sed -n "s/^$1=//p" "${props}" | tr -d '[:space:]'
+}
+
+function sha256
+{
+  if command -v sha256sum >/dev/null; then
+    sha256sum "$1" | cut -d' ' -f1
+  else
+    shasum -a 256 "$1" | cut -d' ' -f1
+  fi
+}
+
+function download
+{
+  if command -v curl >/dev/null; then
+    curl -f -L -sS -o "$2" "$1"

Review Comment:
   ould we add timeouts to both the wrapper JAR download and the `./mvnw 
--version` attempt? A stalled transfer can prevent the retry loop from ever 
reaching the next attempt.



##########
dev-support/bin/mvnw-bootstrap:
##########
@@ -0,0 +1,146 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+# Downloads what ./mvnw needs before its first real use, with retries.
+#
+# mvnw fetches maven-wrapper.jar once and without retry. When that download
+# fails, it deletes the jar and then reports the misleading
+# "Failed to validate Maven wrapper SHA-256" error. maven-wrapper.jar in turn
+# downloads the Maven distribution, also without retry. This script does both
+# downloads up front and retries each of them, so that a transient failure to
+# reach the Maven repository does not fail the build.
+#
+# TODO: This is a temporary workaround. Remove this script, and the
+# "Download Maven wrapper" steps that call it in .github/workflows/, once
+# mvnw retries failed downloads itself and Hadoop has upgraded to that
+# maven-wrapper release. See https://github.com/apache/maven-wrapper/pull/445
+#
+# Environment variables:
+#   MVNW_BOOTSTRAP_ATTEMPTS  number of attempts per download (default 5)
+#   MVNW_BOOTSTRAP_DELAY     seconds to wait after the first failed attempt
+#                            (default 10); the wait doubles after each attempt
+#   MVNW_BOOTSTRAP_MIRROR    mirror of Maven Central to download
+#                            maven-wrapper.jar from when Maven Central fails;
+#                            set to an empty value to disable
+
+attempts=${MVNW_BOOTSTRAP_ATTEMPTS:-5}
+delay=${MVNW_BOOTSTRAP_DELAY:-10}
+central="https://repo.maven.apache.org/maven2/";
+mirror=${MVNW_BOOTSTRAP_MIRROR-https://maven-central.storage-download.googleapis.com/maven2}
+
+basedir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P) || exit 1
+props="${basedir}/.mvn/wrapper/maven-wrapper.properties"
+jar="${basedir}/.mvn/wrapper/maven-wrapper.jar"
+
+function property
+{
+  sed -n "s/^$1=//p" "${props}" | tr -d '[:space:]'
+}
+
+function sha256
+{
+  if command -v sha256sum >/dev/null; then
+    sha256sum "$1" | cut -d' ' -f1
+  else
+    shasum -a 256 "$1" | cut -d' ' -f1
+  fi
+}
+
+function download
+{
+  if command -v curl >/dev/null; then
+    curl -f -L -sS -o "$2" "$1"
+  else
+    wget --no-verbose -O "$2" "$1"
+  fi
+}
+
+## @description  Run a command until it succeeds, up to ${attempts} times
+## @param        description of the command, for the log
+## @param        command and its arguments
+function retry
+{
+  local what=$1
+  local attempt
+  local wait=${delay}
+  shift
+
+  for ((attempt = 1; attempt <= attempts; attempt++)); do
+    if "$@"; then
+      return 0
+    fi
+    echo "Attempt ${attempt}/${attempts} to ${what} failed" >&2
+    if [[ ${attempt} -lt ${attempts} ]]; then
+      echo "Retrying in ${wait} seconds" >&2
+      sleep "${wait}"
+      wait=$((wait * 2))
+    fi
+  done
+  echo "ERROR: Failed to ${what}" >&2
+  return 1
+}
+
+## @description  Download maven-wrapper.jar from a URL and verify it
+## @param        URL
+function fetch_wrapper_jar_from
+{
+  rm -f "${jar}.tmp"
+  # compare the checksums ourselves: `sha256sum -c` skips a malformed line
+  # and still exits 0
+  if download "$1" "${jar}.tmp"; then
+    if [[ "$(sha256 "${jar}.tmp")" == "${wrapper_sha}" ]]; then
+      mv "${jar}.tmp" "${jar}"
+      return 0
+    fi
+    echo "SHA-256 of the file downloaded from $1 is not ${wrapper_sha}" >&2
+  fi
+  rm -f "${jar}.tmp"
+  return 1
+}
+
+function fetch_wrapper_jar
+{
+  if fetch_wrapper_jar_from "${wrapper_url}"; then
+    return 0
+  fi
+  # Maven Central sometimes answers GitHub Actions runners with HTTP 429
+  # (Too Many Requests), and keeps doing so across retries. The mirror is
+  # safe to use, because the jar is verified against the checksum in
+  # maven-wrapper.properties.
+  if [[ -n "${mirror}" && "${wrapper_url}" == "${central}"* ]]; then
+    echo "Trying ${mirror}" >&2
+    fetch_wrapper_jar_from "${mirror%/}/${wrapper_url#"${central}"}"
+    return $?
+  fi
+  return 1
+}
+
+wrapper_url=$(property wrapperUrl)
+wrapper_sha=$(property wrapperSha256Sum)
+if [[ -z "${wrapper_url}" || -z "${wrapper_sha}" ]]; then
+  echo "ERROR: wrapperUrl or wrapperSha256Sum is not set in ${props}" >&2
+  exit 1
+fi
+
+if [[ -f "${jar}" && "$(sha256 "${jar}")" == "${wrapper_sha}" ]]; then
+  echo "Found ${jar}"
+else
+  retry "download ${wrapper_url}" fetch_wrapper_jar || exit 1
+fi
+
+# maven-wrapper.jar downloads and verifies the Maven distribution
+cd "${basedir}" || exit 1
+retry "run ./mvnw --version" ./mvnw --version

Review Comment:
   Could we make a distribution checksum failure trigger a fresh download on 
the next attempt? Wrapper 3.3.4 caches the ZIP before verifying it, so retrying 
`./mvnw --version` alone keeps reusing the same invalid archive.





> GitHub Actions jobs intermittently fail with "Failed to validate Maven 
> wrapper SHA-256"
> ---------------------------------------------------------------------------------------
>
>                 Key: HADOOP-20091
>                 URL: https://issues.apache.org/jira/browse/HADOOP-20091
>             Project: Hadoop Common
>          Issue Type: Improvement
>            Reporter: dzcxzl
>            Priority: Minor
>              Labels: pull-request-available
>
> GitHub Actions jobs occasionally fail at the first ./mvnw invocation with:
> {noformat}
> Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be 
> compromised.
> {noformat}
> This was seen in the "Test other" job of PR #8976 and the "Build Only Java 21 
> rockylinux_8" job of PR #8980. Other jobs in the same workflow run used the 
> same wrapper and passed, and .mvn/wrapper/maven-wrapper.properties was not 
> changed, so the checksum itself is correct.
> The cause is in mvnw (generated by maven-wrapper 3.3.4, the latest release). 
> On first use it downloads maven-wrapper.jar once and without retry. If the 
> download fails, it removes the jar and continues, and the SHA-256 validation 
> that follows fails with the message above. A transient failure to reach Maven 
> Central is therefore reported as a possible wrapper compromise. 
> maven-wrapper.jar then downloads the Maven distribution, also without retry.
> Retry support has been proposed upstream in 
> https://github.com/apache/maven-wrapper/pull/445 but is not merged or 
> released.
> Proposed fix: keep the generated mvnw unchanged and add a script, 
> dev-support/bin/mvnw-bootstrap, that downloads maven-wrapper.jar, verifies it 
> against wrapperSha256Sum, and then runs "./mvnw --version" so that the Maven 
> distribution is downloaded too, retrying each step. The GitHub Actions 
> workflows call it before the first ./mvnw.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to