ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Authelia CLI 参考:使用 authelia storage user identifiers export 备份用户不透明标识符

Authelia CLI 参考:使用 authelia storage user identifiers export 备份用户不透明标识符 Authelia CLI 参考使用 authelia storage user identifiers export 备份用户不透明标识符【免费下载链接】autheliaThe Single Sign-On Multi-Factor portal for web apps. OpenID Certified™ and Post-Quantum Cryptography Ready.项目地址: https://gitcode.com/GitHub_Trending/au/authelia本文围绕 Authelia 命令行中的authelia storage user identifiers export子命令展开讲解它的作用、完整用法示例与参数细节并结合internal/commands下的源码实现剖析导出流程的底层行为文件权限、YAML 结构、Schema 校验等帮助你在迁移、灾备或数据库重建场景中安全地备份与恢复用户的 opaque identifier不透明标识符。什么是 Opaque Identifier为什么需要备份Opaque identifier不透明标识符是 Authelia 为用户生成的一组随机 UUID v4最常见的用途是在 OAuth 2.0 / OpenID Connect 流程中作为sub主题标识使同一用户在不同服务sector之间获得互相隔离的身份映射。其数据模型定义在 UserOpaqueIdentifier 中// UserOpaqueIdentifier represents an opaque identifier for a user. Commonly used with OAuth 2.0 and OpenID Connect. type UserOpaqueIdentifier struct { ID int db:id yaml:- Service string db:service yaml:service // 服务名如 openid SectorID string db:sector_id yaml:sector_id // sector 标识通常留空 Username string db:username yaml:username // 所属用户名 Identifier uuid.UUID db:identifier yaml:identifier // 随机 UUID v4 }每一条记录由「服务 sector 用户名」唯一确定可参考 containsIdentifier 判定逻辑实际标识值是一个随机生成的 UUID v4见 NewUserOpaqueIdentifier。这些标识符保存在 Authelia 的存储后端SQLite/PostgreSQL/MySQL中。如果你需要迁移数据库、更换后端或做灾备直接导出 SQL 可能不够直观也不便于跨环境核对——storage user identifiers export子命令正是为此而生将全部用户 opaque identifier 导出为一个可读的 YAML 文件以便备份。命令概览该命令的官方参考文档位于 authelia_storage_user_identifiers_export.md其定义为ShortExport the identifiers to a YAML fileLongExport the identifiers to a YAML file. This subcommand allows exporting the opaque identifiers for users in order to back them up.调用形式authelia storage user identifiers export [flags]命令在代码中的注册入口是 newStorageUserIdentifiersExportCmdinternal/commands/storage.go它挂靠在identifiers父命令下与import、generate、add组成完整的标识符管理命令族。使用示例文档给出的四种典型用法覆盖了从最简形式到显式指定数据库连接参数的完整场景# 最简形式使用默认配置文件 configuration.yml导出到默认文件名 authelia storage user identifiers export # 指定导出文件名 authelia storage user identifiers export --file export.yml # 指定导出文件名 自定义配置文件 authelia storage user identifiers export --file export.yml --config config.yml # 在命令行上完整指定加密密钥与 PostgreSQL 连接适合容器化或 CI 环境 # 不依赖本地配置文件 authelia storage user identifiers export --file export.yml \ --encryption-key b3453fde-ecc2-4a1f-9422-2707ddbed495 \ --postgres.address tcp://postgres:5432 \ --postgres.password autheliapw参数参考本命令参数参数说明默认值-f, --file stringYAML 导出文件的文件名authelia.export.opaque-identifiers.yml-h, --help显示帮助信息—默认文件名在 newStorageUserIdentifiersExportCmd 中通过cmd.Flags().StringP(cmdFlagNameFile, f, authelia.export.opaque-identifiers.yml, ...)注册与姊妹命令authelia storage user identifiers import的默认文件名保持一致便于导出后直接导入。从父命令继承的参数storage层命令共享一组存储相关的全局参数用于在不修改配置文件的情况下覆盖后端连接-c, --config strings configuration files or directories to load, for more information run authelia -h authelia config (default [configuration.yml]) --config.experimental.filters strings list of filters to apply to all configuration files, for more information run authelia -h authelia filters --encryption-key string the storage encryption key to use --mysql.address string the MySQL server address (default tcp://127.0.0.1:3306) --mysql.database string the MySQL database name (default authelia) --mysql.password string the MySQL password --mysql.username string the MySQL username (default authelia) --postgres.address string the PostgreSQL server address (default tcp://127.0.0.1:5432) --postgres.database string the PostgreSQL database name (default authelia) --postgres.password string the PostgreSQL password --postgres.schema string the PostgreSQL schema name (default public) --postgres.username string the PostgreSQL username (default authelia) --sqlite.path string the SQLite database path要点说明若存储配置了透明数据加密storage encryption必须通过--encryption-key提供与数据加密时一致的密钥否则无法读取密文数据--postgres.*/--mysql.*/--sqlite.path会覆盖配置文件中的同名项这使得「在跳板机上用配置文件 一次性覆盖密码」这类运维操作成为可能避免把密码写进文件。源码级实现剖析导出流程到底做了什么命令的 RunE 入口是 StorageUserIdentifiersExportRunEinternal/commands/storage_run.go第 1948 行起核心逻辑在runStorageUserIdentifiersExport中按顺序执行以下步骤存储 Schema 检查先调用ctx.CheckSchema()确认数据库结构版本避免在结构不匹配如未完成迁移的库上导出数据。拒绝覆盖已有文件通过os.Stat(filename)检查目标文件若文件已存在则直接报错must specify a file that doesnt exist but file exists。这是一个防误删保护——export 永远不会静默覆盖旧备份。加载全部标识符调用存储层store.LoadUserOpaqueIdentifiers(ctx)取出全部记录若结果为空len(export.Identifiers) 0命令以no data to export错误终止不会生成空导出文件。以 0600 权限创建文件os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)确保导出的备份文件仅属主可读写——因为该文件包含「用户名 ↔ UUID 映射」属于敏感的身份关联数据。写入带 Schema 注释的 YAML通过 exportYAMLWithJSONSchema 写入文件其会先在文件头部写入# yaml-language-server: $schema...注释schema 名export.identifiers版本取自构建 tag无法解析时回退为latest见 FormatJSONSchemaYAMLLanguageServer再序列化导出对象。这为使用支持 JSON Schema 的编辑器提供自动补全与校验能力。输出成功信息以Successfully exported ...格式cliOutputFmtSuccessfulUserExportFile打印导出条数、数据类型User Opaque Identifiers、格式YAML与文件名。清理命令结束时 defer 关闭存储连接。导出文件的数据结构导出对象类型是 UserOpaqueIdentifiersExport顶层只有一个identifiers数组每个元素对应一条UserOpaqueIdentifier记录注意ID字段标记为yaml:-不会写入文件。一份导出文件的实际结构形如# yaml-language-server: $schema.../export.identifiers.json identifiers: - service: openid sector_id: username: john identifier: f0919359-9d15-4e15-bcba-83b41620a073 - service: openid sector_id: example.com username: mary identifier: 9d15f093-83b4-4e15-9d15-bcba83b41620字段含义来自 UserOpaqueIdentifier 结构体注释字段说明service该 UUID 所属的服务名opaque identifier 常见用途为 OpenID Connectsector_idSector 标识用于跨服务隔离通常留空username该 UUID 对应的用户名identifier随机的 UUID v4 标识值本身与相关命令配合的备份恢复工作流export并非孤立命令。identifiers命令族见 authelia_storage_user_identifiers.md还包括authelia storage user identifiers import file从 YAML 文件导入标识符见 import 命令文档导入实现逐条调用store.SaveUserOpaqueIdentifier且空文件会被拒绝cant import a YAML file without User Opaque Identifiers dataauthelia storage user identifiers generate --users ... [--services ...] [--sectors ...]为指定用户批量生成标识符会先加载现有记录并跳过已存在的组合authelia storage user identifiers add username --identifier uuid添加单条记录只接受 UUID v4。典型的数据库迁移/灾备流程因此可以概括为在源环境执行authelia storage user identifiers export --file export.yml得到备份将export.yml安全地传输到目标环境注意其 0600 权限与敏感内容在目标环境执行authelia storage user identifiers import export.yml完成恢复可用generate/add补造缺失项。这样即便底层数据库从 SQLite 换成 PostgreSQL或 schema/加密配置发生变化用户与 OIDCsub标识的映射关系也不会丢失避免下游依赖该 UUID 的服务出现身份漂移。适用前提与注意事项命令需要能成功连接并读取存储后端本地运行依赖配置文件默认configuration.yml或命令行覆盖参数启用存储加密时必须额外提供--encryption-key。目标导出文件不能预先存在否则命令报错退出——这是有意设计请为多次导出使用不同文件名或先归档旧文件。数据库中没有任何 opaque identifier 时命令以no data to export报错退出属正常行为。由于该命令只做读取与导出可以在只读访问数据库的环境中安全运行但导出文件包含用户名与 UUID 的映射关系应按敏感数据管理。参考路径命令参考文档、命令注册、运行逻辑、导出结构、YAML 导出工具函数。【免费下载链接】autheliaThe Single Sign-On Multi-Factor portal for web apps. OpenID Certified™ and Post-Quantum Cryptography Ready.项目地址: https://gitcode.com/GitHub_Trending/au/authelia创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表