使用文件初始化和管理数据
本指南介绍如何从 JSON 或 YAML 文件将数据加载到 Casdoor,如何让 Casdoor 使其对象与该文件保持同步,以及如何导出运行中实例的数据。
学习成果
- 在 Casdoor 启动时加载组织、应用、用户和其他对象。
- 将 Casdoor 对象保存在 Git 中的文件中,并在无需重启的情况下应用更改。
- 导出一个实例的数据并将其加载到另一个实例中。
所需条件
- 一个 Casdoor 实例及其
conf/app.conf文件或环境变量的访问权限 - 对于 Kubernetes 部分:集群的
kubectl访问权限,或 Helm chart
关于初始化数据文件
当您将 Casdoor 作为更大产品的一部分进行交付时,可以预加载组织、应用、用户和其他对象,以便该产品无需手动设置即可工作。Casdoor 从一个文件(即初始化数据文件)读取这些对象。
该文件有两个用途:
- 初始化:Casdoor 在启动时加载该文件一次。
- 配置即代码:Casdoor 监视该文件并在运行时应用每次更改。请参阅管理配置即代码。
模板位于 init_data.json.template。以 .yaml 或 .yml 结尾的文件将作为具有相同结构的 YAML 读取。
在启动时加载数据
-
将
init_data.json.template复制到 Casdoor 运行目录下的init_data.json,并进行编辑。若要将文件保留在其他位置,请在
conf/app.conf中设置initDataFile:initDataFile = /path/to/your/init_data.json -
选择对已存在对象的处理方式。在
conf/app.conf中最多设置以下选项之一:Option Objects that already exist Neither option (default) Deleted and created again from the file at every start. Changes made in the admin console are lost on restart initDataNewOnly = trueLeft as they are. Casdoor only adds objects that don't exist yet initDataMerge = trueUpdated with only the fields that the file contains. See Manage configuration as code -
启动 Casdoor。
在 Docker 中加载文件
将文件挂载到容器中:
docker run ... -v /path/to/init_data.json:/init_data.json
在 Kubernetes 中加载文件
使用 Helm chart,将对象放在 values 文件的 initData.data 下,并设置 initData.enabled: true。请参阅管理配置即代码。
不使用 chart 时:
-
将文件存储在 Secret 中,因为它通常包含密码和客户端密钥。ConfigMap 也可以。
apiVersion: v1
kind: Secret
metadata:
name: casdoor-init-data
stringData:
init_data.yaml: |
organizations:
- owner: admin
name: acme
displayName: Acme -
将 Secret 作为目录挂载,并将
initDataFile指向其中的文件:apiVersion: apps/v1
kind: Deployment
...
spec:
template:
...
spec:
containers:
...
env:
- name: initDataFile
value: /init-data/init_data.yaml
- name: initDataMerge
value: "true"
- name: initDataWatchInterval
value: "30"
volumeMounts:
- mountPath: /init-data
name: casdoor-init-data-volume
readOnly: true
volumes:
- secret:
secretName: casdoor-init-data
name: casdoor-init-data-volume
不要使用 subPath 挂载文件。当 Secret 或 ConfigMap 发生变化时,Kubernetes 不会更新 subPath 挂载,因此 Casdoor 永远看不到新内容。
管理配置即代码
为了将组织、应用、用户、提供商、角色和权限保留在 Git 中,并像其他配置一样推出更改,让 Casdoor 持续应用该文件。
-
在
conf/app.conf中设置以下选项,或通过同名环境变量传递它们:initDataFile = ./init_data.yaml
initDataMerge = true
initDataWatchInterval = 30 -
在文件中编写对象。例如,
init_data.yaml:organizations:
- owner: admin
name: acme
displayName: Acme
passwordType: bcrypt
applications:
- owner: admin
name: app-acme
organization: acme
displayName: Acme Portal
redirectUris:
- https://portal.acme.example.com/callback
users:
- owner: acme
name: alice
displayName: Alice
password: change-me
signupApplication: app-acme
roles:
- owner: acme
name: admins
displayName: Admins
users: [acme/alice]
isEnabled: true -
启动 Casdoor。从此以后,每当文件内容发生变化时,Casdoor 都会重新应用该文件。
使用 Helm chart 时,将相同的对象放在 initData.data 下。chart 会将它们存储在 Secret 中,并默认启用合并和监视功能:
initData:
enabled: true
data:
organizations:
- owner: admin
name: acme
displayName: Acme
正在运行的 Pod 会在 initData.watchInterval 秒内应用更改了 initData.data 的 helm upgrade,再加上 kubelet 更新挂载的 Secret 所需的时间(大约一分钟)。
Casdoor 如何应用文件
| Behavior | Description |
|---|---|
| Merge | Casdoor updates an existing object with only the fields that the file contains. The other fields keep their values. The file can therefore manage a few settings of an object, such as the redirect URLs of an application, while you edit the rest in the admin console |
| Watch | Casdoor checks the file every initDataWatchInterval seconds and applies it again when the content has changed, without a restart |
| Passwords | Casdoor uses the password of a user only when it creates the user, so users can change their passwords afterward. Casdoor writes organization secrets, such as masterPassword, on every apply when the file contains them |
| Removed objects | Casdoor doesn't delete objects that you remove from the file |
| Runtime data | Casdoor skips records and sessions in merge mode |
| Errors | If an object fails to apply, Casdoor logs the error once and keeps running with the objects that it applied up to that point. It retries the file on every check until the file applies |
如果您更喜欢 terraform plan、导入现有对象以及漂移检测,请使用 Terraform provider。它通过 API 管理相同的对象。
导出数据
将 Casdoor 实例的所有数据导出为 JSON 文件,用于备份或迁移。
使用二进制文件导出
运行 Casdoor 时添加 -export 标志。这是推荐的方式。它适用于二进制文件、Docker 和 Kubernetes,并且不需要 Go 工具链。
# Export to default location (init_data_dump.json)
./casdoor -export
# Export to a custom path
./casdoor -export -exportPath /path/to/backup.json
Casdoor 初始化数据库连接,写入文件,然后退出。
从源代码导出
在 Casdoor 源码树中,运行:
go test ./object -v -run TestDumpToFile
该命令会在 object 目录中创建 init_data_dump.json。
将导出的数据加载到另一个实例
- 将
init_data_dump.json重命名为init_data.json。 - 将该文件放入目标 Casdoor 实例运行的目录中。
- 启动目标实例。它会在启动时加载数据。
支持的对象
初始数据文件可以包含以下对象:
| 对象 | Go 结构体 | 文档 |
|---|---|---|
| 组织机构 | struct | doc |
| 应用 | struct | doc |
| 用户 | struct | doc |
| 证书 | struct | doc |
| 提供商 | struct | doc |
| ldaps | struct | 文档 |
| 模型 | 结构体 | |
| 权限 | 结构体 | 文档 |
| 支付 | 结构体 | 文档 |
| 产品 | 结构体 | 文档 |
| 资源 | 结构体 | 文档 |
| 角色 | 结构体 | 文档 |
| 同步器 | 结构体 | 文档 |
| 令牌 | 结构体 | 文档 |
| webhooks | 结构体 | 文档 |
| 组 | 结构体 | 文档 |
| 适配器 | 结构体 | 文档 |
| 结构体 | ||
| 计划 | struct | doc |
| struct | 文档 | |
| 邀请 | 结构体 | 文档 |
| 记录 | 结构体 | |
| sessions | struct | |
| subscriptions | 结构体 | 文档 |
| transactions | struct |
每个对象的 JSON 结构与其 REST API 返回的结构相同。要查看示例,请调用相应的 get- 端点,或在使用管理控制台时在浏览器中检查响应。