-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathintegration_permissions_test.go
More file actions
288 lines (242 loc) · 8.37 KB
/
integration_permissions_test.go
File metadata and controls
288 lines (242 loc) · 8.37 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
282
283
284
285
286
287
288
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
"github.com/mozilla-ai/mcpd/internal/cache"
"github.com/mozilla-ai/mcpd/internal/config"
"github.com/mozilla-ai/mcpd/internal/context"
"github.com/mozilla-ai/mcpd/internal/perms"
)
// TestExecutionContextPermissions verifies that execution context files
// are created with secure permissions.
func TestExecutionContextPermissions(t *testing.T) {
t.Parallel()
// Create a base temp dir and then create our own secure subdirectory
// to avoid the issue where t.TempDir() creates directories with 0755 permissions
baseTempDir := t.TempDir()
secureParentDir := filepath.Join(baseTempDir, "secure-exec-context")
err := os.MkdirAll(secureParentDir, perms.SecureDir)
require.NoError(t, err)
configPath := filepath.Join(secureParentDir, "execution.toml")
// Create an execution context config and save it.
cfg := context.NewExecutionContextConfig(configPath)
// Add a test server configuration.
testServer := context.ServerExecutionContext{
Name: "test-server",
Args: []string{"--debug"},
Env: map[string]string{"TEST": "value"},
}
_, err = cfg.Upsert(testServer)
require.NoError(t, err)
// Verify the file was created with secure permissions.
info, err := os.Stat(configPath)
require.NoError(t, err)
require.False(t, info.IsDir())
require.Equal(t, perms.SecureFile, info.Mode().Perm(),
"Execution context file should be created with secure permissions (0600)")
// Verify the parent directory has secure permissions.
parentDir := filepath.Dir(configPath)
parentInfo, err := os.Stat(parentDir)
require.NoError(t, err)
require.True(t, parentInfo.IsDir())
require.Equal(t, perms.SecureDir, parentInfo.Mode().Perm(),
"Execution context directory should have secure permissions (0700)")
}
// TestConfigFilePermissions verifies that configuration files
// are created with regular permissions.
func TestConfigFilePermissions(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "config.toml")
// Create a config using the default loader.
loader := &config.DefaultLoader{}
err := loader.Init(configPath)
require.NoError(t, err)
// Verify the file was created with regular permissions.
info, err := os.Stat(configPath)
require.NoError(t, err)
require.False(t, info.IsDir())
require.Equal(t, perms.RegularFile, info.Mode().Perm(),
"Configuration file should be created with regular permissions (0644)")
}
// TestCacheDirectoryPermissions verifies that cache directories
// are created with regular permissions.
func TestCacheDirectoryPermissions(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
cacheDir := filepath.Join(tempDir, "cache")
logger := hclog.NewNullLogger()
// Create cache with caching enabled to trigger directory creation.
opts := []cache.Option{
cache.WithDirectory(cacheDir),
cache.WithCaching(true),
}
c, err := cache.NewCache(logger, opts...)
require.NoError(t, err)
require.NotNil(t, c)
// Verify the cache directory was created with regular permissions.
info, err := os.Stat(cacheDir)
require.NoError(t, err)
require.True(t, info.IsDir())
require.Equal(t, perms.RegularDir, info.Mode().Perm(),
"Cache directory should be created with regular permissions (0755)")
}
// TestDotEnvFilePermissions verifies that exported dotenv files
// have regular permissions (testing the export functionality).
func TestDotEnvFilePermissions(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
dotenvPath := filepath.Join(tempDir, "test.env")
// Create a test dotenv file using the same pattern as export.go.
testData := map[string]string{
"TEST_VAR": "test_value",
"DEBUG": "true",
}
var b strings.Builder
for k, v := range testData {
b.WriteString(k)
b.WriteString("=")
b.WriteString(v)
b.WriteString("\n")
}
// Write file using the same pattern as cmd/config/export/export.go.
err := os.WriteFile(dotenvPath, []byte(b.String()), perms.RegularFile)
require.NoError(t, err)
// Verify the file was created with regular permissions.
info, err := os.Stat(dotenvPath)
require.NoError(t, err)
require.False(t, info.IsDir())
require.Equal(t, perms.RegularFile, info.Mode().Perm(),
"Exported dotenv file should have regular permissions (0644)")
}
// TestLogFilePermissions verifies that log files
// are created with regular permissions.
func TestLogFilePermissions(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
logPath := filepath.Join(tempDir, "mcpd.log")
// Create log file using the same pattern as internal/cmd/basecmd.go.
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, perms.RegularFile)
require.NoError(t, err)
_, err = f.WriteString("test log entry\n")
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
// Verify the file was created with regular permissions.
info, err := os.Stat(logPath)
require.NoError(t, err)
require.False(t, info.IsDir())
require.Equal(t, perms.RegularFile, info.Mode().Perm(),
"Log file should be created with regular permissions (0644)")
}
// TestPermissionConsistency verifies that different types of files
// have appropriate permission classifications.
func TestPermissionConsistency(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
// Create examples of each file type.
files := map[string]struct {
path string
perm os.FileMode
description string
isSecure bool
}{
"config": {
path: filepath.Join(tempDir, "config.toml"),
perm: perms.RegularFile,
description: "configuration file",
isSecure: false,
},
"execution_context": {
path: filepath.Join(tempDir, "secrets.toml"),
perm: perms.SecureFile,
description: "execution context file",
isSecure: true,
},
"log": {
path: filepath.Join(tempDir, "app.log"),
perm: perms.RegularFile,
description: "log file",
isSecure: false,
},
"export": {
path: filepath.Join(tempDir, "export.env"),
perm: perms.RegularFile,
description: "export file",
isSecure: false,
},
}
for name, fileInfo := range files {
t.Run(name, func(t *testing.T) {
t.Parallel()
// Create file with specified permissions.
err := os.WriteFile(fileInfo.path, []byte("test content"), fileInfo.perm)
require.NoError(t, err)
// Verify permissions are as expected.
info, err := os.Stat(fileInfo.path)
require.NoError(t, err)
require.Equal(t, fileInfo.perm, info.Mode().Perm(),
"%s should have correct permissions", fileInfo.description)
// Verify security classification is correct.
if fileInfo.isSecure {
require.Equal(t, perms.SecureFile, fileInfo.perm,
"Secure %s should use SecureFile permissions", fileInfo.description)
} else {
require.Equal(t, perms.RegularFile, fileInfo.perm,
"Regular %s should use RegularFile permissions", fileInfo.description)
}
})
}
}
// TestDirectoryPermissionConsistency verifies that different types of directories
// have appropriate permission classifications.
func TestDirectoryPermissionConsistency(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
// Create examples of each directory type.
dirs := map[string]struct {
path string
perm os.FileMode
description string
isSecure bool
}{
"cache": {
path: filepath.Join(tempDir, "cache"),
perm: perms.RegularDir,
description: "cache directory",
isSecure: false,
},
"execution_context": {
path: filepath.Join(tempDir, "execution"),
perm: perms.SecureDir,
description: "execution context directory",
isSecure: true,
},
}
for name, dirInfo := range dirs {
t.Run(name, func(t *testing.T) {
t.Parallel()
// Create directory with specified permissions.
err := os.MkdirAll(dirInfo.path, dirInfo.perm)
require.NoError(t, err)
// Verify permissions are as expected.
info, err := os.Stat(dirInfo.path)
require.NoError(t, err)
require.True(t, info.IsDir())
require.Equal(t, dirInfo.perm, info.Mode().Perm(),
"%s should have correct permissions", dirInfo.description)
// Verify security classification is correct.
if dirInfo.isSecure {
require.Equal(t, perms.SecureDir, dirInfo.perm,
"Secure %s should use SecureDir permissions", dirInfo.description)
} else {
require.Equal(t, perms.RegularDir, dirInfo.perm,
"Regular %s should use RegularDir permissions", dirInfo.description)
}
})
}
}