Skip to content

Commit

Permalink
Fix works with manual testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
getvictor committed Jan 2, 2025
1 parent dbb362a commit a222c38
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
29 changes: 21 additions & 8 deletions server/datastore/mysql/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2719,7 +2719,8 @@ func (ds *Datastore) BulkUpsertMDMAppleHostProfiles(ctx context.Context, payload
detail,
command_uuid,
checksum,
secrets_updated_at
secrets_updated_at,
ignore_error
)
VALUES %s
ON DUPLICATE KEY UPDATE
Expand All @@ -2728,6 +2729,7 @@ func (ds *Datastore) BulkUpsertMDMAppleHostProfiles(ctx context.Context, payload
detail = VALUES(detail),
checksum = VALUES(checksum),
secrets_updated_at = VALUES(secrets_updated_at),
ignore_error = VALUES(ignore_error),
profile_identifier = VALUES(profile_identifier),
profile_name = VALUES(profile_name),
command_uuid = VALUES(command_uuid)`,
Expand All @@ -2747,9 +2749,9 @@ func (ds *Datastore) BulkUpsertMDMAppleHostProfiles(ctx context.Context, payload
}

generateValueArgs := func(p *fleet.MDMAppleBulkUpsertHostProfilePayload) (string, []any) {
valuePart := "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?),"
valuePart := "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),"
args := []any{p.ProfileUUID, p.ProfileIdentifier, p.ProfileName, p.HostUUID, p.Status, p.OperationType, p.Detail, p.CommandUUID,
p.Checksum, p.SecretsUpdatedAt}
p.Checksum, p.SecretsUpdatedAt, p.IgnoreError}
return valuePart, args
}

Expand All @@ -2767,14 +2769,25 @@ func (ds *Datastore) BulkUpsertMDMAppleHostProfiles(ctx context.Context, payload
}

func (ds *Datastore) UpdateOrDeleteHostMDMAppleProfile(ctx context.Context, profile *fleet.HostMDMAppleProfile) error {
if profile.OperationType == fleet.MDMOperationTypeRemove &&
profile.Status != nil &&
(*profile.Status == fleet.MDMDeliveryVerifying || *profile.Status == fleet.MDMDeliveryVerified) {
_, err := ds.writer(ctx).ExecContext(ctx, `
if profile.OperationType == fleet.MDMOperationTypeRemove && profile.Status != nil {
var ignoreError bool
if *profile.Status == fleet.MDMDeliveryFailed {
// Check whether we should ignore the error.
err := sqlx.GetContext(ctx, ds.reader(ctx), &ignoreError, `
SELECT ignore_error FROM host_mdm_apple_profiles WHERE host_uuid = ? AND command_uuid = ?`,
profile.HostUUID, profile.CommandUUID)
if err != nil {
return ctxerr.Wrap(ctx, err, "get ignore error")
}
}
if ignoreError ||
(*profile.Status == fleet.MDMDeliveryVerifying || *profile.Status == fleet.MDMDeliveryVerified) {
_, err := ds.writer(ctx).ExecContext(ctx, `
DELETE FROM host_mdm_apple_profiles
WHERE host_uuid = ? AND command_uuid = ?
`, profile.HostUUID, profile.CommandUUID)
return err
return err
}
}

detail := profile.Detail
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tables

import (
"database/sql"
"fmt"
)

func init() {
MigrationClient.AddMigration(Up_20250102121439, Down_20250102121439)
}

func Up_20250102121439(tx *sql.Tx) error {
_, err := tx.Exec(`ALTER TABLE host_mdm_apple_profiles
ADD COLUMN ignore_error TINYINT(1) NOT NULL DEFAULT 0`)
if err != nil {
return fmt.Errorf("failed to add ignore_error to host_mdm_apple_profiles table: %w", err)
}
return nil
}

func Down_20250102121439(_ *sql.Tx) error {
return nil
}
5 changes: 3 additions & 2 deletions server/datastore/mysql/schema.sql

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions server/fleet/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ type MDMAppleProfilePayload struct {
OperationType MDMOperationType `db:"operation_type"`
Detail string `db:"detail"`
CommandUUID string `db:"command_uuid"`
IgnoreError bool `db:"ignore_error"`
}

// DidNotInstallOnHost indicates whether this profile was not installed on the host (and
Expand Down Expand Up @@ -357,6 +358,7 @@ type MDMAppleBulkUpsertHostProfilePayload struct {
Detail string
Checksum []byte
SecretsUpdatedAt *time.Time
IgnoreError bool
}

// MDMAppleFileVaultSummary reports the number of macOS hosts being managed with Apples disk
Expand Down
3 changes: 3 additions & 0 deletions server/service/apple_mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3480,6 +3480,8 @@ func ReconcileAppleProfiles(
// is currently being installed. So, we clean up the profile from the database, but also send
// a remove command to the host.
hostProfilesToCleanup = append(hostProfilesToCleanup, p)
// IgnoreError is set since the removal command is likely to fail.
p.IgnoreError = true
}

target := removeTargets[p.ProfileUUID]
Expand All @@ -3502,6 +3504,7 @@ func ReconcileAppleProfiles(
ProfileName: p.ProfileName,
Checksum: p.Checksum,
SecretsUpdatedAt: p.SecretsUpdatedAt,
IgnoreError: p.IgnoreError,
})
}

Expand Down

0 comments on commit a222c38

Please sign in to comment.