Splunk的剧作家存储状态生成器

一种为生成预认证浏览器状态(存储状态)的工具 剧作家MCP 访问Splunk时。这允许MCP服务器使用已保存的会话,而不是在参数或日志中公开凭据。
先决条件
- 18+(建议使用LTS)
安装
git clone https://github.com/kshin5/playwright-storage-state.git
cd playwright-storage-state
npm install
npx playwright install --with-deps chromium设置
复制包含的模板并填写您的Splunk凭据(目录结构灵感来自 剧作家认证指南).
cp playwright/.auth/splunk-myenv.env.example playwright/.auth/splunk-myenv.env
# Edit the .env file with your credentials这 .env 文件应包含:
SPLUNK_URL=https://your-splunk-server:8000
SPLUNK_USER=your-username
SPLUNK_PASS=''注: 如果您的密码包含特殊字符,例如#或$,用单引号括起来('...').没有引号,#被视为评论的开头,之后的所有内容都会被忽略,从而导致登录失败。
安全
- 这
.env文件将密码存储在 纯文本。限制访问权限,以便只有您可以阅读。 - 中的凭据文件
playwright/.auth/通过git排除在外.gitignore.只有.env.example模板被跟踪。 - 生成的存储状态文件还包含会话凭据。该脚本在保存后会自动限制文件权限(在Windows上,手动验证权限)。
用法
从项目目录运行:
node generate-storage-state.js [output-path]例子:
node generate-storage-state.js playwright/.auth/splunk-myenv.envWindows用户注意事项: 两者/和\在命令参数中充当路径分隔符。
- 如果
output-path省略,它是通过替换自动生成的.env随着-storage.json(例如。,splunk-myenv.env→splunk-myenv-storage.json). - 您还可以显式指定输出路径:
node generate-storage-state.js playwright/.auth/splunk-myenv.env playwright/.auth/custom-output.json- 如果输出目录不存在,则会自动创建。
管理多个Splunk环境
创建单独 .env 每个环境的文件:
# Development
node generate-storage-state.js playwright/.auth/splunk-dev.env
# Production
node generate-storage-state.js playwright/.auth/splunk-prod.envplaywright/.auth/
├── splunk-dev.env # Dev credentials
├── splunk-dev-storage.json # Dev session
├── splunk-prod.env # Prod credentials
└── splunk-prod-storage.json # Prod session将每个环境注册为单独的MCP服务器,以连接到多个Splunk实例。
与剧作家MCP一起使用
使用以下命令将生成的存储状态文件传递给Playwright MCP服务器 --storage-state 带有a的选项 绝对路径:
@playwright/mcp ... --storage-state /absolute/path/to/splunk-myenv-storage.json使用A 绝对路径 为了--storage-state.相对路径和~MCP服务器可能无法正确解析。
Example: Cursor IDE configuration
首先,找到存储状态文件的绝对路径:
# Run from the project directory
cd playwright-storage-state
echo "$(pwd)/playwright/.auth/splunk-myenv-storage.json"添加以下内容 ~/.cursor/mcp.json (Windows: %USERPROFILE%\.cursor\mcp.json).入口进入 mcpServers 对象。更换 --storage-state 值与上面的路径:
{
"mcpServers": {
"playwright-myenv": {
"command": "npx",
"args": [
"-y", "@playwright/mcp@latest",
"--headless",
"--ignore-https-errors",
"--isolated",
"--storage-state", "/your/absolute/path/playwright-storage-state/playwright/.auth/splunk-myenv-storage.json"
]
}
}
}窗户: 这--storage-state路径同时适用于两者/和\\(例如。,"C:/Users/..."或"C:\\Users\\...").
会话续订
Splunk会话在一段时间后过期。如果遇到身份验证错误,请再次运行相同的命令重新生成存储状态。
局限性
- 该工具针对Splunk的 默认登录页面 (用户名/密码表单)。它不支持SSO、SAML或自定义登录页面。
- 超时在脚本中是固定的:初始页面加载为30秒,登录后重定向为10秒。在慢速网络上,您可能需要在脚本中调整这些值。
故障排除
无法连接到Splunk服务器
- 核实一下
SPLUNK_URL正确且包含方案(https://). - 检查服务器是否正在运行,以及端口是否可以通过任何防火墙访问。
- 对于自签名证书,包括
--ignore-https-errors在MCP服务器参数中(已包含在上述示例中)。
登录失败
- 验证
SPLUNK_USER和SPLUNK_PASS是正确的。 - 脚本等待重定向到
**/app/**登录后。超时可能表示凭据不正确或多因素身份验证(MFA)。检查错误消息输出以了解详细信息。
证书错误(ERR_CERT_AUTHORITY_INVALID)
- 脚本使用
ignoreHTTPSErrors: true,因此证书错误不会影响存储状态的生成。 - 如果遇到MCP服务器的证书错误,请添加
--ignore-https-errors其论点(已包含在上述示例中)。
登录或页面加载时超时
- 该脚本使用固定的超时(页面加载为30秒,登录后重定向为10秒)。如果您在慢速网络上遇到超时,请调整
timeout脚本中的值。
项目结构
playwright-storage-state/
├── generate-storage-state.js # Main script
├── package.json # Dependencies
├── package-lock.json # Lock file
├── .gitignore # Excludes credentials and node_modules/
├── README.md # This file (English)
├── README.ja.md # Japanese README
└── playwright/
└── .auth/
├── splunk-myenv.env.example # Template (tracked in git)
├── splunk-myenv.env # Your credentials (git-ignored)
└── splunk-myenv-storage.json # Generated Storage State (git-ignored)许可证
Apache许可证2.0-请参阅 许可证 了解详情。
