Skip to content

Commit 199b1d1

Browse files
committed
Refactor create config file
chmod doesn't work for windows operating system. Hence, in windows don't create default config file and check for permission. This will let users responsible for creating their own config file and set appropriate permission. Signed-off-by: Vijayan Balasubramanian <[email protected]>
1 parent d966330 commit 199b1d1

File tree

5 files changed

+98
-36
lines changed

5 files changed

+98
-36
lines changed

commands/file_operation_unix.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// +build !windows
2+
3+
/*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* The OpenSearch Contributors require contributions made to
7+
* this file be licensed under the Apache-2.0 license or a
8+
* compatible open source license.
9+
*
10+
* Modifications Copyright OpenSearch Contributors. See
11+
* GitHub history for details.
12+
*/
13+
14+
package commands
15+
16+
import (
17+
"fmt"
18+
"os"
19+
"path/filepath"
20+
)
21+
22+
const (
23+
FolderPermission = 0700 // only owner can read, write and execute
24+
FilePermission = 0600 // only owner can read and write
25+
)
26+
27+
// createDefaultConfigFolderIfNotExists creates default config file along with folder if
28+
// it doesn't exists
29+
func createDefaultConfigFileIfNotExists() error {
30+
defaultFilePath := GetDefaultConfigFilePath()
31+
if isExists(defaultFilePath) {
32+
return nil
33+
}
34+
folderPath := filepath.Dir(defaultFilePath)
35+
if !isExists(folderPath) {
36+
err := os.Mkdir(folderPath, FolderPermission)
37+
if err != nil {
38+
return err
39+
}
40+
}
41+
f, err := os.Create(defaultFilePath)
42+
if err != nil {
43+
return err
44+
}
45+
if err = f.Chmod(FilePermission); err != nil {
46+
return err
47+
}
48+
return f.Close()
49+
}
50+
51+
func checkConfigFilePermission(configFilePath string) error {
52+
//check for config file permission
53+
info, err := os.Stat(configFilePath)
54+
if err != nil {
55+
return fmt.Errorf("failed to get config file info due to: %w", err)
56+
}
57+
mode := info.Mode().Perm()
58+
59+
if mode != FilePermission {
60+
return fmt.Errorf("permissions %o for '%s' are too open. It is required that your config file is NOT accessible by others", mode, configFilePath)
61+
}
62+
return nil
63+
}

commands/file_operation_windows.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// +build windows
2+
3+
/*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* The OpenSearch Contributors require contributions made to
7+
* this file be licensed under the Apache-2.0 license or a
8+
* compatible open source license.
9+
*
10+
* Modifications Copyright OpenSearch Contributors. See
11+
* GitHub history for details.
12+
*/
13+
14+
package commands
15+
16+
import "fmt"
17+
18+
func createDefaultConfigFileIfNotExists() error {
19+
return fmt.Errorf("creating default config file is not supported for windows. Please create manually")
20+
}
21+
22+
func checkConfigFilePermission(configFilePath string) error {
23+
// since windows doesn't support create default config file, no validation is required
24+
return nil
25+
}

commands/profile.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,8 @@ func getProfileController(cfgFlagValue string) (profile.Controller, error) {
197197
if err != nil {
198198
return nil, fmt.Errorf("failed to get config file due to: %w", err)
199199
}
200-
//check for config file permission
201-
info, err := os.Stat(configFilePath)
202-
if err != nil {
203-
return nil, fmt.Errorf("failed to get config file info due to: %w", err)
204-
}
205-
mode := info.Mode().Perm()
206-
207-
if mode != FilePermission {
208-
return nil, fmt.Errorf("permissions %o for '%s' are too open. It is required that your config file is NOT accessible by others", mode, configFilePath)
200+
if err = checkConfigFilePermission(configFilePath); err != nil {
201+
return nil, err
209202
}
210203
configController := config.New(configFilePath)
211204
profileController := profile.New(configController)

commands/root.go

-26
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ const (
2626
defaultConfigFileName = "config"
2727
flagConfig = "config"
2828
flagProfileName = "profile"
29-
FolderPermission = 0700 // only owner can read, write and execute
30-
FilePermission = 0600 // only owner can read and write
3129
ConfigEnvVarName = "OPENSEARCH_CLI_CONFIG"
3230
RootCommandName = "opensearch-cli"
3331
version = "1.1.0"
@@ -96,30 +94,6 @@ func GetConfigFilePath(configFlagValue string) (string, error) {
9694
return GetDefaultConfigFilePath(), nil
9795
}
9896

99-
// createDefaultConfigFolderIfNotExists creates default config file along with folder if
100-
// it doesn't exists
101-
func createDefaultConfigFileIfNotExists() error {
102-
defaultFilePath := GetDefaultConfigFilePath()
103-
if isExists(defaultFilePath) {
104-
return nil
105-
}
106-
folderPath := filepath.Dir(defaultFilePath)
107-
if !isExists(folderPath) {
108-
err := os.Mkdir(folderPath, FolderPermission)
109-
if err != nil {
110-
return err
111-
}
112-
}
113-
f, err := os.Create(defaultFilePath)
114-
if err != nil {
115-
return err
116-
}
117-
if err = f.Chmod(FilePermission); err != nil {
118-
return err
119-
}
120-
return f.Close()
121-
}
122-
12397
//isExists check if given path exists or not
12498
//if path is just a name, it will check in current directory
12599
func isExists(path string) bool {

commands/root_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ func createTempConfigFile(testFilePath string) (*os.File, error) {
7777
os.Remove(tmpfile.Name()) // clean up
7878
return nil, err
7979
}
80-
if err := tmpfile.Chmod(FilePermission); err != nil {
80+
if runtime.GOOS == "windows" {
81+
return tmpfile, nil
82+
}
83+
if err := tmpfile.Chmod(0600); err != nil {
8184
os.Remove(tmpfile.Name()) // clean up
8285
return nil, err
8386
}
@@ -139,6 +142,10 @@ func TestGetProfile(t *testing.T) {
139142
assert.EqualError(t, err, "failed to get config file info due to: stat testdata/config1.yaml: no such file or directory", "unexpected error")
140143
})
141144
t.Run("invalid config file permission", func(t *testing.T) {
145+
146+
if runtime.GOOS == "windows" {
147+
t.Skipf("test case does not work on %s", runtime.GOOS)
148+
}
142149
root := GetRoot()
143150
assert.NotNil(t, root)
144151
profileFile, err := createTempConfigFile("testdata/config.yaml")

0 commit comments

Comments
 (0)