按证书托管方式统一自动续期策略并补充回归测试

This commit is contained in:
xuans committed 2026-09-18 15:05:25 +08:00
1 parent e2e35b4800
commit 8a30cae56a
9 files changed
+217 -21

No files matched your search

+26 -3
View File
@@ -7,7 +7,30 @@ func (c *Config) UsesProxyHTTPS() bool {
return c != nil && strings.EqualFold(strings.TrimSpace(c.TLSMode), HTTPSTLSModeProxy)
}
// AutomaticSSLEnabled 保留原证书策略,但反代模式下禁止 Smail 自动签发和续期。
func (c *Config) AutomaticSSLEnabled() bool {
return c != nil && !c.UsesProxyHTTPS() && (c.SSLType == SSLTypeAutoHTTP || c.SSLType == SSLTypeAutoDNS)
// EffectiveSSLType 以证书管理责任为准,仅在旧配置未记录管理方式时兼容旧类型。
func (c *Config) EffectiveSSLType() string {
if c == nil || c.UsesProxyHTTPS() {
return SSLTypeUser
}
management := c.SSL.Management
if management == "" && c.SSL.Mode != "" {
management = EffectiveSSLManagement(c.SSL)
}
switch management {
case SSLManagementProject:
if c.SSL.Challenge == SSLChallengeDNS || (c.SSL.Challenge == "" && c.SSLType == SSLTypeAutoDNS) {
return SSLTypeAutoDNS
}
return SSLTypeAutoHTTP
case "":
if c.SSLType == SSLTypeAutoHTTP || c.SSLType == SSLTypeAutoDNS {
return c.SSLType
}
}
return SSLTypeUser
}
// AutomaticSSLEnabled 仅允许项目自托管证书自动签发和续期。
func (c *Config) AutomaticSSLEnabled() bool {
return c.EffectiveSSLType() != SSLTypeUser
}
+36
View File
@@ -0,0 +1,36 @@
package config
import "testing"
func TestSSLPolicyFollowsManagement(t *testing.T) {
for _, test := range []struct {
name string
cfg *Config
want string
}{
{name: "nil", want: SSLTypeUser},
{name: "empty", cfg: &Config{}, want: SSLTypeUser},
{name: "legacy_http", cfg: &Config{SSLType: SSLTypeAutoHTTP}, want: SSLTypeAutoHTTP},
{name: "legacy_dns", cfg: &Config{SSLType: SSLTypeAutoDNS}, want: SSLTypeAutoDNS},
{name: "legacy_manual", cfg: &Config{SSLType: SSLTypeUser}, want: SSLTypeUser},
{name: "legacy_upload", cfg: &Config{SSLType: SSLTypeAutoHTTP, SSL: SSLConfig{Mode: ManagedSSLModeUpload}}, want: SSLTypeUser},
{name: "legacy_default", cfg: &Config{SSLType: SSLTypeUser, SSL: SSLConfig{Mode: ManagedSSLModeDefault}}, want: SSLTypeAutoHTTP},
{name: "project_http", cfg: &Config{SSLType: SSLTypeAutoDNS, SSL: SSLConfig{Management: SSLManagementProject, Challenge: SSLChallengeHTTP}}, want: SSLTypeAutoHTTP},
{name: "project_dns", cfg: &Config{SSLType: SSLTypeUser, SSL: SSLConfig{Management: SSLManagementProject, Challenge: SSLChallengeDNS}}, want: SSLTypeAutoDNS},
{name: "project_transition", cfg: &Config{SSLType: SSLTypeUser, SSL: SSLConfig{Management: SSLManagementProject, Mode: ManagedSSLModeUpload}}, want: SSLTypeAutoHTTP},
{name: "project_legacy_dns", cfg: &Config{SSLType: SSLTypeAutoDNS, SSL: SSLConfig{Management: SSLManagementProject}}, want: SSLTypeAutoDNS},
{name: "custom_stale_auto", cfg: &Config{SSLType: SSLTypeAutoHTTP, SSL: SSLConfig{Management: SSLManagementCustom, Mode: ManagedSSLModeDefault}}, want: SSLTypeUser},
{name: "web_server_stale_auto", cfg: &Config{SSLType: SSLTypeAutoDNS, SSL: SSLConfig{Management: SSLManagementWebServer}}, want: SSLTypeUser},
{name: "proxy_project", cfg: &Config{TLSMode: " PROXY ", SSL: SSLConfig{Management: SSLManagementProject}}, want: SSLTypeUser},
{name: "invalid_management", cfg: &Config{SSLType: SSLTypeAutoHTTP, SSL: SSLConfig{Management: "invalid"}}, want: SSLTypeUser},
} {
t.Run(test.name, func(t *testing.T) {
if got := test.cfg.EffectiveSSLType(); got != test.want {
t.Fatalf("证书策略错误:got=%s want=%s", got, test.want)
}
if test.cfg.AutomaticSSLEnabled() != (test.want != SSLTypeUser) {
t.Fatal("自动续期开关与托管策略不一致")
}
})
}
}