Skip to content

Commit

Permalink
Merge pull request #179 from kairos-io/2057-file-perms
Browse files Browse the repository at this point in the history
Add config permissions
  • Loading branch information
mauromorales authored Dec 6, 2023
2 parents 6e1d761 + 3df7742 commit 9d95056
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ func (c Config) WriteInstallState(i *v1.InstallState, statePath, recoveryPath st

data = append([]byte("# Autogenerated file by elemental client, do not edit\n\n"), data...)

err = c.Fs.WriteFile(statePath, data, constants.FilePerm)
err = c.Fs.WriteFile(statePath, data, constants.ConfigPerm)
if err != nil {
return err
}

err = c.Fs.WriteFile(recoveryPath, data, constants.FilePerm)
err = c.Fs.WriteFile(recoveryPath, data, constants.ConfigPerm)
if err != nil {
return err
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ package config_test

import (
"fmt"
"path/filepath"
"reflect"
"strings"

"github.com/kairos-io/kairos-agent/v2/pkg/constants"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
v1mocks "github.com/kairos-io/kairos-agent/v2/tests/mocks"
"github.com/twpayne/go-vfs"
"github.com/twpayne/go-vfs/vfst"
"path/filepath"
"reflect"
"strings"

. "github.com/kairos-io/kairos-agent/v2/pkg/config"
. "github.com/kairos-io/kairos-sdk/schema"
Expand Down Expand Up @@ -173,6 +174,12 @@ var _ = Describe("Schema", func() {
Expect(err).ShouldNot(HaveOccurred())
loadedInstallState, err := config.LoadInstallState()
Expect(err).ShouldNot(HaveOccurred())
stat, err := fs.Stat(statePath)
Expect(err).To(BeNil())
Expect(int(stat.Mode().Perm())).To(Equal(constants.ConfigPerm))
stat, err = fs.Stat(recoveryPath)
Expect(err).To(BeNil())
Expect(int(stat.Mode().Perm())).To(Equal(constants.ConfigPerm))

Expect(*loadedInstallState).To(Equal(*installState))
})
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const (
// Default directory and file fileModes
DirPerm = os.ModeDir | os.ModePerm
FilePerm = 0666
ConfigPerm = 0640 // Used for config files that contain secrets or other sensitive data
NoWriteDirPerm = 0555 | os.ModeDir
TempDirPerm = os.ModePerm | os.ModeSticky | os.ModeDir

Expand Down
2 changes: 1 addition & 1 deletion pkg/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (e *Elemental) CopyCloudConfig(cloudInit []string) (err error) {
if err != nil {
return err
}
if err = e.config.Fs.Chmod(customConfig, cnst.FilePerm); err != nil {
if err = e.config.Fs.Chmod(customConfig, cnst.ConfigPerm); err != nil {
e.config.Logger.Debugf("Error on chmod %s: %s\n", customConfig, err.Error())
return err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/elemental/elemental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,14 @@ var _ = Describe("Elemental", Label("elemental"), func() {

err = e.CopyCloudConfig(cloudInit)
Expect(err).To(BeNil())
copiedFile, err := fs.ReadFile(fmt.Sprintf("%s/90_custom.yaml", cnst.OEMDir))
configFilePath := fmt.Sprintf("%s/90_custom.yaml", cnst.OEMDir)
copiedFile, err := fs.ReadFile(configFilePath)
Expect(err).To(BeNil())
Expect(copiedFile).To(ContainSubstring(testString))
stat, err := fs.Stat(configFilePath)
Expect(err).To(BeNil())
Expect(int(stat.Mode().Perm())).To(Equal(cnst.ConfigPerm))

})
It("Doesnt do anything if the config file is not set", func() {
err := e.CopyCloudConfig([]string{})
Expand Down

0 comments on commit 9d95056

Please sign in to comment.