From 3136cc105e27a84ef1d1a24924f83c4ea060e899 Mon Sep 17 00:00:00 2001 From: Lucas Manuel Rodriguez Date: Tue, 26 Oct 2021 09:48:25 -0300 Subject: [PATCH] Replace into software to update empty bundle_identifier entries (#2676) --- ...sue-2657-populate-bundle-identifiers-macos | 1 + ...0210927143116_AddBundleIdentifierColumn.go | 2 +- server/datastore/mysql/software.go | 6 ++-- server/datastore/mysql/software_test.go | 31 +++++++++++++++++++ server/service/osquery_utils/queries.go | 9 ++++-- 5 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 changes/issue-2657-populate-bundle-identifiers-macos diff --git a/changes/issue-2657-populate-bundle-identifiers-macos b/changes/issue-2657-populate-bundle-identifiers-macos new file mode 100644 index 0000000000..f7187b6b88 --- /dev/null +++ b/changes/issue-2657-populate-bundle-identifiers-macos @@ -0,0 +1 @@ +* Populate empty `bundle_identifier`s on macOS software inventory from < 4.4.0 deployments. diff --git a/server/datastore/mysql/migrations/tables/20210927143116_AddBundleIdentifierColumn.go b/server/datastore/mysql/migrations/tables/20210927143116_AddBundleIdentifierColumn.go index d2c34eb301..f4019655d0 100644 --- a/server/datastore/mysql/migrations/tables/20210927143116_AddBundleIdentifierColumn.go +++ b/server/datastore/mysql/migrations/tables/20210927143116_AddBundleIdentifierColumn.go @@ -29,7 +29,7 @@ func Up_20210927143116(tx *sql.Tx) error { } if _, err := tx.Exec(`ALTER TABLE software ADD COLUMN bundle_identifier VARCHAR(255) DEFAULT ''`); err != nil { - return errors.Wrap(err, "add column team_id") + return errors.Wrap(err, "add column bundle_identifier") } return nil } diff --git a/server/datastore/mysql/software.go b/server/datastore/mysql/software.go index 0e2a361475..614461679e 100644 --- a/server/datastore/mysql/software.go +++ b/server/datastore/mysql/software.go @@ -149,8 +149,8 @@ func getOrGenerateSoftwareIdDB(ctx context.Context, tx sqlx.ExtContext, s fleet. var existingId []int64 if err := sqlx.SelectContext(ctx, tx, &existingId, - `SELECT id FROM software WHERE name = ? and version = ? and source = ?`, - s.Name, s.Version, s.Source, + `SELECT id FROM software WHERE name = ? AND version = ? AND source = ? AND bundle_identifier = ?`, + s.Name, s.Version, s.Source, s.BundleIdentifier, ); err != nil { return 0, err } @@ -159,7 +159,7 @@ func getOrGenerateSoftwareIdDB(ctx context.Context, tx sqlx.ExtContext, s fleet. } result, err := tx.ExecContext(ctx, - `INSERT IGNORE INTO software (name, version, source, bundle_identifier) VALUES (?, ?, ?, ?)`, + `REPLACE INTO software (name, version, source, bundle_identifier) VALUES (?, ?, ?, ?)`, s.Name, s.Version, s.Source, s.BundleIdentifier, ) if err != nil { diff --git a/server/datastore/mysql/software_test.go b/server/datastore/mysql/software_test.go index 131f89b33a..a960f75115 100644 --- a/server/datastore/mysql/software_test.go +++ b/server/datastore/mysql/software_test.go @@ -59,6 +59,7 @@ func testSoftwareSaveHost(t *testing.T, ds *Datastore) { {Name: "foo", Version: "0.0.2", Source: "chrome_extensions"}, {Name: "foo", Version: "0.0.3", Source: "chrome_extensions"}, {Name: "bar", Version: "0.0.3", Source: "deb_packages", BundleIdentifier: "com.some.identifier"}, + {Name: "zoo", Version: "0.0.5", Source: "deb_packages", BundleIdentifier: ""}, }, } host2.HostSoftware = soft2 @@ -119,6 +120,36 @@ func testSoftwareSaveHost(t *testing.T, ds *Datastore) { require.NoError(t, ds.LoadHostSoftware(context.Background(), host1)) assert.False(t, host1.HostSoftware.Modified) test.ElementsMatchSkipID(t, soft1.Software, host1.HostSoftware.Software) + + soft2 = fleet.HostSoftware{ + Modified: true, + Software: []fleet.Software{ + {Name: "foo", Version: "0.0.2", Source: "chrome_extensions"}, + {Name: "foo", Version: "0.0.3", Source: "chrome_extensions"}, + {Name: "bar", Version: "0.0.3", Source: "deb_packages", BundleIdentifier: "com.some.identifier"}, + {Name: "zoo", Version: "0.0.5", Source: "deb_packages", BundleIdentifier: "com.zoo"}, // "empty" -> "non-empty" + }, + } + host2.HostSoftware = soft2 + require.NoError(t, ds.SaveHostSoftware(context.Background(), host2)) + require.NoError(t, ds.LoadHostSoftware(context.Background(), host2)) + assert.False(t, host2.HostSoftware.Modified) + test.ElementsMatchSkipID(t, soft2.Software, host2.HostSoftware.Software) + + soft2 = fleet.HostSoftware{ + Modified: true, + Software: []fleet.Software{ + {Name: "foo", Version: "0.0.2", Source: "chrome_extensions"}, + {Name: "foo", Version: "0.0.3", Source: "chrome_extensions"}, + {Name: "bar", Version: "0.0.3", Source: "deb_packages", BundleIdentifier: "com.some.other"}, // "non-empty" -> "non-empty" + {Name: "zoo", Version: "0.0.5", Source: "deb_packages", BundleIdentifier: ""}, // non-empty -> empty + }, + } + host2.HostSoftware = soft2 + require.NoError(t, ds.SaveHostSoftware(context.Background(), host2)) + require.NoError(t, ds.LoadHostSoftware(context.Background(), host2)) + assert.False(t, host2.HostSoftware.Modified) + test.ElementsMatchSkipID(t, soft2.Software, host2.HostSoftware.Software) } func testSoftwareCPE(t *testing.T, ds *Datastore) { diff --git a/server/service/osquery_utils/queries.go b/server/service/osquery_utils/queries.go index 92aae7ad41..3573e428ab 100644 --- a/server/service/osquery_utils/queries.go +++ b/server/service/osquery_utils/queries.go @@ -67,7 +67,7 @@ var detailQueries = map[string]DetailQuery{ } if strings.Contains(row["address"], ":") { - //IPv6 + // IPv6 if firstIPv6 == nil { firstIPv6 = row } @@ -570,7 +570,12 @@ func ingestSoftware(logger log.Logger, host *fleet.Host, rows []map[string]strin ) continue } - s := fleet.Software{Name: name, Version: version, Source: source, BundleIdentifier: bundleIdentifier} + s := fleet.Software{ + Name: name, + Version: version, + Source: source, + BundleIdentifier: bundleIdentifier, + } software.Software = append(software.Software, s) }