From 88708504f3eb51edbf8f5fec265a1b05853a21b1 Mon Sep 17 00:00:00 2001 From: Soumya Ghosh Dastidar <44349253+gdsoumya@users.noreply.github.com> Date: Wed, 22 Jun 2022 00:32:26 +0530 Subject: [PATCH] fix: updated config file permission requirements for windows (#9732) * fix: reduced config file permission restriction on windows Signed-off-by: Soumya Ghosh Dastidar * fix: updated localconfig tests to check error Signed-off-by: Soumya Ghosh Dastidar --- util/localconfig/file_perm_unix.go | 16 ++++++++++++++++ util/localconfig/file_perm_windows.go | 16 ++++++++++++++++ util/localconfig/localconfig.go | 8 -------- util/localconfig/localconfig_test.go | 4 +++- 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 util/localconfig/file_perm_unix.go create mode 100644 util/localconfig/file_perm_windows.go diff --git a/util/localconfig/file_perm_unix.go b/util/localconfig/file_perm_unix.go new file mode 100644 index 0000000000000..558d2b1b38a8e --- /dev/null +++ b/util/localconfig/file_perm_unix.go @@ -0,0 +1,16 @@ +//go:build !windows + +package localconfig + +import ( + "fmt" + "os" +) + +func getFilePermission(fi os.FileInfo) error { + if fi.Mode().Perm() == 0600 || fi.Mode().Perm() == 0400 { + return nil + } + return fmt.Errorf("config file has incorrect permission flags:%s."+ + "change the file permission either to 0400 or 0600.", fi.Mode().Perm().String()) +} diff --git a/util/localconfig/file_perm_windows.go b/util/localconfig/file_perm_windows.go new file mode 100644 index 0000000000000..945461a9021ee --- /dev/null +++ b/util/localconfig/file_perm_windows.go @@ -0,0 +1,16 @@ +//go:build windows + +package localconfig + +import ( + "fmt" + "os" +) + +func getFilePermission(fi os.FileInfo) error { + if fi.Mode().Perm() == 0666 || fi.Mode().Perm() == 0444 { + return nil + } + return fmt.Errorf("config file has incorrect permission flags:%s."+ + "change the file permission either to 0444 or 0666.", fi.Mode().Perm().String()) +} diff --git a/util/localconfig/localconfig.go b/util/localconfig/localconfig.go index 7ca3da56ed6bc..1a3828e240702 100644 --- a/util/localconfig/localconfig.go +++ b/util/localconfig/localconfig.go @@ -311,11 +311,3 @@ func GetUsername(subject string) string { } return subject } - -func getFilePermission(fi os.FileInfo) error { - if fi.Mode().Perm() == 0600 || fi.Mode().Perm() == 0400 { - return nil - } - return fmt.Errorf("config file has incorrect permission flags:%s."+ - "change the file permission either to 0400 or 0600.", fi.Mode().Perm().String()) -} diff --git a/util/localconfig/localconfig_test.go b/util/localconfig/localconfig_test.go index 59dae1159a398..74a5a5d546ffe 100644 --- a/util/localconfig/localconfig_test.go +++ b/util/localconfig/localconfig_test.go @@ -1,3 +1,5 @@ +//go:build !windows + package localconfig import ( @@ -83,7 +85,7 @@ func TestFilePermission(t *testing.T) { if err := getFilePermission(fi); err != nil { assert.EqualError(t, err, c.expectedError.Error()) } else { - require.Nil(t, err) + require.Nil(t, c.expectedError) } }) }