Skip to content

Commit

Permalink
backward compatibility for chown
Browse files Browse the repository at this point in the history
This PR simply takes into account `uid` and `gid` settings in the template, introduced in 0.28.0 but replaced by `user` and `group` in 0.29.0, breaking compatibility.

We address the issue by giving precedence to the newer settings but setting the relevant properties upon type conversion if the legacy settings are provided

Uid (0.28.0) -> User (0.29.0)
Gid (0.28.0) -> Group (0.29.0)

Signed-off-by: Alessandro De Blasis <[email protected]>
  • Loading branch information
deblasis authored and eikenb committed Mar 16, 2022
1 parent 0220c17 commit 007153f
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
93 changes: 93 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"reflect"
"strconv"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -1121,6 +1122,34 @@ func TestParse(t *testing.T) {
},
false,
},
{
"template_uid_backward_compat",
`template {
uid = 1000
}`,
&Config{
Templates: &TemplateConfigs{
&TemplateConfig{
Uid: Int(1000),
},
},
},
false,
},
{
"template_gid_backward_compat",
`template {
gid = 1000
}`,
&Config{
Templates: &TemplateConfigs{
&TemplateConfig{
Gid: Int(1000),
},
},
},
false,
},
{
"template_uid_gid_default",
`template {
Expand All @@ -1130,6 +1159,8 @@ func TestParse(t *testing.T) {
&TemplateConfig{
User: nil,
Group: nil,
Uid: nil,
Gid: nil,
},
},
},
Expand Down Expand Up @@ -1764,6 +1795,68 @@ func TestFinalize(t *testing.T) {
},
},
},
{
"uid_backward_compat",
func(act, exp *Config) (bool, error) {
for i, tA := range *act.Templates {
for j, tE := range *exp.Templates {
if i != j {
continue
}
var userInt, _ = strconv.Atoi(*tE.User)
if userInt != *tA.Uid {
return false, fmt.Errorf("\nexp: %#v\nact: %#v", *tE.User, *tA.User)
}
}
}
return true, nil
},
&Config{
Templates: &TemplateConfigs{
&TemplateConfig{
Uid: Int(1000),
},
},
},
&Config{
Templates: &TemplateConfigs{
&TemplateConfig{
User: String("1000"),
},
},
},
},
{
"gid_backward_compat",
func(act, exp *Config) (bool, error) {
for i, tA := range *act.Templates {
for j, tE := range *exp.Templates {
if i != j {
continue
}
var groupInt, _ = strconv.Atoi(*tE.Group)
if groupInt != *tA.Gid {
return false, fmt.Errorf("\nexp: %#v\nact: %#v", *tE.Group, *tA.Group)
}
}
}
return true, nil
},
&Config{
Templates: &TemplateConfigs{
&TemplateConfig{
Gid: Int(1000),
},
},
},
&Config{
Templates: &TemplateConfigs{
&TemplateConfig{
Group: String("1000"),
},
},
},
},
}

for i, tc := range cases {
Expand Down
19 changes: 19 additions & 0 deletions config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -75,13 +76,19 @@ type TemplateConfig struct {
// with a warning
User *string `mapstructure:"user"`

// Uid is equivalent to User when it's a valid int and it's defined for backward compatibility with v0.28.0
Uid *int `mapstructure:"uid"`

// Group is the group name or gid that will be set when creating the file on disk.
// Useful when simply setting Perms is not enough.
//
// Platform dependent: this doesn't work on Windows but it fails gracefully
// with a warning
Group *string `mapstructure:"group"`

// Gid is equivalent to Group when it's a valid int and it's defined for backward compatibility with v0.28.0
Gid *int `mapstructure:"gid"`

// Source is the path on disk to the template contents to evaluate. Either
// this or Contents should be specified, but not both.
Source *string `mapstructure:"source"`
Expand Down Expand Up @@ -295,6 +302,18 @@ func (c *TemplateConfig) Finalize() {
c.ErrFatal = Bool(true)
}

// Backwards compatibility for uid
if c.User == nil && c.Uid != nil {
var uStr = strconv.Itoa(*c.Uid)
c.User = &uStr
}

// Backwards compatibility for gid
if c.Group == nil && c.Gid != nil {
var gStr = strconv.Itoa(*c.Gid)
c.Group = &gStr
}

if c.Exec == nil {
c.Exec = DefaultExecConfig()
}
Expand Down

0 comments on commit 007153f

Please sign in to comment.