Documentation
¶
Overview ¶
Package remote_storage 定义"远程 Storage"的线协议。
这个包只放协议类型与错误常量,服务端和客户端共用, 保证两边对同一份 JSON 的理解不会漂移。
Index ¶
- Constants
- Variables
- type AuthErrorResponse
- type CapabilitiesResponse
- type CreateRequest
- type DeleteRequest
- type DiscoveryEndpoint
- type DiscoveryResponse
- type ErrorCode
- type ErrorResponse
- type GetResponse
- type ListResponse
- type ListedLock
- type RemoteError
- type TenantResponse
- type TimeResponse
- type UpdateRequest
Constants ¶
const ( // RouteCapabilities 返回服务端 Storage 的能力与名字。 RouteCapabilities = "/v1/capabilities" // RouteTime 返回服务端时间(单调来源由服务端保证)。 RouteTime = "/v1/time" // RouteLock 是 /v1/locks/{lockId} 的前缀。 RouteLock = "/v1/locks/" // RouteLocks 列出全部锁。 RouteLocks = "/v1/locks" // RouteHealth 健康检查。 RouteHealth = "/v1/health" // RouteTenant 签发租户凭据。服务端启用租户模式时,这是唯一不需要凭据的接口。 RouteTenant = "/v1/tenant" )
路由
const ProtocolVersion = "v1"
协议版本。客户端与服务端的这个值必须一致,否则拒绝启动—— 静默降级会让"看起来能用"变成实际语义不一致。
const RouteDiscovery = "/v1/discovery"
RouteDiscovery 返回发现文档:服务端当前地址与后续接替地址。
这条链路存在的理由:客户端的凭据绑在**身份**上(换地址不丢锁), 但客户端出发前必须知道"现在该连哪个地址"。内置默认地址会过时、 老地址可能整台机器下线,所以需要一条"地址变了但身份没变"的告知通道。
不需要鉴权:它只公布公开信息(地址清单),而每一条地址在使用前 都要靠客户端钉住的公钥验签,公开它不带来风险。
const Version = "v0.2.0"
Version 是当前模块版本,用于 build_info 指标与 /v1/capabilities 的排障。
Variables ¶
var ( ErrBaseURLRequired = errors.New("remote storage: base URL is required") ErrProtocolVersion = errors.New("remote storage: protocol version mismatch") )
客户端侧的配置错误。
Functions ¶
This section is empty.
Types ¶
type AuthErrorResponse ¶ added in v0.2.0
type AuthErrorResponse struct {
ErrorResponse
// TenantMode 为真表示这个服务端要求携带租户凭据头。
TenantMode bool `json:"tenant_mode,omitempty"`
}
AuthResponse 是鉴权失败(401)时的补充信息, 便于客户端区分"没带凭据"和"凭据不对"。
type CapabilitiesResponse ¶
type CapabilitiesResponse struct {
ProtocolVersion string `json:"protocol_version"`
Name string `json:"name"`
Capabilities []storage.StorageCapability `json:"capabilities"`
// Identity 是服务端自我声明的稳定身份(与域名无关)。
//
// 客户端据此确认"换了个地址之后连的还是同一个服务",
// 从而继续用同一份凭据、同一个租户。老服务端没有这个字段,
// 客户端读到空串时应视为"未知"而不是"不匹配"。
//
// 注意:identity 是公开字符串,**任何人都能自称**。想真正防冒充
// 必须同时提供 IdentityPublicKey 与 IdentitySignature(见 identity 包),
// 客户端用已钉住的公钥验签。
Identity string `json:"identity,omitempty"`
// IdentityPublicKey 是服务端身份签名的 Ed25519 公钥(base64)。
// 空串表示该服务端不提供可验证身份(老版本),客户端按兼容路径处理。
IdentityPublicKey string `json:"identity_public_key,omitempty"`
// IdentitySignature 是 base64(Ed25519(identity [+ "\n" + base_url]))。
// 与 IdentityPublicKey 成对出现。
IdentitySignature string `json:"identity_signature,omitempty"`
// BaseURL 是服务端对外公布的地址(签名的一部分)。
//
// 客户端据此知道"这个服务认为自己现在在哪个地址",
// 与本地记录的地址不一致时就会去拉发现文档。
BaseURL string `json:"base_url,omitempty"`
}
CapabilitiesResponse 描述服务端 Storage 的自我声明。
客户端会**原样转发**这些能力,而不是自己补全——能力必须是服务端的事, 客户端无权替它背书。
type CreateRequest ¶
type CreateRequest struct {
Version storage.Version `json:"version"`
InformationJSON string `json:"information_json"`
}
CreateRequest 是 CreateWithVersion 的请求体。
type DeleteRequest ¶
type DeleteRequest struct {
ExpectedVersion storage.Version `json:"expected_version"`
InformationJSON string `json:"information_json,omitempty"`
}
DeleteRequest 是 DeleteWithVersion 的请求体。
带上 InformationJSON 是为了与接口签名一致;服务端不会用它做校验, 语义完全由 ExpectedVersion 决定。
type DiscoveryEndpoint ¶ added in v0.2.0
type DiscoveryEndpoint struct {
BaseURL string `json:"base_url"`
// Signature 是该地址对应的签名(message 同上,base_url 取本行的值)。
//
// 同一份私钥签发,因此客户端的同一把钉住公钥即可验证所有行。
Signature string `json:"signature"`
// Note 可选说明(如"新机房"/"旧地址,计划下线")。
Note string `json:"note,omitempty"`
}
DiscoveryEndpoint 是发现文档里的一个地址候选。
type DiscoveryResponse ¶ added in v0.2.0
type DiscoveryResponse struct {
// Identity 是当前服务端声明的身份。
Identity string `json:"identity"`
// BaseURL 是当前服务端对外公布的地址。
BaseURL string `json:"base_url"`
// PublicKey 是签名公钥(客户端首次连接时要钉住的也是它)。
PublicKey string `json:"public_key"`
// Signature 见上文签名范围。
Signature string `json:"signature"`
// Successors 是"同一身份的其他可用地址",按推荐顺序排列。
//
// 用于灰度迁移(新旧地址同时在线)与服务端主动公布的接替者。
// 客户端会逐个探测并验签,任何一个验不过就跳过。
Successors []DiscoveryEndpoint `json:"successors,omitempty"`
// UpdatedAt 最后更新时间,便于判断文档是否过期。
UpdatedAt string `json:"updated_at,omitempty"`
// Note 给运维看的一句话说明,不影响客户端行为。
Note string `json:"note,omitempty"`
}
DiscoveryResponse 是 /v1/discovery 的响应体。
## 签名范围(重要)
服务端用**它自己的**身份私钥签名:
message = "storage-lock-identity/v1:" + identity + "\n" + base_url
客户端拿到文档后先验这个签名(靠已钉住的公钥),验不过就整份丢弃。 因此后续每一行 successor 都必须满足:
- identity 与签名时一致(否则就是另一个服务);
- 每行自带该地址所属身份的 signature,客户端分别验证。
在 domains 之外,结构保持最小:多一层嵌套就多一处可以放"无效数据"的地方。
type ErrorCode ¶
type ErrorCode string
错误码。HTTP 状态码只是传输层,语义必须由 code 表达, 否则客户端无法区分"版本冲突可重试"和"记录不存在是硬错误"。
type ErrorResponse ¶
type ErrorResponse struct {
Code ErrorCode `json:"code"`
Message string `json:"message,omitempty"`
}
ErrorResponse 是任何非 2xx 响应的 body。
type GetResponse ¶
type GetResponse struct {
Version storage.Version `json:"version"`
InformationJSON string `json:"information_json"`
}
GetResponse 是 Get 的响应体。
type ListResponse ¶
type ListResponse struct {
Items []ListedLock `json:"items"`
// Truncated 为真表示服务端达到了返回条数上限,结果不完整。
Truncated bool `json:"truncated,omitempty"`
}
ListResponse 是 List 的响应体。服务端一次性返回全部锁; 对锁数量极大的部署应先改成游标分页(见 README 的边界说明)。
type ListedLock ¶
type ListedLock struct {
LockID string `json:"lock_id"`
Version storage.Version `json:"version"`
InformationJSON string `json:"information_json"`
}
ListedLock 是 List 里的一项。
type RemoteError ¶
RemoteError 是客户端侧还原出来的错误。
func (*RemoteError) Error ¶
func (e *RemoteError) Error() string
func (*RemoteError) ToStorageError ¶
func (e *RemoteError) ToStorageError() error
ToStorageError 把线协议错误映射回 go-storage 期望的 sentinel error。
这一步至关重要:上层 go-storage-lock 只把 ErrVersionMiss 当成"可重试", 把 ErrLockNotFound 当成硬错误。如果这里映射错了,抢占路径会死循环或直接失败。
type TenantResponse ¶ added in v0.2.0
type TenantResponse struct {
TenantID string `json:"tenant_id"`
Secret string `json:"secret"`
Note string `json:"note,omitempty"`
}
TenantResponse 是 POST /v1/tenant 的响应体。
服务端不留明文 secret,只保留 HMAC 主密钥;secret 丢了就回不到原租户, 这是"免注册"的代价,也避免了任何账号体系。
type TimeResponse ¶
type TimeResponse struct {
// UnixNano 服务端单调时间的纳秒表示。
UnixNano int64 `json:"unix_nano"`
}
TimeResponse 是服务端时间。
Directories
¶
| Path | Synopsis |
|---|---|
|
Package client 是一个把远程 Storage 服务当作本地 Storage 使用的实现。
|
Package client 是一个把远程 Storage 服务当作本地 Storage 使用的实现。 |
|
Package identity 解决"地址会变"链条上最容易漏掉的一环:**服务端身份怎么被信任**。
|
Package identity 解决"地址会变"链条上最容易漏掉的一环:**服务端身份怎么被信任**。 |
|
Package metrics 提供服务端的 Prometheus 指标。
|
Package metrics 提供服务端的 Prometheus 指标。 |
|
Package server 把任意 go-storage Storage 暴露成远程 Storage 服务。
|
Package server 把任意 go-storage Storage 暴露成远程 Storage 服务。 |
|
cmd/remote-storage-server
command
remote-storage-server 把本地存储暴露成远程 Storage 服务。
|
remote-storage-server 把本地存储暴露成远程 Storage 服务。 |
|
Package tenant 实现"免注册但真隔离"的租户模型。
|
Package tenant 实现"免注册但真隔离"的租户模型。 |