Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interfaces for workspace, other yamls. #212

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
26 changes: 13 additions & 13 deletions commands/diff/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package diff
import (
"github.com/opencontrol/compliance-masonry/lib"
"github.com/opencontrol/compliance-masonry/tools/certifications"
"github.com/opencontrol/compliance-masonry/lib/components/versions/base"
"fmt"
"github.com/opencontrol/compliance-masonry/lib/common"
)

// Inventory maintains the inventory of all the controls within a given workspace.
type Inventory struct {
*lib.LocalWorkspace
masterControlList map[string]lib.Control
actualSatisfiedControls map[string]base.Satisfies
MissingControlList map[string]lib.Control
lib.Workspace
masterControlList map[string]common.Control
actualSatisfiedControls map[string]common.Satisfies
MissingControlList map[string]common.Control
}

// retrieveMasterControlsList will gather the list of controls needed for a given certification.
func (i *Inventory) retrieveMasterControlsList() {
for standardKey, standard := range i.Certification.Standards {
for controlKey, control := range standard.Controls {
for standardKey, standard := range i.GetCertification().GetStandards() {
for controlKey, control := range standard.GetControls() {
key := standardAndControlString(standardKey, controlKey)
if _, exists := i.masterControlList[key]; !exists {
i.masterControlList[key] = control
Expand All @@ -29,7 +29,7 @@ func (i *Inventory) retrieveMasterControlsList() {

// findDocumentedControls will find the list of all documented controls found within the workspace.
func (i *Inventory) findDocumentedControls() {
for _, component := range i.Components.GetAll() {
for _, component := range i.GetAllComponents() {
for _, satisfiedControl := range component.GetAllSatisfies() {
key := standardAndControlString(satisfiedControl.GetStandardKey(), satisfiedControl.GetControlKey())
if _, exists := i.actualSatisfiedControls[key]; !exists {
Expand Down Expand Up @@ -72,12 +72,12 @@ func ComputeGapAnalysis(config Config) (Inventory, []error) {
}
workspace, _ := lib.LoadData(config.OpencontrolDir, certificationPath)
i := Inventory{
LocalWorkspace: workspace,
masterControlList: make(map[string]lib.Control),
actualSatisfiedControls: make(map[string]base.Satisfies),
MissingControlList: make(map[string]lib.Control),
Workspace: workspace,
masterControlList: make(map[string]common.Control),
actualSatisfiedControls: make(map[string]common.Satisfies),
MissingControlList: make(map[string]common.Control),
}
if i.Certification == nil || i.Components == nil {
if i.GetCertification() == nil || len(i.GetAllComponents()) == 0 {
return Inventory{}, []error{fmt.Errorf("Unable to load data in %s for certification %s", config.OpencontrolDir, config.Certification)}
}

Expand Down
8 changes: 4 additions & 4 deletions commands/docs/gitbook/gitbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/opencontrol/compliance-masonry/lib"
"github.com/opencontrol/compliance-masonry/tools/fs"
"github.com/opencontrol/compliance-masonry/lib/components/versions/base"
"github.com/opencontrol/compliance-masonry/lib/common"
)

// Config contains data for gitbook export configurations
Expand All @@ -21,7 +21,7 @@ type Config struct {
// OpenControlGitBook struct is an extension of models.OpenControl that adds
// an exportPath
type OpenControlGitBook struct {
*lib.LocalWorkspace
lib.Workspace
markdownPath string
exportPath string
FSUtil fs.Util
Expand All @@ -30,14 +30,14 @@ type OpenControlGitBook struct {
// ComponentGitbook struct is an extension of models.Component that adds
// an exportPath
type ComponentGitbook struct {
base.Component
common.Component
exportPath string
}

// ControlGitbook struct is an extension of models.Control that adds
// an exportPath
type ControlGitbook struct {
*lib.Control
common.Control
exportPath string
standardKey string
controlKey string
Expand Down
68 changes: 34 additions & 34 deletions commands/docs/gitbook/gitbookCertification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import (
"path/filepath"

"github.com/opencontrol/compliance-masonry/lib"
"github.com/opencontrol/compliance-masonry/lib/components/versions/base"
"github.com/opencontrol/compliance-masonry/lib/common"
)

func (openControl *OpenControlGitBook) getResponsibleRole(text string, component base.Component) string {
func (openControl *OpenControlGitBook) getResponsibleRole(text string, component common.Component) string {
if component.GetResponsibleRole() != "" {
text = fmt.Sprintf("%s\n##### Responsible Role: %s\n", text, component.GetResponsibleRole())
}
return text
}

func (openControl *OpenControlGitBook) getNarratives(narratives []base.Section, text string, control *ControlGitbook) string {
func (openControl *OpenControlGitBook) getNarratives(narratives []common.Section, text string, control *ControlGitbook) string {
if len(narratives) == 0 {
return fmt.Sprintf("%s\nNo narrative found for the combination of standard %s and control %s\n", text, control.standardKey, control.controlKey)
}
Expand All @@ -28,15 +27,15 @@ func (openControl *OpenControlGitBook) getNarratives(narratives []base.Section,
return text
}

func (openControl *OpenControlGitBook) getNarrative(narrative base.Section, text string) string {
func (openControl *OpenControlGitBook) getNarrative(narrative common.Section, text string) string {
if narrative.GetKey() != "" {
text = fmt.Sprintf("%s\n##### %s\n", text, narrative.GetKey())
}
text = fmt.Sprintf("%s%s\n", text, narrative.GetText())
return text
}

func (openControl *OpenControlGitBook) getParameters(text string, parameters []base.Section) string {
func (openControl *OpenControlGitBook) getParameters(text string, parameters []common.Section) string {
if len(parameters) > 0 {
text = fmt.Sprintf("%s\n##### Parameters:\n", text)
}
Expand All @@ -46,7 +45,7 @@ func (openControl *OpenControlGitBook) getParameters(text string, parameters []b
return text
}

func (openControl *OpenControlGitBook) getParameter(text string, parameter base.Section) string{
func (openControl *OpenControlGitBook) getParameter(text string, parameter common.Section) string{
text = fmt.Sprintf("%s\n###### %s\n", text, parameter.GetKey())
text = fmt.Sprintf("%s%s\n", text, parameter.GetText())
return text
Expand All @@ -62,14 +61,13 @@ func (openControl *OpenControlGitBook) getCoveredBy(text string, justification l
if componentKey == "" {
componentKey = justification.ComponentKey
}
openControl.Components.GetAndApply(componentKey, func(component base.Component) {
text = openControl.getCoveredByVerification(text, component, coveredBy)
})
component := openControl.GetComponent(componentKey)
text = openControl.getCoveredByVerification(text, component, coveredBy)
}
return text
}

func (openControl *OpenControlGitBook) getCoveredByVerification(text string, component base.Component, coveredBy common.CoveredBy) string {
func (openControl *OpenControlGitBook) getCoveredByVerification(text string, component common.Component, coveredBy common.CoveredBy) string {
if component != nil {
verification := component.GetVerifications().Get(coveredBy.VerificationKey)
text += exportLink(
Expand All @@ -90,38 +88,40 @@ func (openControl *OpenControlGitBook) getControlOrigin(text string, controlOrig

func (openControl *OpenControlGitBook) exportControl(control *ControlGitbook) (string, string) {
key := replaceParentheses(fmt.Sprintf("%s-%s", control.standardKey, control.controlKey))
text := fmt.Sprintf("#%s\n##%s\n", key, control.Name)
openControl.Justifications.GetAndApply(control.standardKey, control.controlKey, func(selectJustifications lib.Verifications) {
// In the case that no information was found period for the standard and control
if len(selectJustifications) == 0 {
errorText := fmt.Sprintf("No information found for the combination of standard %s and control %s", control.standardKey, control.controlKey)
text = fmt.Sprintf("%s%s\n", text, errorText)
return
}
for _, justification := range selectJustifications {
openControl.Components.GetAndApply(justification.ComponentKey, func(component base.Component) {
text = fmt.Sprintf("%s\n#### %s\n", text, component.GetName())
text := fmt.Sprintf("#%s\n##%s\n", key, control.GetName())
selectJustifications := openControl.GetJustification(control.standardKey, control.controlKey)
// In the case that no information was found period for the standard and control
if len(selectJustifications) == 0 {
errorText := fmt.Sprintf("No information found for the combination of standard %s and control %s", control.standardKey, control.controlKey)
text = fmt.Sprintf("%s%s\n", text, errorText)
}
for _, justification := range selectJustifications {
component := openControl.GetComponent(justification.ComponentKey)
text = fmt.Sprintf("%s\n#### %s\n", text, component.GetName())

text = openControl.getResponsibleRole(text, component)
text = openControl.getResponsibleRole(text, component)

text = openControl.getParameters(text, justification.SatisfiesData.GetParameters())
text = openControl.getParameters(text, justification.SatisfiesData.GetParameters())

text = openControl.getControlOrigin(text, justification.SatisfiesData.GetControlOrigin())
text = openControl.getControlOrigin(text, justification.SatisfiesData.GetControlOrigin())

text = openControl.getNarratives(justification.SatisfiesData.GetNarratives(), text, control)
text = openControl.getCoveredBy(text, justification)
}

text = openControl.getNarratives(justification.SatisfiesData.GetNarratives(), text, control)
})
text = openControl.getCoveredBy(text, justification)
}
})
return filepath.Join(control.exportPath, key+".md"), text
}

func (openControl *OpenControlGitBook) exportStandards() {
standardsExportPath := filepath.Join(openControl.exportPath, "standards")
openControl.FSUtil.Mkdirs(standardsExportPath)
openControl.Certification.GetSortedData(func(standardKey string, controlKey string) {
control := openControl.Standards.Get(standardKey).Controls[controlKey]
controlPath, controlText := openControl.exportControl(&ControlGitbook{&control, standardsExportPath, standardKey, controlKey})
ioutil.WriteFile(controlPath, []byte(controlText), 0700)
})
standardKeys := openControl.GetCertification().GetSortedStandards()
for _, standardKey := range standardKeys {
controlKeys := openControl.GetStandard(standardKey).GetSortedControls()
for _, controlKey := range controlKeys {
control := openControl.GetStandard(standardKey).GetControls()[controlKey]
controlPath, controlText := openControl.exportControl(&ControlGitbook{control, standardsExportPath, standardKey, controlKey})
ioutil.WriteFile(controlPath, []byte(controlText), 0700)
}
}
}
4 changes: 2 additions & 2 deletions commands/docs/gitbook/gitbookCertification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func TestExportControl(t *testing.T) {
dir,
fs.OSUtil{},
}
control := openControl.Standards.Get(example.standardKey).Controls[example.controlKey]
actualPath, actualText := openControl.exportControl(&ControlGitbook{&control, dir, example.standardKey, example.controlKey})
control := openControl.GetStandard(example.standardKey).GetControl(example.controlKey)
actualPath, actualText := openControl.exportControl(&ControlGitbook{control, dir, example.standardKey, example.controlKey})
expectedPath := filepath.Join(dir, example.expectedPath)
// Verify the expected export path is the same as the actual export path
if expectedPath != actualPath {
Expand Down
2 changes: 1 addition & 1 deletion commands/docs/gitbook/gitbookComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (component *ComponentGitbook) exportComponent() (string, string) {
func (openControl *OpenControlGitBook) exportComponents() {
componentsExportPath := filepath.Join(openControl.exportPath, "components")
openControl.FSUtil.Mkdirs(componentsExportPath)
for _, component := range openControl.Components.GetAll() {
for _, component := range openControl.GetAllComponents() {
componentsGitBook := ComponentGitbook{component, componentsExportPath}
componentPath, componentText := componentsGitBook.exportComponent()
ioutil.WriteFile(componentPath, []byte(componentText), 0700)
Expand Down
39 changes: 23 additions & 16 deletions commands/docs/gitbook/gitbookSummaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// BuildComponentsSummaries creates summaries the components for the general summary
func (openControl *OpenControlGitBook) buildComponentsSummaries() string {
summary := "\n## Components\n"
for _, component := range openControl.Components.GetAll() {
for _, component := range openControl.GetAllComponents() {
summary += exportLink(component.GetName(), filepath.Join("components", component.GetKey()+".md"))
}
return summary
Expand All @@ -21,22 +21,29 @@ func (openControl *OpenControlGitBook) buildStandardsSummaries() (string, *map[s
familySummaryMap := make(map[string]string)
summary := "## Standards\n"

openControl.Certification.GetSortedData(func(standardKey string, controlKey string) {
componentLink := replaceParentheses(standardKey + "-" + controlKey + ".md")
control := openControl.Standards.Get(standardKey).Controls[controlKey]
controlFamily := control.Family
controlName := control.Name
newFamily = standardKey + "-" + controlFamily
// create control family headings
if oldFamily != newFamily {
familySummaryMap[newFamily] = fmt.Sprintf("## %s\n", newFamily)
summary += exportLink(controlFamily, filepath.Join("standards", newFamily+".md"))
oldFamily = newFamily
standardKeys := openControl.GetCertification().GetSortedStandards()
for _, standardKey := range standardKeys {
standard := openControl.GetStandard(standardKey)
// Get the standard in the certification
certificationStandard := openControl.GetCertification().GetStandards()[standardKey]
controlKeys := certificationStandard.GetSortedControls()
for _, controlKey := range controlKeys {
componentLink := replaceParentheses(standardKey + "-" + controlKey + ".md")
control := standard.GetControl(controlKey)
controlFamily := control.GetFamily()
controlName := control.GetName()
newFamily = standardKey + "-" + controlFamily
// create control family headings
if oldFamily != newFamily {
familySummaryMap[newFamily] = fmt.Sprintf("## %s\n", newFamily)
summary += exportLink(controlFamily, filepath.Join("standards", newFamily+".md"))
oldFamily = newFamily
}
controlFullName := fmt.Sprintf("%s: %s", controlKey, controlName)
familySummaryMap[newFamily] += exportLink(controlFullName, componentLink)
summary += "\t" + exportLink(controlFullName, filepath.Join("standards", componentLink))
}
controlFullName := fmt.Sprintf("%s: %s", controlKey, controlName)
familySummaryMap[newFamily] += exportLink(controlFullName, componentLink)
summary += "\t" + exportLink(controlFullName, filepath.Join("standards", componentLink))
})
}
return summary, &familySummaryMap
}

Expand Down
7 changes: 3 additions & 4 deletions commands/docs/gitbook/gitbookSummaries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gitbook

import (
"io/ioutil"
"log"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -48,7 +47,7 @@ func TestBuildComponentsSummaries(t *testing.T) {
actualSummary := openControl.buildComponentsSummaries()
data, err := ioutil.ReadFile(example.expectedSummary)
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
expectedSummary := string(data)
// Check that the actual and expected summaries are similar
Expand Down Expand Up @@ -82,7 +81,7 @@ func TestBuildStandardsSummaries(t *testing.T) {
// Check the summary
data, err := ioutil.ReadFile(example.expectedSummary)
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
expectedSummary := string(data)
// Check that the actual and expected summaries are similar
Expand All @@ -94,7 +93,7 @@ func TestBuildStandardsSummaries(t *testing.T) {
expectedFamilySummaryFile := filepath.Join(example.expectedStandardsSummaries, family+".md")
data, err := ioutil.ReadFile(expectedFamilySummaryFile)
if err != nil {
log.Fatal(err)
t.Fatal(err)
}
expectedFamilySummary := string(data)
// Check that the actual and expected summaries are similar
Expand Down
23 changes: 23 additions & 0 deletions exampleplugin/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/opencontrol/compliance-masonry/lib"
"fmt"
)

type plugin struct {
lib.Workspace
}

func simpleDataExtractAndFormat(p plugin) string {
selectJustifications := p.GetJustification("standard", "control")
if len(selectJustifications) == 0 {
return "no data"
}
return selectJustifications[0].SatisfiesData.GetImplementationStatus()
}

func main() {
workspace, _ := lib.LoadData("sample opencontrol directory", "cert path")
fmt.Println(simpleDataExtractAndFormat(plugin{workspace}))
Copy link
Member

Choose a reason for hiding this comment

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

Minor: I'd split this to two lines so it's clearer what the return value of simpleDataExtractAndFormat() is.

Copy link
Member Author

Choose a reason for hiding this comment

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

will do!

}
33 changes: 33 additions & 0 deletions exampleplugin/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"testing"
"github.com/opencontrol/compliance-masonry/lib/mocks"
commonMocks"github.com/opencontrol/compliance-masonry/lib/common/mocks"
Copy link
Member

Choose a reason for hiding this comment

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

Needs a space (right?)

Copy link
Member Author

Choose a reason for hiding this comment

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

whoops! will fix

"github.com/stretchr/testify/assert"
"github.com/opencontrol/compliance-masonry/lib"
)

func TestSimpleDataExtractAndFormat(t *testing.T) {
// Test the case when there is no data

// create mock workspace
ws := new(mocks.Workspace)
ws.On("GetJustification", "standard", "control").Return(lib.Verifications{})
// test function expecting "no data"
p := plugin{ws}
data := simpleDataExtractAndFormat(p)
assert.Equal(t, data, "no data")

// Test the case when there is data.
Copy link
Member

Choose a reason for hiding this comment

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

This should be a separate test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do!


// create mock workspace
ws = new(mocks.Workspace)
satisfies := new(commonMocks.Satisfies)
satisfies.On("GetImplementationStatus").Return("IMPLEMENTED")
ws.On("GetJustification", "standard", "control").Return(lib.Verifications{lib.Verification{SatisfiesData: satisfies}})
// test function expecting "no data"
p = plugin{ws}
data = simpleDataExtractAndFormat(p)
assert.Equal(t, data, "IMPLEMENTED")
}
Loading