{"record":{"id":"8204567e6d204007","repo":"iflytek/astron-agent","slug":"signature-generation-error-e","errorCode":null,"errorMessage":"Signature generation error: {e}","messagePattern":"Signature generation error: (.+?)","errorType":"exception","errorClass":"ThirdPartyException","httpStatus":null,"severity":"error","filePath":"core/knowledge/utils/spark_signature.py","lineNumber":27,"sourceCode":"\ndef get_signature(appid: str, ts: int, api_secret: str) -> str:\n    \"\"\"\n    Generate API request signature.\n\n    Args:\n        appid: Application ID\n        ts: Timestamp\n        api_secret: API secret key\n\n    Returns:\n        Signature string\n    \"\"\"\n    try:\n        auth = md5(appid + str(ts))\n        return hmac_sha1_encrypt(auth, api_secret)\n    except Exception as e:\n        logger.error(f\"Signature generation failed: {e}\")\n        raise ThirdPartyException(f\"Signature generation error: {e}\")\n\n\ndef md5(cipher_text: str) -> str:\n    \"\"\"\n    Generate MD5 hash value.\n\n    Args:\n        cipher_text: Text to be hashed\n\n    Returns:\n        MD5 hash string\n    \"\"\"\n    try:\n        data = cipher_text.encode(\"utf-8\")\n        md = hashlib.md5()\n        md.update(data)\n        return md.hexdigest()\n    except Exception as e:","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/knowledge/utils/spark_signature.py#L9-L45","documentation":"get_signature() wraps the whole SparkDesk signature computation (md5 of appid+ts, then HMAC-SHA1 with api_secret) in a try/except; any failure is logged and re-raised as ThirdPartyException(f\"Signature generation error: {e}\"). It indicates bad inputs or missing crypto dependencies rather than a remote call.","triggerScenarios":"Calling get_signature with appid=None/non-string that fails str concatenation, or api_secret missing/None causing hmac_sha1_encrypt to fail (e.g. invalid key type for hmac.new).","commonSituations":"Missing or misconfigured SPARK_APP_ID / SPARK_API_SECRET environment variables; secrets loaded as None from config; copy-pasted secret containing whitespace/newlines causing encode failures.","solutions":["Verify the appid and api_secret config/env values are present, non-empty strings (strip whitespace)","Log/inspect the wrapped exception message to identify the failing primitive (md5 vs hmac)","Add startup validation that raises early if SPARK credentials are missing"],"exampleFix":"// before\nappid = os.getenv(\"SPARK_APP_ID\")  # None\nsig = get_signature(appid, secret)  # Signature generation error\n// after\nappid = os.environ[\"SPARK_APP_ID\"].strip()\nsecret = os.environ[\"SPARK_API_SECRET\"].strip()\nif not appid or not secret: raise ConfigError(\"Spark credentials missing\")\nsig = get_signature(appid, secret)","handlingStrategy":"try-catch","validationCode":"def spark_credentials_ok(appid, secret) -> bool:\n    return bool(appid) and bool(secret) and isinstance(appid, str) and isinstance(secret, str)","typeGuard":"def has_valid_spark_config(cfg: dict) -> bool:\n    return isinstance(cfg.get(\"appid\"), str) and bool(cfg[\"appid\"].strip()) and isinstance(cfg.get(\"api_secret\"), str) and bool(cfg[\"api_secret\"].strip())","tryCatchPattern":"try:\n    headers = assemble_spark_auth_headers_async(...)\nexcept ThirdPartyException as e:\n    logger.error(f\"Spark auth failed: {e}\")\n    raise CredentialsConfigError(\"Check SPARK_APP_ID / SPARK_API_SECRET\") from e","preventionTips":["Validate Spark credentials exist and are strings at startup","Strip whitespace/newlines from copied secrets","Never pass None config values into signature functions","Alert on 'Signature generation failed' logs, they almost always mean config issues"],"tags":["python","auth","hmac","sparkdesk"],"backgroundTag":"signature-generation-failed","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}