chore: 使用环境变量管理内部密钥

This commit is contained in:
zxr
2026-08-04 15:48:59 +08:00
parent 8495fdf039
commit db3cb24b4e
3 changed files with 76 additions and 69 deletions

View File

@@ -20,12 +20,12 @@ Ingest:
trap_listen_addr: "0.0.0.0:9162" trap_listen_addr: "0.0.0.0:9162"
rule_refresh_secs: 30 rule_refresh_secs: 30
AlertForward: AlertForward:
enabled: true enabled: true
base_url: https://ops-api.apinb.com base_url: https://ops-api.apinb.com
internal_key: "ops-alert" internal_key: ${LOGS_ALERT_SECRET}
default_policy_id: 0 default_policy_id: 0
ResourceEvent: ResourceEvent:
hmac_secret: "replace-with-dc-control-shared-secret" hmac_secret: ${DC_CONTROL_LOGS_EVENT_SECRET}
max_skew_secs: 300 max_skew_secs: 300

View File

@@ -20,12 +20,12 @@ Ingest:
trap_listen_addr: "0.0.0.0:9162" trap_listen_addr: "0.0.0.0:9162"
rule_refresh_secs: 30 rule_refresh_secs: 30
AlertForward: AlertForward:
enabled: true enabled: true
base_url: https://ops-api.apinb.com base_url: https://ops-api.apinb.com
internal_key: "ops-alert" internal_key: ${LOGS_ALERT_SECRET}
default_policy_id: 0 default_policy_id: 0
ResourceEvent: ResourceEvent:
hmac_secret: "replace-with-dc-control-shared-secret" hmac_secret: ${DC_CONTROL_LOGS_EVENT_SECRET}
max_skew_secs: 300 max_skew_secs: 300

View File

@@ -1,51 +1,58 @@
package config package config
import ( import (
"net" "net"
"strings"
"git.apinb.com/bsm-sdk/core/conf"
) "git.apinb.com/bsm-sdk/core/conf"
)
var Spec SrvConfig
var Spec SrvConfig
type AlertForwardConf struct {
BaseURL string `yaml:"base_url"` type AlertForwardConf struct {
InternalKey string `yaml:"internal_key"` BaseURL string `yaml:"base_url"`
Enabled bool `yaml:"enabled"` InternalKey string `yaml:"internal_key"`
DefaultPolicyID uint `yaml:"default_policy_id"` Enabled bool `yaml:"enabled"`
} DefaultPolicyID uint `yaml:"default_policy_id"`
}
type IngestConf struct {
SyslogListenAddr string `yaml:"syslog_listen_addr"` type IngestConf struct {
TrapListenAddr string `yaml:"trap_listen_addr"` SyslogListenAddr string `yaml:"syslog_listen_addr"`
RuleRefreshSecs int `yaml:"rule_refresh_secs"` TrapListenAddr string `yaml:"trap_listen_addr"`
} RuleRefreshSecs int `yaml:"rule_refresh_secs"`
}
type ResourceEventConf struct {
// HMACSecret 用于校验 dc-control 推送签名X-Event-Signature type ResourceEventConf struct {
HMACSecret string `yaml:"hmac_secret"` // HMACSecret 用于校验 dc-control 推送签名X-Event-Signature
// MaxSkewSecs 允许事件时间与服务端时间的最大偏差(秒)。 HMACSecret string `yaml:"hmac_secret"`
MaxSkewSecs int `yaml:"max_skew_secs"` // MaxSkewSecs 允许事件时间与服务端时间的最大偏差(秒)。
} MaxSkewSecs int `yaml:"max_skew_secs"`
}
type SrvConfig struct {
conf.Base `yaml:",inline"` type SrvConfig struct {
Databases *conf.DBConf `yaml:"Databases"` conf.Base `yaml:",inline"`
MicroService *conf.MicroServiceConf `yaml:"MicroService"` Databases *conf.DBConf `yaml:"Databases"`
Rpc map[string]conf.RpcConf `yaml:"Rpc"` MicroService *conf.MicroServiceConf `yaml:"MicroService"`
Gateway *conf.GatewayConf `yaml:"Gateway"` Rpc map[string]conf.RpcConf `yaml:"Rpc"`
Apm *conf.ApmConf `yaml:"APM"` Gateway *conf.GatewayConf `yaml:"Gateway"`
Etcd *conf.EtcdConf `yaml:"Etcd"` Apm *conf.ApmConf `yaml:"APM"`
AlertForward *AlertForwardConf `yaml:"AlertForward"` Etcd *conf.EtcdConf `yaml:"Etcd"`
Ingest IngestConf `yaml:"Ingest"` AlertForward *AlertForwardConf `yaml:"AlertForward"`
ResourceEvent ResourceEventConf `yaml:"ResourceEvent"` Ingest IngestConf `yaml:"Ingest"`
} ResourceEvent ResourceEventConf `yaml:"ResourceEvent"`
}
func New(srvKey string) {
conf.New(srvKey, &Spec) func New(srvKey string) {
Spec.Port = conf.CheckPort(Spec.Port) conf.New(srvKey, &Spec)
Spec.BindIP = conf.CheckIP(Spec.BindIP) Spec.Port = conf.CheckPort(Spec.Port)
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port) Spec.BindIP = conf.CheckIP(Spec.BindIP)
conf.NotNil(Spec.Service, Spec.Cache) Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
conf.PrintInfo(Spec.Addr) Spec.ResourceEvent.HMACSecret = strings.TrimSpace(Spec.ResourceEvent.HMACSecret)
} conf.NotNil(Spec.Service, Spec.Cache, Spec.ResourceEvent.HMACSecret)
if Spec.AlertForward != nil && Spec.AlertForward.Enabled {
Spec.AlertForward.BaseURL = strings.TrimSpace(Spec.AlertForward.BaseURL)
Spec.AlertForward.InternalKey = strings.TrimSpace(Spec.AlertForward.InternalKey)
conf.NotNil(Spec.AlertForward.BaseURL, Spec.AlertForward.InternalKey)
}
conf.PrintInfo(Spec.Addr)
}