Skip to content

Commit

Permalink
cloudapi: json tailoring options
Browse files Browse the repository at this point in the history
Add support for json tailoring files in the cloudapi. Expand the tests
to check that the options returned are okay and that tailoring options
and json tailoring options can't be provided at the same time.
  • Loading branch information
kingsleyzissou authored and croissanne committed Aug 7, 2024
1 parent 16180e9 commit 2da3a73
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 177 deletions.
18 changes: 18 additions & 0 deletions internal/cloudapi/v2/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ func (request *ComposeRequest) GetCustomizationsFromBlueprintRequest() (*bluepri
if rbpc.Openscap.Datastream != nil {
oscap.DataStream = *rbpc.Openscap.Datastream
}
if rbpc.Openscap.Tailoring != nil && rbpc.Openscap.JsonTailoring != nil {
return nil, fmt.Errorf("OpenSCAP customization error: choose one option between OpenSCAP tailoring and OpenSCAP json tailoring")
}
if tailoring := rbpc.Openscap.Tailoring; tailoring != nil {
tc := blueprint.OpenSCAPTailoringCustomizations{}
if tailoring.Selected != nil && len(*tailoring.Selected) > 0 {
Expand All @@ -243,6 +246,12 @@ func (request *ComposeRequest) GetCustomizationsFromBlueprintRequest() (*bluepri
}
oscap.Tailoring = &tc
}
if jsonTailoring := rbpc.Openscap.JsonTailoring; jsonTailoring != nil {
oscap.JSONTailoring = &blueprint.OpenSCAPJSONTailoringCustomizations{
ProfileID: jsonTailoring.ProfileId,
Filepath: jsonTailoring.Filepath,
}
}
c.OpenSCAP = oscap
}

Expand Down Expand Up @@ -684,6 +693,9 @@ func (request *ComposeRequest) GetBlueprintFromCustomizations() (blueprint.Bluep
openSCAPCustomization := &blueprint.OpenSCAPCustomization{
ProfileID: request.Customizations.Openscap.ProfileId,
}
if request.Customizations.Openscap.Tailoring != nil && request.Customizations.Openscap.JsonTailoring != nil {
return bp, fmt.Errorf("OpenSCAP customization error: choose one option between OpenSCAP tailoring and OpenSCAP json tailoring")
}
if tailoring := request.Customizations.Openscap.Tailoring; tailoring != nil {
tailoringCustomizations := blueprint.OpenSCAPTailoringCustomizations{}
if tailoring.Selected != nil && len(*tailoring.Selected) > 0 {
Expand All @@ -694,6 +706,12 @@ func (request *ComposeRequest) GetBlueprintFromCustomizations() (blueprint.Bluep
}
openSCAPCustomization.Tailoring = &tailoringCustomizations
}
if jsonTailoring := request.Customizations.Openscap.JsonTailoring; jsonTailoring != nil {
openSCAPCustomization.JSONTailoring = &blueprint.OpenSCAPJSONTailoringCustomizations{
ProfileID: jsonTailoring.ProfileId,
Filepath: jsonTailoring.Filepath,
}
}
bp.Customizations.OpenSCAP = openSCAPCustomization
}

Expand Down
43 changes: 43 additions & 0 deletions internal/cloudapi/v2/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,4 +785,47 @@ func TestOpenSCAPTailoringOptions(t *testing.T) {
bp, err := cr.GetBlueprintFromCustomizations()
assert.NoError(t, err)
assert.Equal(t, expectedOscap, bp.Customizations.OpenSCAP)

cr = ComposeRequest{
Customizations: &Customizations{
Openscap: &OpenSCAP{
ProfileId: "test-123",
JsonTailoring: &OpenSCAPJSONTailoring{
ProfileId: "test-123-tailoring",
Filepath: "/some/filepath",
},
},
},
}

expectedOscap = &blueprint.OpenSCAPCustomization{
ProfileID: "test-123",
JSONTailoring: &blueprint.OpenSCAPJSONTailoringCustomizations{
ProfileID: "test-123-tailoring",
Filepath: "/some/filepath",
},
}

bp, err = cr.GetBlueprintFromCustomizations()
assert.NoError(t, err)
assert.Equal(t, expectedOscap, bp.Customizations.OpenSCAP)

cr = ComposeRequest{
Customizations: &Customizations{
Openscap: &OpenSCAP{
ProfileId: "test-123",
Tailoring: &OpenSCAPTailoring{
Selected: common.ToPtr([]string{"one", "two", "three"}),
Unselected: common.ToPtr([]string{"four", "five", "six"}),
},
JsonTailoring: &OpenSCAPJSONTailoring{
ProfileId: "test-123-tailoring",
Filepath: "/some/filepath",
},
},
},
}

bp, err = cr.GetBlueprintFromCustomizations()
assert.Error(t, err)
}
Loading

0 comments on commit 2da3a73

Please sign in to comment.