mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
Fix server URL for hosts enrolled in Fleet MDM (#9952)
This commit is contained in:
parent
e18561fdc9
commit
fa695cef34
1
changes/issue-9898-fleet-mdm-server-url
Normal file
1
changes/issue-9898-fleet-mdm-server-url
Normal file
@ -0,0 +1 @@
|
||||
* Fixed how the Fleet MDM server URL is generated when stored for hosts enrolled in Fleet MDM.
|
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
||||
"github.com/fleetdm/fleet/v4/server/fleet"
|
||||
apple_mdm "github.com/fleetdm/fleet/v4/server/mdm/apple"
|
||||
"github.com/fleetdm/fleet/v4/server/ptr"
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/go-kit/kit/log/level"
|
||||
@ -437,7 +438,7 @@ func updateMDMAppleHostDB(
|
||||
return ctxerr.Wrap(ctx, err, "update mdm apple host")
|
||||
}
|
||||
|
||||
if err := upsertMDMAppleHostMDMInfoDB(ctx, tx, appCfg.ServerSettings.ServerURL, false, hostID); err != nil {
|
||||
if err := upsertMDMAppleHostMDMInfoDB(ctx, tx, appCfg.ServerSettings, false, hostID); err != nil {
|
||||
return ctxerr.Wrap(ctx, err, "ingest mdm apple host upsert MDM info")
|
||||
}
|
||||
|
||||
@ -496,7 +497,7 @@ func insertMDMAppleHostDB(
|
||||
return ctxerr.Wrap(ctx, err, "ingest mdm apple host upsert label membership")
|
||||
}
|
||||
|
||||
if err := upsertMDMAppleHostMDMInfoDB(ctx, tx, appCfg.ServerSettings.ServerURL, false, host.ID); err != nil {
|
||||
if err := upsertMDMAppleHostMDMInfoDB(ctx, tx, appCfg.ServerSettings, false, host.ID); err != nil {
|
||||
return ctxerr.Wrap(ctx, err, "ingest mdm apple host upsert MDM info")
|
||||
}
|
||||
return nil
|
||||
@ -611,7 +612,7 @@ func (ds *Datastore) IngestMDMAppleDevicesFromDEPSync(ctx context.Context, devic
|
||||
for _, h := range hosts {
|
||||
ids = append(ids, h.ID)
|
||||
}
|
||||
if err := upsertMDMAppleHostMDMInfoDB(ctx, tx, appCfg.ServerSettings.ServerURL, true, ids...); err != nil {
|
||||
if err := upsertMDMAppleHostMDMInfoDB(ctx, tx, appCfg.ServerSettings, true, ids...); err != nil {
|
||||
return ctxerr.Wrap(ctx, err, "ingest mdm apple host upsert MDM info")
|
||||
}
|
||||
|
||||
@ -640,7 +641,12 @@ func upsertMDMAppleHostDisplayNamesDB(ctx context.Context, tx sqlx.ExtContext, h
|
||||
return nil
|
||||
}
|
||||
|
||||
func upsertMDMAppleHostMDMInfoDB(ctx context.Context, tx sqlx.ExtContext, serverURL string, fromSync bool, hostIDs ...uint) error {
|
||||
func upsertMDMAppleHostMDMInfoDB(ctx context.Context, tx sqlx.ExtContext, serverSettings fleet.ServerSettings, fromSync bool, hostIDs ...uint) error {
|
||||
serverURL, err := apple_mdm.ResolveAppleMDMURL(serverSettings.ServerURL)
|
||||
if err != nil {
|
||||
return ctxerr.Wrap(ctx, err, "resolve Fleet MDM URL")
|
||||
}
|
||||
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO mobile_device_management_solutions (name, server_url) VALUES (?, ?)
|
||||
ON DUPLICATE KEY UPDATE server_url = VALUES(server_url)`,
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/fleetdm/fleet/v4/server/fleet"
|
||||
apple_mdm "github.com/fleetdm/fleet/v4/server/mdm/apple"
|
||||
"github.com/fleetdm/fleet/v4/server/ptr"
|
||||
"github.com/fleetdm/fleet/v4/server/test"
|
||||
"github.com/jmoiron/sqlx"
|
||||
@ -630,14 +631,16 @@ func checkMDMHostRelatedTables(t *testing.T, ds *Datastore, hostID uint, expecte
|
||||
err = sqlx.GetContext(context.Background(), ds.reader, &hmdm, `SELECT host_id, server_url, mdm_id FROM host_mdm WHERE host_id = ?`, hostID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, hostID, hmdm.HostID)
|
||||
require.Equal(t, appCfg.ServerSettings.ServerURL, hmdm.ServerURL)
|
||||
serverURL, err := apple_mdm.ResolveAppleMDMURL(appCfg.ServerSettings.ServerURL)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, serverURL, hmdm.ServerURL)
|
||||
require.NotEmpty(t, hmdm.MDMID)
|
||||
|
||||
var mdmSolution fleet.MDMSolution
|
||||
err = sqlx.GetContext(context.Background(), ds.reader, &mdmSolution, `SELECT name, server_url FROM mobile_device_management_solutions WHERE id = ?`, hmdm.MDMID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, fleet.WellKnownMDMFleet, mdmSolution.Name)
|
||||
require.Equal(t, appCfg.ServerSettings.ServerURL, mdmSolution.ServerURL)
|
||||
require.Equal(t, serverURL, mdmSolution.ServerURL)
|
||||
}
|
||||
|
||||
// createBuiltinLabels creates entries for "All Hosts" and "macOS" labels, which are assumed to be
|
||||
|
@ -90,6 +90,8 @@ func TestSanitizeColumn(t *testing.T) {
|
||||
{"foobar*baz", "`foobarbaz`"},
|
||||
{"....", ""},
|
||||
{"h.id", "`h`.`id`"},
|
||||
{"id;delete from hosts", "`iddeletefromhosts`"},
|
||||
{"select * from foo", "`selectfromfoo`"},
|
||||
}
|
||||
|
||||
for _, tt := range testCases {
|
||||
|
Loading…
Reference in New Issue
Block a user