Skip to content

Commit a025791

Browse files
committed
Centralize file and directory permission mode usage.
1 parent 44a6515 commit a025791

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

cmd/config/export/export.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/mozilla-ai/mcpd/v2/internal/config"
1414
"github.com/mozilla-ai/mcpd/v2/internal/context"
1515
"github.com/mozilla-ai/mcpd/v2/internal/flags"
16+
"github.com/mozilla-ai/mcpd/v2/internal/perms"
1617
"github.com/mozilla-ai/mcpd/v2/internal/runtime"
1718
)
1819

@@ -141,5 +142,5 @@ func writeDotenvFile(path string, data map[string]string) error {
141142
}
142143
}
143144

144-
return os.WriteFile(path, []byte(b.String()), 0o644)
145+
return os.WriteFile(path, []byte(b.String()), perms.RegularFile)
145146
}

internal/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewCache(logger hclog.Logger, opts ...Option) (*Cache, error) {
4242

4343
// Only create cache directory if caching is enabled.
4444
if options.enabled {
45-
if err := context.EnsureDirectoryExists(options.dir); err != nil {
45+
if err := context.EnsureRegularDir(options.dir); err != nil {
4646
return nil, fmt.Errorf("failed to create cache directory: %w", err)
4747
}
4848
}

internal/cmd/basecmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/mozilla-ai/mcpd/v2/internal/cmd/output"
1212
"github.com/mozilla-ai/mcpd/v2/internal/config"
1313
"github.com/mozilla-ai/mcpd/v2/internal/flags"
14+
"github.com/mozilla-ai/mcpd/v2/internal/perms"
1415
"github.com/mozilla-ai/mcpd/v2/internal/provider/mcpm"
1516
"github.com/mozilla-ai/mcpd/v2/internal/provider/mozilla_ai"
1617
"github.com/mozilla-ai/mcpd/v2/internal/registry"
@@ -59,7 +60,7 @@ func (c *BaseCmd) Logger() (hclog.Logger, error) {
5960
// Configure logger output based on the log file path
6061
output := io.Discard // Default to discarding log output.
6162
if logPath != "" {
62-
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
63+
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, perms.RegularFile)
6364
if err != nil {
6465
return nil, fmt.Errorf("failed to open log file (%s): %w", logPath, err)
6566
} else {

internal/config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/BurntSushi/toml"
1010

1111
"github.com/mozilla-ai/mcpd/v2/internal/flags"
12+
"github.com/mozilla-ai/mcpd/v2/internal/perms"
1213
)
1314

1415
// Init creates the base skeleton configuration file for the mcpd project.
@@ -21,7 +22,7 @@ func (d *DefaultLoader) Init(path string) error {
2122

2223
content := `servers = []`
2324

24-
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
25+
if err := os.WriteFile(path, []byte(content), perms.RegularFile); err != nil {
2526
return fmt.Errorf("failed to write %s: %w", path, err)
2627
}
2728

@@ -158,7 +159,7 @@ func (c *Config) saveConfig() error {
158159
return err
159160
}
160161

161-
return os.WriteFile(c.configFilePath, data, 0o644)
162+
return os.WriteFile(c.configFilePath, data, perms.RegularFile)
162163
}
163164

164165
// validate orchestrates validation of all aspects of the configuration.

internal/context/context.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212

1313
"github.com/BurntSushi/toml"
14+
15+
"github.com/mozilla-ai/mcpd/v2/internal/perms"
1416
)
1517

1618
const (
@@ -217,12 +219,12 @@ func (c *ExecutionContextConfig) SaveConfig() error {
217219
}
218220

219221
// Ensure the directory exists before creating the file.
220-
if err := EnsureDirectoryExists(filepath.Dir(path)); err != nil {
222+
if err := EnsureSecureDir(filepath.Dir(path)); err != nil {
221223
return fmt.Errorf("could not ensure execution context directory exists: %w", err)
222224
}
223225

224226
// owner: rw-, group: ---, others: ---
225-
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
227+
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perms.SecureFile)
226228
if err != nil {
227229
return fmt.Errorf("could not create file '%s': %w", path, err)
228230
}
@@ -293,11 +295,20 @@ func AppDirName() string {
293295
return "mcpd"
294296
}
295297

296-
// EnsureDirectoryExists creates a directory with secure permissions if it doesn't exist.
297-
// The directory is created with mode 0740 (owner: rwx, group: r--, others: ---).
298-
func EnsureDirectoryExists(path string) error {
299-
if err := os.MkdirAll(path, 0o740); err != nil {
300-
return fmt.Errorf("could not ensure directory exists for '%s': %w", path, err)
298+
// EnsureSecureDir creates a directory with secure permissions if it doesn't exist.
299+
// Used for directories containing sensitive data like execution context.
300+
func EnsureSecureDir(path string) error {
301+
if err := os.MkdirAll(path, perms.SecureDir); err != nil {
302+
return fmt.Errorf("could not ensure secure directory exists for '%s': %w", path, err)
303+
}
304+
return nil
305+
}
306+
307+
// EnsureRegularDir creates a directory with standard permissions if it doesn't exist.
308+
// Used for cache directories, data directories, and documentation.
309+
func EnsureRegularDir(path string) error {
310+
if err := os.MkdirAll(path, perms.RegularDir); err != nil {
311+
return fmt.Errorf("could not ensure regular directory exists for '%s': %w", path, err)
301312
}
302313
return nil
303314
}

0 commit comments

Comments
 (0)