Replace into software to update empty bundle_identifier entries (#2676)

This commit is contained in:
Lucas Manuel Rodriguez
2021-10-26 09:48:25 -03:00
committed by GitHub
parent 739a1d8f8d
commit 3136cc105e
5 changed files with 43 additions and 6 deletions
@@ -0,0 +1 @@
* Populate empty `bundle_identifier`s on macOS software inventory from < 4.4.0 deployments.
@@ -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
}
+3 -3
View File
@@ -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 {
+31
View File
@@ -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) {
+7 -2
View File
@@ -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)
}