invgraph

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 14 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

Overview

Package invgraph 为 core/domain/inventory 提供无环叶级纯函数算子: 涵盖资产标识符/密钥路径/指纹/正则字符集归一化、跨命名空间 ProxyJump Tarjan SCC 环路与深度校验、 拓扑跳板链求解、导入冲突与别名碰撞检测,以及通用编解码与规范二进制写入原语。

本包恪守 core/domain/** 纯数学零副作用铁律(零 os/net/context/sync/reflect), 仅依赖白名单标准库与下层纯领域包(core/domain/i18n)。

Index

Constants

View Source
const (
	// DefaultMaxJumpDepth 表示默认允许的最大 ProxyJump 跳板深度。
	DefaultMaxJumpDepth = 16
	// MaxIDLen 表示资产标识符、别名、标签与目标键名称的最大字节长度。
	MaxIDLen = 128
	// MaxDescriptionBytes 表示资产单行描述的最大字节长度。
	MaxDescriptionBytes = 512
	// MaxKeyPathBytes 表示私钥/证书/来源文件路径的最大字节长度。
	MaxKeyPathBytes = 1024
	// MaxCommandBytes 表示质询应答外部命令或单个参数的最大字节长度。
	MaxCommandBytes = 2048
	// MaxCommandArgs 表示质询应答外部命令的最大参数个数。
	MaxCommandArgs = 64
	// MaxPatternBytes 表示质询应答正则表达式的最大字节长度。
	MaxPatternBytes = 512
	// MaxPrivilegePromptPatternBytes 表示持久化提权策略自定义口令提示正则的最大字节长度。
	MaxPrivilegePromptPatternBytes = 256
	// MaxAuthCompositeDepth 表示 AuthComposite 最大允许嵌套深度。
	MaxAuthCompositeDepth = 4
	// MaxCertPaths 表示单私钥认证允许绑定的最大证书路径数量。
	MaxCertPaths = 32
	// MaxAllowedAgentFingerprints 表示单个 AuthAgent 允许绑定的最大公钥指纹过滤条目数。
	MaxAllowedAgentFingerprints = 64
	// MaxTagsOrAliases 表示单实体允许声明的最大标签或别名数量。
	MaxTagsOrAliases = 64
	// MaxFormatNameLen 表示 ImportBundle 来源格式名称的最大字节长度。
	MaxFormatNameLen = 64
	// MaxWarningMsgBytes 表示导入告警单行消息的最大字节长度。
	MaxWarningMsgBytes = 512
	// MaxAgentSpecBytes 表示 SSH Agent 规格字符串的最大字节长度。
	MaxAgentSpecBytes = 512
	// SHA256FingerprintTag 表示 OpenSSH SHA-256 公钥指纹固定前缀。
	SHA256FingerprintTag = "SHA256:"
	// SHA256FingerprintB64 表示 32 字节摘要无填充标准 Base64 长度。
	SHA256FingerprintB64 = 43
)
View Source
const (
	ActionInvalid uint8 = iota
	ActionAdd
	ActionUnchanged
	ActionConflict

	ActionMinValid = ActionAdd
	ActionMaxValid = ActionConflict
)

导入预检决策编码与有效边界,由父包 inventory.ImportActionKind 单源派生。

View Source
const (
	NSInvalid uint8 = iota
	NSManaged
	NSOpenSSH

	NSMinValid = NSManaged
	NSMaxValid = NSOpenSSH
)

跨命名空间代理图顶点命名空间编码与有效边界,由父包 inventory.NamespaceKind 单源派生。

Variables

This section is empty.

Functions

func AllMatch

func AllMatch[T any](items []T, fn func(T) bool) bool

AllMatch 报告切片全部元素是否均满足谓词 fn。

func AppendValidText

func AppendValidText(b []byte, valid bool, text string, baseErr error) ([]byte, error)

AppendValidText 在有效时追加文本至 b,否则返回 baseErr。

func BuildAndValidateProxyGraph

func BuildAndValidateProxyGraph(
	managed []ManagedVertexSpec,
	openSSH []OpenSSHVertexSpec,
	maxDepth int,
	invalidNodeErr error,
	errs GraphErrors,
) (*ProxyGraph, DAGResult, error)

BuildAndValidateProxyGraph 按严格顺序校验 maxDepth、顶点合法性与唯一性、别名冲突、边解析、Tarjan 环路与跳板深度。

func CanonicalizeAliases

func CanonicalizeAliases[T any](
	raw []T,
	selfID string,
	valid func(T) bool,
	strFn func(T) string,
	cmpFn func(T, T) int,
	eqFn func(T, T) bool,
) ([]T, error)

CanonicalizeAliases 校验别名切片上限、非零值及与自身 ID 互斥,并按字典序升序去重。

func CanonicalizeStores

func CanonicalizeStores(raw []string, baseErr error) ([]string, error)

CanonicalizeStores 校验已知凭据存储名称切片并按字典序升序去重。

func CheckAliasCollisions

func CheckAliasCollisions(idSet map[string]bool, aliases []string, kind string, dupErr error) error

CheckAliasCollisions 校验一组待注册别名既不与已存在实体 ID 冲突、彼此之间也无重复。

func ClassifyAssetSlice

func ClassifyAssetSlice[T any, K comparable](
	kind string,
	existing, incoming []T,
	keyOf func(T) K,
	strOf func(K) string,
	eq func(T, T) bool,
	emit func(ImportEntryData),
) map[K]bool

ClassifyAssetSlice 按主键对同类资产执行无状态导入分类(新增/一致跳过/冲突),返回合并后可用主键集。

func ClassifyHostImport

func ClassifyHostImport[T any, K comparable](
	existing, incoming []T,
	keyOf func(T) K,
	strOf func(K) string,
	aliasesOf func(T) []string,
	eq func(T, T) bool,
	emit func(ImportEntryData),
) map[K]bool

ClassifyHostImport 对主机实体执行含 Host.Aliases 双向碰撞检测的无状态导入分类,返回合并后可用主机键集。

func ClassifyNodeImport

func ClassifyNodeImport[T any, K comparable](
	existing, incoming []T,
	keyOf func(T) K,
	strOf func(K) string,
	aliasesOf func(T) []string,
	refsAvail func(T) bool,
	eq func(T, T) bool,
	emit func(ImportEntryData),
) []T

ClassifyNodeImport 对节点实体执行引用完整性与双向别名碰撞检测的无状态导入分类,返回待 DAG 校验的合并节点切片。

func CloneAndSortValid

func CloneAndSortValid[T any](raw []T, valid func(T) bool, cmpFn func(T, T) int, field string, baseErr error) ([]T, error)

CloneAndSortValid 校验切片全部元素有效后克隆并按比较函数确定性排序。

func CompileBoundedRegex

func CompileBoundedRegex(pat string, required bool, baseErr error) (*regexp.Regexp, error)

CompileBoundedRegex 校验默认长度上限(MaxPatternBytes)与字符集并编译有界 Go RE2 正则表达式。

func CompileBoundedRegexMax

func CompileBoundedRegexMax(pat string, required bool, maxBytes int, baseErr error) (*regexp.Regexp, error)

CompileBoundedRegexMax 按指定最大字节数上限校验长度与字符集并编译有界 Go RE2 正则表达式。

func CompileRuleMeta

func CompileRuleMeta(rawName, promptPat, instrPat string, requireName bool, invalidIDErr, baseErr error) (string, *regexp.Regexp, *regexp.Regexp, error)

CompileRuleMeta 校验质询规则名称并编译 prompt/instruction 有界正则表达式。

func EnumName

func EnumName(idx uint8, fallback string, names ...string) string

EnumName 按 0 基索引查询枚举名称,越界返回 fallback。

func FormatKeyFingerprint

func FormatKeyFingerprint(sum [sha256.Size]byte) string

FormatKeyFingerprint 将 32 字节 SHA-256 摘要格式化为规范 OpenSSH 公钥指纹文本。

func HasForbiddenText

func HasForbiddenText(s string) bool

HasForbiddenText 报告字符串是否包含非法 UTF-8 或被禁止的控制/格式字符。

func HasSpaceOrForbidden

func HasSpaceOrForbidden(s string) bool

HasSpaceOrForbidden 报告字符串是否包含空白或任意被禁止的控制/格式字符。

func ImportActionName

func ImportActionName(action uint8) string

ImportActionName 返回导入预检决策底层编码对应的规范小写文本;越界值回退 "invalid"。

func IndexUnique

func IndexUnique[T any, K comparable](raw []T, valid func(T) bool, keyOf func(T) K, cmpKey func(K, K) int, badErr, dupErr error) ([]T, map[K]bool, error)

IndexUnique 校验实体切片全部有效且键唯一,随后按键确定性升序排序并返回集合索引。

func IndexUniqueWithAliases

func IndexUniqueWithAliases[T any, K comparable](
	raw []T,
	valid func(T) bool,
	keyOf func(T) K,
	strOf func(K) string,
	cmpKey func(K, K) int,
	aliasesOf func(T) []string,
	aliasKind string,
	badErr, dupErr error,
) ([]T, map[K]bool, map[string]int, error)

IndexUniqueWithAliases 在 IndexUnique 基础上同步校验实体全部别名不与任何主键或其他别名碰撞, 并返回覆盖主键与全部别名的 O(1) 确定性查找索引(映射至排序后切片下标)。

func IsForbiddenRune

func IsForbiddenRune(r rune) bool

IsForbiddenRune 报告码点是否为控制字符、Unicode Cf 格式/Bidi 字符或行/段分隔符。

func MapSlice

func MapSlice[A, B any](in []A, fn func(A) B) []B

MapSlice 将无错映射函数应用到非空切片;空切片返回 nil 保持序列化一致。

func MapSliceErr

func MapSliceErr[A, B any](in []A, fn func(A) (B, error)) ([]B, error)

MapSliceErr 将可能失败的映射函数依次应用到切片每个元素上。

func MarshalValidWire

func MarshalValidWire[W any](enc *jsontext.Encoder, valid bool, invalidErr error, wire W) error

MarshalValidWire 在 valid 为 true 时编码 wire,否则失败关闭返回 invalidErr。

func MarshalWire

func MarshalWire[T, W any](enc *jsontext.Encoder, v T, nilWriterErr error, build func(T) (W, error)) error

MarshalWire 使用构造器将领域值转为 wire 后编码至 JSON/v2 流。

func MaskFlags

func MaskFlags(flags ...bool) uint8

MaskFlags 将一组布尔存在位编码为紧凑位掩码,用于封闭和类型 JSON 变体字段互斥检查。

func NamespaceName

func NamespaceName(ns uint8) string

NamespaceName 返回命名空间底层编码对应的规范小写文本;越界值回退 "invalid"。

func ParseEnumByName

func ParseEnumByName[T ~uint8](raw string, lo, hi, zero T, nameFn func(T) string, baseErr error) (T, error)

ParseEnumByName 大小写无关地解析 [lo, hi] 范围内的 uint8 枚举名称。

func ParseKeyFingerprint

func ParseKeyFingerprint(raw string, baseErr error) ([sha256.Size]byte, string, error)

ParseKeyFingerprint 严格解析 OpenSSH SHA-256 公钥指纹,返回 32 字节摘要与规范文本。

func ParseTargetKeyRaw

func ParseTargetKeyRaw(raw string, baseErr error) (uint8, string, error)

ParseTargetKeyRaw 解析 "<namespace>:<name>" 规范文本为 (命名空间编码, 名称)。

func SanitizeDescAndSortedTags

func SanitizeDescAndSortedTags[T any](
	rawDesc string,
	rawTags []T,
	isZero func(T) bool,
	cmpFn func(T, T) int,
	eqFn func(T, T) bool,
	baseErr error,
) (string, []T, error)

SanitizeDescAndSortedTags 校验单行描述并对标签切片执行上限校验、去重与字典序排序。

func SanitizeSingleLine

func SanitizeSingleLine(s string, maxBytes int) (string, error)

SanitizeSingleLine 校验字符串长度与字符合法性(禁止换行、控制/格式字符与非法 UTF-8)。

func UnmarshalJSON

func UnmarshalJSON[T, W any](dst *T, dec *jsontext.Decoder, nilErr, baseErr error, build func(W) (T, error)) error

UnmarshalJSON 执行失败关闭的 JSON/v2 反序列化(拒绝顶层 null,失败保持零值)。

func UnmarshalJSONStrict

func UnmarshalJSONStrict[T, W any](dst *T, dec *jsontext.Decoder, nilErr, baseErr error, build func(W) (T, error)) error

UnmarshalJSONStrict 执行严格失败关闭的 JSON/v2 反序列化(拒绝顶层 null 与未知字段,失败保持零值)。

func UnmarshalJSONVal

func UnmarshalJSONVal[T, W any](dec *jsontext.Decoder, nilErr, baseErr error, build func(W) (T, error)) (T, error)

UnmarshalJSONVal 解码并返回多态 JSON/v2 值对象。

func UnmarshalText

func UnmarshalText[T any](dst *T, text []byte, nilErr error, parse func(string) (T, error)) error

UnmarshalText 反序列化文本值对象;失败时重置接收者为零值。

func ValidateAgentSpec

func ValidateAgentSpec(raw string, baseErr error) (string, error)

ValidateAgentSpec 校验并裁剪 SSH Agent 规格文本。

func ValidateAndSortNodes

func ValidateAndSortNodes[N any, H, I comparable](
	raw []N,
	hosts map[H]bool,
	idents map[I]bool,
	valid func(N) bool,
	hostRef func(N) H,
	identRef func(N) I,
	idStr func(N) string,
	cmpFn func(N, N) int,
	invalidNodeErr, missingRefErr error,
) ([]N, error)

ValidateAndSortNodes 校验节点列表有效性、主机与身份引用完整性,并按比较函数排序返回副本。

func ValidateAssetIdent

func ValidateAssetIdent(raw string, forbidOpenSSH bool, baseErr error) error

ValidateAssetIdent 校验资产标识符、别名、标签与规则名称的安全字符集约束。

func ValidateChallengeCommand

func ValidateChallengeCommand(command string, args []string, baseErr error) (string, []string, error)

ValidateChallengeCommand 校验质询应答外部命令与参数切片(拒绝 Shell 元字符、控制字符与越界长度)。

func ValidateImportWarningRaw

func ValidateImportWarningRaw(code, subject, message string, invalidIDErr, invalidBundleErr error) (string, string, error)

ValidateImportWarningRaw 校验导入告警的 code、subject 与 message 字段并返回裁剪后的 subject 与 message。

func ValidateKeyFileSpec

func ValidateKeyFileSpec(raw string, baseErr error) (string, error)

ValidateKeyFileSpec 校验并裁剪私钥/证书文件路径规格(禁止模板占位符、~other 展开与保留字 "none")。

func ValidateKnownStores

func ValidateKnownStores(knownStores, usedStores []string, unknownErr error) error

ValidateKnownStores 校验所有被引用的凭据存储名称均登记在 knownStores 中。

func ValidateOpenSSHJumpAlias

func ValidateOpenSSHJumpAlias(alias string, baseErr error) (string, error)

ValidateOpenSSHJumpAlias 校验 OpenSSH 跳板图顶点的 Host 别名文本。

func ValidateOpenSSHOriginMeta

func ValidateOpenSSHOriginMeta(token, sourceFile string, baseErr error) (string, string, error)

ValidateOpenSSHOriginMeta 校验 OpenSSH 节点来源的 Host 模式/别名与来源文件路径。

func ValidateSourceFormat

func ValidateSourceFormat(raw string, invalidIDErr, invalidBundleErr error) (string, error)

ValidateSourceFormat 校验并规范化 ImportBundle 来源格式标识(小写裁剪)。

func ValidateTargetKeyParts

func ValidateTargetKeyParts(ns uint8, nsName, name string, baseErr error) (string, error)

ValidateTargetKeyParts 校验命名空间编码与目标名称合法性(托管命名空间追加资产 ID 字符集检查)。

func ValidateUniqueRuleNames

func ValidateUniqueRuleNames[T any](items []T, valid func(T) bool, nameOf func(T) string, invalidAuthErr, invalidRuleErr error) error

ValidateUniqueRuleNames 校验质询规则列表中每个条目有效且非空规则名不重复。

func WriteStringers

func WriteStringers[T fmt.Stringer](e *CanonicalEncoder, items []T)

WriteStringers 以数量及逐项字符串形式写入 fmt.Stringer 切片。

Types

type CanonicalEncoder

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

CanonicalEncoder 提供零反射长度前缀规范字节写入原语,供认证方式摘要编码使用。

func NewCanonicalEncoder

func NewCanonicalEncoder(w io.Writer) *CanonicalEncoder

NewCanonicalEncoder 构造绑定至 w 的规范字节写入器。

func (*CanonicalEncoder) Err

func (e *CanonicalEncoder) Err() error

Err 返回写入期间遇到的首个错误。

func (*CanonicalEncoder) WriteRaw

func (e *CanonicalEncoder) WriteRaw(p []byte)

WriteRaw 写入原始字节序列。

func (*CanonicalEncoder) WriteString

func (e *CanonicalEncoder) WriteString(s string)

WriteString 以长度前缀写入字符串。

func (*CanonicalEncoder) WriteUint8

func (e *CanonicalEncoder) WriteUint8(v uint8)

WriteUint8 写入单字节无符号整数。

func (*CanonicalEncoder) WriteUint64

func (e *CanonicalEncoder) WriteUint64(v uint64)

WriteUint64 以大端 8 字节写入无符号整数。

type DAGResult

type DAGResult struct {
	MaxObservedDepth int
	TopoOrder        []VertexKey
	Cycles           [][]VertexKey
}

DAGResult 承载无状态 Tarjan SCC、深度检查与拓扑后序计算结果。

type GraphErrors

type GraphErrors struct {
	InvalidGraph  error
	DuplicateID   error
	MissingHop    error
	Cycle         error
	DepthExceeded error
}

GraphErrors 注入父包哨兵错误,确保纯图算子返回与父包完全一致的错误链与 errors.Is 语义。

type ImportEntryData

type ImportEntryData struct {
	AssetKind string
	AssetID   string
	Action    uint8
	Reason    string
}

ImportEntryData 表示无状态导入分类算子产出的单条分类结果。

type ManagedVertexSpec

type ManagedVertexSpec struct {
	Valid    bool
	ID       string
	Aliases  []string
	HopHosts []string
	RouteErr error
}

ManagedVertexSpec 描述构建跨命名空间代理图时的单个托管节点输入投影。

type OpenSSHVertexSpec

type OpenSSHVertexSpec struct {
	Valid    bool
	Alias    string
	HopHosts []string
	RouteErr error
}

OpenSSHVertexSpec 描述构建跨命名空间代理图时的单个 OpenSSH 虚拟节点输入投影。

type ProxyGraph

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

ProxyGraph 维护跨命名空间代理图的顶点、别名索引与有向邻接表,提供纯图 Tarjan/深度/拓扑算子。

func NewProxyGraph

func NewProxyGraph(managedCap, openSSHCap int, errs GraphErrors) *ProxyGraph

NewProxyGraph 构造具有预分配容量的空跨命名空间代理图。

func (*ProxyGraph) AddManagedAliases

func (g *ProxyGraph) AddManagedAliases(nodeID string, aliases []string) error

AddManagedAliases 为已注册的托管节点登记全部别名,并检测与托管 ID 或其他别名的碰撞。

func (*ProxyGraph) AddManagedVertex

func (g *ProxyGraph) AddManagedVertex(id string) error

AddManagedVertex 注册托管节点顶点,重复 ID 返回 DuplicateID。

func (*ProxyGraph) AddOpenSSHVertex

func (g *ProxyGraph) AddOpenSSHVertex(alias string) error

AddOpenSSHVertex 注册 OpenSSH 虚拟节点顶点,重复别名返回 DuplicateID。

func (*ProxyGraph) ResolveChain

func (g *ProxyGraph) ResolveChain(start VertexKey) ([]VertexKey, error)

ResolveChain 在已验证无环的图中按拓扑去重后序解析 start 的跳板链 [bastion..., start]。

func (*ProxyGraph) SetEdges

func (g *ProxyGraph) SetEdges(src VertexKey, hopHosts []string) error

SetEdges 按来源命名空间解析 hopHosts 对应的目标顶点并写入 src 的有向边;未知跳板返回 MissingHop。

func (*ProxyGraph) Validate

func (g *ProxyGraph) Validate(maxDepth int) (DAGResult, error)

Validate 对已装配完成的代理图执行确定性顶点排序、Tarjan SCC 环路检测、最大去重上游跳数检查与拓扑后序收集。

type VertexKey

type VertexKey struct {
	NS   uint8
	Name string
}

VertexKey 表示跨命名空间纯图算子中的强类型顶点键(命名空间编码 + 规范名称)。

func (VertexKey) Compare

func (k VertexKey) Compare(o VertexKey) int

Compare 定义 VertexKey 上的确定性字典全序。

func (VertexKey) String

func (k VertexKey) String() string

String 返回规范文本 "<namespace>:<name>"。

Jump to

Keyboard shortcuts

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