Skip to content

Commit 9477e39

Browse files
committed
fix(query): scope config insight selectors via view
Use config_analysis_items for resource selector searches so config insight queries can filter on parent config fields such as agent_id, deleted_at, tags, labels, and type. Extend the view with the parent config fields and cover config type filtering in resource selector tests.
1 parent 1ac4ce2 commit 9477e39

4 files changed

Lines changed: 34 additions & 10 deletions

File tree

query/config_analysis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func FindConfigAnalysisByResourceSelector(ctx context.Context, limit int, resour
1717
}
1818

1919
func FindConfigAnalysisIDsByResourceSelector(ctx context.Context, limit int, resourceSelectors ...types.ResourceSelector) ([]uuid.UUID, error) {
20-
return queryTableWithResourceSelectors(ctx, models.ConfigAnalysis{}.TableName(), limit, resourceSelectors...)
20+
return queryTableWithResourceSelectors(ctx, configAnalysisItemsView, limit, resourceSelectors...)
2121
}
2222

2323
func GetConfigAnalysisByIDs(ctx context.Context, ids []uuid.UUID) ([]models.ConfigAnalysis, error) {

query/models.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,25 +412,35 @@ var CanaryQueryModel = QueryModel{
412412
},
413413
}
414414

415-
// ConfigAnalysisQueryModel powers resource selector search for config insights
416-
// (the config_analysis table). The table has neither name/namespace nor
417-
// deleted_at/agent_id/tags/labels columns, so those features stay disabled.
415+
const configAnalysisItemsView = "config_analysis_items"
416+
417+
// ConfigAnalysisQueryModel powers resource selector search for config insights.
418+
// It queries config_analysis_items so config parent fields (agent, deleted_at,
419+
// type, tags, labels) are available like they are for catalog_changes.
418420
var ConfigAnalysisQueryModel = QueryModel{
419-
Table: models.ConfigAnalysis{}.TableName(),
421+
Table: configAnalysisItemsView,
420422
Columns: []string{
421423
"id", "config_id", "scraper_id", "source", "analyzer", "analysis_type",
422424
"severity", "status", "summary", "message", "first_observed", "last_observed",
425+
"name", "type", "config_type", "config_class", "agent_id", "deleted_at", "path",
423426
},
424-
JSONMapColumns: []string{"analysis"},
427+
JSONMapColumns: []string{"analysis", "tags", "labels", "config"},
425428
HasProperties: true,
429+
HasTags: true,
430+
HasLabels: true,
431+
HasAgents: true,
432+
HasDeletedAt: true,
426433
Aliases: map[string]string{
427-
"type": "analysis_type",
428434
"analyzer_type": "analysis_type",
429435
"config": "config_id",
436+
"config_type": "type",
437+
"namespace": "tags.namespace",
430438
},
431439
FieldMapper: map[string]func(ctx context.Context, id string) (any, error){
440+
"agent_id": AgentMapper,
432441
"first_observed": DateMapper,
433442
"last_observed": DateMapper,
443+
"deleted_at": DateMapper,
434444
},
435445
}
436446

@@ -454,7 +464,7 @@ func GetModelFromTable(table string) (QueryModel, error) {
454464
return ConfigItemSummaryQueryModel, nil
455465
case models.View{}.TableName():
456466
return ViewQueryModel, nil
457-
case models.ConfigAnalysis{}.TableName():
467+
case models.ConfigAnalysis{}.TableName(), configAnalysisItemsView:
458468
return ConfigAnalysisQueryModel, nil
459469
default:
460470
return QueryModel{}, fmt.Errorf("invalid table")

tests/query_resource_selector_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,15 @@ var _ = ginkgo.Describe("Config Analysis Resource Selector", func() {
752752
expectedIDs: []uuid.UUID{dummy.LogisticsDBRDSAnalysis.ID},
753753
},
754754
{
755-
description: "by analysis_type alias (type)",
756-
resourceSelector: types.ResourceSelector{Search: "type=security config_id=" + ec2ConfigID},
755+
description: "by analysis_type",
756+
resourceSelector: types.ResourceSelector{Search: "analysis_type=security config_id=" + ec2ConfigID},
757757
expectedIDs: []uuid.UUID{dummy.EC2InstanceBAnalysis.ID},
758758
},
759+
{
760+
description: "by config type",
761+
resourceSelector: types.ResourceSelector{Search: "type=" + *dummy.LogisticsDBRDS.Type + " config_id=" + logisticsConfigID},
762+
expectedIDs: []uuid.UUID{dummy.LogisticsDBRDSAnalysis.ID},
763+
},
759764
{
760765
description: "by severity",
761766
resourceSelector: types.ResourceSelector{Search: "severity=critical config_id=" + logisticsConfigID},

views/006_config_views.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,18 @@ FOR EACH ROW
595595

596596
DROP VIEW IF EXISTS config_analysis_items;
597597

598+
-- Used by resource selector search for config analysis / insights.
598599
CREATE OR REPLACE VIEW config_analysis_items AS
599600
SELECT
600601
ca.*,
602+
ci.name,
603+
ci.deleted_at,
604+
ci.type,
605+
ci.tags,
606+
ci.labels,
607+
ci.config,
608+
ci.agent_id,
609+
ci.path,
601610
ci.name as config_name,
602611
ci.type as config_type,
603612
ci.config_class

0 commit comments

Comments
 (0)