From 14f608de0a99cdefafbc2fae172224f0a4b09a78 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Fri, 23 Sep 2022 15:08:21 +0200 Subject: [PATCH] osbuild: use internal users package structs in stages Don't pass blueprint Users and Groups options all the way down to the osbuild stage bindings. Instead, convert them to the internal users.User and users.Group structs. Ideally we would do this even higher up in the code path, before reaching the distro, but this is the first step towards that. --- internal/distro/fedora/images.go | 4 ++-- internal/distro/rhel7/pipelines.go | 5 +++-- internal/distro/rhel8/pipelines.go | 10 +++++----- internal/distro/rhel9/pipelines.go | 10 +++++----- internal/image/ostree_installer.go | 6 +++--- internal/manifest/iso_tree.go | 6 +++--- internal/osbuild/groups_stage.go | 3 +-- internal/osbuild/kickstart_stage.go | 8 +++----- internal/osbuild/users_stage.go | 3 +-- internal/osbuild/users_stage_test.go | 13 ++++++------- 10 files changed, 32 insertions(+), 36 deletions(-) diff --git a/internal/distro/fedora/images.go b/internal/distro/fedora/images.go index bf9d59aa3..13b555168 100644 --- a/internal/distro/fedora/images.go +++ b/internal/distro/fedora/images.go @@ -257,8 +257,8 @@ func iotInstallerImage(workload workload.Workload, img.Platform = t.platform img.ExtraBasePackages = packageSets[installerPkgsKey] - img.Users = customizations.GetUsers() - img.Groups = customizations.GetGroups() + img.Users = users.UsersFromBP(customizations.GetUsers()) + img.Groups = users.GroupsFromBP(customizations.GetGroups()) img.ISOLabelTempl = d.isolabelTmpl img.Product = d.product diff --git a/internal/distro/rhel7/pipelines.go b/internal/distro/rhel7/pipelines.go index 07d2aceb2..b283c8a1a 100644 --- a/internal/distro/rhel7/pipelines.go +++ b/internal/distro/rhel7/pipelines.go @@ -8,6 +8,7 @@ import ( "github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/osbuild" "github.com/osbuild/osbuild-composer/internal/rpmmd" + "github.com/osbuild/osbuild-composer/internal/users" ) func buildPipeline(repos []rpmmd.RepoConfig, buildPackageSpecs []rpmmd.PackageSpec, runner string) *osbuild.Pipeline { @@ -87,10 +88,10 @@ func osPipeline(t *imageType, } if groups := c.GetGroups(); len(groups) > 0 { - p.AddStage(osbuild.NewGroupsStage(osbuild.NewGroupsStageOptions(groups))) + p.AddStage(osbuild.NewGroupsStage(osbuild.NewGroupsStageOptions(users.GroupsFromBP(groups)))) } - if userOptions, err := osbuild.NewUsersStageOptions(c.GetUsers(), false); err != nil { + if userOptions, err := osbuild.NewUsersStageOptions(users.UsersFromBP(c.GetUsers()), false); err != nil { return nil, err } else if userOptions != nil { p.AddStage(osbuild.NewUsersStage(userOptions)) diff --git a/internal/distro/rhel8/pipelines.go b/internal/distro/rhel8/pipelines.go index 7c7fe0664..06b2dc5f2 100644 --- a/internal/distro/rhel8/pipelines.go +++ b/internal/distro/rhel8/pipelines.go @@ -216,7 +216,7 @@ func edgeInstallerPipelines(t *imageType, customizations *blueprint.Customizatio kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(installerPackages, "kernel") ostreeRepoPath := "/ostree/repo" payloadStages := ostreePayloadStages(options, ostreeRepoPath) - kickstartOptions, err := osbuild.NewKickstartStageOptions(kspath, "", customizations.GetUsers(), customizations.GetGroups(), makeISORootPath(ostreeRepoPath), options.OSTree.Ref, "rhel") + kickstartOptions, err := osbuild.NewKickstartStageOptions(kspath, "", users.UsersFromBP(customizations.GetUsers()), users.GroupsFromBP(customizations.GetGroups()), makeISORootPath(ostreeRepoPath), options.OSTree.Ref, "rhel") if err != nil { return nil, err } @@ -255,7 +255,7 @@ func imageInstallerPipelines(t *imageType, customizations *blueprint.Customizati tarPath := "/liveimg.tar" tarPayloadStages := []*osbuild.Stage{osbuild.NewTarStage(&osbuild.TarStageOptions{Filename: tarPath}, treePipeline.Name)} - kickstartOptions, err := osbuild.NewKickstartStageOptions(kspath, makeISORootPath(tarPath), customizations.GetUsers(), customizations.GetGroups(), "", "", "rhel") + kickstartOptions, err := osbuild.NewKickstartStageOptions(kspath, makeISORootPath(tarPath), users.UsersFromBP(customizations.GetUsers()), users.GroupsFromBP(customizations.GetGroups()), "", "", "rhel") if err != nil { return nil, err } @@ -449,17 +449,17 @@ func osPipeline(t *imageType, // don't put users and groups in the payload of an installer // add them via kickstart instead if groups := c.GetGroups(); len(groups) > 0 { - p.AddStage(osbuild.NewGroupsStage(osbuild.NewGroupsStageOptions(groups))) + p.AddStage(osbuild.NewGroupsStage(osbuild.NewGroupsStageOptions(users.GroupsFromBP(groups)))) } - if userOptions, err := osbuild.NewUsersStageOptions(c.GetUsers(), false); err != nil { + if userOptions, err := osbuild.NewUsersStageOptions(users.UsersFromBP(c.GetUsers()), false); err != nil { return nil, err } else if userOptions != nil { if t.rpmOstree { // for ostree, writing the key during user creation is // redundant and can cause issues so create users without keys // and write them on first boot - userOptionsSansKeys, err := osbuild.NewUsersStageOptions(c.GetUsers(), true) + userOptionsSansKeys, err := osbuild.NewUsersStageOptions(users.UsersFromBP(c.GetUsers()), true) if err != nil { return nil, err } diff --git a/internal/distro/rhel9/pipelines.go b/internal/distro/rhel9/pipelines.go index c3097e987..65b7e2ec7 100644 --- a/internal/distro/rhel9/pipelines.go +++ b/internal/distro/rhel9/pipelines.go @@ -211,7 +211,7 @@ func edgeInstallerPipelines(t *imageType, customizations *blueprint.Customizatio kernelVer := rpmmd.GetVerStrFromPackageSpecListPanic(installerPackages, "kernel") ostreeRepoPath := "/ostree/repo" payloadStages := ostreePayloadStages(options, ostreeRepoPath) - kickstartOptions, err := osbuild.NewKickstartStageOptions(kspath, "", customizations.GetUsers(), customizations.GetGroups(), makeISORootPath(ostreeRepoPath), options.OSTree.Ref, "rhel") + kickstartOptions, err := osbuild.NewKickstartStageOptions(kspath, "", users.UsersFromBP(customizations.GetUsers()), users.GroupsFromBP(customizations.GetGroups()), makeISORootPath(ostreeRepoPath), options.OSTree.Ref, "rhel") if err != nil { return nil, err } @@ -250,7 +250,7 @@ func imageInstallerPipelines(t *imageType, customizations *blueprint.Customizati tarPath := "/liveimg.tar" tarPayloadStages := []*osbuild.Stage{osbuild.NewTarStage(&osbuild.TarStageOptions{Filename: tarPath}, treePipeline.Name)} - kickstartOptions, err := osbuild.NewKickstartStageOptions(kspath, makeISORootPath(tarPath), customizations.GetUsers(), customizations.GetGroups(), "", "", "rhel") + kickstartOptions, err := osbuild.NewKickstartStageOptions(kspath, makeISORootPath(tarPath), users.UsersFromBP(customizations.GetUsers()), users.GroupsFromBP(customizations.GetGroups()), "", "", "rhel") if err != nil { return nil, err } @@ -444,17 +444,17 @@ func osPipeline(t *imageType, // don't put users and groups in the payload of an installer // add them via kickstart instead if groups := c.GetGroups(); len(groups) > 0 { - p.AddStage(osbuild.NewGroupsStage(osbuild.NewGroupsStageOptions(groups))) + p.AddStage(osbuild.NewGroupsStage(osbuild.NewGroupsStageOptions(users.GroupsFromBP(groups)))) } - if userOptions, err := osbuild.NewUsersStageOptions(c.GetUsers(), false); err != nil { + if userOptions, err := osbuild.NewUsersStageOptions(users.UsersFromBP(c.GetUsers()), false); err != nil { return nil, err } else if userOptions != nil { if t.rpmOstree { // for ostree, writing the key during user creation is // redundant and can cause issues so create users without keys // and write them on first boot - userOptionsSansKeys, err := osbuild.NewUsersStageOptions(c.GetUsers(), true) + userOptionsSansKeys, err := osbuild.NewUsersStageOptions(users.UsersFromBP(c.GetUsers()), true) if err != nil { return nil, err } diff --git a/internal/image/ostree_installer.go b/internal/image/ostree_installer.go index 0ba5764a2..823360cc3 100644 --- a/internal/image/ostree_installer.go +++ b/internal/image/ostree_installer.go @@ -4,19 +4,19 @@ import ( "math/rand" "github.com/osbuild/osbuild-composer/internal/artifact" - "github.com/osbuild/osbuild-composer/internal/blueprint" "github.com/osbuild/osbuild-composer/internal/manifest" "github.com/osbuild/osbuild-composer/internal/platform" "github.com/osbuild/osbuild-composer/internal/rpmmd" "github.com/osbuild/osbuild-composer/internal/runner" + "github.com/osbuild/osbuild-composer/internal/users" ) type OSTreeInstaller struct { Base Platform platform.Platform ExtraBasePackages rpmmd.PackageSet - Users []blueprint.UserCustomization - Groups []blueprint.GroupCustomization + Users []users.User + Groups []users.Group ISOLabelTempl string Product string diff --git a/internal/manifest/iso_tree.go b/internal/manifest/iso_tree.go index e7072587d..0624fad7f 100644 --- a/internal/manifest/iso_tree.go +++ b/internal/manifest/iso_tree.go @@ -4,9 +4,9 @@ import ( "fmt" "path" - "github.com/osbuild/osbuild-composer/internal/blueprint" "github.com/osbuild/osbuild-composer/internal/disk" "github.com/osbuild/osbuild-composer/internal/osbuild" + "github.com/osbuild/osbuild-composer/internal/users" ) // An ISOTree represents a tree containing the anaconda installer, @@ -18,8 +18,8 @@ type ISOTree struct { // TODO: review optional and mandatory fields and their meaning OSName string Release string - Users []blueprint.UserCustomization - Groups []blueprint.GroupCustomization + Users []users.User + Groups []users.Group PartitionTable *disk.PartitionTable diff --git a/internal/osbuild/groups_stage.go b/internal/osbuild/groups_stage.go index 46f5fff5d..4b12ae7e3 100644 --- a/internal/osbuild/groups_stage.go +++ b/internal/osbuild/groups_stage.go @@ -1,7 +1,6 @@ package osbuild import ( - "github.com/osbuild/osbuild-composer/internal/blueprint" "github.com/osbuild/osbuild-composer/internal/users" ) @@ -22,7 +21,7 @@ func NewGroupsStage(options *GroupsStageOptions) *Stage { } } -func NewGroupsStageOptions(groups []blueprint.GroupCustomization) *GroupsStageOptions { +func NewGroupsStageOptions(groups []users.Group) *GroupsStageOptions { options := GroupsStageOptions{ Groups: map[string]GroupsStageOptionsGroup{}, } diff --git a/internal/osbuild/kickstart_stage.go b/internal/osbuild/kickstart_stage.go index ab197ed92..e09bf77fa 100644 --- a/internal/osbuild/kickstart_stage.go +++ b/internal/osbuild/kickstart_stage.go @@ -1,8 +1,6 @@ package osbuild -import ( - "github.com/osbuild/osbuild-composer/internal/blueprint" -) +import "github.com/osbuild/osbuild-composer/internal/users" type KickstartStageOptions struct { // Where to place the kickstart file @@ -41,8 +39,8 @@ func NewKickstartStage(options *KickstartStageOptions) *Stage { func NewKickstartStageOptions( path string, imageURL string, - userCustomizations []blueprint.UserCustomization, - groupCustomizations []blueprint.GroupCustomization, + userCustomizations []users.User, + groupCustomizations []users.Group, ostreeURL string, ostreeRef string, osName string) (*KickstartStageOptions, error) { diff --git a/internal/osbuild/users_stage.go b/internal/osbuild/users_stage.go index 5d4d40148..70e2ff436 100644 --- a/internal/osbuild/users_stage.go +++ b/internal/osbuild/users_stage.go @@ -1,7 +1,6 @@ package osbuild import ( - "github.com/osbuild/osbuild-composer/internal/blueprint" "github.com/osbuild/osbuild-composer/internal/crypt" "github.com/osbuild/osbuild-composer/internal/users" ) @@ -30,7 +29,7 @@ func NewUsersStage(options *UsersStageOptions) *Stage { } } -func NewUsersStageOptions(userCustomizations []blueprint.UserCustomization, omitKey bool) (*UsersStageOptions, error) { +func NewUsersStageOptions(userCustomizations []users.User, omitKey bool) (*UsersStageOptions, error) { if len(userCustomizations) == 0 { return nil, nil } diff --git a/internal/osbuild/users_stage_test.go b/internal/osbuild/users_stage_test.go index ec858bc73..5e0aa161c 100644 --- a/internal/osbuild/users_stage_test.go +++ b/internal/osbuild/users_stage_test.go @@ -4,8 +4,7 @@ import ( "strings" "testing" - "github.com/osbuild/osbuild-composer/internal/blueprint" - + "github.com/osbuild/osbuild-composer/internal/users" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -24,20 +23,20 @@ func TestNewUsersStageOptionsPassword(t *testing.T) { EmptyPass := "" CryptPass := "$6$RWdHzrPfoM6BMuIP$gKYlBXQuJgP.G2j2twbOyxYjFDPUQw8Jp.gWe1WD/obX0RMyfgw5vt.Mn/tLLX4mQjaklSiIzoAW3HrVQRg4Q." // #nosec G101 - users := []blueprint.UserCustomization{ - blueprint.UserCustomization{ + users := []users.User{ + { Name: "bart", Password: &Pass, }, - blueprint.UserCustomization{ + { Name: "lisa", Password: &CryptPass, }, - blueprint.UserCustomization{ + { Name: "maggie", Password: &EmptyPass, }, - blueprint.UserCustomization{ + { Name: "homer", }, }