feat(settings): 页面录入测试/生产 WeKnora 连接与密钥并收敛可达性判定
连接(地址 + 写-only 密钥)从文件引用搬进治理库:AES-GCM 密文 + 独立 0600 主密钥,
页面与接口只回显尾 4 位;测试连接真实列库并区分 地址不可达/密钥无效/无权限/成功,
换地址或换密钥必须测通才能保存(迁移 000083)。
可达性判定收敛为 weknora.HTTPAllowed,客户端与监控探活共用;新增
WEKNORA_ALLOW_PRIVATE_HTTP 只放行 RFC1918/回环/链路本地。生产绑定只保留下拉,
保存后回报冻结/未冻结条数,已冻结目标不被改写。
(cherry picked from commit ad02e8bb9c)
This commit is contained in:
@@ -48,6 +48,43 @@ type ClientConfig struct {
|
||||
Timeout time.Duration
|
||||
MaxResponseBytes int64
|
||||
AllowDockerHTTP bool
|
||||
// AllowPrivateHTTP relaxes the http-only rule for RFC1918, link-local and
|
||||
// ULA literals. It exists because the production instance sits behind a
|
||||
// same-LAN nginx that speaks plain http; https stays the default and any
|
||||
// public http host is still refused.
|
||||
AllowPrivateHTTP bool
|
||||
}
|
||||
|
||||
// HTTPPolicy is the single reachability judgement shared by the WeKnora client
|
||||
// and the console probes: two callers must never disagree about whether a
|
||||
// configured endpoint is acceptable.
|
||||
type HTTPPolicy struct {
|
||||
AllowPrivateHTTP bool
|
||||
AllowDockerHTTP bool
|
||||
}
|
||||
|
||||
// HTTPAllowed reports whether baseURL may be called at all. https is always
|
||||
// allowed; http only for loopback, an opted-in private address, or (legacy)
|
||||
// a single-label docker host.
|
||||
func HTTPAllowed(rawURL string, policy HTTPPolicy) bool {
|
||||
parsed, err := url.Parse(strings.TrimSpace(rawURL))
|
||||
if err != nil || parsed.Host == "" || parsed.User != nil {
|
||||
return false
|
||||
}
|
||||
switch parsed.Scheme {
|
||||
case "https":
|
||||
return true
|
||||
case "http":
|
||||
host := parsed.Hostname()
|
||||
if isLoopbackHost(host) {
|
||||
return true
|
||||
}
|
||||
if policy.AllowPrivateHTTP && isPrivateHost(host) {
|
||||
return true
|
||||
}
|
||||
return policy.AllowDockerHTTP && isSingleLabelDockerHost(host)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
@@ -65,7 +102,7 @@ func NewClient(config ClientConfig) (*Client, error) {
|
||||
if baseURL.Scheme == "" || baseURL.Host == "" || baseURL.User != nil || baseURL.RawQuery != "" || baseURL.Fragment != "" {
|
||||
return nil, errors.New("invalid WeKnora base URL")
|
||||
}
|
||||
if baseURL.Scheme != "https" && !(baseURL.Scheme == "http" && (isLoopbackHost(baseURL.Hostname()) || (config.AllowDockerHTTP && isSingleLabelDockerHost(baseURL.Hostname())))) {
|
||||
if !HTTPAllowed(config.BaseURL, HTTPPolicy{AllowPrivateHTTP: config.AllowPrivateHTTP, AllowDockerHTTP: config.AllowDockerHTTP}) {
|
||||
return nil, errors.New("WeKnora base URL must use HTTPS")
|
||||
}
|
||||
baseURL.Path = strings.TrimRight(baseURL.Path, "/")
|
||||
@@ -99,6 +136,17 @@ func isLoopbackHost(host string) bool {
|
||||
return ip != nil && ip.IsLoopback()
|
||||
}
|
||||
|
||||
// isPrivateHost reports the addresses a same-LAN deployment legitimately uses:
|
||||
// RFC1918, link-local and IPv6 ULA. A public name is never private, so opting
|
||||
// in cannot widen the client to the internet.
|
||||
func isPrivateHost(host string) bool {
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
return false
|
||||
}
|
||||
return ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()
|
||||
}
|
||||
|
||||
func isSingleLabelDockerHost(host string) bool {
|
||||
if host == "" || strings.Contains(host, ".") || net.ParseIP(host) != nil || len(host) > 63 {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user