Skip to content

Commit

Permalink
✨ Make querypack + query UIDs mandatory (#2545)
Browse files Browse the repository at this point in the history
During the initial release of cnquery we made it easy for users to write bundles. One of our decisions was to make `uid` fields optional for both querypacks and queries at that time.

However, in the meantime we have learned how important these fields are for exception-handling. Adding them on the fly will lead to changing UIDs. Even if we relied on MQL or metadata contents, it would easily cause auto-generated UIDs to change if these contents change.

We plan to add functionality to the linter and/or initial loading steps
to auto-fill UIDs on the fly - but at the same time modify the bundle
and write them back to disk. This helps new users while keeping the
field stable.

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored Nov 9, 2023
1 parent 3e95c6d commit e81f9d5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
3 changes: 3 additions & 0 deletions examples/os.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ packs:

queries:
- title: Find all SSH packages that are installed
uid: ssh-packages
mql: |
packages.
where(name == /ssh/)
- title: Get SSH services
uid: ssh-services
mql: |
services.
where(name == /ssh/)
- title: All the SSH config
uid: ssh-config
mql: |
sshd.config.params
37 changes: 25 additions & 12 deletions explorer/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func bundleFromSingleFile(path string) (*Bundle, error) {
func BundleFromYAML(data []byte) (*Bundle, error) {
var res Bundle
err := yaml.Unmarshal(data, &res)
res.EnsureUIDs()
res.DeprecatedV9_EnsureUIDs()
return &res, err
}

Expand Down Expand Up @@ -543,20 +543,33 @@ func (p *Bundle) FilterQueryPacks(IDs []string) bool {
return len(res) == 0
}

// Makes sure every query in the bundle and every query pack has a UID set,
// DeprecatedV9_EnsureUIDs makes sure every query in the bundle and every query pack has a UID set,
// IF the MRN is empty. Otherwise MRNs suffice.
func (p *Bundle) EnsureUIDs() {
// FIXME: DEPRECATED, remove in v10.0. Users must either set UIDs or MRNs
func (p *Bundle) DeprecatedV9_EnsureUIDs() {
for i := range p.Packs {
pack := p.Packs[i]
if pack.Mrn == "" && pack.Uid == "" {
pack.Uid = ksuid.New().String()
}
p.Packs[i].DeprecatedV9_ensureUIDs()
}
}

for j := range pack.Queries {
query := pack.Queries[j]
if query.Mrn == "" && query.Uid == "" {
query.Uid = ksuid.New().String()
}
// DeprecatedV9_ensureUIDs makes sure every query in this query pack has a UID set,
// IF the MRN is empty. Otherwise MRNs suffice.
// FIXME: DEPRECATED, remove in v10.0. Users must either set UIDs or MRNs
func (p *QueryPack) DeprecatedV9_ensureUIDs() {
if p.Mrn == "" && p.Uid == "" {
log.Warn().
Str("name", p.Name).
Msg("QueryPack is missing a UID in bundle. Please set one, by v10+ it will be mandatory to have it")
p.Uid = ksuid.New().String()
}

for j := range p.Queries {
query := p.Queries[j]
if query.Mrn == "" && query.Uid == "" {
log.Warn().
Str("title", query.Title).
Msg("Query is missing a UID in bundle. Please set one, by v10+ it will be mandatory to have it")
query.Uid = ksuid.New().String()
}
}
}
Expand Down

0 comments on commit e81f9d5

Please sign in to comment.