Skip to content

Commit

Permalink
Merge pull request k0sproject#337 from jnummelin/fix/remote-machine-f…
Browse files Browse the repository at this point in the history
…ile-perms

Fix file permissions when provisioning RemoteMachines
  • Loading branch information
jnummelin authored Nov 13, 2023
2 parents e115e84 + 85e341e commit a6d05fd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions internal/cloudinit/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (c *CloudInit) AsBytes() ([]byte, error) {
return b.Bytes(), nil
}

func (f File) PermissionsAsInt() (int, error) {
return strconv.Atoi(f.Permissions)
func (f File) PermissionsAsInt() (int64, error) {

return strconv.ParseInt(f.Permissions, 8, 32)
}
12 changes: 12 additions & 0 deletions internal/cloudinit/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ runcmd:
- echo 'hello world'
`, s)
}

func TestPermissions(t *testing.T) {
f := File{
Path: "/etc/hosts",
Content: "foobar",
Permissions: "0644",
}

perm, err := f.PermissionsAsInt()
assert.NoError(t, err)
assert.Equal(t, int64(420), perm)
}
3 changes: 2 additions & 1 deletion internal/controller/infrastructure/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (p *Provisioner) uploadFile(conn *rig.Connection, file cloudinit.File) erro
return fmt.Errorf("failed to create directory: %w", err)
}

destFile, err := fsys.OpenFile(file.Path, rigfs.ModeCreate, rigfs.ModeReadWrite)
destFile, err := fsys.OpenFile(file.Path, rigfs.ModeCreate, rigfs.FileMode(perms))
if err != nil {
return fmt.Errorf("failed to open remote file for writing: %w", err)
}
Expand All @@ -175,5 +175,6 @@ func (p *Provisioner) uploadFile(conn *rig.Connection, file cloudinit.File) erro
return fmt.Errorf("failed to write to remote file: %w", err)
}

p.log.Info("uploaded file", "path", file.Path, "permissions", perms)
return nil
}

0 comments on commit a6d05fd

Please sign in to comment.