Skip to content

Commit

Permalink
fix: folder permissions (keploy#1808)
Browse files Browse the repository at this point in the history
* fix: folder permissions

Signed-off-by: charankamarapu <[email protected]>

* fix: folder permissions

Signed-off-by: charankamarapu <[email protected]>

* fix: unset umask

Signed-off-by: charankamarapu <[email protected]>

* doc: add comment

Signed-off-by: charankamarapu <[email protected]>

* doc: add comment

Signed-off-by: charankamarapu <[email protected]>

---------

Signed-off-by: charankamarapu <[email protected]>
  • Loading branch information
charankamarapu authored May 10, 2024
1 parent f898233 commit b9673e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"syscall"

"go.keploy.io/server/v2/cli"
"go.keploy.io/server/v2/cli/provider"
Expand Down Expand Up @@ -65,6 +66,17 @@ func start(ctx context.Context) {
}
defer utils.DeleteLogs(logger)
defer utils.Recover(logger)

// The 'umask' command is commonly used in various operating systems to regulate the permissions of newly created files.
// These 'umask' values subtract from the permissions assigned by the process, effectively lowering the permissions.
// For example, if a file is created with permissions '777' and the 'umask' is '022', the resulting permissions will be '755',
// reducing certain permissions for security purposes.
// Setting 'umask' to '0' ensures that 'keploy' can precisely control the permissions of the files it creates.
// However, it's important to note that this approach may not work in scenarios involving mounted volumes,
// as the 'umask' is set by the host system, and cannot be overridden by 'keploy' or individual processes.
oldMask := syscall.Umask(0)
defer syscall.Umask(oldMask)

configDb := configdb.NewConfigDb(logger)
if dsn != "" {
utils.SentryInit(logger, dsn)
Expand Down
14 changes: 2 additions & 12 deletions pkg/platform/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func WriteFile(ctx context.Context, logger *zap.Logger, path, fileName string, d
if err != nil {
return err
}
flag := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
flag := os.O_WRONLY | os.O_TRUNC
if isAppend {
data := []byte("---\n")
if isFileEmpty {
data = []byte{}
}
docData = append(data, docData...)
flag = os.O_CREATE | os.O_WRONLY | os.O_APPEND
flag = os.O_WRONLY | os.O_APPEND
}
yamlPath := filepath.Join(path, fileName+".yaml")
file, err := os.OpenFile(yamlPath, flag, fs.ModePerm)
Expand Down Expand Up @@ -141,11 +141,6 @@ func CreateYamlFile(ctx context.Context, Logger *zap.Logger, path string, fileNa
utils.LogError(Logger, err, "failed to create a directory for the yaml file", zap.String("path directory", path), zap.String("yaml", fileName))
return false, err
}
err = os.Chmod(path, 0777)
if err != nil {
utils.LogError(Logger, err, "failed to set permissions for the directory", zap.String("path directory", path))
return false, err
}
file, err := os.OpenFile(yamlPath, os.O_CREATE, 0777) // Set file permissions to 777
if err != nil {
utils.LogError(Logger, err, "failed to create a yaml file", zap.String("path directory", path), zap.String("yaml", fileName))
Expand All @@ -156,11 +151,6 @@ func CreateYamlFile(ctx context.Context, Logger *zap.Logger, path string, fileNa
utils.LogError(Logger, err, "failed to close the yaml file", zap.String("path directory", path), zap.String("yaml", fileName))
return false, err
}
err = os.Chmod(yamlPath, 0777)
if err != nil {
utils.LogError(Logger, err, "failed to set permissions for the directory", zap.String("path directory", path))
return false, err
}
return true, nil
}
return false, err
Expand Down

0 comments on commit b9673e3

Please sign in to comment.