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

fix: updates rules.Store memory to merged extracted rules and checks #47

Merged
merged 4 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
51 changes: 39 additions & 12 deletions rules/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,52 @@ func (m *MemoryStore) IndexAll(components []components.Component) error {
return fmt.Errorf("failed to index components: %w", ErrComponentsNotFound)
}
for _, component := range components {
extractedRules := m.indexComponent(component)

// Catalog information here at the component in the MemoryStore at the
// component level

componentTitle := component.Title()
extractedRules, extractedChecks := m.indexComponent(component)
if len(extractedRules) != 0 {
existingRules, ok := m.rulesByComponent[componentTitle]
if ok {
for rule := range existingRules {
extractedRules.Add(rule)
}
}
m.rulesByComponent[component.Title()] = extractedRules
}
existingRules, ok := m.rulesByComponent[componentTitle]
if ok {
for rule := range existingRules {
extractedRules.Add(rule)
}
}

if len(extractedChecks) != 0 {
existingChecks, ok := m.checksByValidationComponent[componentTitle]
if ok {
for check := range existingChecks {
extractedChecks.Add(check)
}
}
m.checksByValidationComponent[componentTitle] = extractedChecks
}
}
return nil
}

func (m *MemoryStore) indexComponent(component components.Component) set.Set[string] {
// indexComponent returns extracted rules and checks (respectively) from a given component.
func (m *MemoryStore) indexComponent(component components.Component) (set.Set[string], set.Set[string]) {
// Catalog all registered rules for all components and check implementations by validation component for filtering in
// `rules.FindByComponent`.
rules := set.New[string]()
checks := set.New[string]()

if len(component.Props()) == 0 {
return rules
return rules, checks
}

// Catalog all registered check implementations by validation component for filtering in
// `rules.FindByComponent`.
checkIds := set.New[string]()

// Each rule set is linked by a group id in the property remarks
byRemarks := groupPropsByRemarks(component.Props())
for _, propSet := range byRemarks {
Expand All @@ -95,7 +123,8 @@ func (m *MemoryStore) indexComponent(component components.Component) set.Set[str
ruleSet = extensions.RuleSet{}
}

// A check may or may not be registered.
// A check may or may not be registered depending on
// component.Type().
placeholderCheck := extensions.Check{}

for prop := range propSet {
Expand Down Expand Up @@ -130,15 +159,13 @@ func (m *MemoryStore) indexComponent(component components.Component) set.Set[str
if placeholderCheck.ID != "" {
ruleSet.Checks = append(ruleSet.Checks, placeholderCheck)
m.byCheck[placeholderCheck.ID] = ruleSet.Rule.ID
checks.Add(placeholderCheck.ID)
}
rules.Add(ruleSet.Rule.ID)
m.nodes[ruleSet.Rule.ID] = ruleSet
}
if len(checkIds) != 0 {
m.checksByValidationComponent[component.Title()] = checkIds
}

return rules
return rules, checks
}

func (m *MemoryStore) GetByRuleID(_ context.Context, ruleId string) (extensions.RuleSet, error) {
Expand Down
33 changes: 29 additions & 4 deletions rules/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func TestMemoryStore_FindByComponent(t *testing.T) {

validator1RuleSet, err := testMemory.FindByComponent(testCtx, "Validator")
require.NoError(t, err)
require.Len(t, validator1RuleSet, 1)
require.Contains(t, validator1RuleSet, expectedKeyFileRule)

validator2RuleSet, err := testMemory.FindByComponent(testCtx, "Validator2")
Expand All @@ -166,24 +167,48 @@ func TestMemoryStore_FindByComponent(t *testing.T) {

_, err = testMemory.FindByComponent(testCtx, "not_a_component")
require.EqualError(t, err, "failed to find rules for component \"not_a_component\"")

// Add a new target component for ="Validator"
testDataPath := "../testdata/component-definition-test2.json"
loadComponents(t, testMemory, testDataPath)

expectedExampleRule := extensions.RuleSet{
Rule: extensions.Rule{
ID: "example_rule_1",
Description: "Example rule 1 description",
},
Checks: []extensions.Check{
{
ID: "example_check_1",
Description: "Example check 1 description",
},
},
}

validator1RuleSet, err = testMemory.FindByComponent(testCtx, "Validator")
require.NoError(t, err)
require.Len(t, validator1RuleSet, 2)
require.Contains(t, validator1RuleSet, expectedExampleRule, expectedKeyFileRule)
}

func prepMemoryStore(t *testing.T) *MemoryStore {
testDataPath := "../testdata/component-definition-test.json"
testMemory := NewMemoryStore()
loadComponents(t, testMemory, testDataPath)
return testMemory
}

func loadComponents(t *testing.T, store *MemoryStore, testDataPath string) {
file, err := os.Open(testDataPath)
require.NoError(t, err)
definition, err := generators.NewComponentDefinition(file)
require.NoError(t, err)
testMemory := NewMemoryStore()

var comps []components.Component
for _, cp := range *definition.Components {
adapters := components.NewDefinedComponentAdapter(cp)
comps = append(comps, adapters)
}
err = testMemory.IndexAll(comps)
err = store.IndexAll(comps)
require.NoError(t, err)

return testMemory
}
93 changes: 93 additions & 0 deletions testdata/component-definition-test2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"component-definition": {
"uuid": "c14d8812-7098-4a9b-8f89-cba41b6ff0d8",
"metadata": {
"title": "Component definition for TestOS",
"last-modified": "2023-02-21T06:53:42+00:00",
"version": "1.1",
"oscal-version": "1.1.2"
},
"components": [
{
"uuid": "c8106bc8-5174-4e86-91a4-52f2fe0ed027",
"type": "service",
"title": "TestOS",
"description": "TestOS",
"props": [
{
"name": "Rule_Id",
"ns": "https://oscal-compass.github.io/compliance-trestle/schemas/oscal/cd",
"value": "example_rule_1",
"remarks": "rule_set_00"
},
{
"name": "Rule_Description",
"ns": "https://oscal-compass.github.io/compliance-trestle/schemas/oscal/cd",
"value": "Example rule 1 description",
"remarks": "rule_set_00"
}
],
"control-implementations": [
{
"uuid": "f79d6290-8efa-4ea7-b931-27b8435cf707",
"source": "profiles/cis/profile.json",
"description": "CIS Profile",
"implemented-requirements": [
{
"uuid": "a1b5b713-52c7-46fb-ab57-ebac7f576b23",
"control-id": "CIS-2.1",
"description": "",
"props": [
{
"name": "Rule_Id",
"ns": "https://oscal-compass.github.io/compliance-trestle/schemas/oscal/cd",
"value": "example_rule_1"
}
],
"statements": [
{
"statement-id": "CIS-2.1_smt",
"uuid": "cb9219b1-e51c-4680-abb0-616a43bbfbb2",
"description": ""
}
]
}
]
}
]
},
{
"uuid": "701c70f1-482b-42b0-a419-9870158cd9e2",
"type": "validation",
"title": "Validator",
"description": "An example validation component",
"props": [
{
"name": "Rule_Id",
"ns": "https://oscal-compass.github.io/compliance-trestle/schemas/oscal/cd",
"value": "example_rule_1",
"remarks": "rule_set_09"
},
{
"name": "Rule_Description",
"ns": "https://oscal-compass.github.io/compliance-trestle/schemas/oscal/cd",
"value": "Example rule 1 description",
"remarks": "rule_set_10"
},
{
"name": "Check_Id",
"ns": "https://oscal-compass.github.io/compliance-trestle/schemas/oscal/cd",
"value": "example_check_1",
"remarks": "rule_set_09"
},
{
"name": "Check_Description",
"ns": "https://oscal-compass.github.io/compliance-trestle/schemas/oscal/cd",
"value": "Example check 1 description",
"remarks": "rule_set_09"
}
]
}
]
}
}