geyanggang commented on code in PR #14778:
URL: https://github.com/apache/gravitino/pull/14778#discussion_r4547511240


##########
.github/workflows/cve-scan.yml:
##########
@@ -0,0 +1,345 @@
+#
+# 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.
+#
+name: CVE Scan
+
+# Weekly Trivy scan of the artifacts Gravitino ships. See 
.github/trivy/README.md for what is
+# scanned, how to run the same scan locally and how exemptions work.
+#
+# A target (one shipped artifact) fails when a HIGH or CRITICAL finding 
remains after the
+# exemptions in .github/trivy are applied, or when Trivy produced no valid 
report. Expired
+# exemptions bring their findings back, so a red run is also the reminder to 
re-triage them.
+on:
+  schedule:
+    - cron: '0 3 * * 1' # Mondays 03:00 UTC
+  workflow_dispatch:
+
+permissions:
+  contents: read
+
+concurrency:
+  group: cve-scan-${{ github.ref }}
+  cancel-in-progress: false
+
+env:
+  TRIVY_REPORT_DIR: ${{ github.workspace }}/trivy-reports
+  TARGET_DIR: ${{ github.workspace }}/trivy-targets
+
+jobs:
+  # Downloads the vulnerability DBs once per run. The scan jobs restore them 
read-only and run
+  # without network access.
+  trivy-db:
+    name: Trivy DB
+    # Forks can start the workflow manually, but only apache/gravitino runs 
the schedule.
+    if: github.event_name != 'schedule' || github.repository == 
'apache/gravitino'
+    runs-on: ubuntu-latest
+    timeout-minutes: 20
+    outputs:
+      cache-key: ${{ steps.key.outputs.cache-key }}
+    steps:
+      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # 
v7.0.1
+        with:
+          persist-credentials: false
+      - name: Compute the cache key once for the whole run
+        id: key
+        run: echo "cache-key=trivy-db-v1-$(date -u +%Y-%m-%d)" >> 
"${GITHUB_OUTPUT}"
+      - name: Restore the DB cache
+        id: cache
+        uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4
+        with:
+          path: ~/.cache/trivy
+          key: ${{ steps.key.outputs.cache-key }}
+      - name: Download the DBs
+        if: steps.cache.outputs.cache-hit != 'true'
+        run: |
+          .github/trivy/scan.sh pull
+          .github/trivy/scan.sh download-db
+      - name: Record the DB snapshot
+        run: .github/trivy/scan.sh db-info
+
+  server:
+    name: Server packages
+    if: github.event_name != 'schedule' || github.repository == 
'apache/gravitino'
+    needs: trivy-db
+    runs-on: ubuntu-latest
+    timeout-minutes: 90
+    steps:
+      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # 
v7.0.1
+        with:
+          persist-credentials: false
+      - uses: ./.github/actions/setup-java-toolchains
+      - name: Free up disk space
+        run: dev/ci/util_free_space.sh
+      - name: Restore the DB cache
+        uses: actions/cache/restore@668228422ae6a00e4ad889ee87cd7109ec5666a7 # 
v5.0.4
+        with:
+          path: ~/.cache/trivy
+          key: ${{ needs.trivy-db.outputs.cache-key }}
+          fail-on-cache-miss: true
+      - name: Pull the Trivy image
+        run: .github/trivy/scan.sh pull
+      # The web UI is not built: its WAR holds a minified static export that 
Trivy cannot inspect.
+      # Its lock files are scanned in the lockfiles job (see 
.github/trivy/README.md).
+      - name: Build the server packages
+        run: ./gradlew compileDistribution compileIcebergRESTServer 
compileLanceRESTServer -PskipWeb=true -x test
+      # The standalone REST server directories hold the same third-party jars 
as the copies in
+      # distribution/package; they are scanned too so that each shipped 
package has its own report.
+      - name: Scan
+        run: |
+          rc=0
+          .github/trivy/scan.sh scan server-distribution distribution/package 
|| rc=1
+          .github/trivy/scan.sh scan iceberg-rest-server 
distribution/gravitino-iceberg-rest-server || rc=1
+          .github/trivy/scan.sh scan lance-rest-server 
distribution/gravitino-lance-rest-server || rc=1
+          exit "${rc}"
+      - name: Upload the reports
+        if: ${{ !cancelled() }}
+        uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 
# v7.0.2
+        with:
+          name: cve-scan-server
+          path: ${{ env.TRIVY_REPORT_DIR }}
+          retention-days: 30
+          if-no-files-found: error
+
+  # Every separately distributed fat jar. Each jar is staged in its own 
directory and scanned as
+  # its own target, so findings stay attributable to the artifact users 
download.
+  jars:
+    name: Shaded jars
+    if: github.event_name != 'schedule' || github.repository == 
'apache/gravitino'
+    needs: trivy-db
+    runs-on: ubuntu-latest
+    timeout-minutes: 90
+    steps:
+      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # 
v7.0.1
+        with:
+          persist-credentials: false
+      - uses: ./.github/actions/setup-java-toolchains
+      - name: Free up disk space
+        run: dev/ci/util_free_space.sh
+      - name: Restore the DB cache
+        uses: actions/cache/restore@668228422ae6a00e4ad889ee87cd7109ec5666a7 # 
v5.0.4
+        with:
+          path: ~/.cache/trivy
+          key: ${{ needs.trivy-db.outputs.cache-key }}
+          fail-on-cache-miss: true
+      - name: Pull the Trivy image
+        run: .github/trivy/scan.sh pull
+      - name: Build the fat jars
+        run: |
+          ./gradlew -PskipWeb=true \
+            :bundles:aws-bundle:shadowJar :bundles:gcp-bundle:shadowJar \
+            :bundles:azure-bundle:shadowJar :bundles:aliyun-bundle:shadowJar \
+            :bundles:tencent-bundle:shadowJar 
:bundles:iceberg-aws-bundle:shadowJar \
+            :bundles:iceberg-gcp-bundle:shadowJar 
:bundles:iceberg-azure-bundle:shadowJar \
+            :bundles:iceberg-aliyun-bundle:shadowJar \
+            :clients:client-java-runtime:shadowJar 
:clients:filesystem-hadoop3-runtime:shadowJar \
+            :maintenance:jobs:shadowJar \
+            :flink-connector:flink-runtime-1.19:shadowJar 
:flink-connector:flink-runtime-1.20:shadowJar \
+            :flink-connector:flink-runtime-2.1:shadowJar 
:flink-connector:flink-runtime-2.2:shadowJar \
+            :spark-connector:spark-runtime-3.5:shadowJar 
:spark-connector:spark-runtime-4.0:shadowJar
+      # The Spark 3.5 runtime is also released for Scala 2.13. Spark 4.0 is 
Scala 2.13 only.
+      - name: Build the Spark 3.5 runtime for Scala 2.13
+        run: ./gradlew -PskipWeb=true -PscalaVersion=2.13 
:spark-connector:spark-runtime-3.5:shadowJar
+      # Every jar is staged even if another one cannot be found, and the step 
fails at the end. The
+      # Scan step then still scans and reports what was staged.
+      - name: Stage one directory per jar
+        id: stage
+        run: |
+          rc=0
+          stage() { .github/trivy/scan.sh stage "${TARGET_DIR}" "$@" || rc=1; }
+          for cloud in aws gcp azure aliyun tencent iceberg-aws iceberg-gcp 
iceberg-azure iceberg-aliyun; do
+            stage "${cloud}-bundle" 
"bundles/${cloud}-bundle/build/libs/gravitino-${cloud}-bundle-*.jar"
+          done
+          stage client-java-runtime 
'clients/client-java-runtime/build/libs/gravitino-client-java-runtime-*.jar'
+          stage filesystem-hadoop3-runtime 
'clients/filesystem-hadoop3-runtime/build/libs/gravitino-filesystem-hadoop3-runtime-*.jar'
+          stage jobs 'maintenance/jobs/build/libs/gravitino-jobs-*.jar'
+          for flink in 1.19 1.20 2.1 2.2; do
+            stage "flink-runtime-${flink}" 
"flink-connector/v${flink}/flink-runtime/build/libs/gravitino-flink-connector-runtime-${flink}*.jar"
+          done
+          stage spark-runtime-3.5_2.12 
'spark-connector/v3.5/spark-runtime/build/libs/gravitino-spark-connector-runtime-3.5_2.12-*.jar'
+          stage spark-runtime-3.5_2.13 
'spark-connector/v3.5/spark-runtime/build/libs/gravitino-spark-connector-runtime-3.5_2.13-*.jar'
+          stage spark-runtime-4.0 
'spark-connector/v4.0/spark-runtime/build/libs/gravitino-spark-connector-runtime-4.0_2.13-*.jar'
+          exit "${rc}"
+      # Runs after a failed Stage step too, but not when the build already 
failed.
+      - name: Scan
+        if: ${{ !cancelled() && steps.stage.conclusion != 'skipped' }}
+        run: .github/trivy/scan.sh scan-all "${TARGET_DIR}"
+      # A report directory is missing only after a failed build, stage or scan 
step, and that step
+      # already fails the job. 'warn' keeps a second, confusing failure out of 
the run, while a
+      # partial result (some jars staged) is still uploaded.
+      - name: Upload the reports
+        if: ${{ !cancelled() }}
+        uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 
# v7.0.2
+        with:
+          name: cve-scan-jars
+          path: ${{ env.TRIVY_REPORT_DIR }}
+          retention-days: 30
+          if-no-files-found: warn
+
+  # One target per Trino version range. Trino 473 and later need JDK 25, which 
Gradle provisions
+  # through its toolchain resolver, as in the Trino integration tests.
+  trino:
+    name: Trino connectors
+    if: github.event_name != 'schedule' || github.repository == 
'apache/gravitino'
+    needs: trivy-db
+    runs-on: ubuntu-latest
+    timeout-minutes: 120
+    steps:
+      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # 
v7.0.1
+        with:
+          persist-credentials: false
+      - uses: ./.github/actions/setup-java-toolchains
+      - name: Free up disk space
+        run: dev/ci/util_free_space.sh
+      - name: Restore the DB cache
+        uses: actions/cache/restore@668228422ae6a00e4ad889ee87cd7109ec5666a7 # 
v5.0.4
+        with:
+          path: ~/.cache/trivy
+          key: ${{ needs.trivy-db.outputs.cache-key }}
+          fail-on-cache-miss: true
+      - name: Pull the Trivy image
+        run: .github/trivy/scan.sh pull
+      - name: Build the connector libraries
+        run: |
+          ./gradlew -PskipWeb=true \
+            :trino-connector:trino-connector-469-472:copyRuntimeLibs \
+            :trino-connector:trino-connector-473-477:copyRuntimeLibs \
+            :trino-connector:trino-connector-478-479:copyRuntimeLibs \
+            :trino-connector:trino-connector-480:copyRuntimeLibs \
+            :trino-connector:trino-connector-481:copyRuntimeLibs \
+            :trino-connector:trino-connector-482-483:copyRuntimeLibs
+      - name: Scan
+        run: |
+          rc=0
+          for range in 469-472 473-477 478-479 480 481 482-483; do
+            .github/trivy/scan.sh scan "trino-connector-${range}" 
"trino-connector/trino-connector-${range}/build/libs" || rc=1
+          done
+          exit "${rc}"
+      - name: Upload the reports
+        if: ${{ !cancelled() }}
+        uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 
# v7.0.2
+        with:
+          name: cve-scan-trino
+          path: ${{ env.TRIVY_REPORT_DIR }}
+          retention-days: 30
+          if-no-files-found: error
+
+  # Lock files that pin what is installed or compiled into a shipped artifact. 
Each lock file is
+  # copied into its own directory and scanned as its own target (see 
.github/trivy/README.md).
+  lockfiles:
+    name: Lock files
+    if: github.event_name != 'schedule' || github.repository == 
'apache/gravitino'
+    needs: trivy-db
+    runs-on: ubuntu-latest
+    timeout-minutes: 30
+    steps:
+      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # 
v7.0.1
+        with:
+          persist-credentials: false
+      - name: Restore the DB cache
+        uses: actions/cache/restore@668228422ae6a00e4ad889ee87cd7109ec5666a7 # 
v5.0.4
+        with:
+          path: ~/.cache/trivy
+          key: ${{ needs.trivy-db.outputs.cache-key }}
+          fail-on-cache-miss: true
+      - name: Pull the Trivy image
+        run: .github/trivy/scan.sh pull
+      # Trivy leaves out the development dependencies of pnpm lock files by 
default, so only what
+      # the web UI ships is gated. The Python client pins its dependencies 
with '=='; the
+      # requirements files of its extras are merged into one file, without the 
dev and test ones.
+      - name: Stage the lock files
+        id: stage
+        run: |
+          rc=0
+          stage() { mkdir -p "${TARGET_DIR}/$1" && cp "$2" "${TARGET_DIR}/$1/" 
|| rc=1; }
+          stage mcp-server mcp-server/uv.lock
+          stage web web/web/pnpm-lock.yaml
+          stage web-v2 web-v2/web/pnpm-lock.yaml
+          .github/trivy/scan.sh stage-requirements clients/client-python 
"${TARGET_DIR}/client-python" || rc=1

Review Comment:
   Fixed in bdfbe16cf. The Stage step now resolves the package with pip 
(`scan.sh stage-python`) in a throwaway virtualenv, with every extra except 
`dev` read from the package metadata, and scans the resolved `name==version` 
list, including transitive dependencies. It works with pins and with ranges, so 
it no longer depends on whether this PR or #14206 merges first. It fails when 
pip cannot resolve the package or no dependency is left. Offline tests are in 
`cve-scan-stage-python.test.cjs`.



##########
.github/trivy/scan.sh:
##########
@@ -0,0 +1,503 @@
+#!/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.
+#
+# Runs Trivy (in a hardened container) against built Gravitino artifacts. The 
same script is used
+# by .github/workflows/cve-scan.yml and by contributors on their machines.
+#
+# Usage:
+#   scan.sh pull                       Pull the pinned Trivy image (with 
retries).
+#   scan.sh download-db                Download the vulnerability and Java DBs 
into the cache dir.
+#   scan.sh db-info                    Print the snapshot dates of the cached 
DBs (job summary too).
+#   scan.sh scan NAME PATH [TYPE]      Scan one target directory. TYPE is 
rootfs (default) or fs.
+#   scan.sh scan-all DIR [TYPE]        Scan every sub-directory of DIR as its 
own target.
+#   scan.sh stage DIR NAME GLOB        Copy the one jar matching GLOB (not 
*-empty.jar) to DIR/NAME.
+#   scan.sh stage-requirements SRC DEST  Merge the pinned Python client 
requirements; fail if none.
+#   scan.sh unused REPORT_DIR          Warn about exemptions that matched 
nothing in the reports.
+#   scan.sh check-exemptions           Fail when an exemption violates the 
policy in README.md.
+#
+# A target fails when a HIGH or CRITICAL finding remains after the exemptions 
are applied, or when
+# Trivy did not produce a valid report (fail closed). Reports are written to 
$TRIVY_REPORT_DIR as
+# NAME.json (raw), NAME.sarif and NAME.md. A target also fails when its SARIF 
or Markdown report
+# cannot be produced, in both 'scan' and 'scan-all'. Only 'unused' and 
'check-exemptions' need yq (v4).
+#
+# Environment (all optional):
+#   TRIVY_IMAGE         Pinned scanner image (defaults to the digest pinned 
below).
+#   TRIVY_CACHE_DIR     Vulnerability DB cache directory (default: 
$HOME/.cache/trivy).
+#   TRIVY_REPORT_DIR    Output directory (default: ./trivy-reports).
+#   TRIVY_POLICY_DIR    Directory holding trivyignore.yaml and vex.openvex.json
+#                       (default: the directory of this script).
+#   TRIVY_EXEMPTIONS    Set to "off" to scan without exemptions (used to 
generate a baseline).
+#   GITHUB_STEP_SUMMARY When set, the Markdown report is appended to it as 
well.
+
+set -euo pipefail
+
+DEFAULT_IMAGE='ghcr.io/aquasecurity/trivy:0.76.1@sha256:790fd66d1697395d8693d1acaa1ea702406a20f109b6fc7bac8db623eca6c06c'
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+IMAGE="${TRIVY_IMAGE:-${DEFAULT_IMAGE}}"
+CACHE_DIR="${TRIVY_CACHE_DIR:-${HOME}/.cache/trivy}"
+REPORT_DIR="${TRIVY_REPORT_DIR:-$(pwd)/trivy-reports}"
+POLICY_DIR="${TRIVY_POLICY_DIR:-${SCRIPT_DIR}}"
+EXEMPTIONS="${TRIVY_EXEMPTIONS:-on}"
+IGNORE_FILE="${POLICY_DIR}/trivyignore.yaml"
+VEX_FILE="${POLICY_DIR}/vex.openvex.json"
+MAX_EXEMPTION_DAYS=90
+SCAN_TIMEOUT='45m'
+MAX_ROWS=200
+
+# Flags shared by every container: no network is added here, callers decide.
+hardening=(--rm --read-only --cap-drop ALL --security-opt no-new-privileges
+  --user "$(id -u):$(id -g)" --tmpfs /tmp)
+trivy_env=(-e TRIVY_CACHE_DIR=/cache -e TRIVY_SKIP_VERSION_CHECK=true
+  -e TRIVY_DISABLE_TELEMETRY=true -e TRIVY_NO_PROGRESS=true)
+
+log() { printf '%s\n' "$*" >&2; }
+
+abs_dir() { (cd "$1" && pwd); }
+
+# Retries a command with exponential backoff: retry ATTEMPTS COMMAND...
+retry() {
+  local attempts=$1 delay=10 n=1
+  shift
+  until "$@"; do
+    if ((n >= attempts)); then
+      log "Command failed after ${attempts} attempts: $*"
+      return 1
+    fi
+    log "Attempt ${n}/${attempts} failed, retrying in ${delay}s: $*"
+    sleep "${delay}"
+    delay=$((delay * 2))
+    n=$((n + 1))
+  done
+}
+
+cmd_pull() {
+  retry 5 docker pull --quiet "${IMAGE}" >/dev/null
+  log "Pulled ${IMAGE}"
+}
+
+download_db() {
+  # The default repository chain (mirror.gcr.io, then ghcr.io) is kept on 
purpose.
+  docker run "${hardening[@]}" -v "${CACHE_DIR}:/cache" "${trivy_env[@]}" 
"${IMAGE}" \
+    image --download-db-only --no-progress &&
+    docker run "${hardening[@]}" -v "${CACHE_DIR}:/cache" "${trivy_env[@]}" 
"${IMAGE}" \
+      image --download-java-db-only --no-progress
+}
+
+cmd_download_db() {
+  mkdir -p "${CACHE_DIR}"
+  retry 4 download_db
+  log "Vulnerability DB: $(db_snapshot)"
+}
+
+# Prints the update time of the trivy DB and the Java DB in the cache.
+db_snapshot() {
+  local vuln java
+  vuln="$(jq -r '.UpdatedAt // "unknown"' "${CACHE_DIR}/db/metadata.json" 
2>/dev/null || echo unknown)"
+  java="$(jq -r '.UpdatedAt // "unknown"' "${CACHE_DIR}/java-db/metadata.json" 
2>/dev/null || echo unknown)"
+  printf 'trivy-db %s, java-db %s' "${vuln}" "${java}"
+}
+
+cmd_db_info() {
+  local md
+  md="$(mktemp)"
+  printf 'Vulnerability DB snapshot: %s\n\n' "$(db_snapshot)" >"${md}"
+  emit_summary "${md}"
+  rm -f "${md}"
+}
+
+emit_summary() {
+  cat "$1" || return 1
+  if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
+    cat "$1" >>"${GITHUB_STEP_SUMMARY}" || return 1
+  fi
+}
+
+# Writes the Markdown report for one target: write_report NAME JSON OUTPUT.
+# Every step is checked explicitly: the function runs inside '||' lists, where 
errexit is off.
+write_report() {
+  local name=$1 json=$2 md=$3 remaining suppressed snapshot
+  remaining="$(jq '[.Results[]?.Vulnerabilities[]?] | length' "${json}")" || 
return 1
+  suppressed="$(jq '[.Results[]?.ExperimentalModifiedFindings[]?] | length' 
"${json}")" || return 1
+  snapshot="$(db_snapshot)" || return 1
+  {
+    printf '### %s\n\n' "${name}" &&
+      printf 'DB snapshot: %s. Remaining HIGH/CRITICAL: **%s**. Suppressed: 
%s.\n\n' \
+        "${snapshot}" "${remaining}" "${suppressed}" || return 1
+    if ((remaining > 0 || suppressed > 0)); then
+      printf 'Tables are grouped by vulnerability and package and show at most 
%s rows. The JSON report has everything.\n\n' "${MAX_ROWS}" || return 1
+    fi
+    if ((remaining > 0)); then
+      printf '#### Remaining findings\n\n' || return 1
+      printf '| Vulnerability | Severity | Package | Installed | Fixed | 
Location |\n' || return 1
+      printf '| --- | --- | --- | --- | --- | --- |\n' || return 1
+      jq -r --argjson max "${MAX_ROWS}" '
+        def esc: tostring | gsub("\\|"; "\\|");
+        [.Results[]? | .Target as $t | .Vulnerabilities[]?
+          | {id: .VulnerabilityID, sev: .Severity, pkg: .PkgName, ver: 
.InstalledVersion,
+             fix: (.FixedVersion // "-"), url: (.PrimaryURL // ""), path: 
(.PkgPath // $t)}]
+        | group_by([.id, .pkg, .ver])
+        | map(.[0] + {paths: (map(.path) | unique)})
+        | sort_by((if .sev == "CRITICAL" then 0 else 1 end), .id) | .[:$max][]
+        | "| " + (if .url != "" then "[\(.id)](\(.url))" else .id end)
+          + " | \(.sev) | \(.pkg | esc) | \(.ver | esc) | \(.fix | esc) | "
+          + ((.paths[:3] | map("`" + esc + "`") | join("<br>"))
+             + (if (.paths | length) > 3 then "<br>(+\((.paths | length) - 3) 
more)" else "" end)) + " |"
+      ' "${json}" || return 1
+      printf '\n' || return 1
+    fi
+    if ((suppressed > 0)); then
+      printf '<details><summary>Suppressed findings (%s)</summary>\n\n' 
"${suppressed}" || return 1
+      printf '| Vulnerability | Severity | Package | Installed | Status | 
Statement | Source | Locations |\n' || return 1
+      printf '| --- | --- | --- | --- | --- | --- | --- | --- |\n' || return 1
+      jq -r --argjson max "${MAX_ROWS}" '
+        def esc: tostring | gsub("\\|"; "\\|");
+        [.Results[]? | .Target as $t | .ExperimentalModifiedFindings[]?
+          | {id: .Finding.VulnerabilityID, sev: .Finding.Severity, pkg: 
.Finding.PkgName,
+             ver: .Finding.InstalledVersion, status: .Status,
+             stmt: (.Statement // "-"), src: ((.Source // "-") | split("/") | 
last),
+             path: (.Finding.PkgPath // $t)}]
+        | group_by([.id, .pkg, .ver, .status, .stmt, .src])
+        | map(.[0] + {n: length}) | sort_by(.pkg, .id) | .[:$max][]
+        | "| \(.id) | \(.sev) | \(.pkg | esc) | \(.ver | esc) | \(.status) | 
\(.stmt | esc) | \(.src | esc) | \(.n) |"
+      ' "${json}" || return 1
+      printf '\n</details>\n\n' || return 1
+    fi
+  } >"${md}" || return 1
+}
+
+# Converts the JSON report to SARIF with one run per target and a unique id: 
make_sarif NAME SARIF.
+# The result is only installed when it is valid JSON with at least one run.
+make_sarif() {
+  local name=$1 sarif=$2
+  docker run "${hardening[@]}" --network none -v "$(abs_dir 
"${REPORT_DIR}"):/out" \
+    "${IMAGE}" convert --format sarif --output "/out/${name}.sarif" 
"/out/${name}.json" || return 1
+  jq --arg id "trivy/${name}/" \
+    '.runs |= map(.automationDetails = {id: $id})' "${sarif}" >"${sarif}.tmp" 
|| return 1
+  jq -e '(.runs | type == "array") and (.runs | length >= 1)' "${sarif}.tmp" 
>/dev/null || return 1
+  mv "${sarif}.tmp" "${sarif}" || return 1
+}
+
+# Scans one target: scan_target NAME PATH TYPE. Returns non-zero when the 
target fails the gate or
+# any step fails. Callers use it in '||' lists, where errexit is off, so every 
step is checked.
+scan_target() {
+  local name=$1 path=$2 type=${3:-rootfs} json sarif md
+  json="${REPORT_DIR}/${name}.json"
+  sarif="${REPORT_DIR}/${name}.sarif"
+  md="${REPORT_DIR}/${name}.md"
+
+  if [[ ! -d "${path}" ]] || [[ -z "$(find "${path}" -mindepth 1 -print 
-quit)" ]]; then
+    log "::error title=${name}::Scan target '${path}' does not exist or is 
empty"
+    return 1
+  fi
+  if [[ ! -f "${CACHE_DIR}/db/trivy.db" || ! -f 
"${CACHE_DIR}/java-db/trivy-java.db" ]]; then
+    log "::error title=${name}::Vulnerability DBs are missing in ${CACHE_DIR}; 
run 'scan.sh download-db'"
+    return 1
+  fi
+  mkdir -p "${REPORT_DIR}" || return 1
+  rm -f "${json}" "${sarif}" "${sarif}.tmp" "${md}" || return 1
+
+  local policy_args=() mounts=()
+  mounts=(-v "$(abs_dir "${path}"):/scan:ro" -v "${CACHE_DIR}:/cache:ro"
+    -v "$(abs_dir "${REPORT_DIR}"):/out")
+  if [[ "${EXEMPTIONS}" != "off" ]]; then
+    mounts+=(-v "$(abs_dir "${POLICY_DIR}"):/policy:ro")
+    policy_args+=(--ignorefile /policy/trivyignore.yaml --vex 
/policy/vex.openvex.json)
+  fi
+
+  log "::group::Scan ${name}"
+  local rc=0
+  # The scan itself never needs the network: DBs come from the read-only cache.
+  docker run "${hardening[@]}" --network none "${mounts[@]}" "${trivy_env[@]}" 
"${IMAGE}" \
+    "${type}" /scan --scanners vuln --severity HIGH,CRITICAL \
+    --skip-db-update --skip-java-db-update --offline-scan --cache-backend 
memory \
+    --show-suppressed --timeout "${SCAN_TIMEOUT}" 
${policy_args[@]+"${policy_args[@]}"} \
+    --format json --output "/out/${name}.json" || rc=$?
+  log "::endgroup::"
+
+  if ((rc != 0)) || ! jq -e '.SchemaVersion != null and (.ArtifactName | type 
== "string")' \
+    "${json}" >/dev/null 2>&1; then
+    log "::error title=${name}::Trivy did not produce a valid report (exit 
code ${rc})"
+    return 1
+  fi
+
+  # SARIF contains the remaining findings only. A target whose SARIF or report 
cannot be produced
+  # fails, and no partial file is left behind (the SARIF directory is uploaded 
as one unit).
+  if ! make_sarif "${name}" "${sarif}"; then
+    rm -f "${sarif}" "${sarif}.tmp"
+    log "::error title=${name}::Could not produce a valid SARIF report"
+    return 1
+  fi
+  if ! write_report "${name}" "${json}" "${md}" || ! emit_summary "${md}"; then
+    rm -f "${md}"
+    log "::error title=${name}::Could not write the Markdown report"
+    return 1
+  fi
+
+  local remaining
+  remaining="$(jq '[.Results[]?.Vulnerabilities[]?] | length' "${json}")" || 
return 1
+  if [[ ! "${remaining}" =~ ^[0-9]+$ ]]; then
+    log "::error title=${name}::Could not count the remaining findings"
+    return 1
+  fi
+  if ((remaining > 0)); then
+    log "::error title=${name}::${remaining} HIGH/CRITICAL findings remain 
after exemptions"
+    return 1
+  fi
+}
+
+# Copies exactly one jar matching GLOB into DEST/NAME so that it is scanned as 
its own target.
+cmd_stage() {
+  local dest=$1 name=$2 pattern=$3 file matches=()
+  while IFS= read -r file; do
+    [[ "${file}" == *-empty.jar ]] || matches+=("${file}")
+  done < <(compgen -G "${pattern}" || true)
+  if ((${#matches[@]} != 1)); then
+    log "::error title=${name}::Expected exactly one jar matching 
'${pattern}', found ${#matches[@]}"
+    return 1
+  fi
+  mkdir -p "${dest}/${name}"
+  cp "${matches[0]}" "${dest}/${name}/"
+}
+
+# Merges the pinned requirements files of the Python client into 
DEST/requirements.txt:
+# stage_requirements SRC_DIR DEST. Fails (fail closed) when the merged file 
has no pinned '==' line,
+# for example because the glob matched nothing or the files only hold comments.
+cmd_stage_requirements() {

Review Comment:
   Fixed together with the comment above: the `==` check is gone. 
`pip-report-to-requirements` converts the pip report and fails closed on an 
item without name or version, or on an empty result.



##########
.github/workflows/cve-scan.yml:
##########
@@ -0,0 +1,345 @@
+#
+# 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.
+#
+name: CVE Scan
+
+# Weekly Trivy scan of the artifacts Gravitino ships. See 
.github/trivy/README.md for what is
+# scanned, how to run the same scan locally and how exemptions work.
+#
+# A target (one shipped artifact) fails when a HIGH or CRITICAL finding 
remains after the
+# exemptions in .github/trivy are applied, or when Trivy produced no valid 
report. Expired
+# exemptions bring their findings back, so a red run is also the reminder to 
re-triage them.
+on:
+  schedule:
+    - cron: '0 3 * * 1' # Mondays 03:00 UTC

Review Comment:
   Added a `notify` job. It runs only for scheduled runs on apache/gravitino 
and is the only job with `issues: write`. A failed run opens one issue titled 
`Scheduled release artifact scan failed`, or comments on it with the run and 
the failed jobs; the next passing scheduled run closes it. Documented in the 
README, tests in `cve-scan-notify.test.cjs`.



##########
.github/trivy/README.md:
##########
@@ -0,0 +1,129 @@
+<!--
+  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.
+-->
+# Vulnerability scan of shipped artifacts
+
+The [CVE Scan](../workflows/cve-scan.yml) workflow builds the artifacts 
Gravitino ships and scans each
+one with [Trivy](https://trivy.dev) for known HIGH and CRITICAL 
vulnerabilities. It runs every Monday
+and can be started by hand from the Actions tab. It does not run on pull 
requests.
+
+## What is scanned
+
+Each artifact is a separate target, so a finding is attributed to the artifact 
users download.
+
+| Job | Built with | Targets |
+| --- | --- | --- |
+| Server packages | `compileDistribution`, `compileIcebergRESTServer`, 
`compileLanceRESTServer` | `server-distribution`, `iceberg-rest-server`, 
`lance-rest-server` |
+| Shaded jars | `shadowJar` of each module | the nine `*-bundle` jars, 
`client-java-runtime`, `filesystem-hadoop3-runtime`, `jobs`, 
`flink-runtime-{1.19,1.20,2.1,2.2}`, `spark-runtime-3.5_2.12`, 
`spark-runtime-3.5_2.13`, `spark-runtime-4.0` |
+| Trino connectors | `copyRuntimeLibs` of each version range | 
`trino-connector-{469-472,473-477,478-479,480,481,482-483}` |
+| Lock files | none | `mcp-server` (`mcp-server/uv.lock`), `web` 
(`web/web/pnpm-lock.yaml`), `web-v2` (`web-v2/web/pnpm-lock.yaml`), 
`client-python` (see below) |
+
+Notes:
+
+* The standalone Iceberg and Lance REST server directories contain the same 
third-party jars as
+  their copies in `distribution/package`. They are scanned anyway because they 
are released on their own.
+* `distribution/package-all` is not scanned: only 
`gravitino-<version>-bin.tar.gz` is released.
+* The web UI is not built (`-PskipWeb=true`). Its WAR holds a minified static 
export and
+  `WEB-INF/web.xml`, with no `package.json`, lock file or `node_modules`, so 
Trivy cannot inspect it.
+  The dependencies compiled into the WARs are scanned through the two 
`pnpm-lock.yaml` files instead.
+  Trivy 0.76.1 leaves out the development dependencies of pnpm lock files 
unless `--include-dev-deps` is
+  set (`web`: 36 findings without the flag, 86 with it), so only what is 
shipped is gated.
+* The Python client is scanned from its requirements files, because the 
released `apache-gravitino`
+  package pins its dependencies with `==` (`requirements.txt` and the files of 
its extras). The workflow
+  merges them into one file (`scan.sh stage-requirements clients/client-python 
<dir>/client-python`)
+  and leaves out `requirements-dev.txt` and `requirements-ray-iceberg.txt`. 
The step fails when the
+  merged file has no pinned `==` line, so an empty file cannot pass as a clean 
scan.
+  Only the pinned packages are checked: transitive dependencies are resolved 
by pip at install time, so
+  there is no version for Trivy to look up.
+* The Docker images published by `dev/release` are not scanned yet. That is a 
follow-up.
+* The Trino 473 and later modules need JDK 25, which Gradle provisions through 
its toolchain resolver.
+
+## How the scan runs
+
+* Trivy runs from a container image pinned by digest (see `scan.sh`), 
read-only, without capabilities
+  and, for the scan itself, without network access.
+* One job downloads the vulnerability DB and the Java DB once per run and 
caches them under a date key.
+  Scan jobs restore the cache read-only and skip all DB updates, so every 
report of a run uses the same DB snapshot.
+* A target fails when a HIGH or CRITICAL finding remains after the exemptions, 
or when Trivy did not
+  produce a valid report. All targets are scanned even if one of them fails.
+
+## Reading the results
+
+* Each target has a section in the run summary: the DB snapshot, the remaining 
findings, and a
+  collapsed table of the suppressed findings with the reason for each.
+* The raw JSON, the SARIF and the Markdown report of every target are kept for 
30 days as workflow
+  artifacts (`cve-scan-*`).
+* On `apache/gravitino` the SARIF reports are also sent to GitHub code 
scanning, when it is enabled.
+* The `Exemptions` job fails when an entry in `trivyignore.yaml` or 
`vex.openvex.json` breaks the policy
+  below (`scan.sh check-exemptions`). It also warns about entries that matched 
no finding in any target of
+  the run (`scan.sh unused`). Remove those.
+
+## Running the scan locally
+
+You need Docker and `jq`. `yq` (v4) is only needed for `scan.sh unused` and 
`scan.sh check-exemptions`;
+both tools are preinstalled on the GitHub `ubuntu` runners.
+
+```bash
+# Build what you want to scan, for example the server packages and the AWS 
bundle.
+./gradlew compileDistribution :bundles:aws-bundle:shadowJar -PskipWeb=true -x 
test
+
+# Download the vulnerability DBs into ~/.cache/trivy (about 1.1 GB to 
download, 3.1 GB on disk).
+.github/trivy/scan.sh download-db
+
+# Scan a directory. Reports are written to ./trivy-reports.
+.github/trivy/scan.sh scan server-distribution distribution/package
+
+# A fat jar is scanned from a directory that holds only that jar.
+.github/trivy/scan.sh stage /tmp/targets aws-bundle 
'bundles/aws-bundle/build/libs/gravitino-aws-bundle-*.jar'
+.github/trivy/scan.sh scan aws-bundle /tmp/targets/aws-bundle
+```
+
+Set `TRIVY_EXEMPTIONS=off` to see all findings without the exemptions, 
`TRIVY_CACHE_DIR` and
+`TRIVY_REPORT_DIR` to change the directories. Run `.github/trivy/scan.sh` 
without arguments for the list of commands.
+
+## Exemptions
+
+Fix the vulnerability first: upgrade the dependency, or exclude it if it is 
not needed. Add an
+exemption only when that is not possible yet. Exemptions are changed through a 
pull request.
+
+Exemptions are global: they are applied to every target of the run and keyed 
by the versioned package
+URL, so one entry covers every artifact that carries that exact version. Check 
all the places a
+package version appears before pointing an entry at an issue that only fixes 
some of them.
+
+There are two kinds:
+
+| | A: not affected (`vex.openvex.json`) | B: not analysed yet 
(`trivyignore.yaml`) |
+| --- | --- | --- |
+| Meaning | Gravitino does not execute the vulnerable code | Pre-existing 
finding that has not been triaged |
+| Evidence | Required: a `not_affected` statement with the justification 
`vulnerable_code_not_in_execute_path` and an `impact_statement` that cites the 
code or analysis, valid for the artifact where it is reported | None, but a 
tracking issue is required |

Review Comment:
   Aligned by making the check stricter: only `not_affected` statements are 
accepted, with one of the five OpenVEX justifications and an 
`impact_statement`. `fixed` is now rejected, since Trivy suppresses it without 
any evidence, and `affected`/`under_investigation` suppress nothing. The README 
states exactly this.



##########
.github/trivy/scan.sh:
##########
@@ -0,0 +1,503 @@
+#!/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.
+#
+# Runs Trivy (in a hardened container) against built Gravitino artifacts. The 
same script is used
+# by .github/workflows/cve-scan.yml and by contributors on their machines.
+#
+# Usage:
+#   scan.sh pull                       Pull the pinned Trivy image (with 
retries).
+#   scan.sh download-db                Download the vulnerability and Java DBs 
into the cache dir.
+#   scan.sh db-info                    Print the snapshot dates of the cached 
DBs (job summary too).
+#   scan.sh scan NAME PATH [TYPE]      Scan one target directory. TYPE is 
rootfs (default) or fs.
+#   scan.sh scan-all DIR [TYPE]        Scan every sub-directory of DIR as its 
own target.
+#   scan.sh stage DIR NAME GLOB        Copy the one jar matching GLOB (not 
*-empty.jar) to DIR/NAME.
+#   scan.sh stage-requirements SRC DEST  Merge the pinned Python client 
requirements; fail if none.
+#   scan.sh unused REPORT_DIR          Warn about exemptions that matched 
nothing in the reports.
+#   scan.sh check-exemptions           Fail when an exemption violates the 
policy in README.md.
+#
+# A target fails when a HIGH or CRITICAL finding remains after the exemptions 
are applied, or when
+# Trivy did not produce a valid report (fail closed). Reports are written to 
$TRIVY_REPORT_DIR as
+# NAME.json (raw), NAME.sarif and NAME.md. A target also fails when its SARIF 
or Markdown report
+# cannot be produced, in both 'scan' and 'scan-all'. Only 'unused' and 
'check-exemptions' need yq (v4).
+#
+# Environment (all optional):
+#   TRIVY_IMAGE         Pinned scanner image (defaults to the digest pinned 
below).
+#   TRIVY_CACHE_DIR     Vulnerability DB cache directory (default: 
$HOME/.cache/trivy).
+#   TRIVY_REPORT_DIR    Output directory (default: ./trivy-reports).
+#   TRIVY_POLICY_DIR    Directory holding trivyignore.yaml and vex.openvex.json
+#                       (default: the directory of this script).
+#   TRIVY_EXEMPTIONS    Set to "off" to scan without exemptions (used to 
generate a baseline).
+#   GITHUB_STEP_SUMMARY When set, the Markdown report is appended to it as 
well.
+
+set -euo pipefail
+
+DEFAULT_IMAGE='ghcr.io/aquasecurity/trivy:0.76.1@sha256:790fd66d1697395d8693d1acaa1ea702406a20f109b6fc7bac8db623eca6c06c'
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+IMAGE="${TRIVY_IMAGE:-${DEFAULT_IMAGE}}"
+CACHE_DIR="${TRIVY_CACHE_DIR:-${HOME}/.cache/trivy}"
+REPORT_DIR="${TRIVY_REPORT_DIR:-$(pwd)/trivy-reports}"
+POLICY_DIR="${TRIVY_POLICY_DIR:-${SCRIPT_DIR}}"
+EXEMPTIONS="${TRIVY_EXEMPTIONS:-on}"
+IGNORE_FILE="${POLICY_DIR}/trivyignore.yaml"
+VEX_FILE="${POLICY_DIR}/vex.openvex.json"
+MAX_EXEMPTION_DAYS=90
+SCAN_TIMEOUT='45m'
+MAX_ROWS=200
+
+# Flags shared by every container: no network is added here, callers decide.
+hardening=(--rm --read-only --cap-drop ALL --security-opt no-new-privileges
+  --user "$(id -u):$(id -g)" --tmpfs /tmp)
+trivy_env=(-e TRIVY_CACHE_DIR=/cache -e TRIVY_SKIP_VERSION_CHECK=true
+  -e TRIVY_DISABLE_TELEMETRY=true -e TRIVY_NO_PROGRESS=true)
+
+log() { printf '%s\n' "$*" >&2; }
+
+abs_dir() { (cd "$1" && pwd); }
+
+# Retries a command with exponential backoff: retry ATTEMPTS COMMAND...
+retry() {
+  local attempts=$1 delay=10 n=1
+  shift
+  until "$@"; do
+    if ((n >= attempts)); then
+      log "Command failed after ${attempts} attempts: $*"
+      return 1
+    fi
+    log "Attempt ${n}/${attempts} failed, retrying in ${delay}s: $*"
+    sleep "${delay}"
+    delay=$((delay * 2))
+    n=$((n + 1))
+  done
+}
+
+cmd_pull() {
+  retry 5 docker pull --quiet "${IMAGE}" >/dev/null
+  log "Pulled ${IMAGE}"
+}
+
+download_db() {
+  # The default repository chain (mirror.gcr.io, then ghcr.io) is kept on 
purpose.
+  docker run "${hardening[@]}" -v "${CACHE_DIR}:/cache" "${trivy_env[@]}" 
"${IMAGE}" \
+    image --download-db-only --no-progress &&
+    docker run "${hardening[@]}" -v "${CACHE_DIR}:/cache" "${trivy_env[@]}" 
"${IMAGE}" \
+      image --download-java-db-only --no-progress
+}
+
+cmd_download_db() {
+  mkdir -p "${CACHE_DIR}"
+  retry 4 download_db
+  log "Vulnerability DB: $(db_snapshot)"
+}
+
+# Prints the update time of the trivy DB and the Java DB in the cache.
+db_snapshot() {
+  local vuln java
+  vuln="$(jq -r '.UpdatedAt // "unknown"' "${CACHE_DIR}/db/metadata.json" 
2>/dev/null || echo unknown)"
+  java="$(jq -r '.UpdatedAt // "unknown"' "${CACHE_DIR}/java-db/metadata.json" 
2>/dev/null || echo unknown)"
+  printf 'trivy-db %s, java-db %s' "${vuln}" "${java}"
+}
+
+cmd_db_info() {
+  local md
+  md="$(mktemp)"
+  printf 'Vulnerability DB snapshot: %s\n\n' "$(db_snapshot)" >"${md}"
+  emit_summary "${md}"
+  rm -f "${md}"
+}
+
+emit_summary() {
+  cat "$1" || return 1
+  if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
+    cat "$1" >>"${GITHUB_STEP_SUMMARY}" || return 1
+  fi
+}
+
+# Writes the Markdown report for one target: write_report NAME JSON OUTPUT.
+# Every step is checked explicitly: the function runs inside '||' lists, where 
errexit is off.
+write_report() {
+  local name=$1 json=$2 md=$3 remaining suppressed snapshot
+  remaining="$(jq '[.Results[]?.Vulnerabilities[]?] | length' "${json}")" || 
return 1
+  suppressed="$(jq '[.Results[]?.ExperimentalModifiedFindings[]?] | length' 
"${json}")" || return 1
+  snapshot="$(db_snapshot)" || return 1
+  {
+    printf '### %s\n\n' "${name}" &&
+      printf 'DB snapshot: %s. Remaining HIGH/CRITICAL: **%s**. Suppressed: 
%s.\n\n' \
+        "${snapshot}" "${remaining}" "${suppressed}" || return 1
+    if ((remaining > 0 || suppressed > 0)); then
+      printf 'Tables are grouped by vulnerability and package and show at most 
%s rows. The JSON report has everything.\n\n' "${MAX_ROWS}" || return 1
+    fi
+    if ((remaining > 0)); then
+      printf '#### Remaining findings\n\n' || return 1
+      printf '| Vulnerability | Severity | Package | Installed | Fixed | 
Location |\n' || return 1
+      printf '| --- | --- | --- | --- | --- | --- |\n' || return 1
+      jq -r --argjson max "${MAX_ROWS}" '
+        def esc: tostring | gsub("\\|"; "\\|");
+        [.Results[]? | .Target as $t | .Vulnerabilities[]?
+          | {id: .VulnerabilityID, sev: .Severity, pkg: .PkgName, ver: 
.InstalledVersion,
+             fix: (.FixedVersion // "-"), url: (.PrimaryURL // ""), path: 
(.PkgPath // $t)}]
+        | group_by([.id, .pkg, .ver])
+        | map(.[0] + {paths: (map(.path) | unique)})
+        | sort_by((if .sev == "CRITICAL" then 0 else 1 end), .id) | .[:$max][]
+        | "| " + (if .url != "" then "[\(.id)](\(.url))" else .id end)
+          + " | \(.sev) | \(.pkg | esc) | \(.ver | esc) | \(.fix | esc) | "
+          + ((.paths[:3] | map("`" + esc + "`") | join("<br>"))
+             + (if (.paths | length) > 3 then "<br>(+\((.paths | length) - 3) 
more)" else "" end)) + " |"
+      ' "${json}" || return 1
+      printf '\n' || return 1
+    fi
+    if ((suppressed > 0)); then
+      printf '<details><summary>Suppressed findings (%s)</summary>\n\n' 
"${suppressed}" || return 1
+      printf '| Vulnerability | Severity | Package | Installed | Status | 
Statement | Source | Locations |\n' || return 1
+      printf '| --- | --- | --- | --- | --- | --- | --- | --- |\n' || return 1
+      jq -r --argjson max "${MAX_ROWS}" '
+        def esc: tostring | gsub("\\|"; "\\|");
+        [.Results[]? | .Target as $t | .ExperimentalModifiedFindings[]?
+          | {id: .Finding.VulnerabilityID, sev: .Finding.Severity, pkg: 
.Finding.PkgName,
+             ver: .Finding.InstalledVersion, status: .Status,
+             stmt: (.Statement // "-"), src: ((.Source // "-") | split("/") | 
last),
+             path: (.Finding.PkgPath // $t)}]
+        | group_by([.id, .pkg, .ver, .status, .stmt, .src])
+        | map(.[0] + {n: length}) | sort_by(.pkg, .id) | .[:$max][]
+        | "| \(.id) | \(.sev) | \(.pkg | esc) | \(.ver | esc) | \(.status) | 
\(.stmt | esc) | \(.src | esc) | \(.n) |"
+      ' "${json}" || return 1
+      printf '\n</details>\n\n' || return 1
+    fi
+  } >"${md}" || return 1
+}
+
+# Converts the JSON report to SARIF with one run per target and a unique id: 
make_sarif NAME SARIF.
+# The result is only installed when it is valid JSON with at least one run.
+make_sarif() {
+  local name=$1 sarif=$2
+  docker run "${hardening[@]}" --network none -v "$(abs_dir 
"${REPORT_DIR}"):/out" \
+    "${IMAGE}" convert --format sarif --output "/out/${name}.sarif" 
"/out/${name}.json" || return 1
+  jq --arg id "trivy/${name}/" \
+    '.runs |= map(.automationDetails = {id: $id})' "${sarif}" >"${sarif}.tmp" 
|| return 1
+  jq -e '(.runs | type == "array") and (.runs | length >= 1)' "${sarif}.tmp" 
>/dev/null || return 1
+  mv "${sarif}.tmp" "${sarif}" || return 1
+}
+
+# Scans one target: scan_target NAME PATH TYPE. Returns non-zero when the 
target fails the gate or
+# any step fails. Callers use it in '||' lists, where errexit is off, so every 
step is checked.
+scan_target() {
+  local name=$1 path=$2 type=${3:-rootfs} json sarif md
+  json="${REPORT_DIR}/${name}.json"
+  sarif="${REPORT_DIR}/${name}.sarif"
+  md="${REPORT_DIR}/${name}.md"
+
+  if [[ ! -d "${path}" ]] || [[ -z "$(find "${path}" -mindepth 1 -print 
-quit)" ]]; then
+    log "::error title=${name}::Scan target '${path}' does not exist or is 
empty"
+    return 1
+  fi
+  if [[ ! -f "${CACHE_DIR}/db/trivy.db" || ! -f 
"${CACHE_DIR}/java-db/trivy-java.db" ]]; then
+    log "::error title=${name}::Vulnerability DBs are missing in ${CACHE_DIR}; 
run 'scan.sh download-db'"
+    return 1
+  fi
+  mkdir -p "${REPORT_DIR}" || return 1
+  rm -f "${json}" "${sarif}" "${sarif}.tmp" "${md}" || return 1
+
+  local policy_args=() mounts=()
+  mounts=(-v "$(abs_dir "${path}"):/scan:ro" -v "${CACHE_DIR}:/cache:ro"
+    -v "$(abs_dir "${REPORT_DIR}"):/out")
+  if [[ "${EXEMPTIONS}" != "off" ]]; then
+    mounts+=(-v "$(abs_dir "${POLICY_DIR}"):/policy:ro")
+    policy_args+=(--ignorefile /policy/trivyignore.yaml --vex 
/policy/vex.openvex.json)
+  fi
+
+  log "::group::Scan ${name}"
+  local rc=0
+  # The scan itself never needs the network: DBs come from the read-only cache.
+  docker run "${hardening[@]}" --network none "${mounts[@]}" "${trivy_env[@]}" 
"${IMAGE}" \
+    "${type}" /scan --scanners vuln --severity HIGH,CRITICAL \
+    --skip-db-update --skip-java-db-update --offline-scan --cache-backend 
memory \
+    --show-suppressed --timeout "${SCAN_TIMEOUT}" 
${policy_args[@]+"${policy_args[@]}"} \
+    --format json --output "/out/${name}.json" || rc=$?
+  log "::endgroup::"
+
+  if ((rc != 0)) || ! jq -e '.SchemaVersion != null and (.ArtifactName | type 
== "string")' \
+    "${json}" >/dev/null 2>&1; then
+    log "::error title=${name}::Trivy did not produce a valid report (exit 
code ${rc})"
+    return 1
+  fi
+
+  # SARIF contains the remaining findings only. A target whose SARIF or report 
cannot be produced
+  # fails, and no partial file is left behind (the SARIF directory is uploaded 
as one unit).
+  if ! make_sarif "${name}" "${sarif}"; then
+    rm -f "${sarif}" "${sarif}.tmp"
+    log "::error title=${name}::Could not produce a valid SARIF report"
+    return 1
+  fi
+  if ! write_report "${name}" "${json}" "${md}" || ! emit_summary "${md}"; then
+    rm -f "${md}"
+    log "::error title=${name}::Could not write the Markdown report"
+    return 1
+  fi
+
+  local remaining
+  remaining="$(jq '[.Results[]?.Vulnerabilities[]?] | length' "${json}")" || 
return 1
+  if [[ ! "${remaining}" =~ ^[0-9]+$ ]]; then
+    log "::error title=${name}::Could not count the remaining findings"
+    return 1
+  fi
+  if ((remaining > 0)); then
+    log "::error title=${name}::${remaining} HIGH/CRITICAL findings remain 
after exemptions"
+    return 1
+  fi
+}
+
+# Copies exactly one jar matching GLOB into DEST/NAME so that it is scanned as 
its own target.
+cmd_stage() {
+  local dest=$1 name=$2 pattern=$3 file matches=()
+  while IFS= read -r file; do
+    [[ "${file}" == *-empty.jar ]] || matches+=("${file}")
+  done < <(compgen -G "${pattern}" || true)
+  if ((${#matches[@]} != 1)); then
+    log "::error title=${name}::Expected exactly one jar matching 
'${pattern}', found ${#matches[@]}"
+    return 1
+  fi
+  mkdir -p "${dest}/${name}"
+  cp "${matches[0]}" "${dest}/${name}/"
+}
+
+# Merges the pinned requirements files of the Python client into 
DEST/requirements.txt:
+# stage_requirements SRC_DIR DEST. Fails (fail closed) when the merged file 
has no pinned '==' line,
+# for example because the glob matched nothing or the files only hold comments.
+cmd_stage_requirements() {
+  local src=$1 dest=$2 file out="$2/requirements.txt"
+  mkdir -p "${dest}" || return 1
+  for file in "${src}"/requirements*.txt; do
+    [[ -f "${file}" ]] || continue
+    case "${file##*/}" in
+      requirements-dev.txt | requirements-ray-iceberg.txt) continue ;;
+    esac
+    grep -hv '^#' "${file}" || true
+  done | sort -u >"${out}" || return 1
+  if ! grep -Eq '^[^#[:space:]][^#]*==' "${out}"; then
+    log "::error title=client-python::No pinned '==' requirement found in 
'${src}/requirements*.txt'"
+    rm -rf "${dest}"
+    return 1
+  fi
+}
+
+# Fails when a target fails, and also when there is nothing to scan (fail 
closed).
+cmd_scan_all() {
+  local parent=$1 type=${2:-rootfs} dir rc=0 count=0
+  for dir in "${parent}"/*/; do
+    [[ -d "${dir}" ]] || continue
+    count=$((count + 1))
+    scan_target "$(basename "${dir}")" "${dir%/}" "${type}" || rc=1
+  done
+  if ((count == 0)); then
+    log "::error title=Scan::No target directory found in '${parent}'"
+    return 1
+  fi
+  return "${rc}"
+}
+
+require_yq() {
+  command -v yq >/dev/null 2>&1 || { log 'yq (v4) is required for this 
command'; return 1; }
+}
+
+# Lists every exemption entry as "ID PURL" (PURL is empty for entries without 
purls).
+list_exemptions() {
+  # shellcheck disable=SC2016 # $id is a yq variable, not a shell one.
+  yq -r '.vulnerabilities[]? | .id as $id
+    | (.purls // [""])[] | $id + " " + .' "${IGNORE_FILE}"
+}
+
+cmd_unused() {
+  local dir=$1 used entries count=0 reports
+  require_yq || return 1
+  reports="$(find "${dir}" -maxdepth 1 -name '*.json' | wc -l | tr -d ' ')"
+  if ((reports == 0)); then
+    log "No JSON reports found in ${dir}; skipping the unused exemption check"
+    return 0
+  fi
+  used="$(find "${dir}" -maxdepth 1 -name '*.json' -exec jq -r '
+      .Results[]?.ExperimentalModifiedFindings[]?
+      | select(.Status == "ignored")
+      | .Finding.VulnerabilityID + " " + (.Finding.PkgIdentifier.PURL // "")' 
{} + | sort -u)"
+  entries="$(list_exemptions)"
+  local summary=''
+  while read -r id purl; do
+    [[ -n "${id}" ]] || continue
+    if [[ -n "${purl}" ]]; then
+      grep -qxF "${id} ${purl}" <<<"${used}" && continue
+    else
+      grep -q "^${id} " <<<"${used}" && continue
+    fi
+    count=$((count + 1))
+    log "::warning title=Unused exemption::${id} ${purl} matched nothing in 
${reports} reports; remove it from trivyignore.yaml"
+    summary+="| ${id} | ${purl:--} |"$'\n'
+  done <<<"${entries}"
+
+  local md="${dir}/unused-exemptions.md"
+  {
+    printf '### Unused exemptions\n\n'
+    if ((count == 0)); then
+      # shellcheck disable=SC2016 # The backticks are Markdown.
+      printf 'Every exemption in `trivyignore.yaml` matched a finding (%s 
reports checked).\n\n' "${reports}"
+    else
+      printf '%s exemption(s) matched nothing in %s reports. Remove them if 
the finding is fixed.\n\n' \
+        "${count}" "${reports}"
+      printf '| Vulnerability | Package URL |\n| --- | --- |\n%s\n' 
"${summary}"
+    fi
+  } >"${md}"
+  emit_summary "${md}"
+}
+
+# Prints one line per violation of the exemption policy in trivyignore.yaml 
(see README.md).
+ignore_violations() {
+  local now=$1
+  yq -o=json '.vulnerabilities |= ((. // []) | map(. + {"__expired_at_tag": 
(.expired_at | tag)}))' \
+    "${IGNORE_FILE}" | jq -r --argjson now "${now}" --argjson maxdays 
"${MAX_EXEMPTION_DAYS}" '
+    def is_date: type == "string" and test("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")
+      and ((try (. + "T00:00:00Z" | fromdateiso8601 | todate | .[0:10]) catch 
"") == .);
+    def text: type == "string" and (gsub("\\s"; "") | length) > 0;
+    if (.vulnerabilities | length) == 0 then "trivyignore.yaml has no entries" 
else empty end,
+    (.vulnerabilities | to_entries[] | .key as $i | .value as $e
+      | "entry \($i + 1)" + (if ($e.id | text) then " (\($e.id))" else "" end) 
as $label
+      | ( if ($e.id | text) | not then "missing id" else empty end,
+          if (($e.purls | type) != "array") or ($e.purls | length) == 0
+            or ($e.purls | any(type != "string" or 
(test("^pkg:[^/]+/.+@[^@/]+$") | not)))
+          then "purls must be a non-empty list of package URLs with an exact 
version" else empty end,
+          if $e.expired_at == null then "missing expired_at"
+          elif ($e.expired_at | is_date | not) then "malformed expired_at 
\($e.expired_at)"
+          elif $e.__expired_at_tag != "!!timestamp" then "expired_at must be 
an unquoted date, as Trivy ignores a quoted one"
+          else
+            (($e.expired_at + "T00:00:00Z" | fromdateiso8601) - $now) as $left
+            | if $left <= 0 then "expired_at \($e.expired_at) is not in the 
future"
+              elif $left > $maxdays * 86400 then "expired_at \($e.expired_at) 
is more than \($maxdays) days ahead"
+              else empty end
+          end,
+          if ($e.statement | text | not) then "missing statement"
+          elif ($e.statement | contains("TRACKING_ISSUE")) then "statement 
still contains the TRACKING_ISSUE placeholder"
+          elif ($e.statement | test("apache/gravitino#[0-9]+") | not)
+          then "statement must reference a tracking issue as 
apache/gravitino#<number>"
+          else empty end
+        ) | $label + ": " + .)'
+}
+
+# Prints one line per violation of the OpenVEX rules for vex.openvex.json (see 
README.md).
+vex_violations() {
+  if ! jq -e . "${VEX_FILE}" >/dev/null 2>&1; then
+    echo "not valid JSON"
+    return 0
+  fi
+  jq -r '
+    def text: type == "string" and (gsub("\\s"; "") | length) > 0;
+    def justifications: ["component_not_present", 
"vulnerable_code_not_present",
+      "vulnerable_code_not_in_execute_path", 
"vulnerable_code_cannot_be_controlled_by_adversary",
+      "inline_mitigations_already_exist"];
+    if type != "object" then "the document must be a JSON object" else
+      ( if (.["@context"] | type) == "string" and (.["@context"] | 
startswith("https://openvex.dev/ns";))
+        then empty else "@context must be an OpenVEX namespace 
(https://openvex.dev/ns/...)" end,
+        if (.["@id"] | text) then empty else "missing @id" end,
+        if (.author | text) then empty else "missing author" end,
+        if (.timestamp | text) then empty else "missing timestamp" end,
+        if (.version | type) == "number" and .version >= 1 then empty else 
"version must be a number >= 1" end,
+        if (.statements | type) == "array" then empty else "statements must be 
a list" end,
+        ( (.statements | if type == "array" then to_entries else [] end)[] | 
.key as $i | .value as $s
+          | "statement \($i + 1)" + (if ($s.vulnerability.name | text) then " 
(\($s.vulnerability.name))" else "" end) as $label
+          | ( if ($s.vulnerability.name | text) | not then "missing 
vulnerability.name" else empty end,
+              if (($s.products | type) != "array") or ($s.products | length) 
== 0
+                or ($s.products | any(((.["@id"] // "") | text) | not))
+              then "products must be a non-empty list of products with an @id" 
else empty end,
+              if ($s.status | text) | not then "missing status"
+              elif (["not_affected", "fixed", "under_investigation", 
"affected"] | index($s.status)) == null
+              then "unknown status \($s.status)"
+              elif $s.status == "not_affected" then
+                ( if (justifications | index($s.justification)) == null
+                  then "not_affected needs a justification from the OpenVEX 
list" else empty end,
+                  if ($s.impact_statement | text) | not
+                  then "not_affected needs an impact_statement that cites the 
code or analysis" else empty end )
+              elif $s.status == "affected" then
+                ( if ($s.action_statement | text) | not then "affected needs 
an action_statement" else empty end )
+              else empty end
+            ) | $label + ": " + .)
+      )
+    end' "${VEX_FILE}"
+}
+
+cmd_check_exemptions() {

Review Comment:
   Added `cve-scan-exemptions.test.cjs`: missing fields, unversioned purls, 
malformed, quoted, past, today and over-90-day dates, a missing issue 
reference, the VEX status and justification rules, and `unused` with matched 
and unmatched entries. On GitHub Actions a missing `yq`/`jq` fails the test 
instead of skipping it.



##########
.github/workflows/cve-scan.yml:
##########
@@ -0,0 +1,345 @@
+#
+# 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.
+#
+name: CVE Scan
+
+# Weekly Trivy scan of the artifacts Gravitino ships. See 
.github/trivy/README.md for what is
+# scanned, how to run the same scan locally and how exemptions work.
+#
+# A target (one shipped artifact) fails when a HIGH or CRITICAL finding 
remains after the
+# exemptions in .github/trivy are applied, or when Trivy produced no valid 
report. Expired
+# exemptions bring their findings back, so a red run is also the reminder to 
re-triage them.
+on:
+  schedule:
+    - cron: '0 3 * * 1' # Mondays 03:00 UTC
+  workflow_dispatch:
+
+permissions:
+  contents: read
+
+concurrency:
+  group: cve-scan-${{ github.ref }}
+  cancel-in-progress: false
+
+env:
+  TRIVY_REPORT_DIR: ${{ github.workspace }}/trivy-reports
+  TARGET_DIR: ${{ github.workspace }}/trivy-targets
+
+jobs:
+  # Downloads the vulnerability DBs once per run. The scan jobs restore them 
read-only and run
+  # without network access.
+  trivy-db:
+    name: Trivy DB
+    # Forks can start the workflow manually, but only apache/gravitino runs 
the schedule.
+    if: github.event_name != 'schedule' || github.repository == 
'apache/gravitino'
+    runs-on: ubuntu-latest
+    timeout-minutes: 20
+    outputs:
+      cache-key: ${{ steps.key.outputs.cache-key }}
+    steps:
+      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # 
v7.0.1
+        with:
+          persist-credentials: false
+      - name: Compute the cache key once for the whole run
+        id: key
+        run: echo "cache-key=trivy-db-v1-$(date -u +%Y-%m-%d)" >> 
"${GITHUB_OUTPUT}"

Review Comment:
   Documented in the README: about 1 GB per daily entry, the 10 GB quota shared 
with the Gradle caches, and per-branch entries for manual runs, so manual runs 
should start from the default branch.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to