Initial project source

这个提交包含在:
apple
2026-11-20 15:32:57 +08:00
当前提交 fb47c07f7c
共修改 8405 个文件,包含 806907 行新增和 0 行删除
+489
查看文件
@@ -0,0 +1,489 @@
#!/usr/bin/env bash
set -euo pipefail
readonly INSTANCE="${1:-}"
case "${INSTANCE}" in
001)
readonly TASK_ID="M7-MANAGED-CREDENTIAL-BACKUP-RESTORE-ISOLATED-DRILL-001"
;;
002)
readonly TASK_ID="M7-MANAGED-CREDENTIAL-DOCKER-PERMISSION-RECOVERY-002"
;;
003)
readonly TASK_ID="M7-MANAGED-CREDENTIAL-DOCKER-START-OBSERVABILITY-RECOVERY-003"
;;
004)
readonly TASK_ID="M7-MANAGED-CREDENTIAL-SCHEMA-LOG-PERSISTENCE-RECOVERY-004"
;;
005)
readonly TASK_ID="M7-MANAGED-CREDENTIAL-AUTHENTICATED-READINESS-FIX-005"
;;
006)
readonly TASK_ID="M7-MANAGED-CREDENTIAL-JAVA-SQL-CLIENT-DIAGNOSTIC-006"
;;
007)
readonly TASK_ID="M7-MANAGED-CREDENTIAL-COLLATION-LITERAL-FIX-007"
;;
*)
echo "usage: run_isolated_drill.sh 001|002|003|004|005|006|007" >&2
exit 64
;;
esac
readonly PREFIX="aixun-datagov-poc-m7-credential-dr-${INSTANCE}"
readonly MYSQL_CONTAINER="${PREFIX}-mysql"
readonly SOURCE_SCHEMA="aixun_datagov_credential_dr_source_${INSTANCE}"
readonly TARGET_SCHEMA="aixun_datagov_credential_dr_target_${INSTANCE}"
readonly MYSQL_IMAGE="mysql:8.4.9@sha256:c36050afdca850f23cef85703f84c7531a5ae155a11b5ee1c60acb09937c4084"
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
readonly OUT="${PROJECT_ROOT}/evidence/M7/${TASK_ID}"
readonly TEST_CLASS="cn.aixunkeji.datagov.integration.secret.ManagedCredentialBackupRestoreIsolatedDrillTest"
readonly SOURCE_MIGRATION="${PROJECT_ROOT}/db/migration/integration/V20260825.006__create_m2_integration_records.sql"
readonly EXTENSION_MIGRATION="${PROJECT_ROOT}/db/migration/integration/V20260825.011__extend_data_source_management.sql"
readonly BINDING_MIGRATION="${PROJECT_ROOT}/db/migration/integration/V20260824.002__create_secret_binding.sql"
TASK_TMP=""
PHASE="opening"
POC_STARTED="false"
TERMINAL_STATUS="BLOCKED"
DOCKER_RUN_EXIT_CODE="NOT_RUN"
mkdir -p "${OUT}"
chmod 700 "${OUT}"
umask 077
set_phase() {
local next_phase="$1"
local state_tmp="${OUT}/phase-state.properties.tmp"
local marker="${OUT}/phase-${next_phase}.marker"
local marker_tmp="${marker}.tmp"
if [[ ! "${next_phase}" =~ ^[a-z0-9-]+$ ]]; then
echo "unsafe phase name" >&2
return 78
fi
PHASE="${next_phase}"
printf 'phase=%s\n' "${PHASE}" >"${state_tmp}"
chmod 600 "${state_tmp}"
mv "${state_tmp}" "${OUT}/phase-state.properties"
printf 'reached=true\n' >"${marker_tmp}"
chmod 600 "${marker_tmp}"
mv "${marker_tmp}" "${marker}"
}
copy_safe_docker_stderr() {
local source="$1"
local destination="${OUT}/docker-run.stderr"
local temporary="${destination}.tmp"
LC_ALL=C sed -E \
-e 's/([Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]|[Tt][Oo][Kk][Ee][Nn]|[Ss][Ee][Cc][Rr][Ee][Tt])=[^[:space:]]+/\1=<redacted>/g' \
-e 's/[[:cntrl:]]//g' \
"${source}" | awk 'NR <= 40 { print substr($0, 1, 1000) }' >"${temporary}"
chmod 600 "${temporary}"
mv "${temporary}" "${destination}"
}
persist_safe_mysql_log() {
local source="$1"
local destination="$2"
local phase="$3"
local target="$4"
local exit_code="$5"
local temporary="${destination}.tmp"
{
printf 'phase=%s\n' "${phase}"
printf 'target=%s\n' "${target}"
printf 'exitCode=%s\n' "${exit_code}"
LC_ALL=C sed -E \
-e 's/([Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]|[Tt][Oo][Kk][Ee][Nn]|[Ss][Ee][Cc][Rr][Ee][Tt])=[^[:space:]]+/\1=<redacted>/g' \
-e "s/'[^']*'/<redacted-sql-fragment>/g" \
-e 's/[[:cntrl:]]//g' \
"${source}" | awk '
/^(ERROR|Warning|mysql: \[Warning\])/ {
print "diagnostic=" substr($0, 1, 1000)
count++
}
count >= 40 { exit }
'
} >"${temporary}"
chmod 600 "${temporary}"
mv "${temporary}" "${destination}"
}
run_004_schema_init() {
local raw_log="${TASK_TMP}/schema-init.log.raw"
set_phase "schema-init"
set +e
{
printf 'CREATE DATABASE %s CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n' "${SOURCE_SCHEMA}"
printf 'CREATE DATABASE %s CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n' "${TARGET_SCHEMA}"
} | docker exec -i "${MYSQL_CONTAINER}" mysql \
--defaults-extra-file=/run/secrets/client.cnf >"${raw_log}" 2>&1
local exit_code="$?"
set -e
persist_safe_mysql_log "${raw_log}" "${OUT}/schema-init.log" "schema-init" "source-and-target" "${exit_code}"
rm -f -- "${raw_log}"
if [[ "${exit_code}" != "0" ]]; then
return "${exit_code}"
fi
}
run_004_migration() {
local scope="$1"
local schema="$2"
local migration_name="$3"
local migration_file="$4"
local phase="migration-${scope}-${migration_name}"
local raw_log="${TASK_TMP}/${phase}.log.raw"
set_phase "${phase}"
set +e
docker exec -i "${MYSQL_CONTAINER}" mysql \
--defaults-extra-file=/run/secrets/client.cnf --database="${schema}" \
<"${migration_file}" >"${raw_log}" 2>&1
local exit_code="$?"
set -e
persist_safe_mysql_log "${raw_log}" "${OUT}/${phase}.log" "${phase}" "${scope}" "${exit_code}"
rm -f -- "${raw_log}"
if [[ "${exit_code}" != "0" ]]; then
return "${exit_code}"
fi
}
set_phase "opening"
if [[ -e "${OUT}/poc-started.marker" ]]; then
echo "real POC already started; rerun refused" >&2
exit 73
fi
snapshot_containers() {
local destination="$1"
local temporary="${destination}.tmp"
local names
local name
rm -f -- "${temporary}" "${destination}"
if ! names="$(docker ps -a --format '{{.Names}}' | LC_ALL=C sort)"; then
return 1
fi
: >"${temporary}"
while IFS= read -r name; do
[[ -z "${name}" ]] && continue
if ! docker inspect --format '{{.Name}}|{{.Id}}|{{.Image}}|{{.Created}}|{{index .Config.Labels "com.docker.compose.project"}}|{{index .Config.Labels "com.aixun.project"}}' "${name}" >>"${temporary}"; then
rm -f -- "${temporary}"
return 1
fi
done <<<"${names}"
mv "${temporary}" "${destination}"
}
snapshot_networks() {
local destination="$1"
local temporary="${destination}.tmp"
rm -f -- "${temporary}" "${destination}"
if ! docker network ls --no-trunc --format '{{.ID}}|{{.Name}}|{{.Driver}}|{{.Scope}}' |
LC_ALL=C sort >"${temporary}"; then
rm -f -- "${temporary}"
return 1
fi
mv "${temporary}" "${destination}"
}
snapshot_volumes() {
local destination="$1"
local temporary="${destination}.tmp"
rm -f -- "${temporary}" "${destination}"
if ! docker volume ls --format '{{.Name}}|{{.Driver}}' | LC_ALL=C sort >"${temporary}"; then
rm -f -- "${temporary}"
return 1
fi
mv "${temporary}" "${destination}"
}
task_container_count() {
local names
if ! names="$(docker ps -a --filter "name=^/${MYSQL_CONTAINER}$" --format '{{.Names}}')"; then
return 1
fi
awk 'NF { count++ } END { print count+0 }' <<<"${names}"
}
task_network_count() {
local names
if ! names="$(docker network ls --filter "name=^${PREFIX}" --format '{{.Name}}')"; then
return 1
fi
awk 'NF { count++ } END { print count+0 }' <<<"${names}"
}
task_volume_count() {
local names
if ! names="$(docker volume ls --filter "name=${PREFIX}" --format '{{.Name}}')"; then
return 1
fi
awk 'NF { count++ } END { print count+0 }' <<<"${names}"
}
task_tmp_count() {
find /private/tmp -maxdepth 1 -type d -name "${PREFIX}.*" -print | awk 'NF { count++ } END { print count+0 }'
}
copy_safe_result() {
local source="${TASK_TMP}/drill-result.properties"
if [[ -f "${source}" ]]; then
awk -F= '
/^#/ { next }
$1 ~ /^(status|failureClass|cutFrozen|archiveAuthenticated|archiveCiphertextSha256|archiveCiphertextBytes|sourceRowCount|sourceBindingCount|targetRowCount|targetBindingCount|sourceOptimisticVersion|bindingOptimisticVersion|optimisticVersionsIndependent|ownerReferenceVersionFingerprintMatch|directoryMode|fileMode|restoredFileCount|restoredDirectoryCount|symlinkCount|temporaryFileCount|retryMarkerCount|authorizedResolverMatch|unauthorizedResolverRejected|rpoSeconds|rtoSeconds|rpoWithin86400|rtoWithin14400|archiveHashRejected|archiveAuthenticationRejected|archiveCutRejected|archiveParseRejected|targetNonEmptyRejected|pathTraversalRejected|symlinkRejected|permissionRejected|ownerMismatchRejected|referenceMismatchRejected|versionMismatchRejected|fingerprintMismatchRejected|importFailureRejected|resolverFailureRejected|testKeyDeleted|liveCredentialAccessCount|liveDatabaseAccessCount|liveVolumeAccessCount|nacosAccessCount|browserAccessCount|productionBlocked)$/ { print }
' "${source}" >"${OUT}/drill-result.properties"
chmod 600 "${OUT}/drill-result.properties"
fi
}
cleanup_exact_objects() {
docker rm -f "${MYSQL_CONTAINER}" >/dev/null 2>&1 || true
if [[ -n "${TASK_TMP}" && "${TASK_TMP}" == "/private/tmp/${PREFIX}."* && -d "${TASK_TMP}" ]]; then
chmod -R u+rwX "${TASK_TMP}" >/dev/null 2>&1 || true
rm -rf -- "${TASK_TMP}"
fi
}
finalize() {
local exit_code=$?
local failure_phase="NONE"
if [[ "${TERMINAL_STATUS}" != "PASS_WITH_LIMITATION" || "${exit_code}" != "0" ]]; then
failure_phase="${PHASE}"
fi
set +e
set_phase "cleanup"
copy_safe_result
cleanup_exact_objects
local docker_observable="true"
snapshot_containers "${OUT}/d020-containers-after.identity" || docker_observable="false"
snapshot_networks "${OUT}/d020-networks-after.identity" || docker_observable="false"
snapshot_volumes "${OUT}/d020-volumes-after.identity" || docker_observable="false"
local containers_same="UNKNOWN"
local networks_same="UNKNOWN"
local volumes_same="UNKNOWN"
if [[ -f "${OUT}/d020-containers-before.identity" && -f "${OUT}/d020-containers-after.identity" ]]; then
containers_same="false"
cmp -s "${OUT}/d020-containers-before.identity" "${OUT}/d020-containers-after.identity" && containers_same="true"
fi
if [[ -f "${OUT}/d020-networks-before.identity" && -f "${OUT}/d020-networks-after.identity" ]]; then
networks_same="false"
cmp -s "${OUT}/d020-networks-before.identity" "${OUT}/d020-networks-after.identity" && networks_same="true"
fi
if [[ -f "${OUT}/d020-volumes-before.identity" && -f "${OUT}/d020-volumes-after.identity" ]]; then
volumes_same="false"
cmp -s "${OUT}/d020-volumes-before.identity" "${OUT}/d020-volumes-after.identity" && volumes_same="true"
fi
local container_after="UNKNOWN"
local network_after="UNKNOWN"
local volume_after="UNKNOWN"
local tmp_after
local observed_count
if observed_count="$(task_container_count)"; then
container_after="${observed_count}"
else
docker_observable="false"
fi
if observed_count="$(task_network_count)"; then
network_after="${observed_count}"
else
docker_observable="false"
fi
if observed_count="$(task_volume_count)"; then
volume_after="${observed_count}"
else
docker_observable="false"
fi
tmp_after="$(task_tmp_count)"
local docker_volume_created="UNKNOWN"
if [[ -f "${OUT}/d020-volumes-before.identity" && -f "${OUT}/d020-volumes-after.identity" ]]; then
docker_volume_created="$(( $(wc -l <"${OUT}/d020-volumes-after.identity") - $(wc -l <"${OUT}/d020-volumes-before.identity") ))"
fi
{
echo "status=${TERMINAL_STATUS}"
echo "exitCode=${exit_code}"
echo "failurePhase=${failure_phase}"
echo "dockerRunExitCode=${DOCKER_RUN_EXIT_CODE}"
echo "pocStarted=${POC_STARTED}"
echo "dockerApiObservable=${docker_observable}"
echo "containerCountAfter=${container_after}"
echo "networkCountAfter=${network_after}"
echo "volumeCountAfter=${volume_after}"
echo "temporaryDirectoryCountAfter=${tmp_after}"
echo "d020ContainerIdentityUnchanged=${containers_same}"
echo "d020NetworkIdentityUnchanged=${networks_same}"
echo "d020VolumeIdentityUnchanged=${volumes_same}"
echo "dockerVolumeCreatedCount=${docker_volume_created}"
} >"${OUT}/terminal-state.properties"
chmod 600 "${OUT}/terminal-state.properties"
if [[ "${TERMINAL_STATUS}" == "PASS_WITH_LIMITATION" &&
"${docker_observable}" == "true" &&
"${container_after}" == "0" && "${network_after}" == "0" &&
"${volume_after}" == "0" && "${tmp_after}" == "0" &&
"${containers_same}" == "true" && "${networks_same}" == "true" && "${volumes_same}" == "true" ]]; then
echo "POC_STATUS=PASS_WITH_LIMITATION"
return 0
fi
echo "POC_STATUS=BLOCKED PHASE=${PHASE} EXIT=${exit_code}" >&2
return "${exit_code:-1}"
}
trap finalize EXIT
set_phase "opening-snapshots"
container_before="$(task_container_count)"
network_before="$(task_network_count)"
volume_before="$(task_volume_count)"
tmp_before="$(task_tmp_count)"
if [[ "${container_before}" != "0" || "${network_before}" != "0" ||
"${volume_before}" != "0" || "${tmp_before}" != "0" ]]; then
echo "task-private object precondition failed" >&2
exit 74
fi
{
echo "containerCountBefore=${container_before}"
echo "networkCountBefore=${network_before}"
echo "volumeCountBefore=${volume_before}"
echo "temporaryDirectoryCountBefore=${tmp_before}"
} >"${OUT}/task-objects-before.properties"
snapshot_containers "${OUT}/d020-containers-before.identity"
snapshot_networks "${OUT}/d020-networks-before.identity"
snapshot_volumes "${OUT}/d020-volumes-before.identity"
set_phase "local-image"
image_id="$(docker image inspect --format '{{.Id}}' "${MYSQL_IMAGE}")"
if [[ "${image_id}" != "sha256:c36050afdca850f23cef85703f84c7531a5ae155a11b5ee1c60acb09937c4084" ]]; then
echo "fixed local MySQL image identity mismatch" >&2
exit 75
fi
echo "imageId=${image_id}" >"${OUT}/local-image-identity.properties"
set_phase "preflight"
"${PROJECT_ROOT}/script/datagov/preflight.sh" >"${OUT}/preflight.log" 2>&1
set_phase "poc-start"
printf '%s\n' "startedAt=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >"${OUT}/poc-started.marker"
POC_STARTED="true"
TASK_TMP="$(mktemp -d "/private/tmp/${PREFIX}.XXXXXX")"
chmod 700 "${TASK_TMP}"
openssl rand -hex 32 >"${TASK_TMP}/mysql-root-secret"
chmod 400 "${TASK_TMP}/mysql-root-secret"
{
printf '[client]\nuser=root\npassword='
tr -d '\n' <"${TASK_TMP}/mysql-root-secret"
printf '\n'
} >"${TASK_TMP}/client.cnf"
chmod 400 "${TASK_TMP}/client.cnf"
{
echo "drill.instance=${INSTANCE}"
echo "container.name=${MYSQL_CONTAINER}"
echo "source.schema=${SOURCE_SCHEMA}"
echo "target.schema=${TARGET_SCHEMA}"
echo "workspace=${TASK_TMP}"
echo "result.file=${TASK_TMP}/drill-result.properties"
} >"${TASK_TMP}/runtime.properties"
chmod 400 "${TASK_TMP}/runtime.properties"
set_phase "mysql-start"
set +e
docker run -d \
--name "${MYSQL_CONTAINER}" \
--pull never \
--network none \
--label com.aixun.project=aixun-datagov \
--label "com.aixun.task=${TASK_ID}" \
--label com.aixun.purpose=managed-credential-isolated-drill \
--tmpfs /var/lib/mysql:rw,nosuid,nodev,noexec,mode=0700,size=1073741824 \
--tmpfs /tmp:rw,nosuid,nodev,noexec,mode=1777,size=67108864 \
--mount "type=bind,source=${TASK_TMP}/mysql-root-secret,target=/run/secrets/root,readonly" \
--mount "type=bind,source=${TASK_TMP}/client.cnf,target=/run/secrets/client.cnf,readonly" \
-e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/root \
"${MYSQL_IMAGE}" --skip-log-bin >"${OUT}/docker-run.stdout" 2>"${TASK_TMP}/docker-run.stderr.raw"
DOCKER_RUN_EXIT_CODE="$?"
set -e
chmod 600 "${OUT}/docker-run.stdout"
copy_safe_docker_stderr "${TASK_TMP}/docker-run.stderr.raw"
rm -f -- "${TASK_TMP}/docker-run.stderr.raw"
{
echo "phase=mysql-start"
echo "exitCode=${DOCKER_RUN_EXIT_CODE}"
} >"${OUT}/docker-run.properties"
chmod 600 "${OUT}/docker-run.properties"
if [[ "${DOCKER_RUN_EXIT_CODE}" != "0" ]]; then
exit "${DOCKER_RUN_EXIT_CODE}"
fi
set_phase "mysql-ready"
ready="false"
for _ in $(seq 1 90); do
if docker exec "${MYSQL_CONTAINER}" mysql \
--defaults-extra-file=/run/secrets/client.cnf \
--batch --raw --skip-column-names --execute='SELECT 1' >/dev/null 2>&1; then
ready="true"
break
fi
sleep 1
done
if [[ "${ready}" != "true" ]]; then
echo "task-private MySQL did not become ready" >&2
exit 76
fi
if [[ "${INSTANCE}" == "004" || "${INSTANCE}" == "005" || "${INSTANCE}" == "006" ||
"${INSTANCE}" == "007" ]]; then
run_004_schema_init
run_004_migration "source" "${SOURCE_SCHEMA}" "source" "${SOURCE_MIGRATION}"
run_004_migration "source" "${SOURCE_SCHEMA}" "extension" "${EXTENSION_MIGRATION}"
run_004_migration "source" "${SOURCE_SCHEMA}" "binding" "${BINDING_MIGRATION}"
run_004_migration "target" "${TARGET_SCHEMA}" "source" "${SOURCE_MIGRATION}"
run_004_migration "target" "${TARGET_SCHEMA}" "extension" "${EXTENSION_MIGRATION}"
run_004_migration "target" "${TARGET_SCHEMA}" "binding" "${BINDING_MIGRATION}"
else
set_phase "schema-init"
{
printf 'CREATE DATABASE %s CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n' "${SOURCE_SCHEMA}"
printf 'CREATE DATABASE %s CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n' "${TARGET_SCHEMA}"
} | docker exec -i "${MYSQL_CONTAINER}" mysql \
--defaults-extra-file=/run/secrets/client.cnf >"${TASK_TMP}/schema-init.log" 2>&1
for schema in "${SOURCE_SCHEMA}" "${TARGET_SCHEMA}"; do
set_phase "migration-source"
docker exec -i "${MYSQL_CONTAINER}" mysql \
--defaults-extra-file=/run/secrets/client.cnf --database="${schema}" \
<"${SOURCE_MIGRATION}" >"${TASK_TMP}/migration-source.log" 2>&1
set_phase "migration-extension"
docker exec -i "${MYSQL_CONTAINER}" mysql \
--defaults-extra-file=/run/secrets/client.cnf --database="${schema}" \
<"${EXTENSION_MIGRATION}" >"${TASK_TMP}/migration-extension.log" 2>&1
set_phase "migration-binding"
docker exec -i "${MYSQL_CONTAINER}" mysql \
--defaults-extra-file=/run/secrets/client.cnf --database="${schema}" \
<"${BINDING_MIGRATION}" >"${TASK_TMP}/migration-binding.log" 2>&1
done
fi
set_phase "focused-maven-poc"
"${PROJECT_ROOT}/mvnw" \
-pl aixun-modules/aixun-integration \
-am \
-Dtest="${TEST_CLASS}" \
-Dsurefire.failIfNoSpecifiedTests=false \
-Ddatagov.managed.credential.dr=true \
-Ddatagov.managed.credential.dr.config-file="${TASK_TMP}/runtime.properties" \
test >"${OUT}/focused-maven-poc.log" 2>&1
set_phase "result-check"
if [[ ! -f "${TASK_TMP}/drill-result.properties" ]] ||
! grep -qx 'status=PASS' "${TASK_TMP}/drill-result.properties"; then
echo "focused drill did not produce PASS" >&2
exit 77
fi
TERMINAL_STATUS="PASS_WITH_LIMITATION"
set_phase "complete"
exit 0