From bf61cd6b7df5488517bd3178a236290d22694a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Tue, 6 Feb 2024 16:53:01 +0100 Subject: [PATCH] incus-agent: Add support for template ownership/mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- cmd/incus-agent/templates.go | 43 +++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/cmd/incus-agent/templates.go b/cmd/incus-agent/templates.go index dba70d2bb28..6103fb79421 100644 --- a/cmd/incus-agent/templates.go +++ b/cmd/incus-agent/templates.go @@ -3,8 +3,10 @@ package main import ( "fmt" "io" + "io/fs" "os" "path/filepath" + "strconv" "gopkg.in/yaml.v2" @@ -52,6 +54,39 @@ func templatesApply(path string) ([]string, error) { return fmt.Errorf("Failed to create template file: %w", err) } } else { + // UID and GID + fileUID := int64(0) + fileGID := int64(0) + + if tpl.UID != "" { + id, err := strconv.ParseInt(tpl.UID, 10, 64) + if err != nil { + return fmt.Errorf("Bad file UID %q for %q: %w", tpl.UID, tplPath, err) + } + + fileUID = id + } + + if tpl.GID != "" { + id, err := strconv.ParseInt(tpl.GID, 10, 64) + if err != nil { + return fmt.Errorf("Bad file GID %q for %q: %w", tpl.GID, tplPath, err) + } + + fileGID = id + } + + // Mode + fileMode := fs.FileMode(0644) + if tpl.Mode != "" { + mode, err := strconv.ParseInt(tpl.Mode, 0, 0) + if err != nil { + return fmt.Errorf("Bad mode %q for %q: %w", tpl.Mode, tplPath, err) + } + + fileMode = os.FileMode(mode) & os.ModePerm + } + // Create the directories leading to the file. err := os.MkdirAll(filepath.Dir(tplPath), 0755) if err != nil { @@ -64,8 +99,14 @@ func templatesApply(path string) ([]string, error) { return err } + // Fix ownership. + err = w.Chown(int(fileUID), int(fileGID)) + if err != nil { + return err + } + // Fix mode. - err = w.Chmod(0644) + err = w.Chmod(fileMode) if err != nil { return err }