d2f9060b33
build / build (push) Successful in 1m58s
生效状态文件缺失时,扫描与取件仅在显式配置迁移路径时读取旧凭据;默认未配置状态与 API 一致。 新增缺失状态文件的回归,先复现旧凭据被隐式启用,再验证修复。 验证:PP 客户端 7 项隔离服务/配置回归通过;完整 make verify 退出 0,全部本地必需门禁通过。
209 lines
8.5 KiB
Python
209 lines
8.5 KiB
Python
"""PP read-only API client; the only PP authentication protocol implementation.
|
|
|
|
API checks and new worker tasks load the same atomically activated configuration.
|
|
Secrets enter the check subprocess on stdin, never through argv or stdout.
|
|
"""
|
|
import contextlib
|
|
import datetime
|
|
import fcntl
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import stat
|
|
import sys
|
|
import tempfile
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
DEFAULT_BASE = "https://api.dobot.cn"
|
|
DEFAULT_STATE = "/data/source-auth/pp/settings.json"
|
|
MESSAGES = {
|
|
"valid": "认证有效 · 目录可读",
|
|
"incomplete": "配置未完成,请填写账号和密码",
|
|
"auth_failed": "PP 拒绝认证,请检查账号和密码",
|
|
"permission_denied": "已认证,但目录权限不足",
|
|
"unavailable": "PP 暂时无法检查,请稍后重试",
|
|
"invalid_response": "PP 接口地址或返回格式不兼容",
|
|
}
|
|
|
|
class PPError(Exception):
|
|
def __init__(self, state):
|
|
self.state = state
|
|
super().__init__(MESSAGES[state])
|
|
|
|
def observation(state):
|
|
return {"state": state, "message": MESSAGES[state], "checked_at": datetime.datetime.now(datetime.timezone.utc).isoformat()}
|
|
|
|
def validate_base(raw, allow_http=False):
|
|
if not isinstance(raw, str) or len(raw) > 300 or raw.strip() != raw:
|
|
raise PPError("invalid_response")
|
|
try:
|
|
u = urllib.parse.urlsplit(raw)
|
|
if (u.scheme not in (("https", "http") if allow_http else ("https",))
|
|
or not u.hostname or u.username is not None or u.password is not None
|
|
or u.path not in ("", "/") or u.query or u.fragment or "?" in raw or "#" in raw):
|
|
raise PPError("invalid_response")
|
|
_ = u.port
|
|
except ValueError:
|
|
raise PPError("invalid_response") from None
|
|
return raw.rstrip("/")
|
|
|
|
def load_config(path=None):
|
|
p = pathlib.Path(path or os.environ.get("PP_AUTH_STATE_FILE") or DEFAULT_STATE)
|
|
if p.exists():
|
|
info = p.lstat()
|
|
if not stat.S_ISREG(info.st_mode) or stat.S_IMODE(info.st_mode) & 0o077:
|
|
raise PPError("incomplete")
|
|
state = json.loads(p.read_text(encoding="utf-8"))
|
|
config = state.get("active")
|
|
if not isinstance(config, dict):
|
|
raise PPError("incomplete")
|
|
else:
|
|
legacy_path = os.environ.get("PP_LEGACY_CREDENTIALS_FILE")
|
|
if not legacy_path:
|
|
raise PPError("incomplete")
|
|
legacy = pathlib.Path(legacy_path)
|
|
try:
|
|
config = json.loads(legacy.read_text(encoding="utf-8"))
|
|
except (OSError, ValueError):
|
|
raise PPError("incomplete") from None
|
|
config = {**config, "base_url": config.get("base_url") or DEFAULT_BASE, "id": "legacy"}
|
|
validate_base(config.get("base_url"))
|
|
if not config.get("username") or not config.get("password"):
|
|
raise PPError("incomplete")
|
|
return config
|
|
|
|
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
return None
|
|
|
|
class Client:
|
|
def __init__(self, config, allow_http=False, timeout=20):
|
|
self.config = dict(config)
|
|
self.base = validate_base(config.get("base_url"), allow_http)
|
|
self.timeout = timeout
|
|
self.opener = urllib.request.build_opener(NoRedirect())
|
|
|
|
def request(self, path, token="", body=None, login=False):
|
|
headers = {"Accept": "application/json", "User-Agent": "KnowledgeGovernance/PPReadOnly"}
|
|
if token:
|
|
headers["Authorization"] = "Bearer " + token
|
|
if body is not None:
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
|
req = urllib.request.Request(self.base + path, data=body, headers=headers)
|
|
try:
|
|
with self.opener.open(req, timeout=self.timeout) as response:
|
|
raw = response.read((8 << 20) + 1)
|
|
if len(raw) > 8 << 20:
|
|
raise PPError("invalid_response")
|
|
payload = json.loads(raw)
|
|
except urllib.error.HTTPError as error:
|
|
error.close()
|
|
if error.code == 401 or (error.code == 403 and login):
|
|
raise PPError("auth_failed") from None
|
|
if error.code == 403:
|
|
raise PPError("permission_denied") from None
|
|
if error.code in (408, 425, 429) or error.code >= 500:
|
|
raise PPError("unavailable") from None
|
|
raise PPError("invalid_response") from None
|
|
except (TimeoutError, OSError):
|
|
raise PPError("unavailable") from None
|
|
except (ValueError, TypeError):
|
|
raise PPError("invalid_response") from None
|
|
if not isinstance(payload, dict) or not isinstance(payload.get("code"), int) or isinstance(payload.get("code"), bool):
|
|
raise PPError("invalid_response")
|
|
if payload["code"] != 0:
|
|
code = payload["code"]
|
|
if code in (408, 429) or 500 <= code < 600:
|
|
raise PPError("unavailable")
|
|
if login or code == 401:
|
|
raise PPError("auth_failed")
|
|
if code == 403:
|
|
raise PPError("permission_denied")
|
|
raise PPError("invalid_response")
|
|
return payload.get("data")
|
|
|
|
def login(self):
|
|
if not self.config.get("username") or not self.config.get("password"):
|
|
raise PPError("incomplete")
|
|
body = urllib.parse.urlencode({"username": self.config["username"], "password": self.config["password"]}).encode()
|
|
data = self.request("/admin/login/token", body=body, login=True)
|
|
token = data.get("access_token") if isinstance(data, dict) else None
|
|
if not isinstance(token, str) or not token:
|
|
raise PPError("invalid_response")
|
|
return token
|
|
|
|
def page(self, token, current=1, size=200):
|
|
query = urllib.parse.urlencode({"current": current, "size": size})
|
|
data = self.request("/potal/file/back/page?" + query, token=token)
|
|
if (not isinstance(data, dict) or not isinstance(data.get("list"), list)
|
|
or not isinstance(data.get("total"), int) or isinstance(data.get("total"), bool)
|
|
or data["total"] < 0 or not isinstance(data.get("hasNextPage"), bool)):
|
|
raise PPError("invalid_response")
|
|
return data
|
|
|
|
def check(self):
|
|
try:
|
|
self.page(self.login(), size=1)
|
|
return observation("valid")
|
|
except PPError as error:
|
|
return observation(error.state)
|
|
|
|
def inventory(self, token):
|
|
items = []
|
|
for current in range(1, 10001):
|
|
page = self.page(token, current)
|
|
items.extend(page["list"])
|
|
if not page["hasNextPage"]:
|
|
if len(items) != page["total"]:
|
|
raise PPError("invalid_response")
|
|
return {"reported_total": page["total"], "pages": current, "items": items}
|
|
raise PPError("invalid_response")
|
|
|
|
def record_observation(config, result, path=None):
|
|
"""An old task may update only the observation of its own active revision."""
|
|
p = pathlib.Path(path or os.environ.get("PP_AUTH_STATE_FILE") or DEFAULT_STATE)
|
|
if not p.is_file():
|
|
return
|
|
with os.fdopen(os.open(str(p) + ".lock", os.O_CREAT | os.O_RDWR, 0o600), "r+", encoding="utf-8") as lock:
|
|
fcntl.flock(lock, fcntl.LOCK_EX)
|
|
state = json.loads(p.read_text(encoding="utf-8"))
|
|
if not state.get("active") or state["active"].get("id") != config.get("id"):
|
|
return
|
|
old = state.get("observation")
|
|
if result.get("state") == "unavailable" and isinstance(old, dict):
|
|
result = {**result, "previous": old.get("previous") or old}
|
|
state["observation"] = result
|
|
fd, temp = tempfile.mkstemp(prefix=".pp-next-", dir=p.parent)
|
|
try:
|
|
with os.fdopen(fd, "w", encoding="utf-8") as out:
|
|
json.dump(state, out, ensure_ascii=False)
|
|
out.flush()
|
|
os.fsync(out.fileno())
|
|
os.replace(temp, p)
|
|
directory = os.open(p.parent, os.O_RDONLY)
|
|
try:
|
|
os.fsync(directory)
|
|
finally:
|
|
os.close(directory)
|
|
finally:
|
|
with contextlib.suppress(FileNotFoundError):
|
|
os.unlink(temp)
|
|
|
|
def active_client():
|
|
return Client(load_config())
|
|
|
|
def main():
|
|
try:
|
|
config = json.load(sys.stdin)
|
|
result = Client(config).check()
|
|
except PPError as error:
|
|
result = observation(error.state)
|
|
except Exception:
|
|
result = observation("unavailable")
|
|
print(json.dumps(result, ensure_ascii=False))
|
|
|
|
if __name__ == "__main__":
|
|
main()
|