Skip to content

Commit

Permalink
fix: merge commits and cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: George Vauter <[email protected]>
  • Loading branch information
gvauter committed Jan 30, 2025
2 parents 16493a5 + 92b4e11 commit 85567c7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
23 changes: 13 additions & 10 deletions framework/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hashicorp/go-hclog"
"github.com/oscal-compass/oscal-sdk-go/rules"
"github.com/oscal-compass/oscal-sdk-go/settings"

"github.com/oscal-compass/compliance-to-policy-go/v2/framework/config"
"github.com/oscal-compass/compliance-to-policy-go/v2/plugin"
Expand Down Expand Up @@ -101,29 +102,31 @@ func (m *PluginManager) LaunchPolicyPlugins() (map[string]policy.Provider, error
}

// GeneratePolicy identifies policy configuration for each provider in the given pluginSet to execute the Generate() method
// each policy.Provider.
func (m *PluginManager) GeneratePolicy(ctx context.Context, pluginSet map[string]policy.Provider) error {
// each policy.Provider. The rule set passed to each plugin can be configured with compliance specific settings with the
// complianceSettings input.
func (m *PluginManager) GeneratePolicy(ctx context.Context, pluginSet map[string]policy.Provider, complianceSettings settings.Settings) error {
for providerId, policyPlugin := range pluginSet {
componentTitle, ok := m.pluginIdMap[providerId]
if !ok {
return fmt.Errorf("missing title for provider %s", providerId)
}
m.log.Debug(fmt.Sprintf("Generating policy for provider %s", providerId))

ruleSets, err := m.rulesStore.FindByComponent(ctx, componentTitle)
appliedRuleSet, err := settings.ApplyToComponent(ctx, componentTitle, m.rulesStore, complianceSettings)
if err != nil {
return err
return fmt.Errorf("failed to get rule sets for component %s: %w", componentTitle, err)
}
if err := policyPlugin.Generate(ruleSets); err != nil {
if err := policyPlugin.Generate(appliedRuleSet); err != nil {
return fmt.Errorf("plugin %s: %w", providerId, err)
}
}
return nil
}

// AggregateResults identifies policy configuration for each provider in the given pluginSet to execute the GetResults() method
// each policy.Provider.
func (m *PluginManager) AggregateResults(ctx context.Context, pluginSet map[string]policy.Provider) ([]policy.PVPResult, error) {
// each policy.Provider. The rule set passed to each plugin can be configured with compliance specific settings with the
// // complianceSettings input.
func (m *PluginManager) AggregateResults(ctx context.Context, pluginSet map[string]policy.Provider, complianceSettings settings.Settings) ([]policy.PVPResult, error) {
var allResults []policy.PVPResult
for providerId, policyPlugin := range pluginSet {
// get the provider ids here to grab the policy
Expand All @@ -132,12 +135,12 @@ func (m *PluginManager) AggregateResults(ctx context.Context, pluginSet map[stri
return allResults, fmt.Errorf("missing title for provider %s", providerId)
}
m.log.Debug(fmt.Sprintf("Aggregating results for provider %s", providerId))
ruleSets, err := m.rulesStore.FindByComponent(ctx, componentTitle)
appliedRuleSet, err := settings.ApplyToComponent(ctx, componentTitle, m.rulesStore, complianceSettings)
if err != nil {
return allResults, err
return allResults, fmt.Errorf("failed to get rule sets for component %s: %w", componentTitle, err)
}

pluginResults, err := policyPlugin.GetResults(ruleSets)
pluginResults, err := policyPlugin.GetResults(appliedRuleSet)
if err != nil {
return allResults, fmt.Errorf("plugin %s: %w", providerId, err)
}
Expand Down
26 changes: 20 additions & 6 deletions framework/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import (
oscalTypes "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/oscal-compass/oscal-sdk-go/extensions"
"github.com/oscal-compass/oscal-sdk-go/generators"
"github.com/oscal-compass/oscal-sdk-go/settings"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/oscal-compass/compliance-to-policy-go/v2/framework/config"
"github.com/oscal-compass/compliance-to-policy-go/v2/policy"
)

const testDataPath = "../test/testdata/component-definition-test.json"

var (
expectedCertFileRule = extensions.RuleSet{
Rule: extensions.Rule{
Expand Down Expand Up @@ -77,11 +76,14 @@ func TestPluginManager_GeneratePolicy(t *testing.T) {

// Create pluginSet
providerTestObj := new(policyProvider)
providerTestObj.On("Generate", policy.Policy{expectedKeyFileRule, expectedCertFileRule}).Return(nil)
providerTestObj.On("Generate", policy.Policy{expectedCertFileRule}).Return(nil)
pluginSet := map[string]policy.Provider{
"mypvpvalidator": providerTestObj,
}
err = pluginManager.GeneratePolicy(context.TODO(), pluginSet)

testSettings := settings.NewSettings(map[string]struct{}{"etcd_cert_file": {}}, map[string]string{})

err = pluginManager.GeneratePolicy(context.TODO(), pluginSet, testSettings)
require.NoError(t, err)
providerTestObj.AssertExpectations(t)
}
Expand All @@ -101,13 +103,25 @@ func TestPluginManager_AggregateResults(t *testing.T) {
},
}

updatedParam := &extensions.Parameter{
ID: "file_name",
Description: "A parameter for a file name",
Value: "my_file",
}

updatedKeyFileRule := expectedKeyFileRule
updatedKeyFileRule.Rule.Parameter = updatedParam

// Create pluginSet
providerTestObj := new(policyProvider)
providerTestObj.On("GetResults", policy.Policy{expectedKeyFileRule, expectedCertFileRule}).Return(wantResults, nil)
providerTestObj.On("GetResults", policy.Policy{updatedKeyFileRule}).Return(wantResults, nil)
pluginSet := map[string]policy.Provider{
"mypvpvalidator": providerTestObj,
}
gotResults, err := pluginManager.AggregateResults(context.TODO(), pluginSet)

testSettings := settings.NewSettings(map[string]struct{}{"etcd_key_file": {}}, map[string]string{"file_name": "my_file"})

gotResults, err := pluginManager.AggregateResults(context.TODO(), pluginSet, testSettings)
require.NoError(t, err)
providerTestObj.AssertExpectations(t)
require.Len(t, gotResults, 1)
Expand Down
10 changes: 5 additions & 5 deletions framework/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func (r *Reporter) generateFindings(findings []oscalTypes.Finding, observation o
return findings, err
}

for _, controlId := range applicableControls {
for _, control := range applicableControls {

targetId := fmt.Sprintf("%s_smt", controlId)
targetId := fmt.Sprintf("%s_smt", control.ControlId)

finding := r.getFindingForTarget(findings, targetId)

if finding == nil { // if an empty finding was returned, populate initial values and append
if finding == nil { // if an empty finding was returned, create a new one and append to findings
newFinding := oscalTypes.Finding{
UUID: uuid.NewUUID(),
RelatedObservations: &[]oscalTypes.RelatedObservation{
Expand All @@ -107,7 +107,7 @@ func (r *Reporter) generateFindings(findings []oscalTypes.Finding, observation o
relObs := oscalTypes.RelatedObservation{
ObservationUuid: observation.UUID,
}
*finding.RelatedObservations = append(*finding.RelatedObservations, relObs)
*finding.RelatedObservations = append(*finding.RelatedObservations, relObs) // add new related obs to existing finding for targetId
}
}

Expand Down Expand Up @@ -209,7 +209,7 @@ func (r *Reporter) GenerateAssessmentResults(ctx context.Context, planHref strin
opt(&options)
}

r.log.Info(fmt.Sprintf("Generating assessments results for plan %s", planHref))
r.log.Info(fmt.Sprintf("generating assessments results for plan %s", planHref))

importAp := oscalTypes.ImportAp{
Href: planHref,
Expand Down
2 changes: 2 additions & 0 deletions framework/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package framework
import (
"context"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -53,6 +54,7 @@ var (
},
},
}
testDataPath = filepath.Join("../test/testdata", "component-definition-test.json")
)

func TestReporter_GenereateAssessmentResults(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,4 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

replace github.com/oscal-compass/oscal-sdk-go => github.com/jpower432/oscal-sdk-go v0.0.0-20250122232134-3f0661c3946b
replace github.com/oscal-compass/oscal-sdk-go => github.com/complytime/oscal-sdk-go v0.0.0-20250125144051-7723a7b6b06b
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUo
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
github.com/complytime/oscal-sdk-go v0.0.0-20250125144051-7723a7b6b06b h1:F3tqX70f1WSdGrOnSFlc0AU/QiGxBKw6MfmC/ZX0wdE=
github.com/complytime/oscal-sdk-go v0.0.0-20250125144051-7723a7b6b06b/go.mod h1:O922y+Za9/ez0jKTR3/97BilPnrjabtFJ6rCVhHk37s=
github.com/containerd/stargz-snapshotter/estargz v0.15.1 h1:eXJjw9RbkLFgioVaTG+G/ZW/0kEe2oEKCdS/ZxIyoCU=
github.com/containerd/stargz-snapshotter/estargz v0.15.1/go.mod h1:gr2RNwukQ/S9Nv33Lt6UC7xEx58C+LHRdoqbEKjz1Kk=
github.com/coreos/go-oidc/v3 v3.10.0 h1:tDnXHnLyiTVyT/2zLDGj09pFPkhND8Gl8lnTRhoEaJU=
Expand Down Expand Up @@ -490,8 +492,6 @@ github.com/jmhodges/clock v1.2.0 h1:eq4kys+NI0PLngzaHEe7AmPT90XMGIEySD1JfV1PDIs=
github.com/jmhodges/clock v1.2.0/go.mod h1:qKjhA7x7u/lQpPB1XAqX1b1lCI/w3/fNuYpI/ZjLynI=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpower432/oscal-sdk-go v0.0.0-20250122232134-3f0661c3946b h1:blT482UCu7rbakdxnpplqfOqEFnSjVJrdtI/4beqP78=
github.com/jpower432/oscal-sdk-go v0.0.0-20250122232134-3f0661c3946b/go.mod h1:O922y+Za9/ez0jKTR3/97BilPnrjabtFJ6rCVhHk37s=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand Down

0 comments on commit 85567c7

Please sign in to comment.