使用专用证书默认目录并拒绝接管其他账户目录

This commit is contained in:
xuans committed 2026-09-27 00:45:29 +08:00
1 parent 4dfcecfd21
commit 2d84f16554
9 files changed
+89 -12

No files matched your search

+62 -1
View File
@@ -73,6 +73,66 @@ systemctl() {
}
}
func TestLinuxInstallerCertificateOwnership(t *testing.T) {
bash, err := exec.LookPath("bash")
if err != nil {
t.Skip(err)
}
data, err := os.ReadFile(filepath.Join("..", "..", "..", "deploy", "install-linux.sh"))
if err != nil {
t.Fatal(err)
}
content := string(data)
start := strings.Index(content, "validate_existing_certificate_dir() {")
if start < 0 {
t.Fatal("validate_existing_certificate_dir")
}
end := strings.Index(content[start:], "\n}\n")
if end < 0 {
t.Fatal("validate_existing_certificate_dir")
}
guard := content[start : start+end+3]
base := t.TempDir()
file := filepath.Join(base, "keep.pem")
if err := os.WriteFile(file, []byte("preserve"), 0600); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
path string
owner string
allowed bool
}{
{"new-directory", filepath.Join(base, "new"), "root:root", true},
{"owned-directory", base, "Scert:Scert", true},
{"shared-directory", base, "root:root", false},
{"other-service", base, "nginx:nginx", false},
{"wrong-group", base, "Scert:root", false},
{"missing-owner", base, "", false},
{"existing-file", file, "Scert:Scert", false},
} {
t.Run(tc.name, func(t *testing.T) {
script := `set -Eeuo pipefail
SERVICE_USER=Scert
SERVICE_GROUP=Scert
certificate_dir="$TEST_PATH"
fail() { printf '%s\n' "$1" >&2; exit 1; }
stat() { printf '%s\n' "$TEST_OWNER"; }
` + guard + "\nvalidate_existing_certificate_dir\n"
command := exec.Command(bash, "--noprofile", "--norc", "-c", script)
command.Env = append(os.Environ(), "TEST_PATH="+filepath.ToSlash(tc.path), "TEST_OWNER="+tc.owner)
output, err := command.CombinedOutput()
if (err == nil) != tc.allowed {
t.Fatalf("allowed=%t, err=%v, output=%s", tc.allowed, err, output)
}
preserved, err := os.ReadFile(file)
if err != nil || string(preserved) != "preserve" {
t.Fatalf("preserved=%q, err=%v", preserved, err)
}
})
}
}
func TestLinuxInstallerPolicy(t *testing.T) {
installerPath := filepath.Join("..", "..", "..", "deploy", "install-linux.sh")
data, err := os.ReadFile(installerPath)
@@ -83,7 +143,8 @@ func TestLinuxInstallerPolicy(t *testing.T) {
required := []string{
`readonly INSTALL_DIR="/scert"`,
`readonly DEFAULT_CERTIFICATE_DIR="/ssl"`,
`readonly DEFAULT_CERTIFICATE_DIR="/ssl/scert"`,
`validate_existing_certificate_dir`,
`readonly SERVICE_USER="Scert"`,
`readonly INSTALL_METADATA_FILE="$INSTALL_DIR/conf/installation.json"`,
`useradd --system`,
+1 -1
View File
@@ -38,7 +38,7 @@ func TestScertSystemdUnitPolicy(t *testing.T) {
"RestrictSUIDSGID=yes",
"RemoveIPC=yes",
"SystemCallArchitectures=native",
"ReadWritePaths=/ssl /scert/conf /scert/data /scert/log /scert/backups",
"ReadWritePaths=/ssl/scert /scert/conf /scert/data /scert/log /scert/backups",
"[Install]",
"WantedBy=multi-user.target",
} {
+1 -1
View File
@@ -168,7 +168,7 @@ func Default(baseDir string) Config {
func DefaultPaths(baseDir string) PathsConfig {
if runtime.GOOS == "linux" {
certificateRoot := "/ssl"
certificateRoot := "/ssl/scert"
if strings.TrimSpace(baseDir) == "" {
if installedRoot, err := readInstalledCertificateRoot(LinuxInstallationMetadataPath); err == nil {
certificateRoot = installedRoot
+1 -1
View File
@@ -11,7 +11,7 @@ import (
func TestDefaultPaths(t *testing.T) {
paths := DefaultPaths(t.TempDir())
if runtime.GOOS == "linux" {
if paths.Install != "/scert" || paths.Certificate != "/ssl" {
if paths.Install != "/scert" || paths.Certificate != "/ssl/scert" {
t.Fatalf("Linux 默认路径不正确:%+v", paths)
}
return