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

Set param #64

Merged
merged 1 commit into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minhaj10p can we write a negative test case as well to make sure that if a parameter isn't found in a prose, it shouldn't be changed after applying a profile?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding negative test

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