Skip to content

Publish command: GraphApiService acquires a new token for every Graph API call (no caching in Azure CLI path) #265

@pratapladhani

Description

@pratapladhani

Summary

During a365 publish, the MOS prerequisites check acquires a new Graph API token for every single API call by spawning 2 az CLI subprocesses each time. A single publish command spawns ~22 subprocesses just for token acquisition.

Evidence

Captured from a365 publish --verbose output:

  • 11 separate token acquisitions during MOS prerequisites check alone
  • Each acquisition spawns 2 az CLI processes = 22 subprocess spawns
  • The token is valid for ~60 minutes but is never cached/reused

Breakdown of calls in CheckMosPrerequisitesAsync:

Operation Graph API calls Token acquisitions
1 SP lookup (first-party) 1 1
3 SP lookups (resource apps) 3 3
3 admin consent checks (SP lookup + grant query each) 6 6
MOS token acquisition 1 1
Total 11 11 x 2 = 22 az processes

Redundant lookups

Admin consent verification re-looks up service principals that were already verified in the service principal check phase.

Root Cause

GraphApiService.EnsureGraphHeadersAsync() is called before every GraphGetAsync/GraphPostAsync call, and in the Azure CLI path it always calls GetGraphAccessTokenAsync() which spawns fresh az subprocesses. There is no token caching for the Azure CLI code path (though the IMicrosoftGraphTokenProvider path does cache).

Proposed Fix

  1. Cache the Azure CLI token in GraphApiService with a reasonable TTL (e.g., 5 minutes or until expiry)
  2. Pass cached service principal IDs from the first check phase into the admin consent check phase to avoid redundant lookups
  3. Consider batching Graph API calls where possible

Impact

  • Publish command takes significantly longer than necessary due to subprocess overhead
  • Each az process spawn adds ~2-3 seconds, so ~22 spawns = ~45-60 seconds of token acquisition overhead
  • User experience: verbose output is cluttered with repetitive 'Acquiring Graph token' debug messages

Related

Metadata

Metadata

Assignees

Labels

P2High prioritybugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions