-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
281 lines (258 loc) · 7.91 KB
/
Copy pathmain_test.go
File metadata and controls
281 lines (258 loc) · 7.91 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2026 HAProxy Technologies LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/haproxytech/gopherd/internal/yml"
)
func TestLoadConfigValid(t *testing.T) {
t.Parallel()
dir := t.TempDir()
cfgPath := filepath.Join(dir, "test.yml")
os.WriteFile(cfgPath, []byte(`
processes:
- name: app
command: "true"
`), 0o644)
cfg, err := yml.Load(cfgPath)
if err != nil {
t.Fatalf("Load: %v", err)
}
if len(cfg.Processes) != 1 {
t.Fatalf("expected 1 process, got %d", len(cfg.Processes))
}
if cfg.Processes[0].Name != "app" {
t.Errorf("process name = %q, want app", cfg.Processes[0].Name)
}
}
func TestLoadConfigNoProcesses(t *testing.T) {
t.Parallel()
dir := t.TempDir()
cfgPath := filepath.Join(dir, "empty.yml")
os.WriteFile(cfgPath, []byte("# empty\n"), 0o644)
_, err := yml.Load(cfgPath)
if err == nil {
t.Error("expected error for config with no processes")
}
}
func TestLoadConfigFileNotFound(t *testing.T) {
t.Parallel()
_, err := yml.Load("/nonexistent/path.yml")
if err == nil {
t.Error("expected error for missing file")
}
}
func TestLoadConfigFull(t *testing.T) {
t.Parallel()
dir := t.TempDir()
cfgPath := filepath.Join(dir, "full.yml")
os.WriteFile(cfgPath, []byte(`
prefix: "service"
control:
socket: /tmp/test.sock
processes:
- name: init-task
command: "true"
startup: oneshot
- name: app
command: myapp
args: ["--verbose"]
working-dir: /tmp
stop-signal: SIGUSR1
kill-delay: 30s
on-success: ignore
on-failure: restart
backoff-delay: 1s
backoff-factor: 1.5
backoff-limit: 60s
after: [init-task]
requires: [init-task]
ready-check: app-health
ready-timeout: 30s
environment:
FOO: bar
on-check-failure:
app-health: restart
checks:
app-health:
http:
url: http://localhost:8080/health
socket: /var/run/app.sock
period: 5s
timeout: 2s
threshold: 3
initial-delay: 10s
tcp-check:
tcp:
host: localhost
port: 5432
period: 10s
exec-check:
exec:
command: "true"
log-targets:
syslog:
type: syslog
location: udp://localhost:514
services: [app]
labels:
env: test
`), 0o644)
cfg, err := yml.Load(cfgPath)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Prefix != "service" {
t.Errorf("expected Prefix=%q, got %q", "service", cfg.Prefix)
}
if cfg.Control.SocketPath != "/tmp/test.sock" {
t.Errorf("socket = %q", cfg.Control.SocketPath)
}
if len(cfg.Processes) != 2 {
t.Fatalf("expected 2 processes, got %d", len(cfg.Processes))
}
if cfg.Processes[0].Startup != "oneshot" {
t.Errorf("init.Startup = %q", cfg.Processes[0].Startup)
}
app := cfg.Processes[1]
if app.StopSignal != "SIGUSR1" {
t.Errorf("app.StopSignal = %q", app.StopSignal)
}
if app.ReadyCheck != "app-health" {
t.Errorf("app.ReadyCheck = %q", app.ReadyCheck)
}
if app.Environment["FOO"] != "bar" {
t.Errorf("env FOO = %q", app.Environment["FOO"])
}
if len(cfg.Checks) != 3 {
t.Fatalf("expected 3 checks, got %d", len(cfg.Checks))
}
if cfg.Checks["app-health"].HTTP.Socket != "/var/run/app.sock" {
t.Errorf("http socket = %q", cfg.Checks["app-health"].HTTP.Socket)
}
if cfg.Checks["app-health"].InitialDelay != "10s" {
t.Errorf("initial-delay = %q", cfg.Checks["app-health"].InitialDelay)
}
if len(cfg.LogTargets) != 1 {
t.Fatalf("expected 1 log target, got %d", len(cfg.LogTargets))
}
if cfg.LogTargets["syslog"].Labels["env"] != "test" {
t.Errorf("label env = %q", cfg.LogTargets["syslog"].Labels["env"])
}
}
// TestReadConfigFileRejectsUntrustedFile pins the trust checks on the config
// path. They have no functional symptom by construction — a world-writable or
// foreign-owned config parses exactly like a safe one — so only a negative test
// keeps them in place. The file dictates what PID 1 executes and as whom, so
// anyone who can write it owns the container.
func TestReadConfigFileRejectsUntrustedFile(t *testing.T) {
const body = "processes:\n - name: app\n command: \"true\"\n"
// Accepted: owner-only and group-readable modes owned by us.
for _, mode := range []os.FileMode{0o400, 0o600, 0o640, 0o644} {
dir := t.TempDir()
path := filepath.Join(dir, "ok.yml")
if err := os.WriteFile(path, []byte(body), mode); err != nil {
t.Fatalf("write: %v", err)
}
if err := os.Chmod(path, mode); err != nil {
t.Fatalf("chmod: %v", err)
}
if _, err := readConfigFile(path); err != nil {
t.Errorf("mode %04o: unexpected error: %v", mode, err)
}
}
// Rejected: any mode another user can write to.
for _, mode := range []os.FileMode{0o602, 0o606, 0o642, 0o666, 0o777} {
dir := t.TempDir()
path := filepath.Join(dir, "loose.yml")
if err := os.WriteFile(path, []byte(body), mode); err != nil {
t.Fatalf("write: %v", err)
}
if err := os.Chmod(path, mode); err != nil {
t.Fatalf("chmod: %v", err)
}
_, err := readConfigFile(path)
if err == nil {
t.Errorf("mode %04o: expected refusal for a world-writable config", mode)
continue
}
if !strings.Contains(err.Error(), "world-writable") {
t.Errorf("mode %04o: error %q should say the config is world-writable",
mode, err)
}
}
// Rejected: a symlinked config path. O_NOFOLLOW makes the check atomic with
// the open, so there is no window to swap the target.
t.Run("symlink", func(t *testing.T) {
dir := t.TempDir()
target := filepath.Join(dir, "real.yml")
link := filepath.Join(dir, "link.yml")
if err := os.WriteFile(target, []byte(body), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
if err := os.Symlink(target, link); err != nil {
t.Fatalf("symlink: %v", err)
}
if _, err := readConfigFile(link); err == nil {
t.Error("expected refusal for a symlinked config path")
} else if !strings.Contains(err.Error(), "symlink") {
t.Errorf("error %q should say the config is a symlink", err)
}
})
// Rejected: owned by a different, non-root uid. Only meaningful when the
// test process can chown, i.e. running as root.
t.Run("foreign owner", func(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("needs root to chown the config to another uid")
}
dir := t.TempDir()
path := filepath.Join(dir, "foreign.yml")
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
const otherUID = 65534 // nobody
if err := os.Chown(path, otherUID, otherUID); err != nil {
t.Skipf("chown unavailable: %v", err)
}
if _, err := readConfigFile(path); err == nil {
t.Errorf("expected refusal for a config owned by uid %d", otherUID)
} else if !strings.Contains(err.Error(), "owned by uid") {
t.Errorf("error %q should name the unexpected owner", err)
}
})
}
// TestReadConfigFileRejectsOversized verifies that readConfigFile refuses a
// config file larger than the size cap. Without the cap, a misconfigured
// GOPHERD_CONFIG or a swapped-out config could drive PID 1 into an
// unbounded allocation.
func TestReadConfigFileRejectsOversized(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "big.yml")
huge := make([]byte, maxConfigFileSize+1)
for i := range huge {
huge[i] = ' '
}
if err := os.WriteFile(path, huge, 0o644); err != nil {
t.Fatalf("write: %v", err)
}
if _, err := readConfigFile(path); err == nil {
t.Fatal("expected error for oversized config, got nil")
} else if !strings.Contains(err.Error(), "size cap") {
t.Errorf("error %q does not mention size cap", err.Error())
}
}