From 42a8b6567548be6ca3e969f3b3f48219047907f2 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sat, 25 May 2024 19:31:41 -0700 Subject: [PATCH] add missing nil checks on group and user lookups --- ingredients/group/groupPresent.go | 2 +- ingredients/user/userPresent.go | 5 ++--- testing/recipes/userCreation.grlx | 11 +++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 testing/recipes/userCreation.grlx diff --git a/ingredients/group/groupPresent.go b/ingredients/group/groupPresent.go index 4562865..b76b6ec 100644 --- a/ingredients/group/groupPresent.go +++ b/ingredients/group/groupPresent.go @@ -47,7 +47,7 @@ func (g Group) present(ctx context.Context, test bool) (types.Result, error) { result.Changed = true return result, nil } - if groupByName.Gid != gid { + if groupByName == nil || groupByName.Gid != gid { cmd := exec.CommandContext(ctx, "groupmod", args...) if test { result.Succeeded = true diff --git a/ingredients/user/userPresent.go b/ingredients/user/userPresent.go index aa1d53a..ffeb83d 100644 --- a/ingredients/user/userPresent.go +++ b/ingredients/user/userPresent.go @@ -47,10 +47,10 @@ func (u User) present(ctx context.Context, test bool) (types.Result, error) { userCmd = "useradd" } args := []string{userName} - if uid != "" && uid != user.Uid { + if uid != "" && user == nil || uid != user.Uid { args = append(args, "-u"+uid) } - if gid != "" && gid != user.Gid { + if gid != "" && user == nil || gid != user.Gid { args = append(args, "-g"+gid) } if shell != "" { @@ -72,7 +72,6 @@ func (u User) present(ctx context.Context, test bool) (types.Result, error) { return result, nil } err = cmd.Run() - if err != nil { result.Failed = true result.Succeeded = false diff --git a/testing/recipes/userCreation.grlx b/testing/recipes/userCreation.grlx new file mode 100644 index 0000000..c75bbc8 --- /dev/null +++ b/testing/recipes/userCreation.grlx @@ -0,0 +1,11 @@ +steps: + create void user: + user.present: + - name: void + - uid: "1000" + - gid: "100" + - home: /home/void + - shell: /usr/bin/zsh + - groups: + - wheel + - docker