fleet icon indicating copy to clipboard operation
fleet copied to clipboard

Android host fails to enroll into Fleet

Open AndreyKizimenko opened this issue 2 months ago • 2 comments

Fleet versions

  • Discovered: 4.90.2
  • Reproduced:

Web browser and operating system:


💥  Actual behavior

11:27:33 ERROR ts=2026-09-11T18:27:33Z level=error msg="failed to send to AMAPI" cron=mdm_android_profile_manager host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O err="google api: modify fleet agent application for host KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O: modifying application policy enterprises/LC014m9du5/policies/KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O: googleapi: got HTTP response code 404 with body: Not Found"
11:28:08 WARN  ts=2026-09-11T18:28:08Z level=warn msg="no matching policy request UUID found for non-compliance verification" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O applied_policy_version=2 pending_profiles=0 nil_policy_request_uuid=0 nil_included_in_policy_version=0 non_compliance_count=1
11:28:08 ERROR ts=2026-09-11T18:28:08Z level=error msg="policy request not found" policy_request_uuid="" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O
11:28:10 WARN  ts=2026-09-11T18:28:10Z level=warn msg="no matching policy request UUID found for non-compliance verification" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O applied_policy_version=2 pending_profiles=0 nil_policy_request_uuid=0 nil_included_in_policy_version=0 non_compliance_count=1
11:28:10 ERROR ts=2026-09-11T18:28:10Z level=error msg="policy request not found" policy_request_uuid="" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O
11:28:14 WARN  ts=2026-09-11T18:28:14Z level=warn msg="no matching policy request UUID found for non-compliance verification" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O applied_policy_version=2 pending_profiles=0 nil_policy_request_uuid=0 nil_included_in_policy_version=0 non_compliance_count=1
11:28:14 ERROR ts=2026-09-11T18:28:14Z level=error msg="policy request not found" policy_request_uuid="" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O
11:28:23 WARN  ts=2026-09-11T18:28:23Z level=warn msg="no matching policy request UUID found for non-compliance verification" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O applied_policy_version=2 pending_profiles=0 nil_policy_request_uuid=0 nil_included_in_policy_version=0 non_compliance_count=1
11:28:23 ERROR ts=2026-09-11T18:28:23Z level=error msg="policy request not found" policy_request_uuid="" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O
11:28:24 WARN  ts=2026-09-11T18:28:24Z level=warn msg="no matching policy request UUID found for non-compliance verification" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O applied_policy_version=2 pending_profiles=0 nil_policy_request_uuid=0 nil_included_in_policy_version=0 non_compliance_count=1
11:28:24 ERROR ts=2026-09-11T18:28:24Z level=error msg="policy request not found" policy_request_uuid="" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O
11:28:27 WARN  ts=2026-09-11T18:28:27Z level=warn msg="no matching policy request UUID found for non-compliance verification" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O applied_policy_version=2 pending_profiles=0 nil_policy_request_uuid=0 nil_included_in_policy_version=0 non_compliance_count=1
11:28:27 ERROR ts=2026-09-11T18:28:27Z level=error msg="policy request not found" policy_request_uuid="" host_uuid=KOE2-2PGR-KDTO-EJ3LY-RM7Z-NXAA-O
  1. Profile reconciler creates policy at v1, profile is tracked with included_in_policy_version=1
  2. Cert template delivery bumps policy to v2 via ModifyPolicyApplications. no profile record for v2
  3. Device applies v2, reports non-compliance
  4. verifyDevicePolicy looks for pending/failed profiles at v2. Finds none leads to perpetual warning

Root cause

In ReconcileProfiles (profiles.go:116), reconcileCertificateTemplates runs before profile reconciliation. On a newly enrolled host, the per-device AMAPI policy (created by patchPolicy in profile reconciliation) doesn't exist yet, so cert delivery 404s.

🛠️ Expected behavior

Certificate template delivery should not attempt to call AMAPI before the per-device policy exists. verifyDevicePolicy should not log errors when there are no pending/failed profiles, even if the device reports non-compliance details.

🧑‍💻  Steps to reproduce

These steps:

  • [ ] Have been confirmed to consistently lead to reproduction in multiple Fleet instances.
  • [ ] Describe the workflow that led to the error, but have not yet been reproduced in multiple Fleet instances.

Pre-qualification. If you have used this device with the current enterprise id on your fleet server.

Grab the device uuid:

SELECT enterprise_specific_id FROM android_devices;

You need you un-enroll your device from your local fleet instance, do this through the UI. You need to delete clear out all android host related tables:

-- Delete all Android host data (run after unenrolling via UI)
DELETE FROM host_certificate_templates WHERE host_uuid IN (SELECT uuid FROM hosts WHERE platform = 'android');
DELETE FROM host_mdm_android_profiles WHERE host_uuid IN (SELECT uuid FROM hosts WHERE platform = 'android');
DELETE FROM android_devices WHERE host_id IN (SELECT id FROM hosts WHERE platform = 'android');
DELETE FROM host_mdm WHERE host_id IN (SELECT id FROM hosts WHERE platform = 'android');
DELETE FROM host_display_names WHERE host_id IN (SELECT id FROM hosts WHERE platform = 'android');
DELETE FROM host_seen_times WHERE host_id IN (SELECT id FROM hosts WHERE platform = 'android');
DELETE FROM host_orbit_info WHERE host_id IN (SELECT id FROM hosts WHERE platform = 'android');
DELETE FROM label_membership WHERE host_id IN (SELECT id FROM hosts WHERE platform = 'android');
DELETE FROM host_software WHERE host_id IN (SELECT id FROM hosts WHERE platform = 'android');
DELETE FROM host_operating_system WHERE host_id IN (SELECT id FROM hosts WHERE platform = 'android');
DELETE FROM hosts WHERE platform = 'android';

Then you need to delete the device from the AMAPI enterprise: export FLEET_DEV_ANDROID_GOOGLE_SERVICE_CREDENTIALS=$(cat gcloudshared.key.json) && go run delete_amapi_policy.go "enterprises//policies/"

//go:build ignore

package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	"google.golang.org/api/androidmanagement/v1"
	"google.golang.org/api/option"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, "Usage: go run delete_amapi_policy.go <policy-name>\n")
		fmt.Fprintf(os.Stderr, "Example: go run delete_amapi_policy.go enterprises/{id}/policies/{device_UUID}\n")
		os.Exit(1)
	}
	policyName := os.Args[1]

	parts := strings.SplitN(policyName, "/policies/", 2)
	if len(parts) != 2 {
		fmt.Fprintln(os.Stderr, "invalid policy name format, expected enterprises/{id}/policies/{id}")
		os.Exit(1)
	}
	enterpriseName := parts[0]

	creds := os.Getenv("FLEET_DEV_ANDROID_GOOGLE_SERVICE_CREDENTIALS")
	if creds == "" {
		fmt.Fprintln(os.Stderr, "FLEET_DEV_ANDROID_GOOGLE_SERVICE_CREDENTIALS not set")
		os.Exit(1)
	}

	ctx := context.Background()

	mgmt, err := androidmanagement.NewService(ctx, option.WithCredentialsJSON([]byte(creds)))
	if err != nil {
		fmt.Fprintf(os.Stderr, "creating service: %v\n", err)
		os.Exit(1)
	}

	// List devices linked to this policy and delete them first
	fmt.Println("Listing devices for enterprise:", enterpriseName)
	resp, err := mgmt.Enterprises.Devices.List(enterpriseName).Context(ctx).Do()
	if err != nil {
		fmt.Fprintf(os.Stderr, "listing devices: %v\n", err)
		os.Exit(1)
	}

	for _, device := range resp.Devices {
		if device.AppliedPolicyName == policyName || device.PolicyName == policyName {
			fmt.Printf("Deleting device: %s\n", device.Name)
			_, err := mgmt.Enterprises.Devices.Delete(device.Name).Context(ctx).Do()
			if err != nil {
				fmt.Fprintf(os.Stderr, "deleting device %s: %v\n", device.Name, err)
			} else {
				fmt.Printf("Deleted device: %s\n", device.Name)
			}
		}
	}

	// Now delete the policy
	fmt.Println("Deleting policy:", policyName)
	_, err = mgmt.Enterprises.Policies.Delete(policyName).Context(ctx).Do()
	if err != nil {
		fmt.Fprintf(os.Stderr, "deleting policy: %v\n", err)
		os.Exit(1)
	}

	fmt.Println("Policy deleted:", policyName)
}
  1. Enroll an Android host into Fleet with several certs

🕯️ More info (optional)

Bug 1. ordering in ReconcileProfiles (server/mdm/android/service/profiles.go:116): Swap the order so profile reconciliation, which creates the per-device policy via patchPolicy, runs before reconcileCertificateTemplates, which depends on that policy existing.

Bug 2. missing early return in verifyDevicePolicy (server/mdm/android/service/pubsub.go:1540): Return early when there are no pending/failed profiles, regardless of non-compliance details.

AndreyKizimenko avatar Sep 11 '26 19:09 AndreyKizimenko

QA'd on main (local dev server, build b8e6df7a61, includes #55928) with a physical Pixel 8a, BYOD, against Google's AMAPI.

Case Setup Checked Result
1. Fresh enroll, certificates + profiles New Android enterprise binding, so the device had a new enterprise-specific ID with no existing host policy. Fleet with 2 certificate templates + 4 profiles. The profile cron ran 6 s after enrollment while the device was still on policy 1: no certificate call to AMAPI, no 404. Certificates delivered after the host policy existed, all verified about 60 s after enrollment. Pass
2. Certificates, no profiles Fleet with 1 certificate template, 0 profiles. The setup experience created the host policy and delivered the certificate. Verified 37 s after enrollment. Pass
3. No team 1 certificate template + 2 profiles in Unassigned. Certificate and both profiles verified, certificate 36 s after enrollment. Pass
4. Certificate added to a settled host Added a certificate template while the host was enrolled and on its own policy. The certificate cron delivered it with no 404. Verified once the Fleet app was opened. Pass
5. Log noise Many status reports with non-compliance details and no pending profiles. 0 hits for failed to send to AMAPI, no matching policy request UUID found, policy request not found. No errors in the server log. Pass
6. Removal Deleted a certificate template and a profile from Unassigned. Profile removed and its host record cleaned up. Certificate removal delivered and confirmed by the Fleet app, then its host record cleared. Pass

terry-kwok-code avatar Oct 30 '26 16:10 terry-kwok-code

Android waits for home Policy created too late then logs fill with ghosts

fleet-release avatar Nov 17 '26 00:11 fleet-release