config: fix parsing of file permissions
The file permissions can be specified (unfortunately) as a string or an octal integer. During the normalization step, every field is unmarshalled into an interface{}. String types are kept in tact but integers are converted to decimal integers. If the raw config represented the permissions as an octal, it would be converted to decimal _before_ it was saved to RawFilePermissions. Permissions() would then try to convert it again, assuming it was an octal. The new behavior doesn't assume the radix of the number, allowing decimal and octal input.
This commit is contained in:
parent
97758b343b
commit
5c80ccacc4
@ -276,6 +276,40 @@ Address=10.209.171.177/19
|
||||
if cfg.Coreos.Update.RebootStrategy != "reboot" {
|
||||
t.Errorf("Failed to parse locksmith strategy")
|
||||
}
|
||||
|
||||
contents = `
|
||||
coreos:
|
||||
write_files:
|
||||
- path: /home/me/notes
|
||||
permissions: 0744
|
||||
`
|
||||
cfg, err = NewCloudConfig(contents)
|
||||
if err != nil {
|
||||
t.Fatalf("Encountered unexpected error :%v", err)
|
||||
}
|
||||
|
||||
if len(cfg.WriteFiles) != 1 {
|
||||
t.Error("Failed to parse correct number of write_files")
|
||||
} else {
|
||||
wf := cfg.WriteFiles[0]
|
||||
if wf.Content != "" {
|
||||
t.Errorf("WriteFile has incorrect contents '%s'", wf.Content)
|
||||
}
|
||||
if wf.Encoding != "" {
|
||||
t.Errorf("WriteFile has incorrect encoding %s", wf.Encoding)
|
||||
}
|
||||
// Verify that the normalization of the config converted 0744 to its decimal
|
||||
// representation, 484.
|
||||
if wf.RawFilePermissions != "484" {
|
||||
t.Errorf("WriteFile has incorrect permissions %s", wf.RawFilePermissions)
|
||||
}
|
||||
if wf.Path != "/home/me/notes" {
|
||||
t.Errorf("WriteFile has incorrect path %s", wf.Path)
|
||||
}
|
||||
if wf.Owner != "" {
|
||||
t.Errorf("WriteFile has incorrect owner %s", wf.Owner)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Assert that our interface conversion doesn't panic
|
||||
|
@ -17,7 +17,6 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@ -39,10 +38,10 @@ func (f *File) Permissions() (os.FileMode, error) {
|
||||
return os.FileMode(0644), nil
|
||||
}
|
||||
|
||||
// Parse string representation of file mode as octal
|
||||
perm, err := strconv.ParseInt(f.RawFilePermissions, 8, 32)
|
||||
// Parse string representation of file mode as integer
|
||||
perm, err := strconv.ParseInt(f.RawFilePermissions, 0, 32)
|
||||
if err != nil {
|
||||
return 0, errors.New("Unable to parse file permissions as octal integer")
|
||||
return 0, fmt.Errorf("Unable to parse file permissions %q as integer", f.RawFilePermissions)
|
||||
}
|
||||
return os.FileMode(perm), nil
|
||||
}
|
||||
|
@ -85,6 +85,38 @@ func TestWriteFileInvalidPermission(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalFilePermissions(t *testing.T) {
|
||||
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to create tempdir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
fn := "foo"
|
||||
fullPath := path.Join(dir, fn)
|
||||
|
||||
wf := File{config.File{
|
||||
Path: fn,
|
||||
RawFilePermissions: "484", // Decimal representation of 0744
|
||||
}}
|
||||
|
||||
path, err := WriteFile(&wf, dir)
|
||||
if err != nil {
|
||||
t.Fatalf("Processing of WriteFile failed: %v", err)
|
||||
} else if path != fullPath {
|
||||
t.Fatalf("WriteFile returned bad path: want %s, got %s", fullPath, path)
|
||||
}
|
||||
|
||||
fi, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to stat file: %v", err)
|
||||
}
|
||||
|
||||
if fi.Mode() != os.FileMode(0744) {
|
||||
t.Errorf("File has incorrect mode: %v", fi.Mode())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFilePermissions(t *testing.T) {
|
||||
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user