Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/execution-report-heartbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ jobs:
FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID }}
EXECUTION_REPORT_GCS_URI: ${{ vars.EXECUTION_REPORT_GCS_URI || secrets.EXECUTION_REPORT_GCS_URI }}
EXECUTION_EVIDENCE_SYNC_URL: ${{ vars.EXECUTION_EVIDENCE_SYNC_URL }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_URL: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_URL }}
QSL_CONSOLE_BASE_URL: ${{ vars.QSL_CONSOLE_BASE_URL }}
EXECUTION_EVIDENCE_SYNC_TOKEN: ${{ secrets.EXECUTION_EVIDENCE_SYNC_TOKEN }}
RUNTIME_TARGET_JSON: ${{ secrets.RUNTIME_TARGET_JSON }}
steps:
Expand Down
49 changes: 48 additions & 1 deletion .github/workflows/ft-adopt-facts-revision.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
CLOUD_RUN_REGION: ${{ vars.CLOUD_RUN_REGION }}
CLOUD_RUN_SERVICE: ${{ secrets.CLOUD_RUN_SERVICE }}
EXPECTED_SHA: ${{ inputs.expected_sha }}
QSL_CONSOLE_BASE_URL: ${{ vars.QSL_CONSOLE_BASE_URL }}
EXECUTION_EVIDENCE_SYNC_URL: ${{ vars.EXECUTION_EVIDENCE_SYNC_URL }}
APPROVED_ACCOUNT_FACTS_SYNC_URL: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_URL }}
steps:
- name: Validate source
run: |
Expand Down Expand Up @@ -73,6 +76,7 @@ jobs:
rev_json="$(gcloud run revisions describe "${revision}" --region="${CLOUD_RUN_REGION}" --format=json)"
REVISION_JSON="${rev_json}" EXPECTED_SHA="${EXPECTED_SHA}" python3 - <<'PY'
import json, os, re, sys
from urllib.parse import urlsplit
rev=json.loads(os.environ["REVISION_JSON"])
expected=os.environ["EXPECTED_SHA"]
labels=((rev.get("metadata") or {}).get("labels") or {})
Expand All @@ -96,14 +100,57 @@ jobs:
def has_value_or_secret(row):
return bool((row or {}).get("value")) or has_secret_ref(row or {})

host_re = re.compile(r"(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}\Z", re.ASCII)

def resolve_origin(raw, allowed_paths):
if not raw or raw != raw.strip() or any(ord(c) < 0x20 or ord(c) == 0x7F for c in raw):
return None
try:
parsed = urlsplit(raw)
port = parsed.port
except ValueError:
return None
host = parsed.hostname or ""
if (
parsed.scheme != "https"
or not host
or parsed.netloc != host
or parsed.username is not None
or parsed.password is not None
or port is not None
or parsed.path not in allowed_paths
or parsed.query
or parsed.fragment
or host_re.fullmatch(host) is None
):
return None
return f"https://{host}"

raw_origin = os.environ.get("QSL_CONSOLE_BASE_URL") or ""
if raw_origin:
expected_origin = resolve_origin(raw_origin, {""})
else:
expected_origin = resolve_origin(
os.environ.get("EXECUTION_EVIDENCE_SYNC_URL") or "",
{"", "/", "/api/execution-evidence/sync"},
)
if not expected_origin:
print("approved_origin_invalid", file=sys.stderr); sys.exit(1)
expected_url = f"{expected_origin}/api/account-facts/sync"
if (os.environ.get("APPROVED_ACCOUNT_FACTS_SYNC_URL") or "") != expected_url:
print("approved_sync_url_invalid", file=sys.stderr); sys.exit(1)

enabled=(values.get("FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED") or {}).get("value")
url=(values.get("FIRSTRADE_ACCOUNT_FACTS_SYNC_URL") or {}).get("value")
console_origin=(values.get("QSL_CONSOLE_BASE_URL") or {}).get("value")
token=values.get("FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN")
needed=["FIRSTRADE_ACCOUNT_FACTS_TARGET_ID","FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID","FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY","FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE"]
if enabled != "true":
print(f"sync_not_enabled value={enabled!r}", file=sys.stderr); sys.exit(1)
if url != "https://qsl-strategy-switch-console.pigbibi-e06.workers.dev/api/account-facts/sync":
if url != expected_url:
print("sync_url_invalid", file=sys.stderr); sys.exit(1)
if console_origin != expected_origin:
print("console_origin_invalid", file=sys.stderr); sys.exit(1)
if not has_secret_ref(token):
keys=sorted((token or {}).keys())
print(f"sync_token_missing keys={keys}", file=sys.stderr); sys.exit(1)
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/publish-archived-account-history.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
CLOUD_RUN_SERVICE: ${{ secrets.CLOUD_RUN_SERVICE }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_URL: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_URL }}
QSL_CONSOLE_BASE_URL: ${{ vars.QSL_CONSOLE_BASE_URL }}
EXECUTION_EVIDENCE_SYNC_URL: ${{ vars.EXECUTION_EVIDENCE_SYNC_URL }}
FIRSTRADE_ACCOUNT_FACTS_TARGET_ID: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_TARGET_ID }}
FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID }}
FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/publish-runtime-daily-once.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ jobs:
FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID }}
EXECUTION_REPORT_GCS_URI: ${{ vars.EXECUTION_REPORT_GCS_URI || secrets.EXECUTION_REPORT_GCS_URI }}
EXECUTION_EVIDENCE_SYNC_URL: ${{ vars.EXECUTION_EVIDENCE_SYNC_URL }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_URL: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_URL }}
QSL_CONSOLE_BASE_URL: ${{ vars.QSL_CONSOLE_BASE_URL }}
EXECUTION_EVIDENCE_SYNC_TOKEN: ${{ secrets.EXECUTION_EVIDENCE_SYNC_TOKEN }}
CLOUD_RUN_SERVICE: ${{ secrets.CLOUD_RUN_SERVICE }}
CLOUD_RUN_REGION: ${{ vars.CLOUD_RUN_REGION }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/runtime-target-lifecycle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ jobs:
env:
CLOUD_RUN_REGION: ${{ vars.CLOUD_RUN_REGION }}
CLOUD_RUN_SERVICE: ${{ secrets.CLOUD_RUN_SERVICE }}
QSL_CONSOLE_BASE_URL: ${{ vars.QSL_CONSOLE_BASE_URL }}
EXECUTION_EVIDENCE_SYNC_URL: ${{ vars.EXECUTION_EVIDENCE_SYNC_URL }}
steps:
- name: Checkout repository
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/sync-cloud-run-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ jobs:
GCP_WORKLOAD_IDENTITY_SERVICE_ACCOUNT: firstrade-platform-deploy@firstradequant.iam.gserviceaccount.com
FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_URL: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_URL }}
QSL_CONSOLE_BASE_URL: ${{ vars.QSL_CONSOLE_BASE_URL }}
EXECUTION_EVIDENCE_SYNC_URL: ${{ vars.EXECUTION_EVIDENCE_SYNC_URL }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN_SECRET_NAME: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN_SECRET_NAME }}
FIRSTRADE_ACCOUNT_FACTS_TARGET_ID: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_TARGET_ID }}
FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID }}
Expand Down Expand Up @@ -190,7 +192,8 @@ jobs:
REVISION_JSON: ${{ runner.temp }}/account-facts-revision-after.json
run: |
set -euo pipefail
env_updates="^|^FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED=${FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED}|FIRSTRADE_ACCOUNT_FACTS_SYNC_URL=${FIRSTRADE_ACCOUNT_FACTS_SYNC_URL}|FIRSTRADE_ACCOUNT_FACTS_TARGET_ID=${FIRSTRADE_ACCOUNT_FACTS_TARGET_ID}|FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID=${FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID}|FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY=${FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY}|FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE=${FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE}"
resolved_console_origin="$(python3 scripts/account_facts_config_sync.py origin)"
env_updates="^|^FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED=${FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED}|FIRSTRADE_ACCOUNT_FACTS_SYNC_URL=${FIRSTRADE_ACCOUNT_FACTS_SYNC_URL}|QSL_CONSOLE_BASE_URL=${resolved_console_origin}|FIRSTRADE_ACCOUNT_FACTS_TARGET_ID=${FIRSTRADE_ACCOUNT_FACTS_TARGET_ID}|FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID=${FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID}|FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY=${FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY}|FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE=${FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE}"
secret_updates="^|^FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN=${FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN_SECRET_NAME}:latest"
if ! gcloud run services update "${CLOUD_RUN_SERVICE}" \
--project="${GCP_PROJECT_ID}" --region="${CLOUD_RUN_REGION}" \
Expand Down Expand Up @@ -532,6 +535,8 @@ jobs:
FIRSTRADE_MFA_CODE_SECRET_NAME: ${{ vars.FIRSTRADE_MFA_CODE_SECRET_NAME }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_URL: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_URL }}
QSL_CONSOLE_BASE_URL: ${{ vars.QSL_CONSOLE_BASE_URL }}
EXECUTION_EVIDENCE_SYNC_URL: ${{ vars.EXECUTION_EVIDENCE_SYNC_URL }}
FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN_SECRET_NAME: ${{ vars.FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN_SECRET_NAME }}
FIRSTRADE_ACCOUNT_FACTS_TARGET_ID: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_TARGET_ID }}
FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID: ${{ secrets.FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ The existing daily Execution Report Heartbeat also invokes this snapshot path on

The Cloud Run service must remain IAM-protected. The caller needs `roles/run.invoker` and must send its Google-signed ID token in `X-Serverless-Authorization`; the separate application token goes in `Authorization: Bearer …`. See [Cloud Run service-to-service authentication](https://cloud.google.com/run/docs/authenticating/service-to-service). Neither token is a Firstrade credential.

Configure the following only through protected Cloud Run environment variables and Secret Manager references: `FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED`, `FIRSTRADE_ACCOUNT_FACTS_SYNC_URL` (must equal the approved sync URL defined as `_APPROVED_SYNC_URL` in `src/firstrade_platform/application/account_facts_publisher.py`), `FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN` (a dedicated secret), `FIRSTRADE_ACCOUNT_FACTS_TARGET_ID`, `FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID`, `FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY`, and `FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE`. The account identity is taken from the single exact selector in the protected runtime target; if `FIRSTRADE_ACCOUNT` is also set, it must match. All values must match the trusted QRS binding and current runtime target. Do not derive or invent a binding ID.
Configure the following only through protected Cloud Run environment variables and Secret Manager references: `FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED`, `QSL_CONSOLE_BASE_URL` (the approved HTTPS console origin; when unset in workflows, the canonical HTTPS origin is extracted and validated from `vars.EXECUTION_EVIDENCE_SYNC_URL`), `FIRSTRADE_ACCOUNT_FACTS_SYNC_URL` (must equal `resolve_approved_sync_url` in `src/firstrade_platform/application/account_facts_publisher.py`, i.e. `<QSL_CONSOLE_BASE_URL>/api/account-facts/sync`), `FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN` (a dedicated secret), `FIRSTRADE_ACCOUNT_FACTS_TARGET_ID`, `FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID`, `FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY`, and `FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE`. The account identity is taken from the single exact selector in the protected runtime target; if `FIRSTRADE_ACCOUNT` is also set, it must match. All values must match the trusted QRS binding and current runtime target. Do not derive or invent a binding ID.

The existing manual Cloud Run environment sync carries the six non-token settings through the selected Firstrade target's `env` configuration in `CLOUD_RUN_SERVICE_TARGETS_JSON`; the legacy single-service path can use matching `FIRSTRADE_ACCOUNT_FACTS_*` inputs. Keep target, binding, account key, and scope in protected GitHub configuration. Set the protected GitHub variable `FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN_SECRET_NAME` to the name of the already-approved Secret Manager secret. The workflow maps that Secret Manager reference to `FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN`; it never accepts a raw token value from GitHub Actions. Keep the enable switch unset or `false` until deployment adoption is separately reviewed. This source change does not apply Cloud Run configuration or establish deployment adoption.

Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ FirstradePlatform 是 QuantStrategyLab 的实验性 Firstrade 执行平台。实

Cloud Run 服务必须继续受 IAM 保护。调用方需要 `roles/run.invoker`,并把 Google 签名的 ID token 放在 `X-Serverless-Authorization`;独立的应用 token 放在 `Authorization: Bearer …`。参见 [Cloud Run 服务间认证文档](https://cloud.google.com/run/docs/authenticating/service-to-service)。这两种 token 都不是 Firstrade 凭据。

以下配置只能通过受保护的 Cloud Run 环境变量和 Secret Manager 引用配置:`FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED`、`FIRSTRADE_ACCOUNT_FACTS_SYNC_URL`(必须与 `src/firstrade_platform/application/account_facts_publisher.py` 中 `_APPROVED_SYNC_URL` 定义的已批准同步地址完全一致)、`FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN`(专用 secret)、`FIRSTRADE_ACCOUNT_FACTS_TARGET_ID`、`FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID`、`FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY` 和 `FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE`。账户身份复用受保护 runtime target 中唯一且精确的 selector;若同时设置 `FIRSTRADE_ACCOUNT`,它必须完全匹配。所有值都必须与 QRS 可信绑定及当前 runtime target 一致;不要推导或编造 binding ID。
以下配置只能通过受保护的 Cloud Run 环境变量和 Secret Manager 引用配置:`FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED`、`QSL_CONSOLE_BASE_URL`(受保护的 HTTPS 控制台 origin;工作流未单独设置时,从 `vars.EXECUTION_EVIDENCE_SYNC_URL` 提取并校验规范 HTTPS origin)、`FIRSTRADE_ACCOUNT_FACTS_SYNC_URL`(必须与 `src/firstrade_platform/application/account_facts_publisher.py` 中 `resolve_approved_sync_url` 计算出的 `<QSL_CONSOLE_BASE_URL>/api/account-facts/sync` 完全一致)、`FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN`(专用 secret)、`FIRSTRADE_ACCOUNT_FACTS_TARGET_ID`、`FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID`、`FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY` 和 `FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_SCOPE`。账户身份复用受保护 runtime target 中唯一且精确的 selector;若同时设置 `FIRSTRADE_ACCOUNT`,它必须完全匹配。所有值都必须与 QRS 可信绑定及当前 runtime target 一致;不要推导或编造 binding ID。

现有手动 Cloud Run 环境同步会从 `CLOUD_RUN_SERVICE_TARGETS_JSON` 中所选 Firstrade target 的 `env` 配置传递六个非 token 项;单服务旧配置路径也可使用匹配的 `FIRSTRADE_ACCOUNT_FACTS_*` 输入。target、binding、account key 和 scope 放在受保护的 GitHub 配置中。受保护的 GitHub variable `FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN_SECRET_NAME` 只填写已批准 Secret Manager secret 的名称;workflow 只把这个 Secret Manager 引用映射到 `FIRSTRADE_ACCOUNT_FACTS_SYNC_TOKEN`,不会接受 GitHub Actions 传入的明文 token。部署另行审查和采用前,启用开关应保持未设置或 `false`。本次源码修改没有应用 Cloud Run 配置,也不代表部署已采用。

Expand Down
63 changes: 59 additions & 4 deletions scripts/account_facts_config_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@
import sys
from collections.abc import Mapping
from typing import Any
from urllib.parse import urlsplit


APPROVED_SYNC_URL = "https://qsl-strategy-switch-console.pigbibi-e06.workers.dev/api/account-facts/sync"
ACCOUNT_FACTS_SYNC_PATH = "/api/account-facts/sync"
APPROVED_CONSOLE_BASE_URL_ENV = "QSL_CONSOLE_BASE_URL"
EXECUTION_EVIDENCE_SYNC_URL_ENV = "EXECUTION_EVIDENCE_SYNC_URL"
_EXPLICIT_ORIGIN_PATHS = frozenset({""})
_FALLBACK_EVIDENCE_PATHS = frozenset({"", "/", "/api/execution-evidence/sync"})
_CONSOLE_HOST_RE = re.compile(
r"(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}\Z",
re.ASCII,
)
FACTS_ENV_KEYS = (
"FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED",
"FIRSTRADE_ACCOUNT_FACTS_SYNC_URL",
"QSL_CONSOLE_BASE_URL",
"FIRSTRADE_ACCOUNT_FACTS_TARGET_ID",
"FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID",
"FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY",
Expand Down Expand Up @@ -53,11 +63,52 @@ def _required(name: str) -> str:
return value


def _parse_console_origin(raw: object, *, allowed_paths: frozenset[str]) -> str:
if not isinstance(raw, str) or not raw or raw != raw.strip():
raise ConfigSyncError("destination_not_approved")
if any(ord(char) < 0x20 or ord(char) == 0x7F for char in raw):
raise ConfigSyncError("destination_not_approved")
try:
parsed = urlsplit(raw)
port = parsed.port
except ValueError:
raise ConfigSyncError("destination_not_approved") from None
host = parsed.hostname or ""
if (
parsed.scheme != "https"
or not host
or parsed.netloc != host
or parsed.username is not None
or parsed.password is not None
or port is not None
or parsed.path not in allowed_paths
or parsed.query
or parsed.fragment
or _CONSOLE_HOST_RE.fullmatch(host) is None
):
raise ConfigSyncError("destination_not_approved")
return f"https://{host}"


def approved_console_origin(environ: Mapping[str, str] | None = None) -> str:
env = os.environ if environ is None else environ
raw_origin = env.get(APPROVED_CONSOLE_BASE_URL_ENV)
if raw_origin is not None and raw_origin != "":
return _parse_console_origin(raw_origin, allowed_paths=_EXPLICIT_ORIGIN_PATHS)
raw_fallback = env.get(EXECUTION_EVIDENCE_SYNC_URL_ENV)
if raw_fallback is not None and raw_fallback != "":
return _parse_console_origin(raw_fallback, allowed_paths=_FALLBACK_EVIDENCE_PATHS)
raise ConfigSyncError("destination_not_approved")


def desired_facts_config() -> dict[str, str]:
enabled = _required("FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED")
if enabled not in {"true", "false"}:
raise ConfigSyncError("configuration_invalid")
if _required("FIRSTRADE_ACCOUNT_FACTS_SYNC_URL") != APPROVED_SYNC_URL:
sync_url = _required("FIRSTRADE_ACCOUNT_FACTS_SYNC_URL")
console_origin = approved_console_origin()
approved_sync_url = f"{console_origin}{ACCOUNT_FACTS_SYNC_PATH}"
if sync_url != approved_sync_url:
raise ConfigSyncError("destination_not_approved")
target_id = _required("FIRSTRADE_ACCOUNT_FACTS_TARGET_ID")
binding_id = _required("FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID")
Expand All @@ -76,7 +127,8 @@ def desired_facts_config() -> dict[str, str]:
raise ConfigSyncError("secret_reference_invalid")
return {
"FIRSTRADE_ACCOUNT_FACTS_SYNC_ENABLED": enabled,
"FIRSTRADE_ACCOUNT_FACTS_SYNC_URL": APPROVED_SYNC_URL,
"FIRSTRADE_ACCOUNT_FACTS_SYNC_URL": approved_sync_url,
"QSL_CONSOLE_BASE_URL": console_origin,
"FIRSTRADE_ACCOUNT_FACTS_TARGET_ID": target_id,
"FIRSTRADE_ACCOUNT_FACTS_SOURCE_BINDING_ID": binding_id,
"FIRSTRADE_ACCOUNT_FACTS_ACCOUNT_KEY": account_key,
Expand Down Expand Up @@ -475,7 +527,7 @@ def _load_stdin() -> Mapping[str, Any]:

def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("mode", choices=("config", "before", "revision", "after"))
parser.add_argument("mode", choices=("config", "origin", "before", "revision", "after"))
parser.add_argument("--service", default=os.environ.get("CLOUD_RUN_SERVICE", ""))
parser.add_argument("--expected-sha", default=os.environ.get("EXPECTED_SHA", ""))
args = parser.parse_args()
Expand All @@ -484,6 +536,9 @@ def main() -> int:
desired_facts_config()
print("account_facts_config_validated=true")
return 0
if args.mode == "origin":
print(desired_facts_config()["QSL_CONSOLE_BASE_URL"])
return 0
payload = _load_stdin()
if not args.service:
raise ConfigSyncError("service_target_unavailable")
Expand Down
Loading
Loading