Repository navigation
Expand file tree
/
Copy pathcert_apache_test.go
More file actions
190 lines (171 loc) · 6.83 KB
/
Copy pathcert_apache_test.go
File metadata and controls
190 lines (171 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"os"
"path/filepath"
"testing"
)
// 类 Apache 配置(含注释、多个 VirtualHost、大小写混用、ServerAlias)
const apacheConf = `
# 全局配置
Listen 443
<VirtualHost *:443>
ServerName apache-a.com
ServerAlias www.apache-a.com api.apache-a.com
SSLEngine on
SSLCertificateFile /etc/apache2/certs/apache-a.com/fullchain.pem
SSLCertificateKeyFile /etc/apache2/certs/apache-a.com/privkey.pem
# SSLCertificateFile /comment/should/not/match.pem
</VirtualHost>
<VirtualHost 1.2.3.4:443>
servername apache-b.com
sslcertificatefile /etc/httpd/certs/b.pem
sslcertificatekeyfile /etc/httpd/certs/b.key
</VirtualHost>
`
func TestParseApacheConfig(t *testing.T) {
dir := t.TempDir()
confPath := filepath.Join(dir, "ssl.conf")
if err := os.WriteFile(confPath, []byte(apacheConf), 0644); err != nil {
t.Fatal(err)
}
sites := parseApacheConfig(confPath)
if len(sites) != 2 {
t.Fatalf("应解析出 2 个站点,实际 %d: %+v", len(sites), sites)
}
// 站点 A:ServerName + ServerAlias + 大小写不敏感指令
a := sites[0]
if a.Domain != "apache-a.com" {
t.Fatalf("站点A主域名错误: %s", a.Domain)
}
if len(a.Domains) != 3 || a.Domains[2] != "api.apache-a.com" {
t.Fatalf("站点A Domains(含 ServerAlias)错误: %v", a.Domains)
}
if a.CertPath != "/etc/apache2/certs/apache-a.com/fullchain.pem" {
t.Fatalf("站点A证书路径错误: %s", a.CertPath)
}
// 注释中的路径不应被匹配
if a.CertPath == "/comment/should/not/match.pem" {
t.Fatal("注释中的证书路径被误匹配")
}
// 站点 B:小写指令
b := sites[1]
if b.Domain != "apache-b.com" || b.CertPath != "/etc/httpd/certs/b.pem" {
t.Fatalf("站点B解析错误: %+v", b)
}
}
func TestExtractApacheCertPaths(t *testing.T) {
dir := t.TempDir()
confPath := filepath.Join(dir, "ssl.conf")
if err := os.WriteFile(confPath, []byte(apacheConf), 0644); err != nil {
t.Fatal(err)
}
// 主域名匹配
cp, kp, ok := extractCertPathsFromFile(confPath, "apache-a.com")
if !ok || cp != "/etc/apache2/certs/apache-a.com/fullchain.pem" || kp != "/etc/apache2/certs/apache-a.com/privkey.pem" {
t.Fatalf("Apache 路径提取失败: ok=%v cp=%s kp=%s", ok, cp, kp)
}
// ServerAlias 域名匹配
if _, _, ok := extractCertPathsFromFile(confPath, "api.apache-a.com"); !ok {
t.Fatal("ServerAlias 域名应匹配成功")
}
// 不存在的域名
if _, _, ok := extractCertPathsFromFile(confPath, "nope.com"); ok {
t.Fatal("不应匹配不存在的域名")
}
}
func TestIsApacheConfig(t *testing.T) {
if !isApacheConfig(apacheConf) {
t.Fatal("含 <VirtualHost 应识别为 Apache 配置")
}
if isApacheConfig("server { server_name a.com; }") {
t.Fatal("Nginx 配置不应识别为 Apache")
}
// 大小写不敏感
if !isApacheConfig("<virtualhost *:443>...</virtualhost>") {
t.Fatal("<virtualhost 小写也应识别为 Apache")
}
}
// 注释中的 SSLCertificateFile 出现在真实路径之前,且关闭标签小写
const apacheCommentFirst = `
<VirtualHost *:443>
# SSLCertificateFile /comment/first.pem
ServerName comment-first.com
SSLCertificateFile /etc/apache2/certs/comment-first.com/real.pem
SSLCertificateKeyFile /etc/apache2/certs/comment-first.com/real.key
ServerAlias alias.com # 行内注释
</virtualhost>
`
func TestExtractApacheCertPathsCommentFirst(t *testing.T) {
dir := t.TempDir()
confPath := filepath.Join(dir, "ssl.conf")
if err := os.WriteFile(confPath, []byte(apacheCommentFirst), 0644); err != nil {
t.Fatal(err)
}
// 注释在前的 SSLCertificateFile 不应被误取
cp, _, ok := extractCertPathsFromFile(confPath, "comment-first.com")
if !ok || cp != "/etc/apache2/certs/comment-first.com/real.pem" {
t.Fatalf("应返回真实证书路径而非注释中的路径: ok=%v cp=%s", ok, cp)
}
// ServerAlias 行内注释不应进入域名(仅验证不报错,alias 命中真实域名即可)
if _, _, ok := extractCertPathsFromFile(confPath, "alias.com"); !ok {
t.Fatal("ServerAlias 域名应匹配成功")
}
}
// 无 VirtualHost 块:应回退整文件匹配(与 parseApacheConfig 行为一致)
const apacheNoBlock = `
ServerName nohost.com
SSLCertificateFile /etc/httpd/nohost.pem
SSLCertificateKeyFile /etc/httpd/nohost.key
`
// Apache 配置路径带引号(小皮面板场景:SSLCertificateFile "D:/...crt")不应残留引号
const apacheQuoted = `
<VirtualHost *:443>
ServerName cccc.yum6.cn
SSLCertificateFile "D:/phpstudy_pro/Extensions/Apache2.4.39/conf/ssl/cccc.yum6.cn.crt"
SSLCertificateKeyFile 'D:/phpstudy_pro/Extensions/Apache2.4.39/conf/ssl/cccc.yum6.cn.key'
</VirtualHost>
`
// Nginx 配置路径带引号同样支持(ssl_certificate "/path";)
const nginxQuoted = `
server {
server_name www.qq.com;
ssl_certificate "/etc/nginx/ssl/qq/fullchain.pem";
ssl_certificate_key "/etc/nginx/ssl/qq/privkey.pem";
}
`
// 路径值带引号时(Apache/Nginx 均支持)应去除首尾引号,避免路径残留导致文件操作失败
func TestQuotedPaths(t *testing.T) {
dir := t.TempDir()
apPath := filepath.Join(dir, "apache.conf")
os.WriteFile(apPath, []byte(apacheQuoted), 0644)
ngPath := filepath.Join(dir, "nginx.conf")
os.WriteFile(ngPath, []byte(nginxQuoted), 0644)
// Apache 双引号 + 单引号路径
ap := parseApacheConfig(apPath)
if len(ap) != 1 || ap[0].CertPath != "D:/phpstudy_pro/Extensions/Apache2.4.39/conf/ssl/cccc.yum6.cn.crt" || ap[0].KeyPath != "D:/phpstudy_pro/Extensions/Apache2.4.39/conf/ssl/cccc.yum6.cn.key" {
t.Fatalf("Apache 带引号路径解析错误: %+v", ap)
}
// Nginx 双引号路径
ng := parseNginxConfig(ngPath)
if len(ng) != 1 || ng[0].CertPath != "/etc/nginx/ssl/qq/fullchain.pem" || ng[0].KeyPath != "/etc/nginx/ssl/qq/privkey.pem" {
t.Fatalf("Nginx 带引号路径解析错误: %+v", ng)
}
// extract 路径提取同样去引号
cp, kp, ok := extractCertPathsFromFile(apPath, "cccc.yum6.cn")
if !ok || cp != "D:/phpstudy_pro/Extensions/Apache2.4.39/conf/ssl/cccc.yum6.cn.crt" || kp != "D:/phpstudy_pro/Extensions/Apache2.4.39/conf/ssl/cccc.yum6.cn.key" {
t.Fatalf("extract 带引号路径错误: ok=%v cp=%q kp=%q", ok, cp, kp)
}
// basename 不应再带尾部引号(用户报告的现象:cccc.yum6.cn.crt")
if filepath.Base(ap[0].CertPath) != "cccc.yum6.cn.crt" {
t.Fatalf("basename 应无尾部引号: %q", filepath.Base(ap[0].CertPath))
}
}
func TestExtractApacheCertPathsNoBlock(t *testing.T) {
// 单元级验证 extractApacheCertPaths 内部回退(无 VirtualHost 块时整文件匹配)。
// 注意:真实分发场景下,不含 <VirtualHost 的配置会被 isApacheConfig 判为 Nginx 语法,
// 因此该回退属于解析器内部的防御性逻辑。
cp, _, ok := extractApacheCertPaths([]byte(apacheNoBlock), "nohost.com")
if !ok || cp != "/etc/httpd/nohost.pem" {
t.Fatalf("无 VirtualHost 块时应回退整文件匹配: ok=%v cp=%s", ok, cp)
}
}