驱动接入(全部本机真实例验收): - Elasticsearch 原生 SQL API、InfluxDB 原生 HTTP+Flux(官方 JDBC 付费许可,实测弃用) - ZooKeeper/MongoDB JVM agent(连接时自动下载官方驱动 jar,sha256 钉版本到 ~/.opsxdb/drivers/) - 达梦 JDBC agent 桥就绪(待厂商 jar+实例);TiDB 真实例验收 - 协议族代码审查放行 PolarDB/TDSQL/OceanBase/Kingbase/GaussDB/Oracle - 驱动能力模型:按驱动开关对象树分组/右键菜单/行编辑,NoSQL 树拍平(对象直挂库节点) 驱动管理: - 驱动管理弹框(状态/预装/离线包导入/JDK 检测)+ /api/drivers/* 三接口 - opsxdb-driver 驱动商店仓库(清单/fetch.sh/pack.sh 离线包/import.sh) - 连接时懒加载:DMG 零 Java 产物,首连自动下载(实测 ZK 九件套 1.35s),离线包自动回退 UI/交互: - 设置中心(⚙):常规/执行/结果/驱动/快捷键/关于,左导航+右面板(对标 opsxshell) - 实例级展开箭头(行首 SVG chevron,替代字体回退失真的 Unicode 三角) - 树内嵌套渲染修复:对象树挂在激活连接下;工具栏窄窗口不断行(按钮 nowrap+整颗换行) - 侧栏连接行精简(图标+连接名)、SQLite SSH 守卫、Oracle 死参数清理 质量: - Go 协议层/离线回退单测、E2E 门控测试(ES/ZK/Influx/Mongo/TiDB/JDBC 桥) - 驱动注册表单一事实来源 connection-types.json,README/BACKLOG/AGENTS 同步
184 lines
5.4 KiB
Go
184 lines
5.4 KiB
Go
package db
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"math/big"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestEffectiveSSLMode(t *testing.T) {
|
|
if got := EffectiveSSLMode(ConnReq{}); got != "disable" {
|
|
t.Fatalf("empty -> disable, got %s", got)
|
|
}
|
|
if got := EffectiveSSLMode(ConnReq{SSL: true}); got != "require" {
|
|
t.Fatalf("ssl true -> require, got %s", got)
|
|
}
|
|
if got := EffectiveSSLMode(ConnReq{SSL: true, SSLMode: "skip-verify"}); got != "skip-verify" {
|
|
t.Fatalf("mode wins, got %s", got)
|
|
}
|
|
if got := EffectiveSSLMode(ConnReq{SSL: true, SSLMode: "disable"}); got != "disable" {
|
|
t.Fatalf("explicit disable, got %s", got)
|
|
}
|
|
if !SSLEnabled(ConnReq{SSLMode: "require"}) {
|
|
t.Fatal("require should enable")
|
|
}
|
|
}
|
|
|
|
func TestTLSConfigFromDisable(t *testing.T) {
|
|
cfg, err := tlsConfigFrom(ConnReq{})
|
|
if err != nil || cfg != nil {
|
|
t.Fatalf("disable => nil,nil got %#v %v", cfg, err)
|
|
}
|
|
}
|
|
|
|
func TestTLSConfigFromSkipVerify(t *testing.T) {
|
|
cfg, err := tlsConfigFrom(ConnReq{SSL: true, SSLMode: "skip-verify", Host: "db.example"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg == nil || !cfg.InsecureSkipVerify {
|
|
t.Fatalf("expected InsecureSkipVerify, got %#v", cfg)
|
|
}
|
|
if cfg.ServerName != "db.example" {
|
|
t.Fatalf("ServerName %q", cfg.ServerName)
|
|
}
|
|
}
|
|
|
|
func TestTLSConfigFromVerifyCARequiresPath(t *testing.T) {
|
|
_, err := tlsConfigFrom(ConnReq{SSLMode: "verify-ca"})
|
|
if err == nil {
|
|
t.Fatal("expected error without CA")
|
|
}
|
|
}
|
|
|
|
func TestTLSConfigFromVerifyCAWithPEM(t *testing.T) {
|
|
dir := t.TempDir()
|
|
caPath := filepath.Join(dir, "ca.pem")
|
|
if err := writeTestCA(caPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := tlsConfigFrom(ConnReq{SSLMode: "verify-ca", SSLCA: caPath, Host: "127.0.0.1"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg == nil || cfg.RootCAs == nil {
|
|
t.Fatal("expected RootCAs")
|
|
}
|
|
if cfg.VerifyPeerCertificate == nil {
|
|
t.Fatal("expected custom verify for verify-ca")
|
|
}
|
|
}
|
|
|
|
func TestTLSConfigFromBadCAPath(t *testing.T) {
|
|
_, err := tlsConfigFrom(ConnReq{SSLMode: "require", SSLCA: "/no/such/ca.pem"})
|
|
if err == nil {
|
|
t.Fatal("expected read error")
|
|
}
|
|
}
|
|
|
|
// R2-B1:显式 tlsServerName 优先于 Host(tlsConfigFrom 层)。
|
|
func TestTLSConfigFromTLSServerName(t *testing.T) {
|
|
cfg, err := tlsConfigFrom(ConnReq{SSLMode: "require", Host: "127.0.0.1", TLSServerName: "db.example.com"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg == nil || cfg.ServerName != "db.example.com" {
|
|
t.Fatalf("ServerName 应取 tlsServerName, got %#v", cfg)
|
|
}
|
|
}
|
|
|
|
// R2-B2:CA/客户端证书内容优先于路径;成对校验仍然生效。
|
|
func TestTLSConfigFromContentCerts(t *testing.T) {
|
|
caPEM, certPEM, keyPEM := genTestCertMaterials(t)
|
|
|
|
cfg, err := tlsConfigFrom(ConnReq{
|
|
SSLMode: "require", Host: "db.example.com",
|
|
SSLCAContent: caPEM, SSLCertContent: certPEM, SSLKeyContent: keyPEM,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg == nil || cfg.RootCAs == nil {
|
|
t.Fatal("内容 CA 应构建 RootCAs")
|
|
}
|
|
if len(cfg.Certificates) != 1 {
|
|
t.Fatal("内容客户端证书应加载")
|
|
}
|
|
if cfg.ServerName != "db.example.com" {
|
|
t.Fatalf("ServerName 错误: %q", cfg.ServerName)
|
|
}
|
|
|
|
// 内容 CA 非法 PEM 应报错(不回退路径)
|
|
if _, err := tlsConfigFrom(ConnReq{SSLMode: "require", SSLCAContent: "not-a-pem"}); err == nil {
|
|
t.Fatal("非法 CA 内容应报错")
|
|
}
|
|
// 证书与密钥仍需成对:只给内容证书不给内容密钥
|
|
if _, err := tlsConfigFrom(ConnReq{SSLMode: "require", SSLCertContent: certPEM}); err == nil {
|
|
t.Fatal("半对内容证书应报错")
|
|
}
|
|
}
|
|
|
|
// genTestCertMaterials 生成自签名 CA 与客户端证书 PEM 串。
|
|
func genTestCertMaterials(t *testing.T) (caPEM, certPEM, keyPEM string) {
|
|
t.Helper()
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keyDER, err := x509.MarshalECPrivateKey(key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keyPEM = string(pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}))
|
|
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: big.NewInt(2),
|
|
Subject: pkix.Name{CommonName: "opsxdb-test-cert"},
|
|
NotBefore: time.Now().Add(-time.Hour),
|
|
NotAfter: time.Now().Add(24 * time.Hour),
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
BasicConstraintsValid: true,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
certPEM = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}))
|
|
return certPEM, certPEM, keyPEM // 自签名,CA 与客户端证书同一张
|
|
}
|
|
|
|
func writeTestCA(path string) error {
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: big.NewInt(1),
|
|
Subject: pkix.Name{CommonName: "opsxdb-test-ca"},
|
|
NotBefore: time.Now().Add(-time.Hour),
|
|
NotAfter: time.Now().Add(24 * time.Hour),
|
|
IsCA: true,
|
|
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature,
|
|
BasicConstraintsValid: true,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
f, err := os.Create(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
return pem.Encode(f, &pem.Block{Type: "CERTIFICATE", Bytes: der})
|
|
}
|