OpenFGA CLI 是一款跨平台命令行工具,用于与 OpenFGA 服务器交互。OpenFGA 是基于 Google Zanzibar 论文实现的开源细粒度授权(Fine-Grained Authorization)解决方案,而该 CLI 提供了便捷的命令集,支持管理存储实例、授权模型、关系元组及执行权限查询等核心操作,适用于开发、测试及生产环境中 OpenFGA 服务的日常运维与集成。
4.1.1 拉取镜像
bashdocker pull openfga/cli
4.1.2 基本运行
bashdocker run -it openfga/cli --help # 查看帮助
4.1.3 带配置文件运行
挂载本地配置文件(~/.fga.yaml)到容器中:
bashdocker run -it -v ~/.fga.yaml:/root/.fga.yaml openfga/cli store list
4.1.4 环境变量配置示例
bashdocker run -it -e FGA_API_URL=https://api.openfga.example.com \ -e FGA_STORE_ID=01H0H015178Y2V4CX10C2KGHF4 \ openfga/cli model list
4.1.5 Docker Compose 示例
yamlversion: '3' services: fga-cli: image: openfga/cli environment: - FGA_API_URL=https://api.openfga.example.com - 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
4.2.1 Brew(macOS/Linux)
bashbrew install openfga/tap/fga
4.2.2 Linux 包管理
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
4.2.3 Windows
通过 Scoop 安装:
bashscoop install openfga
4.2.4 Go 源码安装
bashgo install github.com/openfga/cli/cmd/fga@latest
4.2.5 手动下载
从 https://github.com/openfga/cli/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: https://api.us1.fga.dev client-id: 4Zb..UYjaHreLKOJuU8 client-secret: J3...2pBwiauD api-audience: https://api.us1.fga.dev/ api-token-issuer: auth.fga.dev store-id: 01H0H015178Y2V4CX10C2KGHF4
5.1.1 创建存储实例
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" } }
5.1.2 导入存储实例
bashfga store import --file store.fga.yaml --max-parallel-requests 8
(支持导入模型、元组及测试用例,文件格式参考 https://github.com/openfga/cli/blob/main/docs/STORE_FILE.md%EF%BC%89
5.1.3 导出存储实例
bash# 导出到终端 fga store export --store-id 01H0H015178Y2V4CX10C2KGHF4 # 导出到文件 fga store export --store-id <id> --output-file backup.fga.yaml
5.1.4 列出存储实例
bashfga store list --max-pages 10
5.1.5 查询存储实例详情
bashfga store get --store-id 01H0H015178Y2V4CX10C2KGHF4
5.1.6 删除存储实例
bashfga store delete --store-id 01H0H015178Y2V4CX10C2KGHF4
5.2.1 写入授权模型
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
5.2.2 查询授权模型
bash# 查询最新模型(FGA 格式) fga model get --store-id <id> # 查询指定模型(JSON 格式) fga model get --store-id <id> --model-id <model-id> --format json
5.2.3 验证授权模型
bashfga model validate --file model.fga --format fga
响应示例(有效模型):
json{"is_valid":true}
5.2.4 运行模型测试用例
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
5.3.1 写入关系元组
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"} ]
5.3.2 查询关系元组
bash# 查询所有元组 fga tuple read --store-id <id> # 按对象筛选 fga tuple read --store-id <id> --object document:roadmap
5.3.3 监控元组变更
bashfga tuple changes --store-id <id> --type document --start-time 2024-01-01T00:00:00Z
5.4.1 权限检查(Check)
bashfga query check --store-id <id> --user user:anne --relation can_view --object document:roadmap
响应示例:
json{"allowed":true}
5.4.2 展开权限链(Expand)
bashfga query expand --store-id <id> --user user:anne --relation can_view --object document:roadmap
5.4.3 列出用户可访问的对象
bashfga query list-objects --store-id <id> --user user:anne --type document --relation can_view
bash# 克隆仓库 git clone https://github.com/openfga/cli.git && cd cli # 构建二进制文件 go build -o ./dist/fga ./cmd/fga/main.go # 或使用 make make build # 运行 ./dist/fga --version
您可以使用以下命令拉取该镜像。请将 <标签> 替换为具体的标签版本。如需查看所有可用标签版本,请访问 标签列表页面。



探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
无需登录使用专属域名
Kubernetes 集群配置 Containerd
K3s 轻量级 Kubernetes 镜像加速
VS Code Dev Containers 配置
Podman 容器引擎配置
HPC 科学计算容器配置
ghcr、Quay、nvcr 等镜像仓库
Harbor Proxy Repository 对接专属域名
Portainer Registries 加速拉取
Nexus3 Docker Proxy 内网缓存
需要其他帮助?请查看我们的 常见问题Docker 镜像访问常见问题解答 或 提交工单
docker search 限制
站内搜不到镜像
离线 save/load
插件要用 plugin install
WSL 拉取慢
安全与 digest
新手拉取配置
镜像合规机制
manifest unknown
no matching manifest(架构)
invalid tar header(解压)
TLS 证书失败
DNS 超时
域名连通性排查
410 Gone 排查
402 与流量用尽
401 认证失败
429 限流
D-Bus 凭证提示
413 与超大单层
来自真实用户的反馈,见证轩辕镜像的优质服务