Skip to content

Commit 99f76e4

Browse files
authored
fix: additional permission check (#135)
* fix: additional permission check * update * fix tests
1 parent 84c7896 commit 99f76e4

File tree

15 files changed

+110
-45
lines changed

15 files changed

+110
-45
lines changed

src/cmd/root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,19 @@ func rootCmd() *cmdBuilder.Cmd {
103103
}
104104

105105
func guestInfoPart(tableBody *uxBlock.TableBody) {
106-
cliDataFilePath, err := constants.CliDataFilePath()
106+
cliDataFilePath, _, err := constants.CliDataFilePath()
107107
if err != nil {
108108
cliDataFilePath = err.Error()
109109
}
110110
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoCliDataFilePath), cliDataFilePath)
111111

112-
logFilePath, err := constants.LogFilePath()
112+
logFilePath, _, err := constants.LogFilePath()
113113
if err != nil {
114114
logFilePath = err.Error()
115115
}
116116
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoLogFilePath), logFilePath)
117117

118-
wgConfigFilePath, err := constants.WgConfigFilePath()
118+
wgConfigFilePath, _, err := constants.WgConfigFilePath()
119119
if err != nil {
120120
wgConfigFilePath = err.Error()
121121
}

src/cmd/statusShowDebugLogs.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
"io"
77
"os"
88

9+
"github.com/pkg/errors"
10+
911
"github.com/zeropsio/zcli/src/cmdBuilder"
1012
"github.com/zeropsio/zcli/src/constants"
13+
"github.com/zeropsio/zcli/src/file"
1114
"github.com/zeropsio/zcli/src/i18n"
1215
"github.com/zeropsio/zcli/src/uxBlock/styles"
1316
)
@@ -18,15 +21,16 @@ func statusShowDebugLogsCmd() *cmdBuilder.Cmd {
1821
Short(i18n.T(i18n.CmdStatusShowDebugLogs)).
1922
HelpFlag(i18n.T(i18n.StatusShowDebugLogsHelp)).
2023
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
21-
logFilePath, err := constants.LogFilePath()
24+
logFilePath, fileMode, err := constants.LogFilePath()
2225
if err != nil {
2326
return err
2427
}
2528

26-
f, err := os.OpenFile(logFilePath, os.O_RDONLY, 0777)
29+
f, err := file.Open(logFilePath, os.O_RDONLY, fileMode)
2730
if err != nil {
2831
return err
2932
}
33+
defer f.Close()
3034

3135
line := ""
3236
var cursor int64 = 0
@@ -43,13 +47,13 @@ func statusShowDebugLogsCmd() *cmdBuilder.Cmd {
4347
cursor -= 1
4448
_, err = f.Seek(cursor, io.SeekEnd)
4549
if err != nil {
46-
return err
50+
return errors.WithStack(err)
4751
}
4852

4953
char := make([]byte, 1)
5054
_, err = f.Read(char)
5155
if err != nil {
52-
return err
56+
return errors.WithStack(err)
5357
}
5458

5559
if cursor != -1 && (char[0] == 10 || char[0] == 13) { // stop if we find a line

src/cmd/vpnDown.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/zeropsio/zcli/src/cmdBuilder"
1111
"github.com/zeropsio/zcli/src/cmdRunner"
1212
"github.com/zeropsio/zcli/src/constants"
13+
"github.com/zeropsio/zcli/src/file"
1314
"github.com/zeropsio/zcli/src/i18n"
1415
"github.com/zeropsio/zcli/src/uxBlock"
1516
"github.com/zeropsio/zcli/src/uxBlock/styles"
@@ -31,13 +32,13 @@ func disconnectVpn(ctx context.Context, uxBlocks uxBlock.UxBlocks) error {
3132
return errors.New(i18n.T(i18n.VpnWgQuickIsNotInstalled))
3233
}
3334

34-
filePath, err := constants.WgConfigFilePath()
35+
filePath, fileMode, err := constants.WgConfigFilePath()
3536
if err != nil {
3637
return err
3738
}
3839

3940
// create empty file if not exists, only thing wg-quick needs is a proper file name
40-
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0666)
41+
f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE, fileMode)
4142
if err != nil {
4243
return err
4344
}

src/cmd/vpnUp.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/zeropsio/zcli/src/cmdRunner"
1717
"github.com/zeropsio/zcli/src/constants"
1818
"github.com/zeropsio/zcli/src/entity"
19+
"github.com/zeropsio/zcli/src/file"
1920
"github.com/zeropsio/zcli/src/i18n"
2021
"github.com/zeropsio/zcli/src/nettools"
2122
"github.com/zeropsio/zcli/src/uxBlock"
@@ -92,12 +93,12 @@ func vpnUpCmd() *cmdBuilder.Cmd {
9293
return err
9394
}
9495

95-
filePath, err := constants.WgConfigFilePath()
96+
filePath, fileMode, err := constants.WgConfigFilePath()
9697
if err != nil {
9798
return err
9899
}
99100

100-
f, err := os.Create(filePath)
101+
f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE, fileMode)
101102
if err != nil {
102103
return err
103104
}

src/cmdBuilder/executeRootCmd.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99

1010
"github.com/mattn/go-isatty"
1111
"github.com/pkg/errors"
12+
"golang.org/x/term"
13+
"gopkg.in/yaml.v3"
14+
1215
"github.com/zeropsio/zcli/src/cliStorage"
1316
"github.com/zeropsio/zcli/src/constants"
1417
"github.com/zeropsio/zcli/src/errorsx"
@@ -20,8 +23,6 @@ import (
2023
"github.com/zeropsio/zcli/src/uxBlock"
2124
"github.com/zeropsio/zcli/src/uxBlock/styles"
2225
"github.com/zeropsio/zerops-go/apiError"
23-
"golang.org/x/term"
24-
"gopkg.in/yaml.v3"
2526
)
2627

2728
func ExecuteRootCmd(rootCmd *Cmd) (err error) {
@@ -121,13 +122,14 @@ func createLoggers(isTerminal bool) (*logger.Handler, *logger.Handler) {
121122
IsTerminal: isTerminal,
122123
})
123124

124-
loggerFilePath, err := constants.LogFilePath()
125+
loggerFilePath, fileMode, err := constants.LogFilePath()
125126
if err != nil {
126127
outputLogger.Warning(styles.WarningLine(err.Error()))
127128
}
128129

129130
debugFileLogger := logger.NewDebugFileLogger(logger.DebugFileConfig{
130131
FilePath: loggerFilePath,
132+
FileMode: fileMode,
131133
})
132134

133135
return outputLogger, debugFileLogger
@@ -145,13 +147,14 @@ func regSignals(contextCancel func()) {
145147
}
146148

147149
func createCliStorage() (*cliStorage.Handler, error) {
148-
filePath, err := constants.CliDataFilePath()
150+
filePath, fileMode, err := constants.CliDataFilePath()
149151
if err != nil {
150152
return nil, err
151153
}
152154
s, err := storage.New[cliStorage.Data](
153155
storage.Config{
154156
FilePath: filePath,
157+
FileMode: fileMode,
155158
},
156159
)
157160
return &cliStorage.Handler{Handler: s}, err

src/constants/darwin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ func getDataFilePathsReceivers() []pathReceiver {
1313
receiverFromEnv(CliDataFilePathEnvVar),
1414
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, CliDataFileName),
1515
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, CliDataFileName),
16+
receiverFromOsFunc(os.UserHomeDir, "zerops."+CliDataFileName),
17+
receiverFromOsTemp("zerops." + CliDataFileName),
1618
}
1719
}
1820

@@ -22,6 +24,8 @@ func getLogFilePathReceivers() []pathReceiver {
2224
receiverFromPath(path.Join("/usr/local/var/log/", ZeropsLogFile)),
2325
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, ZeropsLogFile),
2426
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, ZeropsLogFile),
27+
receiverFromOsFunc(os.UserHomeDir, "zerops."+ZeropsLogFile),
28+
receiverFromOsTemp("zerops." + ZeropsLogFile),
2529
}
2630
}
2731

@@ -33,5 +37,7 @@ func getWgConfigFilePathReceivers() []pathReceiver {
3337
receiverFromPath(path.Join("/opt/homebrew/etc/wireguard/", WgConfigFile)),
3438
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, WgConfigFile),
3539
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, WgConfigFile),
40+
receiverFromOsFunc(os.UserHomeDir, WgConfigFile),
41+
receiverFromOsTemp("zerops." + WgConfigFile),
3642
}
3743
}

src/constants/linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ func getDataFilePathsReceivers() []pathReceiver {
1313
receiverFromEnv(CliDataFilePathEnvVar),
1414
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, CliDataFileName),
1515
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, CliDataFileName),
16+
receiverFromOsFunc(os.UserHomeDir, "zerops."+CliDataFileName),
17+
receiverFromOsTemp("zerops." + CliDataFileName),
1618
}
1719
}
1820

@@ -22,6 +24,8 @@ func getLogFilePathReceivers() []pathReceiver {
2224
receiverFromPath(path.Join("/var/log/", ZeropsLogFile)),
2325
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, ZeropsLogFile),
2426
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, ZeropsLogFile),
27+
receiverFromOsFunc(os.UserHomeDir, "zerops."+ZeropsLogFile),
28+
receiverFromOsTemp("zerops." + ZeropsLogFile),
2529
}
2630
}
2731

@@ -33,5 +37,7 @@ func getWgConfigFilePathReceivers() []pathReceiver {
3337
receiverFromPath(path.Join("/opt/homebrew/etc/wireguard/", WgConfigFile)),
3438
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, WgConfigFile),
3539
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, WgConfigFile),
40+
receiverFromOsFunc(os.UserHomeDir, WgConfigFile),
41+
receiverFromOsTemp("zerops." + WgConfigFile),
3642
}
3743
}

src/constants/windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func getDataFilePathsReceivers() []pathReceiver {
1616
receiverFromEnv(CliDataFilePathEnvVar),
1717
receiverFromOsFunc(os.UserConfigDir, "Zerops", CliDataFileName),
1818
receiverFromOsFunc(os.UserHomeDir, "Zerops", CliDataFileName),
19+
receiverFromOsFunc(os.UserHomeDir, "zerops."+CliDataFileName),
20+
receiverFromOsTemp("zerops." + CliDataFileName),
1921
}
2022
}
2123

@@ -24,6 +26,8 @@ func getLogFilePathReceivers() []pathReceiver {
2426
receiverFromEnv(CliLogFilePathEnvVar),
2527
receiverFromOsFunc(os.UserConfigDir, "Zerops", ZeropsLogFile),
2628
receiverFromOsFunc(os.UserHomeDir, "Zerops", ZeropsLogFile),
29+
receiverFromOsFunc(os.UserHomeDir, "zerops."+ZeropsLogFile),
30+
receiverFromOsTemp("zerops." + ZeropsLogFile),
2731
}
2832
}
2933

@@ -32,5 +36,7 @@ func getWgConfigFilePathReceivers() []pathReceiver {
3236
receiverFromEnv(CliWgConfigPathEnvVar),
3337
receiverFromOsFunc(os.UserConfigDir, "Zerops", WgConfigFile),
3438
receiverFromOsFunc(os.UserHomeDir, "Zerops", WgConfigFile),
39+
receiverFromOsFunc(os.UserHomeDir, WgConfigFile),
40+
receiverFromOsTemp("zerops." + WgConfigFile),
3541
}
3642
}

src/constants/zerops.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strings"
88

99
"github.com/pkg/errors"
10+
11+
"github.com/zeropsio/zcli/src/file"
1012
"github.com/zeropsio/zcli/src/i18n"
1113
)
1214

@@ -22,64 +24,69 @@ const (
2224
CliTerminalMode = "ZEROPS_CLI_TERMINAL_MODE"
2325
)
2426

25-
type pathReceiver func() (path string, err error)
27+
type pathReceiver func(fileMode os.FileMode) (path string, err error)
2628

27-
func CliDataFilePath() (string, error) {
28-
return checkReceivers(getDataFilePathsReceivers(), i18n.UnableToWriteCliData)
29+
func CliDataFilePath() (string, os.FileMode, error) {
30+
return checkReceivers(getDataFilePathsReceivers(), 0600, i18n.UnableToWriteCliData)
2931
}
3032

31-
func LogFilePath() (string, error) {
32-
return checkReceivers(getLogFilePathReceivers(), i18n.UnableToWriteLogFile)
33+
func LogFilePath() (string, os.FileMode, error) {
34+
return checkReceivers(getLogFilePathReceivers(), 0666, i18n.UnableToWriteLogFile)
3335
}
3436

35-
func WgConfigFilePath() (string, error) {
36-
return checkReceivers(getWgConfigFilePathReceivers(), i18n.UnableToWriteLogFile)
37+
func WgConfigFilePath() (string, os.FileMode, error) {
38+
return checkReceivers(getWgConfigFilePathReceivers(), 0600, i18n.UnableToWriteLogFile)
3739
}
3840

39-
func checkReceivers(pathReceivers []pathReceiver, errorText string) (string, error) {
40-
path := findFirstWritablePath(pathReceivers)
41+
func checkReceivers(pathReceivers []pathReceiver, fileMode os.FileMode, errorText string) (string, os.FileMode, error) {
42+
path := findFirstWritablePath(pathReceivers, fileMode)
4143
if path == "" {
4244
paths := make([]string, 0, len(pathReceivers))
4345
for _, p := range pathReceivers {
44-
_, err := p()
46+
_, err := p(fileMode)
4547
paths = append(paths, err.Error())
4648
}
47-
return "", errors.New(i18n.T(errorText, "\n"+strings.Join(paths, "\n")+"\n"))
49+
return "", 0, errors.New(i18n.T(errorText, "\n"+strings.Join(paths, "\n")+"\n"))
4850
}
49-
return path, nil
51+
return path, fileMode, nil
5052
}
5153

5254
func receiverFromPath(path string) pathReceiver {
53-
return func() (string, error) {
54-
return checkPath(path)
55+
return func(fileMode os.FileMode) (string, error) {
56+
return checkPath(path, fileMode)
5557
}
5658
}
5759

5860
func receiverFromEnv(envName string) pathReceiver {
59-
return func() (string, error) {
61+
return func(fileMode os.FileMode) (string, error) {
6062
env := os.Getenv(envName)
6163
if env == "" {
6264
return "", errors.Errorf("env %s is empty", envName)
6365
}
64-
return checkPath(env)
66+
return checkPath(env, fileMode)
6567
}
6668
}
6769

6870
func receiverFromOsFunc(osFunc func() (string, error), elem ...string) pathReceiver {
69-
return func() (string, error) {
71+
return func(fileMode os.FileMode) (string, error) {
7072
dir, err := osFunc()
7173
if err != nil {
7274
return "", err
7375
}
74-
elem = append([]string{dir}, elem...)
7576

76-
return filepath.Join(elem...), nil
77+
return checkPath(filepath.Join(append([]string{dir}, elem...)...), fileMode)
78+
}
79+
}
80+
81+
func receiverFromOsTemp(elem ...string) pathReceiver {
82+
return func(fileMode os.FileMode) (string, error) {
83+
return checkPath(filepath.Join(append([]string{os.TempDir()}, elem...)...), fileMode)
7784
}
7885
}
7986

80-
func findFirstWritablePath(paths []pathReceiver) string {
87+
func findFirstWritablePath(paths []pathReceiver, fileMode os.FileMode) string {
8188
for _, p := range paths {
82-
path, err := p()
89+
path, err := p(fileMode)
8390
if err == nil {
8491
return path
8592
}
@@ -88,14 +95,14 @@ func findFirstWritablePath(paths []pathReceiver) string {
8895
return ""
8996
}
9097

91-
func checkPath(filePath string) (string, error) {
98+
func checkPath(filePath string, fileMode os.FileMode) (string, error) {
9299
dir := path.Dir(filePath)
93100

94-
if err := os.MkdirAll(dir, 0775); err != nil {
101+
if err := os.MkdirAll(dir, 0755); err != nil {
95102
return "", err
96103
}
97104

98-
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
105+
f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE, fileMode)
99106
if err != nil {
100107
return "", err
101108
}

src/file/file.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package file
2+
3+
import (
4+
"os"
5+
6+
"github.com/pkg/errors"
7+
)
8+
9+
func Open(filePath string, flag int, fileMode os.FileMode) (*os.File, error) {
10+
f, err := os.OpenFile(filePath, flag, fileMode)
11+
if err != nil {
12+
return nil, errors.WithStack(err)
13+
}
14+
err = os.Chmod(filePath, fileMode)
15+
if err != nil {
16+
return nil, errors.WithStack(err)
17+
}
18+
19+
return f, nil
20+
}

0 commit comments

Comments
 (0)