feat: replace cert_dir with support_dir in agent and server configurations

- Updated README.md to reflect the new support_dir for auxiliary files.
- Refactored agent main.go to use support_dir instead of cert_dir.
- Modified config.go to replace cert_dir with support_dir and added legacy support.
- Adjusted config tests to validate support_dir usage.
- Changed nginx manager to utilize support_dir for file paths.
- Updated server configuration to use support_dir for SSL certificates.
- Revised documentation to clarify the new configuration parameters.
- Enhanced security checks for support file paths to prevent traversal attacks.
This commit is contained in:
ryan
2026-03-14 17:16:54 +08:00
parent bdfa80f214
commit 4be44733e8
11 changed files with 159 additions and 143 deletions
+29 -15
View File
@@ -14,9 +14,9 @@ import (
const (
defaultDockerMainConfigRelativePath = "etc/nginx/nginx.conf"
defaultDockerRouteConfigRelativePath = "etc/nginx/conf.d/atsflare_routes.conf"
defaultCertDirRelativePath = "etc/nginx/certs"
defaultSupportDirRelativePath = "etc/nginx/support"
defaultDockerStateRelativePath = "var/lib/atsflare/agent-state.json"
defaultDockerOpenRestyCertDir = "/etc/nginx/atsflare-certs"
defaultDockerOpenRestySupportDir = "/etc/nginx/atsflare-support"
defaultOpenRestyObservabilityPort = 18081
)
@@ -35,8 +35,8 @@ type Config struct {
DataDir string `json:"data_dir"`
MainConfigPath string `json:"main_config_path"`
RouteConfigPath string `json:"route_config_path"`
CertDir string `json:"cert_dir"`
OpenrestyCertDir string `json:"openresty_cert_dir"`
SupportDir string `json:"support_dir"`
OpenrestySupportDir string `json:"openresty_support_dir"`
OpenrestyObservabilityPort int `json:"openresty_observability_port"`
StatePath string `json:"state_path"`
HeartbeatInterval MillisecondDuration `json:"heartbeat_interval"`
@@ -57,8 +57,10 @@ type configFile struct {
DataDir string `json:"data_dir"`
MainConfigPath string `json:"main_config_path"`
RouteConfigPath string `json:"route_config_path"`
CertDir string `json:"cert_dir"`
OpenrestyCertDir string `json:"openresty_cert_dir"`
SupportDir string `json:"support_dir"`
OpenrestySupportDir string `json:"openresty_support_dir"`
LegacyCertDir string `json:"cert_dir"`
LegacyOpenrestyCertDir string `json:"openresty_cert_dir"`
OpenrestyObservabilityPort int `json:"openresty_observability_port"`
StatePath string `json:"state_path"`
HeartbeatInterval MillisecondDuration `json:"heartbeat_interval"`
@@ -87,8 +89,8 @@ func Load(path string) (*Config, error) {
DataDir: file.DataDir,
MainConfigPath: file.MainConfigPath,
RouteConfigPath: file.RouteConfigPath,
CertDir: file.CertDir,
OpenrestyCertDir: file.OpenrestyCertDir,
SupportDir: firstNonEmpty(file.SupportDir, file.LegacyCertDir),
OpenrestySupportDir: firstNonEmpty(file.OpenrestySupportDir, file.LegacyOpenrestyCertDir),
OpenrestyObservabilityPort: file.OpenrestyObservabilityPort,
StatePath: file.StatePath,
HeartbeatInterval: file.HeartbeatInterval,
@@ -138,14 +140,14 @@ func applyDefaults(cfg *Config, baseDir string) {
cfg.StatePath = joinManagedPath(cfg.DataDir, defaultDockerStateRelativePath)
}
}
if cfg.CertDir == "" {
cfg.CertDir = joinManagedPath(cfg.DataDir, defaultCertDirRelativePath)
if cfg.SupportDir == "" {
cfg.SupportDir = joinManagedPath(cfg.DataDir, defaultSupportDirRelativePath)
}
if cfg.OpenrestyCertDir == "" {
if cfg.OpenrestySupportDir == "" {
if cfg.OpenrestyPath != "" {
cfg.OpenrestyCertDir = cfg.CertDir
cfg.OpenrestySupportDir = cfg.SupportDir
} else {
cfg.OpenrestyCertDir = defaultDockerOpenRestyCertDir
cfg.OpenrestySupportDir = defaultDockerOpenRestySupportDir
}
}
if cfg.OpenrestyObservabilityPort <= 0 {
@@ -173,8 +175,11 @@ func normalizeManagedPaths(cfg *Config) {
if usesSlashPath(cfg.RouteConfigPath) {
cfg.RouteConfigPath = filepath.ToSlash(cfg.RouteConfigPath)
}
if usesSlashPath(cfg.CertDir) {
cfg.CertDir = filepath.ToSlash(cfg.CertDir)
if usesSlashPath(cfg.SupportDir) {
cfg.SupportDir = filepath.ToSlash(cfg.SupportDir)
}
if usesSlashPath(cfg.OpenrestySupportDir) {
cfg.OpenrestySupportDir = filepath.ToSlash(cfg.OpenrestySupportDir)
}
if usesSlashPath(cfg.StatePath) {
cfg.StatePath = filepath.ToSlash(cfg.StatePath)
@@ -243,6 +248,15 @@ func detectHostname() string {
return strings.TrimSpace(host)
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {
return value
}
}
return ""
}
func detectNodeIP() string {
interfaces, err := net.Interfaces()
if err != nil {
+8 -8
View File
@@ -39,8 +39,8 @@ func TestLoadDockerModeUsesManagedPaths(t *testing.T) {
if cfg.RouteConfigPath != filepath.Join(dir, "data", defaultDockerRouteConfigRelativePath) {
t.Fatalf("unexpected route config path: %s", cfg.RouteConfigPath)
}
if cfg.CertDir != filepath.Join(dir, "data", defaultCertDirRelativePath) {
t.Fatalf("unexpected cert dir: %s", cfg.CertDir)
if cfg.SupportDir != filepath.Join(dir, "data", defaultSupportDirRelativePath) {
t.Fatalf("unexpected support dir: %s", cfg.SupportDir)
}
if cfg.OpenrestyContainerName != "atsflare-openresty" {
t.Fatalf("unexpected openresty container name: %s", cfg.OpenrestyContainerName)
@@ -48,8 +48,8 @@ func TestLoadDockerModeUsesManagedPaths(t *testing.T) {
if cfg.OpenrestyDockerImage != "openresty/openresty:alpine" {
t.Fatalf("unexpected openresty image: %s", cfg.OpenrestyDockerImage)
}
if cfg.OpenrestyCertDir != defaultDockerOpenRestyCertDir {
t.Fatalf("unexpected openresty cert dir: %s", cfg.OpenrestyCertDir)
if cfg.OpenrestySupportDir != defaultDockerOpenRestySupportDir {
t.Fatalf("unexpected openresty support dir: %s", cfg.OpenrestySupportDir)
}
if cfg.StatePath != filepath.Join(dir, "data", defaultDockerStateRelativePath) {
t.Fatalf("unexpected state path: %s", cfg.StatePath)
@@ -94,8 +94,8 @@ func TestLoadPathModeKeepsExplicitPaths(t *testing.T) {
if cfg.StatePath != "/tmp/agent-state.json" {
t.Fatalf("unexpected state path: %s", cfg.StatePath)
}
if cfg.OpenrestyCertDir != cfg.CertDir {
t.Fatalf("expected path mode openresty cert dir to equal cert dir, got %s / %s", cfg.OpenrestyCertDir, cfg.CertDir)
if cfg.OpenrestySupportDir != cfg.SupportDir {
t.Fatalf("expected path mode openresty support dir to equal support dir, got %s / %s", cfg.OpenrestySupportDir, cfg.SupportDir)
}
if cfg.OpenrestyObservabilityPort != defaultOpenRestyObservabilityPort {
t.Fatalf("unexpected path mode openresty observability port: %d", cfg.OpenrestyObservabilityPort)
@@ -134,8 +134,8 @@ func TestLoadUsesCustomDataDirForGeneratedFiles(t *testing.T) {
if cfg.StatePath != "/srv/atsflare/"+defaultDockerStateRelativePath {
t.Fatalf("unexpected state path: %s", cfg.StatePath)
}
if cfg.CertDir != "/srv/atsflare/"+defaultCertDirRelativePath {
t.Fatalf("unexpected cert dir: %s", cfg.CertDir)
if cfg.SupportDir != "/srv/atsflare/"+defaultSupportDirRelativePath {
t.Fatalf("unexpected support dir: %s", cfg.SupportDir)
}
}