Skip to content

Commit bbada67

Browse files
authored
✨ extract property handler for bundles/packs + update examples (#5900)
1 parent ed86273 commit bbada67

File tree

6 files changed

+170
-147
lines changed

6 files changed

+170
-147
lines changed

examples/os.mql.yaml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
# SPDX-License-Identifier: BUSL-1.1
33

44
packs:
5-
- uid: linux-mixed-queries
6-
name: Linux Mixed Queries
7-
filters:
8-
- asset.family.contains("unix")
5+
- uid: linux-mixed-queries
6+
name: Linux Mixed Queries
7+
filters:
8+
- mql: asset.family.contains("unix")
99

10-
queries:
11-
- title: Find all SSH packages that are installed
12-
uid: ssh-packages
13-
mql: |
14-
packages.
15-
where(name == /ssh/)
16-
- title: Get SSH services
17-
uid: ssh-services
18-
mql: |
19-
services.
20-
where(name == /ssh/)
21-
- title: All the SSH config
22-
uid: ssh-config
23-
mql: |
24-
sshd.config.params
10+
queries:
11+
- title: Find all SSH packages that are installed
12+
uid: ssh-packages
13+
mql: |
14+
packages.
15+
where(name == /ssh/)
16+
- title: Get SSH services
17+
uid: ssh-services
18+
mql: |
19+
services.
20+
where(name == /ssh/)
21+
- title: All the SSH config
22+
uid: ssh-config
23+
mql: |
24+
sshd.config.params

explorer/bundle.go

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/rs/zerolog/log"
1717
"go.mondoo.com/cnquery/v12"
1818
"go.mondoo.com/cnquery/v12/checksums"
19-
"go.mondoo.com/cnquery/v12/llx"
2019
"go.mondoo.com/cnquery/v12/mqlc"
2120
"go.mondoo.com/cnquery/v12/mrn"
2221
"go.mondoo.com/cnquery/v12/providers-sdk/v1/resources"
@@ -402,9 +401,8 @@ func (c *bundleCache) error() error {
402401
}
403402

404403
func (c *bundleCache) compileQueries(queries []*Mquery, pack *QueryPack) error {
405-
propsCache := map[string]PropertyRef{}
406404
for i := range queries {
407-
c.precompileQuery(queries[i], pack, propsCache)
405+
c.precompileQuery(queries[i], pack)
408406
}
409407

410408
// After the first pass we may have errors. We try to collect as many errors
@@ -416,7 +414,7 @@ func (c *bundleCache) compileQueries(queries []*Mquery, pack *QueryPack) error {
416414
}
417415

418416
for i := range queries {
419-
c.compileQuery(queries[i], propsCache)
417+
c.compileQuery(queries[i])
420418
}
421419

422420
// The second pass on errors is done after we have compiled as much as possible.
@@ -427,7 +425,7 @@ func (c *bundleCache) compileQueries(queries []*Mquery, pack *QueryPack) error {
427425

428426
// precompileQuery indexes the query, turns UIDs into MRNs, compiles properties
429427
// and filters, and pre-processes variants. Also makes sure the query isn't nil.
430-
func (c *bundleCache) precompileQuery(query *Mquery, pack *QueryPack, propsCache map[string]PropertyRef) {
428+
func (c *bundleCache) precompileQuery(query *Mquery, pack *QueryPack) {
431429
if query == nil {
432430
c.errors = append(c.errors, errors.New("received null query"))
433431
return
@@ -458,14 +456,6 @@ func (c *bundleCache) precompileQuery(query *Mquery, pack *QueryPack, propsCache
458456
c.lookupQuery[query.Mrn] = query
459457
}
460458

461-
// ensure MRNs for properties
462-
for i := range query.Props {
463-
if err := c.compileProp(query.Mrn, query.Props[i], propsCache); err != nil {
464-
c.errors = append(c.errors, errors.New("failed to compile properties for query "+query.Mrn))
465-
return
466-
}
467-
}
468-
469459
// filters have no dependencies, so we can compile them early
470460
if err := query.Filters.Compile(c.ownerMrn, c.conf.CompilerConfig); err != nil {
471461
c.errors = append(c.errors, errors.New("failed to compile filters for query "+query.Mrn))
@@ -494,60 +484,25 @@ func (c *bundleCache) precompileQuery(query *Mquery, pack *QueryPack, propsCache
494484
}
495485
}
496486

497-
type propsHandler struct {
498-
queryCache map[string]PropertyRef
499-
packCache map[string]PropertyRef
500-
query *Mquery
501-
}
502-
503-
func (p *propsHandler) Available() map[string]*llx.Primitive {
504-
res := make(map[string]*llx.Primitive, len(p.queryCache))
505-
for k, v := range p.queryCache {
506-
res[k] = &llx.Primitive{Type: v.Property.Type}
507-
}
508-
return res
509-
}
510-
511-
func (p *propsHandler) All() map[string]*llx.Primitive {
512-
// note: this allocates possibly too much space, because we don't need as much
513-
// but the call is in errors and auto-complete, so it's rarer
514-
res := make(map[string]*llx.Primitive, len(p.queryCache)+len(p.packCache))
515-
for k, v := range p.packCache {
516-
res[k] = &llx.Primitive{Type: v.Property.Type}
517-
}
518-
for k, v := range p.queryCache {
519-
res[k] = &llx.Primitive{Type: v.Property.Type}
520-
}
521-
return res
522-
}
523-
524-
func (p *propsHandler) Get(name string) *llx.Primitive {
525-
// Property lookup happens on 2 layers:
526-
// 1. the property is part of the query definition, we are ll set
527-
// 2. the property is part of the pack definition, then we have to add it to the query
528-
ref, ok := p.queryCache[name]
529-
if ok {
530-
return &llx.Primitive{Type: ref.Property.Type}
531-
}
532-
533-
ref, ok = p.packCache[name]
534-
if !ok {
535-
return nil
536-
}
537-
p.query.Props = append(p.query.Props, ref.Property)
538-
return &llx.Primitive{Type: ref.Property.Type}
539-
}
540-
541487
// Note: you only want to run this, after you are sure that all connected
542488
// dependencies have been processed. Properties must be compiled. Connected
543489
// queries may not be ready yet, but we have to have precompiled them.
544-
func (c *bundleCache) compileQuery(query *Mquery, propsCache map[string]PropertyRef) {
545-
props := &propsHandler{
546-
queryCache: propsCache,
547-
packCache: c.lookupProps,
548-
query: query,
490+
func (c *bundleCache) compileQuery(query *Mquery) {
491+
props := &PropsResolver{
492+
QueryCache: map[string]PropertyRef{},
493+
GlobalCache: c.lookupProps,
494+
Query: query,
495+
}
496+
497+
// ensure MRNs for properties
498+
for i := range query.Props {
499+
if err := c.compileProp(query.Mrn, query.Props[i], props.QueryCache); err != nil {
500+
c.errors = append(c.errors, errors.New("failed to compile properties for query "+query.Mrn))
501+
return
502+
}
549503
}
550504

505+
// now that everything is updated, we can update checksums and compile
551506
_, err := query.RefreshChecksumAndType(c.lookupQuery, props, c.conf.CompilerConfig)
552507
if err != nil {
553508
if c.conf.RemoveFailing {

explorer/cnquery_explorer.pb.go

Lines changed: 63 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

explorer/cnquery_explorer.proto

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,6 @@ message Mquery {
161161
Action action = 41;
162162
}
163163

164-
enum ServerFeature {
165-
// Default value, should not be used
166-
SERVER_FEATURE_UNSPECIFIED = 0;
167-
// StoreResourcesData as structure recording data
168-
STORE_RESOURCES_DATA = 1;
169-
}
170-
171164
// protolint:disable ENUM_FIELD_NAMES_PREFIX
172165
// protolint:disable ENUM_FIELD_NAMES_ZERO_VALUE_END_WITH
173166
enum ScoringSystem {
@@ -380,6 +373,17 @@ message ResolveReq {
380373
repeated Mquery asset_filters = 2;
381374
}
382375

376+
// ServerFeature which tells us if the upstream endpoint can handle certain
377+
// requests and data. Usually proto handles missing fields etc really well,
378+
// but we are also trying to avoid work on the client or sending large
379+
// amounts of data to the server that aren't necessary.
380+
enum ServerFeature {
381+
// Default value, should not be used
382+
SERVER_FEATURE_UNSPECIFIED = 0;
383+
// StoreResourcesData as structure recording data
384+
STORE_RESOURCES_DATA = 1;
385+
}
386+
383387
// ResolvedPack is returned from a resolve request. It includes the execution
384388
// job with all things that need to be run.
385389
message ResolvedPack {

0 commit comments

Comments
 (0)