Skip to content
Merged
36 changes: 26 additions & 10 deletions server/datastore/mysql/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,8 @@ func (ds *Datastore) NewMDMAndroidConfigProfile(ctx context.Context, cp fleet.MD
profileUUID := fleet.MDMAndroidProfileUUIDPrefix + uuid.New().String()
insertProfileStmt := `
INSERT INTO
mdm_android_configuration_profiles (profile_uuid, team_id, name, raw_json, uploaded_at)
(SELECT ?, ?, ?, ?, CURRENT_TIMESTAMP() FROM DUAL WHERE
mdm_android_configuration_profiles (profile_uuid, team_id, name, description, raw_json, uploaded_at)
(SELECT ?, ?, ?, ?, ?, CURRENT_TIMESTAMP() FROM DUAL WHERE
NOT EXISTS (
SELECT 1 FROM mdm_apple_configuration_profiles WHERE name = ? AND team_id = ?
) AND NOT EXISTS (
Expand All @@ -856,7 +856,7 @@ INSERT INTO
}

err := ds.withTx(ctx, func(tx sqlx.ExtContext) error {
res, err := tx.ExecContext(ctx, insertProfileStmt, profileUUID, teamID, cp.Name, cp.RawJSON, cp.Name, teamID, cp.Name, teamID, cp.Name, teamID)
res, err := tx.ExecContext(ctx, insertProfileStmt, profileUUID, teamID, cp.Name, cp.Description, cp.RawJSON, cp.Name, teamID, cp.Name, teamID, cp.Name, teamID)
if err != nil {
switch {
case IsDuplicate(err):
Expand Down Expand Up @@ -920,13 +920,14 @@ INSERT INTO
return &fleet.MDMAndroidConfigProfile{
ProfileUUID: profileUUID,
Name: cp.Name,
Description: cp.Description,
RawJSON: cp.RawJSON,
TeamID: cp.TeamID,
}, nil
}

func (ds *Datastore) GetMDMAndroidConfigProfile(ctx context.Context, profileUUID string) (*fleet.MDMAndroidConfigProfile, error) {
stmt := `SELECT profile_uuid, team_id, name, raw_json, auto_increment, created_at, uploaded_at FROM mdm_android_configuration_profiles WHERE profile_uuid = ?`
stmt := `SELECT profile_uuid, team_id, name, description, raw_json, auto_increment, created_at, uploaded_at FROM mdm_android_configuration_profiles WHERE profile_uuid = ?`
var profile fleet.MDMAndroidConfigProfile
err := sqlx.GetContext(ctx, ds.reader(ctx), &profile, stmt, profileUUID)
if err != nil {
Expand Down Expand Up @@ -966,10 +967,11 @@ func (ds *Datastore) GetMDMAndroidConfigProfile(ctx context.Context, profileUUID
func (ds *Datastore) UpdateMDMAndroidConfigProfile(ctx context.Context, cp fleet.MDMAndroidConfigProfile, usesFleetVars []fleet.FleetVarName) (*fleet.MDMAndroidConfigProfile, error) {
err := ds.withTx(ctx, func(tx sqlx.ExtContext) error {
var existing struct {
Name string `db:"name"`
Name string `db:"name"`
Description string `db:"description"`
}
err := sqlx.GetContext(ctx, tx, &existing,
`SELECT name FROM mdm_android_configuration_profiles WHERE profile_uuid = ?`, cp.ProfileUUID)
`SELECT name, description FROM mdm_android_configuration_profiles WHERE profile_uuid = ?`, cp.ProfileUUID)
if err != nil {
if err == sql.ErrNoRows {
return ctxerr.Wrap(ctx, notFound("MDMAndroidConfigProfile").WithName(cp.ProfileUUID))
Expand All @@ -988,8 +990,8 @@ func (ds *Datastore) UpdateMDMAndroidConfigProfile(ctx context.Context, cp fleet
// the pre-update raw_json (SET evaluates left to right), and the
// parameter must be CAST to JSON -- a json column never equals a
// bare string.
stmt := `UPDATE mdm_android_configuration_profiles SET uploaded_at = IF(raw_json = CAST(? AS JSON), uploaded_at, CURRENT_TIMESTAMP()), raw_json = ? WHERE profile_uuid = ? AND name = ?`
res, err := tx.ExecContext(ctx, stmt, cp.RawJSON, cp.RawJSON, cp.ProfileUUID, cp.Name)
stmt := `UPDATE mdm_android_configuration_profiles SET uploaded_at = IF(raw_json = CAST(? AS JSON), uploaded_at, CURRENT_TIMESTAMP()), raw_json = ?, description = ? WHERE profile_uuid = ? AND name = ?`
res, err := tx.ExecContext(ctx, stmt, cp.RawJSON, cp.RawJSON, cp.Description, cp.ProfileUUID, cp.Name)
if err != nil {
return ctxerr.Wrap(ctx, err, "updating android mdm config profile contents")
}
Expand All @@ -1006,6 +1008,18 @@ func (ds *Datastore) UpdateMDMAndroidConfigProfile(ctx context.Context, cp fleet
}, "android", false); err != nil {
return ctxerr.Wrap(ctx, err, "updating android profile variable associations")
}
} else if existing.Description != cp.Description {
// Description is not part of the checksum, so it is written without
// touching uploaded_at.
res, err := tx.ExecContext(ctx,
`UPDATE mdm_android_configuration_profiles SET description = ? WHERE profile_uuid = ?`,
Comment thread
jbelbo marked this conversation as resolved.
Resolved
cp.Description, cp.ProfileUUID)
if err != nil {
return ctxerr.Wrap(ctx, err, "updating android mdm config profile description")
}
if aff, _ := res.RowsAffected(); aff == 0 {
return ctxerr.Wrap(ctx, notFound("MDMAndroidConfigProfile").WithName(cp.ProfileUUID))
}
}

labels := make([]fleet.ConfigurationProfileLabel, 0, len(cp.LabelsIncludeAll)+len(cp.LabelsIncludeAny)+len(cp.LabelsExcludeAny))
Expand Down Expand Up @@ -2226,17 +2240,19 @@ WHERE
profile_uuid,
team_id,
name,
description,
raw_json,
uploaded_at
) VALUES (CONCAT('` + fleet.MDMAndroidProfileUUIDPrefix + `', CONVERT(uuid() USING utf8mb4)), ?, ?, ?, CURRENT_TIMESTAMP(6))
) VALUES (CONCAT('` + fleet.MDMAndroidProfileUUIDPrefix + `', CONVERT(uuid() USING utf8mb4)), ?, ?, ?, ?, CURRENT_TIMESTAMP(6))
ON DUPLICATE KEY UPDATE
description = VALUES(description),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
raw_json = VALUES(raw_json),
name = VALUES(name),
uploaded_at = IF(raw_json = VALUES(raw_json) AND name = VALUES(name), uploaded_at, CURRENT_TIMESTAMP(6))
`
for _, p := range profiles {
var res sql.Result
if res, err = tx.ExecContext(ctx, insertNewOrEditedProfile, profileTeamID, p.Name, p.RawJSON); err != nil {
if res, err = tx.ExecContext(ctx, insertNewOrEditedProfile, profileTeamID, p.Name, p.Description, p.RawJSON); err != nil {
return false, ctxerr.Wrap(ctx, err, "insert or update profile")
}

Expand Down
32 changes: 32 additions & 0 deletions server/datastore/mysql/android_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,38 @@ func testUpdateMDMAndroidConfigProfile(t *testing.T, ds *Datastore) {
}, nil)
require.NoError(t, err)
require.Greater(t, contentChangedProf.UploadedAt.Year(), 2020, "a content change must bump uploaded_at")

// the description isn't part of the checksum, so changing it alone must
// not bump uploaded_at, while it is still written with a content change
ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error {
_, err := q.ExecContext(ctx, `UPDATE mdm_android_configuration_profiles SET uploaded_at = '2020-01-01 00:00:00' WHERE profile_uuid = ?`, uploadedAtProfile.ProfileUUID)
return err
})
// the repeat covers the skipped write when the description is unchanged
for _, desc := range []string{"new description", "new description"} {
_, err = ds.UpdateMDMAndroidConfigProfile(ctx, fleet.MDMAndroidConfigProfile{
ProfileUUID: uploadedAtProfile.ProfileUUID,
Name: uploadedAtProfile.Name,
Description: desc,
}, nil)
require.NoError(t, err)
stored, err = ds.GetMDMAndroidConfigProfile(ctx, uploadedAtProfile.ProfileUUID)
require.NoError(t, err)
require.Equal(t, desc, stored.Description)
require.Equal(t, 2020, stored.UploadedAt.Year(), "a description-only edit must not bump uploaded_at")
}

_, err = ds.UpdateMDMAndroidConfigProfile(ctx, fleet.MDMAndroidConfigProfile{
ProfileUUID: uploadedAtProfile.ProfileUUID,
Name: uploadedAtProfile.Name,
Description: "description with content",
RawJSON: []byte(`{"uploadedAt": "described"}`),
}, nil)
require.NoError(t, err)
stored, err = ds.GetMDMAndroidConfigProfile(ctx, uploadedAtProfile.ProfileUUID)
require.NoError(t, err)
require.Equal(t, "description with content", stored.Description)
require.Greater(t, stored.UploadedAt.Year(), 2020, "a content change must bump uploaded_at")
}

func testMDMAndroidProfilesSummary(t *testing.T, ds *Datastore) {
Expand Down
55 changes: 40 additions & 15 deletions server/datastore/mysql/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ func (ds *Datastore) NewMDMAppleConfigProfile(ctx context.Context, cp fleet.MDMA

stmt := `
INSERT INTO
mdm_apple_configuration_profiles (profile_uuid, team_id, identifier, name, scope, mobileconfig, checksum, uploaded_at, secrets_updated_at)
(SELECT ?, ?, ?, ?, ?, ?, UNHEX(MD5(?)), CURRENT_TIMESTAMP(), ? FROM DUAL WHERE
mdm_apple_configuration_profiles (profile_uuid, team_id, identifier, name, description, scope, mobileconfig, checksum, uploaded_at, secrets_updated_at)
(SELECT ?, ?, ?, ?, ?, ?, ?, UNHEX(MD5(?)), CURRENT_TIMESTAMP(), ? FROM DUAL WHERE
NOT EXISTS (
SELECT 1 FROM mdm_windows_configuration_profiles WHERE name = ? AND team_id = ?
) AND NOT EXISTS (
Expand All @@ -219,7 +219,7 @@ INSERT INTO
return err
}
res, err := tx.ExecContext(ctx, stmt,
profUUID, teamID, cp.Identifier, cp.Name, cp.Scope, cp.Mobileconfig, cp.Mobileconfig, cp.SecretsUpdatedAt, cp.Name, teamID, cp.Name,
profUUID, teamID, cp.Identifier, cp.Name, cp.Description, cp.Scope, cp.Mobileconfig, cp.Mobileconfig, cp.SecretsUpdatedAt, cp.Name, teamID, cp.Name,
teamID, cp.Name, teamID)
if err != nil {
switch {
Expand Down Expand Up @@ -286,6 +286,7 @@ INSERT INTO
ProfileID: uint(profileID), //nolint:gosec // dismiss G115
Identifier: cp.Identifier,
Name: cp.Name,
Description: cp.Description,
Scope: cp.Scope,
Mobileconfig: cp.Mobileconfig,
TeamID: cp.TeamID,
Expand All @@ -298,11 +299,12 @@ INSERT INTO
func (ds *Datastore) UpdateMDMAppleConfigProfile(ctx context.Context, cp fleet.MDMAppleConfigProfile, usesFleetVars []fleet.FleetVarName) (*fleet.MDMAppleConfigProfile, error) {
err := ds.withTx(ctx, func(tx sqlx.ExtContext) error {
var existing struct {
Identifier string `db:"identifier"`
Name string `db:"name"`
Identifier string `db:"identifier"`
Name string `db:"name"`
Description string `db:"description"`
}
err := sqlx.GetContext(ctx, tx, &existing,
`SELECT identifier, name FROM mdm_apple_configuration_profiles WHERE profile_uuid = ?`, cp.ProfileUUID)
`SELECT identifier, name, description FROM mdm_apple_configuration_profiles WHERE profile_uuid = ?`, cp.ProfileUUID)
if err != nil {
if err == sql.ErrNoRows {
return ctxerr.Wrap(ctx, notFound("MDMAppleConfigProfile").WithName(cp.ProfileUUID))
Expand Down Expand Up @@ -361,9 +363,9 @@ func (ds *Datastore) UpdateMDMAppleConfigProfile(ctx context.Context, cp fleet.M
stmt := `
UPDATE mdm_apple_configuration_profiles
SET uploaded_at = IF(checksum = UNHEX(MD5(?)) AND name = ?, uploaded_at, CURRENT_TIMESTAMP()),
mobileconfig = ?, checksum = UNHEX(MD5(?)), name = ?, secrets_updated_at = ?
mobileconfig = ?, checksum = UNHEX(MD5(?)), name = ?, description = ?, secrets_updated_at = ?
WHERE profile_uuid = ? AND identifier = ?`
res, err := tx.ExecContext(ctx, stmt, cp.Mobileconfig, cp.Name, cp.Mobileconfig, cp.Mobileconfig, cp.Name, cp.SecretsUpdatedAt, cp.ProfileUUID, cp.Identifier)
res, err := tx.ExecContext(ctx, stmt, cp.Mobileconfig, cp.Name, cp.Mobileconfig, cp.Mobileconfig, cp.Name, cp.Description, cp.SecretsUpdatedAt, cp.ProfileUUID, cp.Identifier)
if err != nil {
switch {
case IsDuplicate(err):
Expand All @@ -375,6 +377,18 @@ WHERE profile_uuid = ? AND identifier = ?`
if aff, _ := res.RowsAffected(); aff == 0 {
return ctxerr.Wrap(ctx, notFound("MDMAppleConfigProfile").WithName(cp.ProfileUUID))
}
} else if existing.Description != cp.Description {
// Description is not part of the checksum, so it is written without
// touching uploaded_at: a bump would re-verify every installed copy.
res, err := tx.ExecContext(ctx,
`UPDATE mdm_apple_configuration_profiles SET description = ? WHERE profile_uuid = ?`,
cp.Description, cp.ProfileUUID)
if err != nil {
return ctxerr.Wrap(ctx, err, "updating apple mdm config profile description")
}
if aff, _ := res.RowsAffected(); aff == 0 {
return ctxerr.Wrap(ctx, notFound("MDMAppleConfigProfile").WithName(cp.ProfileUUID))
}
}

labels := make([]fleet.ConfigurationProfileLabel, 0, len(cp.LabelsIncludeAll)+len(cp.LabelsIncludeAny)+len(cp.LabelsExcludeAny))
Expand Down Expand Up @@ -474,6 +488,7 @@ SELECT
profile_id,
team_id,
name,
description,
scope,
identifier,
mobileconfig,
Expand Down Expand Up @@ -521,6 +536,7 @@ SELECT
profile_id,
team_id,
name,
description,
Comment thread
jbelbo marked this conversation as resolved.
Resolved
scope,
identifier,
mobileconfig,
Expand Down Expand Up @@ -589,6 +605,7 @@ SELECT
declaration_uuid,
team_id,
name,
description,
identifier,
raw_json,
scope,
Expand Down Expand Up @@ -945,6 +962,7 @@ SELECT
profile_id,
team_id,
name,
description,
scope,
identifier,
mobileconfig,
Expand Down Expand Up @@ -2980,16 +2998,17 @@ WHERE
const insertNewOrEditedProfile = `
INSERT INTO
mdm_apple_configuration_profiles (
profile_uuid, team_id, identifier, name, scope, mobileconfig, checksum, uploaded_at, secrets_updated_at
profile_uuid, team_id, identifier, name, description, scope, mobileconfig, checksum, uploaded_at, secrets_updated_at
)
VALUES
-- see https://stackoverflow.com/a/51393124/1094941
( CONCAT('` + fleet.MDMAppleProfileUUIDPrefix + `', CONVERT(uuid() USING utf8mb4)), ?, ?, ?, ?, ?, UNHEX(MD5(mobileconfig)), CURRENT_TIMESTAMP(6), ?)
( CONCAT('` + fleet.MDMAppleProfileUUIDPrefix + `', CONVERT(uuid() USING utf8mb4)), ?, ?, ?, ?, ?, ?, UNHEX(MD5(mobileconfig)), CURRENT_TIMESTAMP(6), ?)
ON DUPLICATE KEY UPDATE
uploaded_at = IF(checksum = VALUES(checksum) AND name = VALUES(name), uploaded_at, CURRENT_TIMESTAMP(6)),
secrets_updated_at = VALUES(secrets_updated_at),
checksum = VALUES(checksum),
name = VALUES(name),
description = VALUES(description),
mobileconfig = VALUES(mobileconfig)
`

Expand Down Expand Up @@ -3083,7 +3102,7 @@ ON DUPLICATE KEY UPDATE
// the profile did not change, its variables cannot have changed since the
// contents is the same as it was already).
for _, p := range incomingProfs {
if result, err = tx.ExecContext(ctx, insertNewOrEditedProfile, profTeamID, p.Identifier, p.Name, p.Scope,
if result, err = tx.ExecContext(ctx, insertNewOrEditedProfile, profTeamID, p.Identifier, p.Name, p.Description, p.Scope,
p.Mobileconfig, p.SecretsUpdatedAt); err != nil {
return false, ctxerr.Wrapf(ctx, err, "insert new/edited profile with identifier %q", p.Identifier)
}
Expand Down Expand Up @@ -5246,19 +5265,21 @@ INSERT INTO mdm_apple_declarations (
declaration_uuid,
identifier,
name,
description,
raw_json,
scope,
secrets_updated_at,
uploaded_at,
team_id
)
VALUES (
?,?,?,?,?,?,NOW(6),?
?,?,?,?,?,?,?,NOW(6),?
)
ON DUPLICATE KEY UPDATE
uploaded_at = IF(raw_json = VALUES(raw_json) AND name = VALUES(name) AND IFNULL(secrets_updated_at = VALUES(secrets_updated_at), TRUE), uploaded_at, NOW(6)),
secrets_updated_at = VALUES(secrets_updated_at),
name = VALUES(name),
description = VALUES(description),
identifier = VALUES(identifier),
scope = VALUES(scope),
raw_json = VALUES(raw_json)
Expand All @@ -5277,6 +5298,7 @@ ON DUPLICATE KEY UPDATE
declUUID,
d.Identifier,
d.Name,
d.Description,
d.RawJSON,
scope,
d.SecretsUpdatedAt,
Expand Down Expand Up @@ -5398,11 +5420,12 @@ INSERT INTO mdm_apple_declarations (
team_id,
identifier,
name,
description,
raw_json,
scope,
secrets_updated_at,
uploaded_at)
(SELECT ?,?,?,?,?,?,?,CURRENT_TIMESTAMP() FROM DUAL WHERE
(SELECT ?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP() FROM DUAL WHERE
NOT EXISTS (
SELECT 1 FROM mdm_windows_configuration_profiles WHERE name = ? AND team_id = ?
) AND NOT EXISTS (
Expand All @@ -5429,11 +5452,12 @@ INSERT INTO mdm_apple_declarations (
team_id,
identifier,
name,
description,
raw_json,
scope,
secrets_updated_at,
uploaded_at)
(SELECT ?,?,?,?,?,?,?,NOW(6) FROM DUAL WHERE
(SELECT ?,?,?,?,?,?,?,?,NOW(6) FROM DUAL WHERE
NOT EXISTS (
SELECT 1 FROM mdm_windows_configuration_profiles WHERE name = ? AND team_id = ?
) AND NOT EXISTS (
Expand All @@ -5444,6 +5468,7 @@ INSERT INTO mdm_apple_declarations (
)
ON DUPLICATE KEY UPDATE
identifier = VALUES(identifier),
description = VALUES(description),
scope = VALUES(scope),
uploaded_at = IF(raw_json = VALUES(raw_json) AND name = VALUES(name) AND IFNULL(secrets_updated_at = VALUES(secrets_updated_at), TRUE), uploaded_at, NOW(6)),
raw_json = VALUES(raw_json)`
Expand Down Expand Up @@ -5483,7 +5508,7 @@ func (ds *Datastore) insertOrUpsertMDMAppleDeclaration(ctx context.Context, insO

err := ds.withTx(ctx, func(tx sqlx.ExtContext) error {
res, err := tx.ExecContext(ctx, insOrUpsertStmt,
declUUID, tmID, declaration.Identifier, declaration.Name, declaration.RawJSON,
declUUID, tmID, declaration.Identifier, declaration.Name, declaration.Description, declaration.RawJSON,
scope, declaration.SecretsUpdatedAt,
declaration.Name, tmID, declaration.Name, tmID, declaration.Name, tmID)
if err != nil {
Expand Down
Loading
Loading