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

Commit

Permalink
Remove profile dependency from implementation code generation (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
minhaj10p authored and anweiss committed Feb 7, 2019
1 parent 4836dbd commit ec6032a
Show file tree
Hide file tree
Showing 5 changed files with 491 additions and 208 deletions.
23 changes: 1 addition & 22 deletions cli/cmd/generate/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ var Implementation = cli.Command{
Name: "implementation",
Usage: "generates go code for implementation against provided profile and excel sheet",
Flags: []cli.Flag{
cli.StringFlag{
Name: "profile, p",
Usage: "profile to intersect against",
Destination: &profile,
},
cli.StringFlag{
Name: "excel, e",
Usage: "excel sheet to get component configs",
Expand All @@ -50,9 +45,6 @@ var Implementation = cli.Command{
},
},
Before: func(c *cli.Context) error {
if profile == "" {
return cli.NewExitError("oscalkit generate is missing the --profile flag", 1)
}
if excelSheet == "" {
return cli.NewExitError("oscalkit implementation is missing --excel flag", 1)
}
Expand All @@ -64,19 +56,6 @@ var Implementation = cli.Command{
return cli.NewExitError(err, 1)
}

profileF, err := generator.GetFilePath(profile)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
f, err := os.Open(profileF)
if err != nil {
return cli.NewExitError(fmt.Sprintf("cannot open profile %v", err), 1)
}
defer f.Close()
profile, err := generator.ReadProfile(f)
if err != nil {
return err
}
excelF, err := generator.GetFilePath(excelSheet)
if err != nil {
return err
Expand Down Expand Up @@ -107,7 +86,7 @@ var Implementation = cli.Command{
}

catalog := impl.NISTCatalog{ID: "NIST_SP-800-53"}
implementationData := impl.GenerateImplementation(records, profile, &catalog)
implementationData := impl.GenerateImplementation(records, &catalog)
t, err := templates.GetImplementationTemplate()
if err != nil {
return fmt.Errorf("cannot get implementation template err %v", err)
Expand Down
303 changes: 253 additions & 50 deletions impl/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package impl

import (
"fmt"
"net/url"
"testing"

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

const (
Expand Down Expand Up @@ -71,53 +69,7 @@ func TestGenerateImplementation(t *testing.T) {
}
}

p := profile.Profile{
ID: temporaryProfileID,
Imports: []profile.Import{
profile.Import{
Href: &catalog.Href{
URL: func() *url.URL {
uri, _ := url.Parse(catalogRef)
return uri
}(),
},
Include: &profile.Include{
IdSelectors: []profile.Call{
profile.Call{
ControlId: "ac-2",
},
profile.Call{
ControlId: "ac-4",
},
profile.Call{
SubcontrolId: "ac-4.2",
},
},
},
},
},
Modify: &profile.Modify{
ParamSettings: []profile.SetParam{
profile.SetParam{
Id: "ac-2_prm",
Constraints: []catalog.Constraint{catalog.Constraint{Value: "some constraint"}},
},
profile.SetParam{
Id: "ac-2_prm_obj",
Constraints: []catalog.Constraint{catalog.Constraint{Value: "some constraint"}},
},
profile.SetParam{
Id: "",
Constraints: []catalog.Constraint{},
},
profile.SetParam{
Id: "ac-4_prm",
Constraints: []catalog.Constraint{},
},
},
},
}
i := GenerateImplementation(csvs, &p, &NISTCatalog{"NISTSP80053"})
i := GenerateImplementation(csvs, &NISTCatalog{"NISTSP80053"})

if len(i.ComponentDefinitions) != len(comps) {
t.Error("mismatch number of component definitions")
Expand All @@ -130,3 +82,254 @@ func TestGenerateImplementation(t *testing.T) {
}

}

func TestFindOrCreateImplementsProfile(t *testing.T) {
cd := implementation.ComponentDefinition{}
profileID := "123"
implementsProfile := findOrCreateImplementsProfile(&cd, profileID)
if implementsProfile == nil {
t.Error("nothing created")
return
}
for _, x := range cd.ImplementsProfiles {
if x.ProfileID == profileID {
return
}
}
t.Error("profile did not get appended")
}

func TestFindOrCreateControlConfig(t *testing.T) {
cd := implementation.ComponentDefinition{
ImplementsProfiles: []*implementation.ImplementsProfile{
&implementation.ImplementsProfile{
ProfileID: "123",
},
},
}
configIDRef := "some-guid"
controlConfig := findOrCreateControlConfig(cd.ImplementsProfiles[0], configIDRef)
if controlConfig == nil {
t.Error("empty ctrl conf")
}
for _, x := range cd.ImplementsProfiles[0].ControlConfigurations {
if x.ConfigurationIDRef == configIDRef {
return
}
}
t.Error("coudnt find conf id")
}

func TestMapImplementsProfile(t *testing.T) {

parameterID := "ac-10_prm_3"
profileID := "uuid-fedramp-high-20180806-195540"
configurationIDRef := "random-guid-id"
Value := "<=2"
checkAndValue := fmt.Sprintf("MakeItRight(%s)", Value)
cd := implementation.ComponentDefinition{
ComponentConfigurations: []*implementation.ComponentConfiguration{
&implementation.ComponentConfiguration{
ID: configurationIDRef,
Name: "MakeItRight",
},
},
}
mapImplementsProfile(&cd, parameterID, profileID, checkAndValue)
if len(cd.ImplementsProfiles) < 1 {
t.Error("implements profile array should have one element")
}
if cd.ImplementsProfiles[0].ProfileID != profileID {
t.Errorf("profile id should be %s", profileID)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations) < 1 {
t.Error("control configurations array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].ConfigurationIDRef != configurationIDRef {
t.Errorf("config ref id should be %s", configurationIDRef)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters) < 1 {
t.Error("parameters array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].ParameterID != parameterID {
t.Errorf("parameter id should be %s", parameterID)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues) < 1 {
t.Error("possible value array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues[0] != Value {
t.Errorf("possible value should be %s", Value)
}
}

func TestMapImplementsProfileWithMultiplePossibleValues(t *testing.T) {

parameterID := "ac-10_prm_3"
profileID := "uuid-fedramp-high-20180806-195540"
configurationIDRef := "random-guid-id"
Value := "<=2"
Value2 := "<=3"
checkAndValue := fmt.Sprintf("MakeItRight(%s)", Value)
checkAndValue2 := fmt.Sprintf("MakeItRight(%s)", Value2)

cd := implementation.ComponentDefinition{
ComponentConfigurations: []*implementation.ComponentConfiguration{
&implementation.ComponentConfiguration{
ID: configurationIDRef,
Name: "MakeItRight",
},
},
}
mapImplementsProfile(&cd, parameterID, profileID, checkAndValue)
mapImplementsProfile(&cd, parameterID, profileID, checkAndValue2)
if len(cd.ImplementsProfiles) < 1 {
t.Error("implements profile array should have one element")
}
if cd.ImplementsProfiles[0].ProfileID != profileID {
t.Errorf("profile id should be %s", profileID)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations) < 1 {
t.Error("control configurations array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].ConfigurationIDRef != configurationIDRef {
t.Errorf("config ref id should be %s", configurationIDRef)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters) < 1 {
t.Error("parameters array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].ParameterID != parameterID {
t.Errorf("parameter id should be %s", parameterID)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues) < 2 {
t.Error("possible value array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues[0] != Value {
t.Errorf("possible value should be %s", Value)
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues[1] != Value2 {
t.Errorf("possible value should be %s", Value)
}
}

func TestMapImplementsProfileWithMultipleProfiles(t *testing.T) {

parameterID := "ac-10_prm_3"
profileID := "uuid-fedramp-high-20180806-195540"
profileID2 := "uuid-fedramp-moderate-20180806-195540"
configurationIDRef := "random-guid-id"
Value := "<=2"
checkAndValue := fmt.Sprintf("MakeItRight(%s)", Value)

cd := implementation.ComponentDefinition{
ComponentConfigurations: []*implementation.ComponentConfiguration{
&implementation.ComponentConfiguration{
ID: configurationIDRef,
Name: "MakeItRight",
},
},
}
mapImplementsProfile(&cd, parameterID, profileID, checkAndValue)
mapImplementsProfile(&cd, parameterID, profileID2, checkAndValue)
if len(cd.ImplementsProfiles) < 2 {
t.Error("implements profile array should have one element")
}
if cd.ImplementsProfiles[0].ProfileID != profileID {
t.Errorf("profile id should be %s", profileID)
}
if cd.ImplementsProfiles[1].ProfileID != profileID2 {
t.Errorf("profile id should be %s", profileID2)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations) < 1 {
t.Error("control configurations array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].ConfigurationIDRef != configurationIDRef {
t.Errorf("config ref id should be %s", configurationIDRef)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters) < 1 {
t.Error("parameters array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].ParameterID != parameterID {
t.Errorf("parameter id should be %s", parameterID)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues) < 1 {
t.Error("possible value array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues[0] != Value {
t.Errorf("possible value should be %s", Value)
}
}

func TestMapImplementsProfileWithMultipleParameters(t *testing.T) {

parameterID := "ac-10_prm_3"
parameterID2 := "ac-10_prm_2"
profileID := "uuid-fedramp-high-20180806-195540"
configurationIDRef := "random-guid-id"
Value := "<=2"
checkAndValue := fmt.Sprintf("MakeItRight(%s)", Value)

cd := implementation.ComponentDefinition{
ComponentConfigurations: []*implementation.ComponentConfiguration{
&implementation.ComponentConfiguration{
ID: configurationIDRef,
Name: "MakeItRight",
},
},
}
mapImplementsProfile(&cd, parameterID, profileID, checkAndValue)
mapImplementsProfile(&cd, parameterID2, profileID, checkAndValue)
if len(cd.ImplementsProfiles) < 1 {
t.Error("implements profile array should have one element")
}
if cd.ImplementsProfiles[0].ProfileID != profileID {
t.Errorf("profile id should be %s", profileID)
}

if len(cd.ImplementsProfiles[0].ControlConfigurations) < 1 {
t.Error("control configurations array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].ConfigurationIDRef != configurationIDRef {
t.Errorf("config ref id should be %s", configurationIDRef)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters) < 2 {
t.Error("parameters array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].ParameterID != parameterID {
t.Errorf("parameter id should be %s", parameterID)
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[1].ParameterID != parameterID2 {
t.Errorf("parameter id should be %s", parameterID2)
}
if len(cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues) < 1 {
t.Error("possible value array should have one element")
}
if cd.ImplementsProfiles[0].ControlConfigurations[0].Parameters[0].PossibleValues[0] != Value {
t.Errorf("possible value should be %s", Value)
}
}

func TestGetProfileIDWithValidProfile(t *testing.T) {
x := "FedRAMP_High"
o := getProfileID(x)
if o != profileMap[x] {
t.Error("failed to map profile id")
}
}
func TestGetProfileIDWithInvalidProfile(t *testing.T) {
x := "123"
o := getProfileID(x)
if o == profileMap[x] {
t.Error("mapped invalid profile id")
}
}

func TestDetokenizeParameterString(t *testing.T) {

x := "FedRAMP_High->SetParam(5)"
p := "FedRAMP_High"
c := "SetParam(5)"
profileID, checkAndValue := detokenizeParameterString(x)
if profileMap[p] != profileID || c != checkAndValue {
t.Errorf("failed to tokenize parameter string %s| output %s:%s", x, profileMap[p], checkAndValue)
}
}
Loading

0 comments on commit ec6032a

Please sign in to comment.