tls

package
v2.2.44 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package tls 提供 go-reqx 测试体系下"把内存 PEM 落到临时文件"等 TLS 配套工具。

与 testkit/server/tls_server.go 的关系:

  • tls_server.go 在内存里生成自签 CA + leaf;
  • 但 rest.Client.SetCert(caPath, certPath, keyPath) 要求文件路径;
  • 本包提供 WritePEMFiles / NewKeyPairTree 等工具,把 server 的内存证书 落到 helper.TempTree 上,再传给 client.SetCert。

不引入新依赖;纯标准库。

Package tls —— files.go

把"内存里的 PEM 字节"落到临时目录,方便 rest.Client.SetCert 等 API 直接 path 形式使用。

Package tls —— loader.go

测试期专用的 TLS 加载器:

  • LoadCertPool:把 CA PEM 装载成 *x509.CertPool;
  • LoadKeyPair:把 cert + key PEM 装载成 tls.Certificate。

这两个函数本质是 stdlib 的轻量包装,但提供"测试侧友好"的错误诊断:

  • 出错时直接 t.Fatalf;
  • 提供更明确的失败原因(哪个 PEM 块未识别等)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadCertPool

func LoadCertPool(tb testing.TB, caPEM []byte) *x509.CertPool

LoadCertPool 把 caPEM 装载成 *x509.CertPool。

失败原因:

  • PEM 块解析失败;
  • 没有任何可信任的证书(AppendCertsFromPEM 返回 false)。

func LoadKeyPair

func LoadKeyPair(tb testing.TB, certPEM, keyPEM []byte) stdtls.Certificate

LoadKeyPair 把 cert+key PEM 装载成 tls.Certificate。

出错时通过 t.Fatalf 终止;返回值在失败路径不会到达。

Types

type PEMFiles

type PEMFiles struct {
	CACertPath     string
	ClientCertPath string
	ClientKeyPath  string
}

PEMFiles 是 WritePEMFiles 的返回结果,记录三个文件的路径。

func WritePEMFiles

func WritePEMFiles(tb testing.TB, caCertPEM, clientCertPEM, clientKeyPEM []byte) PEMFiles

WritePEMFiles 把给定的 CA cert / client cert / client key 字节写到一棵临时目录树。

任一字段为空(nil 切片)时对应的路径返回 ""。

调用方只需:

srv := server.NewTLS(t, server.WithMTLS())
caPEM := srv.CACertPEM()
cliCert, cliKey := srv.ClientCert()
files := tls.WritePEMFiles(t, caPEM, cliCert, cliKey)
client.SetCert(files.CACertPath, files.ClientCertPath, files.ClientKeyPath)

Jump to

Keyboard shortcuts

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