Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
Add processing of set param
Browse files Browse the repository at this point in the history
  • Loading branch information
minhaj10p committed Jan 24, 2019
1 parent f32755e commit 3b7efe8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
94 changes: 94 additions & 0 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -421,6 +422,99 @@ 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. <insert param-id="%s">`, 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 TestProcessSetParamWithUnmatchParam(t *testing.T) {
parameterID := "ac-1_prm_1"
parameterVal := "777"
ctrl := "ac-1"
shouldChange := fmt.Sprintf(`this should change. <insert param-id="%s">`, parameterID)
afterChange := fmt.Sprintf(`this should change. %s`, parameterVal)
sp := []profile.SetParam{
profile.SetParam{
Id: "ac-1_prm_2",
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("should not change parameter with mismatching parameter id")
}
}

func failTest(err error, t *testing.T) {
if err != nil {
t.Error(err)
Expand Down
21 changes: 21 additions & 0 deletions generator/manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {

Expand Down
3 changes: 3 additions & 0 deletions generator/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3b7efe8

Please sign in to comment.