JumpServer PAM 账号密码查询 API:Python SDK 集成实战指南
JumpServer PAM 账号密码查询 APIPython SDK 集成实战指南【免费下载链接】jumpserverJumpServer is an open-source Privileged Access Management (PAM) platform that provides DevOps and IT teams with on-demand and secure access to SSH, RDP, Kubernetes, Database and RemoteApp endpoints through a web browser.项目地址: https://gitcode.com/GitHub_Trending/ju/jumpserver导读本文基于 JumpServer开源特权访问管理 PAM 平台官方提供的 Python 示例代码系统讲解如何通过 RESTful API 从 PAM 平台安全地获取资产账号的密码Secret。你将掌握集成应用的创建与 API 密钥获取、基于 HTTPSignatureHMAC-SHA256的请求签名、account-secret接口的参数与响应结构以及开箱即用的jms-pam客户端封装类可直接对接自有系统实现应用免密调用、凭据统一托管的自动化场景。1. 接口能力与适用场景JumpServer 在 apps/accounts/api/account/application.py 中提供了面向集成应用Integration Application的账号密码查询接口其核心能力是以 RESTful 风格提供 PAM 资产账号密码查询服务以 JSON 格式返回数据通过应用级 API 密钥KEY_ID / KEY_SECRET进行身份认证与请求签名无需在第三方系统中明文保存资产密码。典型应用场景包括业务系统启动时自动拉取数据库密码、CI/CD 流水线获取服务器凭据、运维平台代管 SSH 密钥等。底层实现上该接口由IntegrationApplicationViewSet.get_account_secret动作承载并通过 apps/accounts/urls.py 中的路由integration-applications暴露实际权限由 RBAC 与集成应用自身的账户授权模型共同约束。2. 环境要求官方 Python 示例见 apps/accounts/demos/python/demo.py依赖以下运行环境依赖版本要求用途Python3.11解释器版本requests2.31.0HTTP 客户端httpsig1.3.0HTTP 请求签名HTTPSignature 规范安装命令pip install requests2.31.0 httpsig1.3.03. 获取 API 密钥在调用接口之前需要先获得一组 API 密钥登录 JumpServer PAM 控制台进入PAM - 应用管理Gerenciamento de aplicações创建集成应用系统会生成一对KEY_ID与KEY_SECRET。从源码看apps/accounts/models/application.py 中的refresh_secret()使用random_string(36)生成 36 位随机密钥apps/accounts/serializers/account/service.py 在create()时自动调用refresh_secret()完成密钥初始化且支持随时通过管理界面刷新密钥以轮换凭据。密钥具有明确的权限边界集成应用模型 IntegrationApplication 通过accountsJSONManyToManyField 关联的账号集合、ip_group允许访问的 IP 段和is_active字段共同决定了该应用能查询哪些资产账号、从哪些来源 IP 调用、以及当前是否启用。4. 请求规范请求方式GET api/v1/accounts/integration-applications/account-secret/请求参数参数名类型必填说明assetstr是资产名称accountstr是账号名称响应示例{ id: 72b0b0aa-ad82-4182-a631-ae4865e8ae0e, secret: 123456 }4.1 参数校验的源码级细节虽然 README 只列出了asset与account两个参数但从接口层的序列化器 IntegrationAccountSecretSerializer 可以确认接口实际还支持asset_id与account_idUUID 类型当提供account_id时可直接定位账号asset/asset_id/account均可省略否则asset与asset_id必须至少提供一个account与account_id必须至少提供一个。服务端在 apps/accounts/models/application.py 的get_account()中按account_id→account asset_id/asset的顺序精确匹配并最终用应用关联的账号集合过滤确保应用只能取到被授权的账号密码。4.2 返回结果的业务约束接口返回的secret并非始终为明文apps/accounts/api/account/application.py 中若全局安全设置SECURITY_DISABLE_VIEW_SECRET开启则返回secret: null禁止查看密码模式。同时每次成功查询都会写入审计日志IntegrationApplicationLog记录来源 IP、服务名、账号与资产信息满足 PAM 的审计合规要求。5. 最小可用示例demo.py 解析官方示例 apps/accounts/demos/python/demo.py 完整演示了调用过程关键要素如下import requests import os from datetime import datetime from httpsig.requests_auth import HTTPSignatureAuth API_URL os.getenv(API_URL, http://127.0.0.1:8080) KEY_ID os.getenv(API_KEY_ID, 72b0b0aa-ad82-4182-a631-ae4865e8ae0e) KEY_SECRET os.getenv(API_KEY_SECRET, 6fuSO7P1m4cj8SSlgaYdblOjNAmnxDVD7tr8) ORG_ID os.getenv(ORG_ID, 00000000-0000-0000-0000-000000000002) class APIClient: def __init__(self): self.session requests.Session() self.auth HTTPSignatureAuth( key_idKEY_ID, secretKEY_SECRET, algorithmhmac-sha256, headers[(request-target), accept, date, x-jms-org] ) def get_account_secret(self, asset, account): url f{API_URL}/api/v1/accounts/integration-applications/account-secret/ headers { Accept: application/json, X-JMS-ORG: ORG_ID, Date: datetime.utcnow().strftime(%a, %d %b %Y %H:%M:%S GMT), X-Source: jms-pam } params {asset: asset, account: account} try: response self.session.get(url, authself.auth, headersheaders, paramsparams, timeout10) response.raise_for_status() return response.json() except requests.RequestException as e: print(fAPI request failed: {e}) return None if __name__ __main__: client APIClient() result client.get_account_secret(assetubuntu_docker, accountroot) print(result)使用前通过环境变量注入真实密钥export API_URLhttps://your-jms.example.com export API_KEY_ID你的 KEY_ID export API_KEY_SECRET你的 KEY_SECRET export ORG_ID组织 ID python demo.py5.1 签名与请求头说明HTTPSignatureAuth以hmac-sha256算法对指定请求头做签名参与签名的头集合为(request-target)、accept、date、x-jms-org。因此发送请求时必须携带对应头请求头说明Accept: application/json声明返回 JSONDate标准 GMT 格式时间戳如Mon, 09 Sep 2026 02:39:43 GMT参与签名防重放X-JMS-ORG目标组织 IDX-Source调用来源标识示例中固定为jms-pam签名机制保证了请求的完整性与不可否认性任何头部被篡改都会导致签名校验失败。6. 生产级封装jms-pam 客户端除单文件示例外仓库还提供了一套更完整的客户端封装 apps/accounts/demos/python/jms_pam/main.py可通过 setup.py 打包安装python_requires3.6依赖requests与httpsig。其设计分为三个核心类SecretRequest请求参数模型构造时即执行 UUID 合法性校验与参数互斥规则account_id与asset_id不可同时提供等不符合规则时抛出RequestParamsError通过get_url()返回接口路径/api/v1/accounts/service-integrations/account-secret/get_query()组装查询串。Secret响应模型from_response()解析 JSON 并区分成功secret与失败desc携带服务端错误详情from_exception()将网络异常转换为错误描述。JumpServerPAM客户端主类初始化时传入endpoint服务地址、key_id、key_secret和org_id默认00000000-0000-0000-0000-000000000002内部自动生成 GMTDate头、懒加载签名认证器_get_auth缓存HTTPSignatureAuth实例、通过send()统一发送请求并返回Secret对象。使用示例from jms_pam import JumpServerPAM, SecretRequest client JumpServerPAM( endpointhttp://127.0.0.1:8080, key_id你的 KEY_ID, key_secret你的 KEY_SECRET, org_id组织 ID, ) req SecretRequest(assetubuntu_docker, accountroot) secret client.send(req) if secret.valid: print(secret.secret) else: print(secret.desc)7. 常见问题FAQQ如何获取 API KeyA在 PAM - 应用管理中创建集成应用即可生成 KEY_ID 和 KEY_SECRET应用密钥支持刷新轮换见 refresh_secret。Q为什么返回的 secret 是 nullA当全局开启SECURITY_DISABLE_VIEW_SECRET禁止查看密码配置时接口会返回secret: null这是 PAM 的安全策略并非接口异常。Q调用报 403 / 账号找不到如何处理A请依次检查集成应用is_active是否启用、调用来源 IP 是否在ip_group白名单内、目标账号是否已关联到该应用的accounts集合、asset/account名称是否与资产、账号名称完全一致。Q示例中演示了哪些语言的实现A仓库在 apps/accounts/demos 下提供了 Python、Java、Go、Node.js、curl 等多语言示例README 亦有多语言版本签名与调用逻辑一致可互相参考迁移。8. 版本历史Changelog版本号变更内容日期1.0.0初始版本2025-02-11【免费下载链接】jumpserverJumpServer is an open-source Privileged Access Management (PAM) platform that provides DevOps and IT teams with on-demand and secure access to SSH, RDP, Kubernetes, Database and RemoteApp endpoints through a web browser.项目地址: https://gitcode.com/GitHub_Trending/ju/jumpserver创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考