From 1343d50fb76e0e717905a780cfab89320b47a826 Mon Sep 17 00:00:00 2001 From: Minhaj Uddin Khan Date: Thu, 24 Jan 2019 16:31:40 +0500 Subject: [PATCH] Add processing of set param --- generator/generator_test.go | 48 +++++++++++++++++++++++++++++++++++++ generator/manipulation.go | 21 ++++++++++++++++ generator/mapper.go | 3 +++ 3 files changed, 72 insertions(+) diff --git a/generator/generator_test.go b/generator/generator_test.go index 8bd475e1..6caf8582 100644 --- a/generator/generator_test.go +++ b/generator/generator_test.go @@ -6,6 +6,7 @@ import ( "net/url" "testing" + "github.com/docker/oscalkit/impl" "github.com/docker/oscalkit/types/oscal/catalog" "github.com/docker/oscalkit/types/oscal/profile" ) @@ -421,6 +422,53 @@ func TestProcessAdditionWithDifferentPartClass(t *testing.T) { } } + +func TestProcessSetParam(t *testing.T) { + parameterID := "ac-1_prm_1" + parameterVal := "777" + ctrl := "ac-1" + shouldChange := fmt.Sprintf(`this should change. `, parameterID) + afterChange := fmt.Sprintf(`this should change. %s`, parameterVal) + sp := []profile.SetParam{ + profile.SetParam{ + Id: parameterID, + Constraints: []catalog.Constraint{ + catalog.Constraint{ + Value: parameterVal, + }, + }, + }, + } + controls := []catalog.Control{ + catalog.Control{ + Id: ctrl, + Parts: []catalog.Part{ + catalog.Part{ + Prose: &catalog.Prose{ + P: []catalog.P{ + catalog.P{ + Raw: shouldChange, + }, + }, + }, + }, + }, + }, + } + ctlg := &catalog.Catalog{ + Groups: []catalog.Group{ + catalog.Group{ + Controls: controls, + }, + }, + } + nc := impl.NISTCatalog{} + ctlg = ProcessSetParam(sp, ctlg, &nc) + if ctlg.Groups[0].Controls[0].Parts[0].Prose.P[0].Raw != afterChange { + t.Error("failed to parse set param template") + } +} + func failTest(err error, t *testing.T) { if err != nil { t.Error(err) diff --git a/generator/manipulation.go b/generator/manipulation.go index 28d0d651..b40a1e9f 100644 --- a/generator/manipulation.go +++ b/generator/manipulation.go @@ -8,6 +8,7 @@ import ( "github.com/sirupsen/logrus" + "github.com/docker/oscalkit/impl" "github.com/docker/oscalkit/types/oscal" "github.com/docker/oscalkit/types/oscal/catalog" "github.com/docker/oscalkit/types/oscal/profile" @@ -71,6 +72,26 @@ func ProcessAlteration(alterations []profile.Alter, c *catalog.Catalog) *catalog return c } +// ProcessSetParam processes set-param of a profile +func ProcessSetParam(setParams []profile.SetParam, c *catalog.Catalog, catalogHelper impl.Catalog) *catalog.Catalog { + for _, sp := range setParams { + ctrlID := catalogHelper.GetControl(sp.Id) + for i, g := range c.Groups { + for j, catalogCtrl := range g.Controls { + if ctrlID == catalogCtrl.Id { + for k := range catalogCtrl.Parts { + if len(sp.Constraints) == 0 { + continue + } + c.Groups[i].Controls[j].Parts[k].ModifyProse(sp.Id, sp.Constraints[0].Value) + } + } + } + } + } + return c +} + // ModifyParts modifies parts func ModifyParts(p catalog.Part, controlParts []catalog.Part) []catalog.Part { diff --git a/generator/mapper.go b/generator/mapper.go index 41a9753e..08feb593 100644 --- a/generator/mapper.go +++ b/generator/mapper.go @@ -6,6 +6,7 @@ import ( "os" "strings" + "github.com/docker/oscalkit/impl" "github.com/docker/oscalkit/types/oscal" "github.com/docker/oscalkit/types/oscal/catalog" "github.com/docker/oscalkit/types/oscal/profile" @@ -39,7 +40,9 @@ func CreateCatalogsFromProfile(profileArg *profile.Profile) ([]*catalog.Catalog, case importedCatalog := <-c: // Prepare a new catalog object to merge into the final List of OutputCatalogs if profileArg.Modify != nil { + nc := impl.NISTCatalog{} importedCatalog = ProcessAlteration(profileArg.Modify.Alterations, importedCatalog) + importedCatalog = ProcessSetParam(profileArg.Modify.ParamSettings, importedCatalog, &nc) } newCatalog, err := GetMappedCatalogControlsFromImport(importedCatalog, profileImport) if err != nil {