Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
123 changes: 73 additions & 50 deletions central/complianceoperator/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
pkgFramework "github.com/stackrox/rox/pkg/compliance/framework"
"github.com/stackrox/rox/pkg/complianceoperator/api/v1alpha1"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/pkg/postgres/pgutils"
"github.com/stackrox/rox/pkg/sac"
"github.com/stackrox/rox/pkg/set"
"github.com/stackrox/rox/pkg/sync"
Expand Down Expand Up @@ -76,6 +77,7 @@ func NewManager(registry *standards.Registry, profiles profileDatastore.DataStor
rules: rules,
results: results,
}
// Postgres retries in addProfileNoLock(...)
err := profiles.Walk(allAccessCtx, func(profile *storage.ComplianceOperatorProfile) error {
return mgr.addProfileNoLock(profile)
})
Expand Down Expand Up @@ -165,15 +167,19 @@ func (m *managerImpl) AddProfile(profile *storage.ComplianceOperatorProfile) err
}

func (m *managerImpl) addProfileNoLock(profile *storage.ComplianceOperatorProfile) error {
existingProfiles := []*storage.ComplianceOperatorProfile{
profile,
}
if err := m.profiles.Walk(allAccessCtx, func(existingProfile *storage.ComplianceOperatorProfile) error {
if existingProfile.GetClusterId() != profile.GetClusterId() && existingProfile.GetName() == profile.GetName() {
existingProfiles = append(existingProfiles, existingProfile)
var existingProfiles []*storage.ComplianceOperatorProfile
walkFn := func() error {
existingProfiles = []*storage.ComplianceOperatorProfile{
profile,
}
return nil
}); err != nil {
return m.profiles.Walk(allAccessCtx, func(existingProfile *storage.ComplianceOperatorProfile) error {
if existingProfile.GetClusterId() != profile.GetClusterId() && existingProfile.GetName() == profile.GetName() {
existingProfiles = append(existingProfiles, existingProfile)
}
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil {
return err
}

Expand Down Expand Up @@ -324,15 +330,19 @@ func (m *managerImpl) IsStandardActive(standardID string) bool {
}

var found bool
if err := m.scanSettingBindings.Walk(allAccessCtx, func(binding *storage.ComplianceOperatorScanSettingBinding) error {
for _, p := range binding.GetProfiles() {
if standardID == p.GetName() {
found = true
return errConditionMet
walkFn := func() error {
found = false
return m.scanSettingBindings.Walk(allAccessCtx, func(binding *storage.ComplianceOperatorScanSettingBinding) error {
for _, p := range binding.GetProfiles() {
if standardID == p.GetName() {
found = true
return errConditionMet
}
}
}
return nil
}); err != nil && err != errConditionMet {
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil && err != errConditionMet {
log.Errorf("error walking scan setting bindings datastore: %v", err)
return false
}
Expand All @@ -353,17 +363,21 @@ func (m *managerImpl) IsStandardActiveForCluster(standardID, clusterID string) b
}

var found bool
if err := m.scanSettingBindings.Walk(allAccessCtx, func(binding *storage.ComplianceOperatorScanSettingBinding) error {
if binding.GetClusterId() == clusterID {
for _, p := range binding.GetProfiles() {
if standardID == p.GetName() {
found = true
return errConditionMet
walkFn := func() error {
found = false
return m.scanSettingBindings.Walk(allAccessCtx, func(binding *storage.ComplianceOperatorScanSettingBinding) error {
if binding.GetClusterId() == clusterID {
for _, p := range binding.GetProfiles() {
if standardID == p.GetName() {
found = true
return errConditionMet
}
}
}
}
return nil
}); err != nil && err != errConditionMet {
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil && err != errConditionMet {
log.Errorf("error walking scan setting bindings datastore: %v", err)
return false
}
Expand All @@ -383,44 +397,53 @@ func (m *managerImpl) getRule(name string) (*storage.ComplianceOperatorRule, err

func (m *managerImpl) GetMachineConfigs(clusterID string) (map[string][]string, error) {
profileIDsToNames := make(map[string]string)
err := m.profiles.Walk(allAccessCtx, func(profile *storage.ComplianceOperatorProfile) error {
if profile.GetClusterId() == clusterID && profile.Annotations[v1alpha1.ProductTypeAnnotation] == string(v1alpha1.ScanTypeNode) {
profileIDsToNames[profile.GetProfileId()] = profile.GetName()
}
return nil
})
if err != nil {
walkFn := func() error {
profileIDsToNames = make(map[string]string)
return m.profiles.Walk(allAccessCtx, func(profile *storage.ComplianceOperatorProfile) error {
if profile.GetClusterId() == clusterID && profile.Annotations[v1alpha1.ProductTypeAnnotation] == string(v1alpha1.ScanTypeNode) {
profileIDsToNames[profile.GetProfileId()] = profile.GetName()
}
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil {
return nil, err
}

profilesToScan := make(map[string][]string)
err = m.scans.Walk(allAccessCtx, func(scan *storage.ComplianceOperatorScan) error {
if scan.GetClusterId() != clusterID {
walkFn = func() error {
profilesToScan = make(map[string][]string)
return m.scans.Walk(allAccessCtx, func(scan *storage.ComplianceOperatorScan) error {
if scan.GetClusterId() != clusterID {
return nil
}
if profileName, ok := profileIDsToNames[scan.GetProfileId()]; ok {
profilesToScan[profileName] = append(profilesToScan[profileName], scan.GetName())
}
return nil
}
if profileName, ok := profileIDsToNames[scan.GetProfileId()]; ok {
profilesToScan[profileName] = append(profilesToScan[profileName], scan.GetName())
}
return nil
})
if err != nil {
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil {
return nil, err
}
return profilesToScan, nil
}

func (m *managerImpl) findProfilesWithRuleNoLock(ruleName string) ([]*storage.ComplianceOperatorProfile, error) {
var profiles []*storage.ComplianceOperatorProfile
err := m.profiles.Walk(allAccessCtx, func(profile *storage.ComplianceOperatorProfile) error {
for _, rule := range profile.GetRules() {
if rule.GetName() == ruleName {
profiles = append(profiles, profile)
break
walkFn := func() error {
profiles = profiles[:0]
return m.profiles.Walk(allAccessCtx, func(profile *storage.ComplianceOperatorProfile) error {
for _, rule := range profile.GetRules() {
if rule.GetName() == ruleName {
profiles = append(profiles, profile)
break
}
}
}
return nil
})
if err != nil {
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil {
return nil, err
}
return profiles, nil
Expand Down
1 change: 1 addition & 0 deletions central/complianceoperator/profiles/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (d *datastoreImpl) Walk(ctx context.Context, fn func(result *storage.Compli
} else if !ok {
return errors.Wrap(sac.ErrResourceAccessDenied, "compliance operator profiles read")
}
// Postgres retry in caller.
return d.store.Walk(ctx, fn)
}

Expand Down
1 change: 1 addition & 0 deletions central/complianceoperator/rules/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (d *datastoreImpl) Walk(ctx context.Context, fn func(rule *storage.Complian
} else if !ok {
return errors.Wrap(sac.ErrResourceAccessDenied, "compliance operator rules read")
}
// Postgres retry in caller.
return d.store.Walk(ctx, fn)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (d *datastoreImpl) Walk(ctx context.Context, fn func(binding *storage.Compl
} else if !ok {
return errors.Wrap(sac.ErrResourceAccessDenied, "compliance operator scan setting bindings read")
}
// Postgres retry in caller.
return d.store.Walk(ctx, fn)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/metrics"
"github.com/stackrox/rox/pkg/postgres/pgutils"
"github.com/stackrox/rox/pkg/set"
)

Expand All @@ -35,13 +36,16 @@ type pipelineImpl struct {

func (s *pipelineImpl) Reconcile(ctx context.Context, clusterID string, storeMap *reconciliation.StoreMap) error {
existingIDs := set.NewStringSet()
err := s.datastore.Walk(ctx, func(profile *storage.ComplianceOperatorProfile) error {
if profile.GetClusterId() == clusterID {
existingIDs.Add(profile.GetId())
}
return nil
})
if err != nil {
walkFn := func() error {
existingIDs.Clear()
return s.datastore.Walk(ctx, func(profile *storage.ComplianceOperatorProfile) error {
if profile.GetClusterId() == clusterID {
existingIDs.Add(profile.GetId())
}
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil {
return err
}
store := storeMap.Get((*central.SensorEvent_ComplianceOperatorProfile)(nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/complianceoperator/api/v1alpha1"
"github.com/stackrox/rox/pkg/metrics"
"github.com/stackrox/rox/pkg/postgres/pgutils"
"github.com/stackrox/rox/pkg/set"
)

Expand All @@ -37,14 +38,16 @@ type pipelineImpl struct {

func (s *pipelineImpl) Reconcile(ctx context.Context, clusterID string, storeMap *reconciliation.StoreMap) error {
existingIDs := set.NewStringSet()

err := s.datastore.Walk(ctx, func(rule *storage.ComplianceOperatorRule) error {
if rule.GetClusterId() == clusterID {
existingIDs.Add(rule.GetId())
}
return nil
})
if err != nil {
walkFn := func() error {
existingIDs.Clear()
return s.datastore.Walk(ctx, func(rule *storage.ComplianceOperatorRule) error {
if rule.GetClusterId() == clusterID {
existingIDs.Add(rule.GetId())
}
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil {
return err
}
store := storeMap.Get((*central.SensorEvent_ComplianceOperatorRule)(nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/metrics"
"github.com/stackrox/rox/pkg/postgres/pgutils"
"github.com/stackrox/rox/pkg/set"
)

Expand All @@ -35,14 +36,16 @@ type pipelineImpl struct {

func (s *pipelineImpl) Reconcile(ctx context.Context, clusterID string, storeMap *reconciliation.StoreMap) error {
existingIDs := set.NewStringSet()

err := s.datastore.Walk(ctx, func(scan *storage.ComplianceOperatorScan) error {
if scan.GetClusterId() == clusterID {
existingIDs.Add(scan.GetId())
}
return nil
})
if err != nil {
walkFn := func() error {
existingIDs.Clear()
return s.datastore.Walk(ctx, func(scan *storage.ComplianceOperatorScan) error {
if scan.GetClusterId() == clusterID {
existingIDs.Add(scan.GetId())
}
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil {
return err
}
store := storeMap.Get((*central.SensorEvent_ComplianceOperatorScan)(nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/metrics"
"github.com/stackrox/rox/pkg/postgres/pgutils"
"github.com/stackrox/rox/pkg/set"
)

Expand All @@ -32,13 +33,16 @@ type pipelineImpl struct {

func (s *pipelineImpl) Reconcile(ctx context.Context, clusterID string, storeMap *reconciliation.StoreMap) error {
existingIDs := set.NewStringSet()
err := s.datastore.Walk(ctx, func(rule *storage.ComplianceOperatorScanSettingBinding) error {
if rule.GetClusterId() == clusterID {
existingIDs.Add(rule.GetId())
}
return nil
})
if err != nil {
walkFn := func() error {
existingIDs.Clear()
return s.datastore.Walk(ctx, func(rule *storage.ComplianceOperatorScanSettingBinding) error {
if rule.GetClusterId() == clusterID {
existingIDs.Add(rule.GetId())
}
return nil
})
}
if err := pgutils.RetryIfPostgres(walkFn); err != nil {
return err
}
store := storeMap.Get((*central.SensorEvent_ComplianceOperatorScanSettingBinding)(nil))
Expand Down