openfga/cliOpenFGA CLI 是一款跨平台命令行工具,用于与 OpenFGA 服务器交互。OpenFGA 是基于 Google Zanzibar 论文实现的开源细粒度授权(Fine-Grained Authorization)解决方案,而该 CLI 提供了便捷的命令集,支持管理存储实例、授权模型、关系元组及执行权限查询等核心操作,适用于开发、测试及生产环境中 OpenFGA 服务的日常运维与集成。
bashdocker pull openfga/cli
bashdocker run -it openfga/cli --help # 查看帮助
挂载本地配置文件(~/.fga.yaml)到容器中:
bashdocker run -it -v ~/.fga.yaml:/root/.fga.yaml openfga/cli store list
bashdocker run -it -e FGA_API_URL=[***] \ -e FGA_STORE_ID=01H0H015178Y2V4CX10C2KGHF4 \ openfga/cli model list
yamlversion: '3' services: fga-cli: image: openfga/cli environment: - FGA_API_URL=[***] - FGA_CLIENT_ID=your-client-id - FGA_CLIENT_SECRET=your-client-secret - FGA_STORE_ID=your-store-id volumes: - ./models:/app/models # 挂载本地模型文件 command: model write --file /app/models/model.fga
bashbrew install openfga/tap/fga
bashsudo apt install ./fga_<version>_linux_<arch>.deb
bashsudo dnf install ./fga_<version>_linux_<arch>.rpm
bashsudo apk add --allow-untrusted ./fga_<version>_linux_<arch>.apk
通过 Scoop 安装:
bashscoop install openfga
bashgo install github.com/openfga/cli/cmd/fga@latest
从 GitHub Releases 下载预编译二进制文件,添加到系统 PATH。
支持三种配置方式(优先级:命令行标志 > 环境变量 > 配置文件):
| 配置项 | 命令行标志 | 环境变量 | 配置文件(~/.fga.yaml) |
|---|---|---|---|
| API 地址 | --api-url | FGA_API_URL | api-url |
| 共享密钥 | --api-token | FGA_API_TOKEN | api-token |
| OAuth 客户端 ID | --client-id | FGA_CLIENT_ID | client-id |
| OAuth 客户端密钥 | --client-secret | FGA_CLIENT_SECRET | client-secret |
| OAuth 作用域 | --api-scopes | FGA_API_SCOPES | api-scopes |
| Token 签发者 | --api-token-issuer | FGA_API_TOKEN_ISSUER | api-token-issuer |
| Token 受众 | --api-audience | FGA_API_AUDIENCE | api-audience |
| 存储实例 ID | --store-id | FGA_STORE_ID | store-id |
| 授权模型 ID | --model-id | FGA_MODEL_ID | model-id |
yaml# Auth0 FGA 示例配置 api-url: [***] client-id: 4Zb..UYjaHreLKOJuU8 client-secret: J3...2pBwiauD api-audience: [***] api-token-issuer: auth.fga.dev store-id: 01H0H015178Y2V4CX10C2KGHF4
bash# 仅创建存储 fga store create --name "FGA Demo Store" # 创建存储并自动导入模型 fga store create --name "Demo" --model model.fga
响应示例:
json{ "store": { "id": "01H6H9CNQRP2TVCFR7899XGNY8", "name": "Demo", "created_at": "2023-07-29T16:58:28.984402Z", "updated_at": "2023-07-29T16:58:28.984402Z" }, "model": { "authorization_model_id": "01H6H9CNQV36Y9WS1RJGRN8D06" } }
bashfga store import --file store.fga.yaml --max-parallel-requests 8
(支持导入模型、元组及测试用例,文件格式参考 Store File Format)
bash# 导出到终端 fga store export --store-id 01H0H015178Y2V4CX10C2KGHF4 # 导出到文件 fga store export --store-id <id> --output-file backup.fga.yaml
bashfga store list --max-pages 10
bashfga store get --store-id 01H0H015178Y2V4CX10C2KGHF4
bashfga store delete --store-id 01H0H015178Y2V4CX10C2KGHF4
bash# 从文件写入(FGA 格式) fga model write --store-id <id> --file model.fga # 从 JSON 字符串写入 fga model write --store-id <id> '{"schema_version":"1.1","type_definitions":[{"type":"user"},{"type":"document","relations":{"can_view":{"this":{}}}}]}' --format json
bash# 查询最新模型(FGA 格式) fga model get --store-id <id> # 查询指定模型(JSON 格式) fga model get --store-id <id> --model-id <model-id> --format json
bashfga model validate --file model.fga --format fga
响应示例(有效模型):
json{"is_valid":true}
bash# 运行单个测试文件 fga model test --tests tests/demo.fga.yaml # 运行目录下所有测试文件 fga model test --tests "tests/**/*.fga.yaml"
测试文件格式示例(YAML):
yamlmodel: | model schema 1.1 type user type document relations define can_view: [user] tuples: - user: user:anne relation: can_view object: document:1 tests: - name: anne_can_view check: - user: user:anne object: document:1 assertions: can_view: true
bash# 单行写入 fga tuple write user:anne can_view document:roadmap --store-id <id> # 批量写入(从文件) fga tuple write --store-id <id> --file tuples.json --max-parallel-requests 8
批量文件示例(JSON):
json[ {"user":"user:bob","relation":"can_edit","object":"document:roadmap"}, {"user":"user:carol","relation":"can_view","object":"document:roadmap"} ]
bash# 查询所有元组 fga tuple read --store-id <id> # 按对象筛选 fga tuple read --store-id <id> --object document:roadmap
bashfga tuple changes --store-id <id> --type document --start-time 2024-01-01T00:00:00Z
bashfga query check --store-id <id> --user user:anne --relation can_view --object document:roadmap
响应示例:
json{"allowed":true}
bashfga query expand --store-id <id> --user user:anne --relation can_view --object document:roadmap
bashfga query list-objects --store-id <id> --user user:anne --type document --relation can_view
bash# 克隆仓库 git clone [***] && cd cli # 构建二进制文件 go build -o ./dist/fga ./cmd/fga/main.go # 或使用 make make build # 运行 ./dist/fga --version



manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务