Skip to content

Commit

Permalink
Merge pull request #214 from opencontrol/move-standards-interface
Browse files Browse the repository at this point in the history
Move standards interface
  • Loading branch information
afeld committed Aug 19, 2016
2 parents cbe89a9 + 0e1d033 commit abd21ce
Show file tree
Hide file tree
Showing 19 changed files with 412 additions and 305 deletions.
10 changes: 5 additions & 5 deletions commands/diff/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
// Inventory maintains the inventory of all the controls within a given workspace.
type Inventory struct {
*lib.LocalWorkspace
masterControlList map[string]lib.Control
masterControlList map[string]common.Control
actualSatisfiedControls map[string]common.Satisfies
MissingControlList map[string]lib.Control
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 controlKey, control := range standard.GetControls() {
key := standardAndControlString(standardKey, controlKey)
if _, exists := i.masterControlList[key]; !exists {
i.masterControlList[key] = control
Expand Down Expand Up @@ -73,9 +73,9 @@ func ComputeGapAnalysis(config Config) (Inventory, []error) {
workspace, _ := lib.LoadData(config.OpencontrolDir, certificationPath)
i := Inventory{
LocalWorkspace: workspace,
masterControlList: make(map[string]lib.Control),
masterControlList: make(map[string]common.Control),
actualSatisfiedControls: make(map[string]common.Satisfies),
MissingControlList: make(map[string]lib.Control),
MissingControlList: make(map[string]common.Control),
}
if i.Certification == nil || i.Components == nil {
return Inventory{}, []error{fmt.Errorf("Unable to load data in %s for certification %s", config.OpencontrolDir, config.Certification)}
Expand Down
2 changes: 1 addition & 1 deletion commands/docs/gitbook/gitbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ComponentGitbook struct {
// 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
6 changes: 3 additions & 3 deletions commands/docs/gitbook/gitbookCertification.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ 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)
text := fmt.Sprintf("#%s\n##%s\n", key, control.GetName())
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 {
Expand Down Expand Up @@ -117,8 +117,8 @@ 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})
control := openControl.Standards.Get(standardKey).GetControl(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.Standards.Get(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
6 changes: 3 additions & 3 deletions commands/docs/gitbook/gitbookSummaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func (openControl *OpenControlGitBook) buildStandardsSummaries() (string, *map[s

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
control := openControl.Standards.Get(standardKey).GetControl(controlKey)
controlFamily := control.GetFamily()
controlName := control.GetName()
newFamily = standardKey + "-" + controlFamily
// create control family headings
if oldFamily != newFamily {
Expand Down
13 changes: 8 additions & 5 deletions lib/certifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (

"gopkg.in/yaml.v2"
"vbom.ml/util/sortorder"
v1 "github.com/opencontrol/compliance-masonry/lib/standards/versions/1_0_0"
"github.com/opencontrol/compliance-masonry/lib/common"
)

// Certification struct is a collection of specific standards and controls
// Schema info: https://github.com/opencontrol/schemas#certifications
type Certification struct {
Key string `yaml:"name" json:"name"`
Standards map[string]Standard `yaml:"standards" json:"standards"`
Standards map[string]v1.Standard `yaml:"standards" json:"standards"`
}

// GetSortedData returns a list of sorted standards
Expand All @@ -23,9 +25,10 @@ func (certification Certification) GetSortedData(callback func(string, string))
}
sort.Sort(sortorder.Natural(standardNames))
for _, standardKey := range standardNames {
certification.Standards[standardKey].GetSortedData(func(controlKey string) {
controlKeys := certification.Standards[standardKey].GetSortedControls()
for _, controlKey := range controlKeys {
callback(standardKey, controlKey)
})
}
}
}

Expand All @@ -35,11 +38,11 @@ func (ws *LocalWorkspace) LoadCertification(certificationFile string) error {
var certification Certification
certificationData, err := ioutil.ReadFile(certificationFile)
if err != nil {
return ErrReadFile
return common.ErrReadFile
}
err = yaml.Unmarshal(certificationData, &certification)
if err != nil {
return ErrCertificationSchema
return common.ErrCertificationSchema
}
ws.Certification = &certification
return nil
Expand Down
105 changes: 0 additions & 105 deletions lib/certifications_test.go

This file was deleted.

12 changes: 12 additions & 0 deletions lib/common/control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package common

// Control is the interface for getting all the attributes for a given control.
// Schema info: https://github.com/opencontrol/schemas#standards-documentation
//
// GetName returns the string representation of the control.
//
// GetFamily returns which family the control belongs to.
type Control interface {
GetName() string
GetFamily() string
}
9 changes: 7 additions & 2 deletions lib/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ var (
ErrUnknownSchemaVersion = errors.New("Unknown schema version")
// ErrCantParseSemver is thrown when the semantic versioning can not be parsed.
ErrCantParseSemver = errors.New("Can't parse semantic versioning of schema_version")
)

// ErrReadFile is raised when a file can not be read
ErrReadFile = errors.New("Unable to read the file")
// ErrCertificationSchema is raised a certification cannot be parsed
ErrCertificationSchema = errors.New("Unable to parse certification")
// ErrStandardSchema is raised a standard cannot be parsed
ErrStandardSchema = errors.New("Unable to parse standard")
)
71 changes: 71 additions & 0 deletions lib/common/mocks/Standard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package mocks

import "github.com/opencontrol/compliance-masonry/lib/common"
import "github.com/stretchr/testify/mock"

// Standard is an autogenerated mock type for the Standard type
type Standard struct {
mock.Mock
}

// GetControl provides a mock function with given fields: _a0
func (_m *Standard) GetControl(_a0 string) common.Control {
ret := _m.Called(_a0)

var r0 common.Control
if rf, ok := ret.Get(0).(func(string) common.Control); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(common.Control)
}
}

return r0
}

// GetControls provides a mock function with given fields:
func (_m *Standard) GetControls() map[string]common.Control {
ret := _m.Called()

var r0 map[string]common.Control
if rf, ok := ret.Get(0).(func() map[string]common.Control); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(map[string]common.Control)
}
}

return r0
}

// GetName provides a mock function with given fields:
func (_m *Standard) GetName() string {
ret := _m.Called()

var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}

return r0
}

// GetSortedControls provides a mock function with given fields:
func (_m *Standard) GetSortedControls() []string {
ret := _m.Called()

var r0 []string
if rf, ok := ret.Get(0).(func() []string); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]string)
}
}

return r0
}
18 changes: 18 additions & 0 deletions lib/common/standard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package common

// Standard is the container of all the information for a particular Standard.
// Schema info: https://github.com/opencontrol/schemas#standards-documentation
//
// GetName returns the name
//
// GetControls returns all controls associated with the standard
//
// GetControl returns a particular control
//
// GetSortedControls returns a list of sorted controls
type Standard interface {
GetName() string
GetControls() map[string]Control
GetControl(string) Control
GetSortedControls() []string
}

0 comments on commit abd21ce

Please sign in to comment.