// compareAndAdd compares to see if the component exists in the map. If not, it adds the component.
// Returns true if the component was added, returns false if the component was not added.
// This function is thread-safe.
func (components *componentsMap) compareAndAdd(component common.Component) bool {
	components.Lock()
	defer components.Unlock()
	_, exists := components.mapping[component.GetKey()]
	if !exists {
		components.mapping[component.GetKey()] = component
		return true
	}
	return false
}
// Load will read the file at the given path and attempt to return a component object.
func Load(path string) (common.Component, error) {
	// Get file system assistance.
	fs := fs.OSUtil{}
	// Read the component file.
	fileName := filepath.Join(path, "component.yaml")
	componentData, err := fs.OpenAndReadFile(fileName)
	if err != nil {
		return nil, errors.New(constants.ErrComponentFileDNE)
	}
	// Parse the component.
	var component common.Component
	component, err = parseComponent(componentData, fileName)
	if err != nil {
		return nil, err
	}
	// Ensure we have a key for the component.
	if component.GetKey() == "" {
		component.SetKey(getKey(path))
	}
	return component, nil
}
Esempio n. 3
0
func parseComponent(componentData []byte, fileName string) (common.Component, error) {
	b := Base{}
	err := yaml.Unmarshal(componentData, &b)
	if err != nil {
		// If we have a human friendly BaseComponentParseError, return it.
		switch err.(type) {
		case BaseComponentParseError:
			return nil, err
		}
		// Otherwise, just return a generic error about the schema.
		return nil, fmt.Errorf("Unable to parse component %s. Error: %s", fileName, err.Error())
	}
	var component common.Component
	switch {
	case ComponentV2_0_0.EQ(b.SchemaVersion):
		c := new(v2.Component)
		err = yaml.Unmarshal(componentData, c)
		component = c
	case ComponentV3_0_0.EQ(b.SchemaVersion):
		c := new(v3.Component)
		err = yaml.Unmarshal(componentData, c)
		component = c
	case ComponentV3_1_0.EQ(b.SchemaVersion):
		c := new(v31.Component)
		err = yaml.Unmarshal(componentData, c)
		component = c
	default:
		return nil, common.ErrUnknownSchemaVersion

	}
	if err != nil {
		return nil, fmt.Errorf("Unable to parse component. Please check component.yaml schema for version %s\n"+
			"\tFile: %v\n\tParse error: %v", b.SchemaVersion.String(), fileName, err)
	}
	// Copy version from base because some versions of the component can not expect to parse directly into it's own struct
	// e.g. version 2.0.0 with 2.0 float
	component.SetVersion(b.SchemaVersion)
	return component, nil
}
func (openControl *OpenControlGitBook) getCoveredByVerification(text string, component common.Component, coveredBy common.CoveredBy) string {
	if component != nil {
		verification := component.GetVerifications().Get(coveredBy.VerificationKey)
		text += exportLink(
			fmt.Sprintf("%s - %s", component.GetName(), verification.Name),
			filepath.Join("..", "components", component.GetKey()+".md"),
		)
	}
	return text
}
func testSet(example common.Component, actual common.Component, t *testing.T) {
	// Check that the key was loaded
	if example.GetKey() != actual.GetKey() {
		t.Errorf("Expected %s, Actual: %s", example.GetKey(), actual.GetKey())
	}
	// Check that the name was loaded
	if example.GetName() != actual.GetName() {
		t.Errorf("Expected %s, Actual: %s", example.GetName(), actual.GetName())
	}
	// Check that the schema version was loaded
	if example.GetVersion().NE(actual.GetVersion()) {
		t.Errorf("Expected %v, Actual: %v", example.GetVersion(), actual.GetVersion())
	}
	// Check that the references were loaded
	if example.GetReferences().Len() != actual.GetReferences().Len() {
		t.Errorf("Expected %d, Actual: %d", example.GetReferences().Len(), actual.GetReferences().Len())
	}
	// Check that the satisfies data was loaded
	if len(example.GetAllSatisfies()) != len(actual.GetAllSatisfies()) {
		t.Errorf("Expected %d, Actual: %d", len(example.GetAllSatisfies()), len(actual.GetAllSatisfies()))
	}
	// Check Narratives and Parameters.
	for idx, _ := range actual.GetAllSatisfies() {
		assert.Equal(t, (example.GetAllSatisfies())[idx].GetNarratives(), (actual.GetAllSatisfies())[idx].GetNarratives())
		assert.Equal(t, (example.GetAllSatisfies())[idx].GetParameters(), (actual.GetAllSatisfies())[idx].GetParameters())
		assert.Equal(t, (example.GetAllSatisfies())[idx].GetControlOrigin(), (actual.GetAllSatisfies())[idx].GetControlOrigin())
		assert.Equal(t, (example.GetAllSatisfies())[idx].GetControlOrigins(), (actual.GetAllSatisfies())[idx].GetControlOrigins())
		assert.Equal(t, (example.GetAllSatisfies())[idx].GetImplementationStatus(), (actual.GetAllSatisfies())[idx].GetImplementationStatus())
		assert.Equal(t, (example.GetAllSatisfies())[idx].GetImplementationStatuses(), (actual.GetAllSatisfies())[idx].GetImplementationStatuses())
	}
	// Check the responsible role.
	assert.Equal(t, example.GetResponsibleRole(), actual.GetResponsibleRole())
	// Check that the verifications were loaded
	if example.GetVerifications().Len() != actual.GetVerifications().Len() {
		t.Errorf("Expected %d, Actual: %d", example.GetVerifications().Len(), actual.GetVerifications())
	}
}
// add adds a new component to the component map
func (components *componentsMap) add(component common.Component) {
	components.Lock()
	components.mapping[component.GetKey()] = component
	components.Unlock()
}
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
}
// LoadMappings loads a set of mappings from a component
func (justifications *Justifications) LoadMappings(component common.Component) {
	for _, satisfies := range component.GetAllSatisfies() {
		justifications.Add(satisfies.GetStandardKey(), satisfies.GetControlKey(), component.GetKey(), satisfies)
	}
}