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

Commit

Permalink
Add part parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
minhaj10p committed Jan 18, 2019
1 parent c9a8399 commit 9fc5e8b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 36 deletions.
66 changes: 66 additions & 0 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/url"
"testing"

"github.com/davecgh/go-spew/spew"

"github.com/docker/oscalkit/types/oscal/catalog"
"github.com/docker/oscalkit/types/oscal/profile"
)
Expand Down Expand Up @@ -216,3 +218,67 @@ func failTest(err error, t *testing.T) {
t.Error(t)
}
}

func TestAddPartInCatalog(t *testing.T) {

alt := []profile.Alter{
profile.Alter{
ControlId: "ac-10",
Additions: []profile.Add{
profile.Add{
Parts: []catalog.Part{
catalog.Part{
Id: "ac-10_prm",
Class: "guidance",
Title: "parent prm guidance",
},
catalog.Part{
Id: "ac-2_prm3",
Class: "whatever",
},
},
},
},
},
profile.Alter{
SubcontrolId: "ac-10.1",
Additions: []profile.Add{
profile.Add{
Parts: []catalog.Part{
catalog.Part{
Id: "ac-10_obj",
Class: "yolo",
},
},
},
},
},
}

cat := catalog.Catalog{
Groups: []catalog.Group{
catalog.Group{
Controls: []catalog.Control{
catalog.Control{
Id: "ac-10",
Subcontrols: []catalog.Subcontrol{
catalog.Subcontrol{
Id: "ac-10.1",
Parts: []catalog.Part{},
},
},
Parts: []catalog.Part{
catalog.Part{
Id: "ac_10_prm",
Class: "guidance",
Title: "sdfsdfsd",
},
},
},
},
},
},
}

spew.Dump(AddPartInCatalog(alt, &cat))
}
65 changes: 65 additions & 0 deletions generator/manipulation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package generator

import (
"fmt"

"github.com/docker/oscalkit/types/oscal/catalog"
"github.com/docker/oscalkit/types/oscal/profile"
)

func AddPartInCatalog(alterations []profile.Alter, c *catalog.Catalog) *catalog.Catalog {
for _, x := range alterations {
for i, g := range c.Groups {
for j, ctrl := range g.Controls {
if ctrl.Id == x.ControlId {
ctrlIncrement := 1
for _, add := range x.Additions {
for _, p := range add.Parts {
appended := false
for catalogCtrlPartIndex, catalogPart := range ctrl.Parts {
if p.Class == catalogPart.Class {
appended = true
ctrl.Parts[catalogCtrlPartIndex].Id = p.Id + fmt.Sprintf("_%d", ctrlIncrement)
ctrlIncrement++
p.Id = p.Id + fmt.Sprintf("_%d", ctrlIncrement)
position := (ctrlIncrement + catalogCtrlPartIndex) - 1
ctrl.Parts = append(ctrl.Parts[:position], append([]catalog.Part{p}, ctrl.Parts[position:]...)...)
}
}
if !appended {
ctrl.Parts = append(ctrl.Parts, p)
}
}
}
c.Groups[i].Controls[j] = ctrl
}
for k, subctrl := range c.Groups[i].Controls[j].Subcontrols {
subCtrlIncrement := 1
if subctrl.Id == x.SubcontrolId {
for _, add := range x.Additions {
for _, p := range add.Parts {
appended := false
for catalogSubCtrlPartIndex, catalogPart := range subctrl.Parts {
if p.Class == catalogPart.Class {
appended = true
subctrl.Parts[catalogSubCtrlPartIndex].Id = p.Id + fmt.Sprintf("_%d", subCtrlIncrement)
subCtrlIncrement++
position := (subCtrlIncrement + catalogSubCtrlPartIndex) - 1
subctrl.Parts = append(subctrl.Parts[:position], append([]catalog.Part{p}, subctrl.Parts[:position]...)...)
}
}
if !appended {
subctrl.Parts = append(subctrl.Parts, p)
}
}

}
}
c.Groups[i].Controls[j].Subcontrols[k] = subctrl

}
}
}
}
return c
}
39 changes: 3 additions & 36 deletions generator/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func CreateCatalogsFromProfile(profileArg *profile.Profile) ([]*catalog.Catalog,
select {
case importedCatalog := <-c:
//Prepare a new catalog object to merge into the final List of OutputCatalogs
importedCatalog = AddPartInCatalog(profileArg.Modify.Alterations, importedCatalog)
if profileArg.Modify != nil {
importedCatalog = AddPartInCatalog(profileArg.Modify.Alterations, importedCatalog)
}
newCatalog, err := GetMappedCatalogControlsFromImport(importedCatalog, profileImport)
if err != nil {
errChan <- err
Expand Down Expand Up @@ -157,38 +159,3 @@ func getCatalogForImport(ctx context.Context, i profile.Import, c chan *catalog.
}
}(i)
}

func AddPartInCatalog(alterations []profile.Alter, c *catalog.Catalog) *catalog.Catalog {
for _, x := range alterations {
for i, g := range c.Groups {
for j, ctrl := range g.Controls {
if x.ControlId == "" {
continue
}
if ctrl.Id == x.ControlId {
var parts []catalog.Part
for _, add := range x.Additions {
for _, p := range add.Parts {
for _, catalogPart := range ctrl.Parts {
var subctrlParts []catalog.Part
for k, subctrls := range ctrl.Subcontrols {
for _, scp := range subctrls.Parts {
if scp.Class == p.Class {
subctrlParts = append(subctrlParts, scp)
}
}
c.Groups[i].Controls[j].Subcontrols[k].Parts = subctrlParts
}
if p.Class == catalogPart.Class {
parts = append(parts, catalogPart)
}
}
}
}
c.Groups[i].Controls[j].Parts = parts
}
}
}
}
return c
}

0 comments on commit 9fc5e8b

Please sign in to comment.