credentialhelper

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// DefaultPassPrefix 是 pass 存储项在 password-store 中的默认根路径前缀。
	DefaultPassPrefix = "xops"

	// DefaultPassCommand 是原生 pass 命令行工具的二进制名称。
	DefaultPassCommand = "pass"
)
View Source
const (
	// ProtocolVersion 是当前支持的 helper 协议主版本。
	ProtocolVersion = 1

	// MaxResponseBytes 是 helper 响应 stdout 允许读取的最大字节数(64KB)。
	MaxResponseBytes = 64 * 1024

	// MaxStderrBytes 是 helper 诊断 stderr 允许读取的最大字节数(4KB)。
	MaxStderrBytes = 4 * 1024
)
View Source
const DefaultAnswerCommandTimeout = 15 * time.Second

DefaultAnswerCommandTimeout 是外部答案获取命令执行的默认超时时间(15 秒)。

View Source
const DefaultHelperTimeout = 30 * time.Second

DefaultHelperTimeout 是 credential helper 进程执行的默认超时时间。

View Source
const DefaultProcessWaitDelay = 50 * time.Millisecond

DefaultProcessWaitDelay 是主进程退出后等待继承管道的子孙进程排空的最大窗口。

View Source
const DefaultSystemHelperCommand = "xops-credential-system"

DefaultSystemHelperCommand 是系统密钥库 helper 的默认二进制名称。

View Source
const MaxAnswerStdoutBytes = 4096

MaxAnswerStdoutBytes 是外部答案命令允许读取的最大 stdout 字节数(4096 字节)。

Variables

This section is empty.

Functions

func CheckSystemAvailability

func CheckSystemAvailability() error

CheckSystemAvailability 检查当前平台系统密钥库环境是否满足可用性前置要求。

func DecodeSecretBytes

func DecodeSecretBytes(resp *Response) ([]byte, error)

DecodeSecretBytes 从 Response 中提取并解码机密字节。调用方需负责清零。

func EncodeRequest

func EncodeRequest(w io.Writer, req *Request) error

EncodeRequest 将请求序列化为 JSON 并写入 writer。

func MapErrorCode

func MapErrorCode(code, message string) error

MapErrorCode 将 helper 协议返回的错误代码映射为系统标准的凭据哨兵错误。 优先返回受控哨兵错误分类,避免将 helper 返回的任意非受控诊断文本拼接入错误导致已知请求机密泄漏。

func RunAnswerCommand added in v0.16.0

func RunAnswerCommand(ctx context.Context, opts AnswerCommandOptions) ([]byte, error)

RunAnswerCommand 执行外部答案命令,捕获其标准输出作为答案返回。 满足需求: 1. exec.CommandContext 直接执行,无 shell 包装; 2. 子进程 Stdin 绑定至 os.DevNull; 3. 注入只读 XOPS_* 环境变量; 4. 超时 (默认 15s) 或上下文取消时通过 KillTree 终止整个子进程树; 5. 限制 Stdout 最大读取 4096 字节 (io.LimitReader),并排空剩余管道防止死锁,剥离末尾单个换行符; 6. 退出码非 0 时对 Stderr 进行敏感信息脱敏 (SanitizeDiagnostic) 后返回错误。

func SanitizeDiagnostic

func SanitizeDiagnostic(raw string, sensitive ...string) string

SanitizeDiagnostic 对错误和诊断信息进行脱敏处理,屏蔽可疑 Base64 与长敏感字符串。

Types

type Action

type Action string

Action 表示向 helper 发起的操作动作。

const (
	// ActionGet 获取指定引用的凭据。
	ActionGet Action = "get"
	// ActionStore 存储凭据。
	ActionStore Action = "store"
	// ActionErase 擦除凭据。
	ActionErase Action = "erase"
)

type AnswerCommandOptions added in v0.16.0

type AnswerCommandOptions struct {
	// Command 是要执行的可执行文件绝对路径或名称(直接调用,绝不通过 shell 包装)。
	Command string
	// Args 是传递给可执行文件的参数列表。
	Args []string
	// Timeout 是命令执行的超时时间,若 <= 0 则使用默认超时 DefaultAnswerCommandTimeout (15s)。
	Timeout time.Duration
	// Env 是可选的额外环境变量。
	Env []string

	// 上下文信息,将注入为只读的 XOPS_* 环境变量:
	// Node 是目标节点标识/名称,对应 XOPS_NODE。
	Node string
	// Host 是目标主机地址,对应 XOPS_HOST。
	Host string
	// User 是 SSH 登录用户名,对应 XOPS_USER。
	User string
	// Identity 是身份配置名称,对应 XOPS_IDENTITY。
	Identity string
	// Prompt 是服务端提示/问题文本(清洗控制字符后注入),对应 XOPS_PROMPT。
	Prompt string
	// Instruction 是服务端指令/描述文本(清洗控制字符后注入),对应 XOPS_INSTRUCTION。
	Instruction string
	// Echo 标识用户输入是否在终端回显,对应 XOPS_ECHO ("true" 或 "false")。
	Echo bool
	// NonInteractive 显式声明是否处于非交互模式,对应 XOPS_NON_INTERACTIVE ("1" 或 "0")。
	NonInteractive bool
}

AnswerCommandOptions 包含执行外部答案命令所需的调用参数与上下文注入参数。

type HelperStore

type HelperStore struct {
	// contains filtered or unexported fields
}

HelperStore 基于外部 credential helper 进程实现 credential.Store 接口。

func NewHelperStore

func NewHelperStore(storeID string, opts ProcessOptions, readOnly bool) (*HelperStore, error)

NewHelperStore 创建外部 credential helper 存储实例。

func (*HelperStore) Delete

func (s *HelperStore) Delete(ctx context.Context, ref credential.Ref) error

Delete 从 helper 中删除凭据。只读模式下直接拒绝。

func (*HelperStore) Get

Get 从 helper 中获取凭据。

func (*HelperStore) IsReadOnly

func (s *HelperStore) IsReadOnly() bool

IsReadOnly 返回该存储是否为只读模式。

func (*HelperStore) Put

func (s *HelperStore) Put(ctx context.Context, ref credential.Ref, secret credential.Secret) error

Put 向 helper 中写入凭据。只读模式下直接拒绝。

func (*HelperStore) StoreID

func (s *HelperStore) StoreID() string

StoreID 返回该存储的注册标识。

type PassStore

type PassStore struct {
	// contains filtered or unexported fields
}

PassStore 提供基于 pass 密码管理器的凭据存储实现。 支持原生 pass 命令行工具或遵循 Helper 协议 v1 的 pass helper 二进制。

func NewPassStore

func NewPassStore(storeID string, cfg PassStoreConfig) (*PassStore, error)

NewPassStore 创建 pass 凭据存储实例。

func (*PassStore) Delete

func (p *PassStore) Delete(ctx context.Context, ref credential.Ref) error

Delete 从 pass 中删除指定凭据项。

func (*PassStore) Get

Get 从 pass 中检索凭据。

func (*PassStore) IsReadOnly

func (p *PassStore) IsReadOnly() bool

IsReadOnly 返回该存储是否为只读。

func (*PassStore) Put

func (p *PassStore) Put(ctx context.Context, ref credential.Ref, secret credential.Secret) error

Put 向 pass 中插入或更新凭据。机密通过 stdin 传递,严禁进入 argv。

func (*PassStore) StoreID

func (p *PassStore) StoreID() string

StoreID 返回该存储的注册标识。

type PassStoreConfig

type PassStoreConfig struct {
	NonInteractive   bool
	Prefix           string
	Command          string
	Args             []string
	Env              []string
	Timeout          time.Duration
	ReadOnly         bool
	IsHelperProtocol bool
}

PassStoreConfig 描述 pass 凭据存储配置。

type ProcessOptions

type ProcessOptions struct {
	// NonInteractive declares that the helper honors the nonInteractive request field.
	NonInteractive bool
	Command        string
	Args           []string
	Env            []string
	Timeout        time.Duration
}

ProcessOptions 包含启动子进程 credential helper 的选项。

type Request

type Request struct {
	NonInteractive  bool   `json:"nonInteractive,omitempty"`
	ProtocolVersion int    `json:"protocolVersion"`
	StoreID         string `json:"storeID"`
	ItemID          string `json:"itemID"`
	Secret          string `json:"secret,omitempty"` // Base64 编码
}

Request 表示通过 stdin 传递给 helper 的请求 JSON 载荷。

type Response

type Response struct {
	Secret    string     `json:"secret,omitempty"` // Base64 编码
	ExpiresAt *time.Time `json:"expiresAt,omitempty"`
	Code      string     `json:"code,omitempty"`
	Message   string     `json:"message,omitempty"`
}

Response 表示从 helper stdout 读取的响应 JSON 载荷。

func DecodeResponse

func DecodeResponse(r io.Reader) (*Response, error)

DecodeResponse 从 reader 读取并反序列化响应 JSON,严格限制最大字节数、强制唯一响应并校验 Base64。

func Run

func Run(ctx context.Context, opts ProcessOptions, action Action, req *Request) (*Response, error)

Run 启动 credential helper 子进程,传递 stdin 并解析 stdout 响应。

type SystemStore

type SystemStore struct {
	// contains filtered or unexported fields
}

SystemStore 封装操作系统原生密钥库或外部 helper 的凭据存储。

func NewSystemStore

func NewSystemStore(storeID string, cfg SystemStoreConfig) (*SystemStore, error)

NewSystemStore 创建系统密钥库存储实例。

func (*SystemStore) Delete

func (s *SystemStore) Delete(ctx context.Context, ref credential.Ref) error

Delete 从系统密钥库删除凭据。

func (*SystemStore) Get

Get 从系统密钥库检索凭据。

func (*SystemStore) IsReadOnly

func (s *SystemStore) IsReadOnly() bool

IsReadOnly 返回该存储是否为只读。

func (*SystemStore) Put

func (s *SystemStore) Put(ctx context.Context, ref credential.Ref, secret credential.Secret) error

Put 向系统密钥库写入凭据。

func (*SystemStore) StoreID

func (s *SystemStore) StoreID() string

StoreID 返回该存储的注册标识。

type SystemStoreConfig

type SystemStoreConfig struct {
	NonInteractive bool
	Command        string
	Args           []string
	Env            []string
	Timeout        time.Duration
	ReadOnly       bool
}

SystemStoreConfig 包含系统密钥库后端的配置选项。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL