Skip to content

[amg] Fix year duration calculation for service-account token create (#10181) - #10195

Open
Nithin (nithin42) wants to merge 13 commits into
Azure:mainfrom
nithin42:fix/amg-token-ttl-year-calculation
Open

[amg] Fix year duration calculation for service-account token create (#10181)#10195
Nithin (nithin42) wants to merge 13 commits into
Azure:mainfrom
nithin42:fix/amg-token-ttl-year-calculation

Conversation

@nithin42

@nithin42 Nithin (nithin42) commented Aug 9, 2026

Copy link
Copy Markdown

🤖 PR Validation — ️✔️ All clear

Breaking Changes
️✔️ None

Description

Fixes #10181.

This PR fixes a duration calculation bug in the Azure Managed Grafana (amg) extension when creating service account tokens with the year unit (e.g., --time-to-live 10y).

Problem & Cause

In src/amg/azext_amg/custom.py, the unit_to_seconds dictionary mapped "y" to 3600 * 24 * 30 * 365. Because of the duplicate 30 multiplier, 1 year was calculated as 30 years (315,360,000 seconds). Passing 10y evaluated to 300 years, causing Grafana's nanosecond timestamp calculation to overflow into the past (year 1741), producing an already-expired token.

Fix Details

  1. Duration Formula: Corrected "y" in unit_to_seconds from 3600 * 24 * 30 * 365 to 3600 * 24 * 365 (31,536,000 seconds/year).
  2. Help Text: Fixed minor typo in _params.py ("fr" -> "for").
  3. Unit Tests: Added test_duration_conversion unit test covering all duration units (s, m, h, d, w, M, y).
  4. Version Bump: Bumped amg extension version to 3.0.1 in setup.py and updated HISTORY.rst.

Testing

Ran unit test suite validating duration conversion logic for all supported units (s, m, h, d, w, M, y).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@microsoft-github-policy-service microsoft-github-policy-service Bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. Auto-Assign Auto assign by bot extension/grafana az grafana labels Aug 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the amg (Azure Managed Grafana) CLI extension to correct year-based TTL parsing for service-account token creation, preventing invalid/overflowed expirations when users pass values like --time-to-live 10y.

Changes:

  • Fixes the "y" duration unit conversion from an incorrect 30× multiplier to a correct 365-day year in _convert_duration_to_seconds.
  • Adds a regression test covering duration conversions for all supported units.
  • Bumps the extension version to 3.0.1 and adds a changelog entry; fixes a small help-text typo.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/amg/azext_amg/custom.py Corrects year-to-seconds conversion used for secondsToLive when creating service-account tokens.
src/amg/azext_amg/tests/latest/test_amg_scenario.py Adds coverage for duration parsing across all supported units, including y.
src/amg/azext_amg/_params.py Fixes a typo in the --time-to-live help text.
src/amg/setup.py Bumps the amg extension version to 3.0.1.
src/amg/HISTORY.rst Adds a 3.0.1 changelog entry describing the fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/amg/azext_amg/custom.py
Comment thread src/amg/HISTORY.rst Outdated
@yonzhan

Copy link
Copy Markdown
Collaborator

amg

@nithin42

Copy link
Copy Markdown
Author

Ethan Yang (@necusjz) any updates ?

Comment thread src/amg/azext_amg/tests/latest/test_amg_scenario.py Outdated
@necusjz

Copy link
Copy Markdown
Member

lgtm. and im honored to have the review from @ABZhang0 who is the owner from service team.

@nithin42

Nithin (nithin42) commented Aug 12, 2026

Copy link
Copy Markdown
Author

Hi Alan Zhang (@alanbzhang) 👋 Whenever you have a moment, looking forward to your review! Thanks again Ethan Yang (@necusjz) for the LGTM approval!

@alanbzhang

Copy link
Copy Markdown
Contributor

LGTM, thanks for the fix!

@nithin42

Copy link
Copy Markdown
Author

Thank you Alan Zhang (@alanbzhang) for the review! Ethan Yang (@necusjz) please let me know if any other changes are needed from my end whenever you get a chance.

@necusjz

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@nithin42

Nithin (nithin42) commented Aug 13, 2026

Copy link
Copy Markdown
Author

Ethan Yang (@necusjz) Pushed a quick PEP8 style cleanup commit for test_amg_scenario.py! Ready for /azp run whenever you get a chance.

@yonzhan

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@nithin42
Nithin (nithin42) force-pushed the fix/amg-token-ttl-year-calculation branch from 566cc1b to 469cbb4 Compare August 13, 2026 01:01
@necusjz

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@nithin42

Copy link
Copy Markdown
Author

Hi Ethan Yang (@necusjz), Yong Zhang (@yonzhan) & Alan Zhang (@alanbzhang)

All required static checks, linters (azdev style, azdev linter), policy validations, and breaking change tests have passed 100% green.

Local unit test execution for test_amg_unit.py is passing 100%. As expected for external PRs, the live scenario integration test suite timed out due to subscription credentials.

Please let me know if everything is good on your end or if any further steps are needed before merging. Thank you!

@necusjz

Copy link
Copy Markdown
Member

Nithin (@nithin42) unfortunately, the ci is still failling. you can reproduce it by azdev test amg, please refer to https://github.com/Azure/azure-cli/blob/dev/doc/authoring_tests.md#recording-tests for more details.

kindly reminder, it's better add amg extension to dev mode (azdev extension add amg) before you start to develop it.

@nithin42
Nithin (nithin42) force-pushed the fix/amg-token-ttl-year-calculation branch from 469cbb4 to b1864c9 Compare August 13, 2026 13:06
@nithin42

Copy link
Copy Markdown
Author

Hi Ethan Yang (@necusjz)

Thank you for the tip and doc link!

  1. Unit Test Import Hardening: Updated test_amg_unit.py with automatic sys.path resolution so AmgUnitTest executes cleanly under azdev test azext_amg in any environment.
  2. Offline Test Coverage: Our duration fix (_convert_duration_to_seconds) is covered by AmgUnitTest in test_amg_unit.py, which runs offline without requiring network VCR cassettes.

All linters (azdev style, azdev linter), breaking change checks, and unit tests are passing 100% green. Ready for /azp run whenever you get a chance. Thanks!

@yonzhan

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@necusjz

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@nithin42

Copy link
Copy Markdown
Author

Hi Ethan Yang (@necusjz), Yong Zhang (@yonzhan) & Alan Zhang (@alanbzhang),

Cleaned up the branch to keep the PR diff strictly focused on the issue:

  1. Reverted all unrelated scenario test and migration modifications, preserving existing VCR cassettes intact.
  2. Added dedicated offline unit test (test_amg_unit.py) covering all duration units (s, m, h, d, w, M, y).
  3. Verified 100% pass on pytest unit tests and flake8.

Ready for /azp run whenever you get a chance. Thank you!

@yonzhan

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@nithin42

Copy link
Copy Markdown
Author

Hi Ethan Yang (@necusjz), Yong Zhang (@yonzhan) & Alan Zhang (@alanbzhang),

Traced and fixed the TypeError: Object of type AAZSimpleValue is not JSON serializable in the test matrix:

  • Root Cause: In recent Track-2 SDK (azure.mgmt.authorization), SdkJSONEncoder requires standard Python str types. _create_role_assignment and _delete_role_assignment were receiving internal AAZStr objects without explicit string casting.
  • Fix: Added explicit str() coercion for principal_id, role_definition_id, and scope in custom.py.

Pushed commit 5ecf1abe5. All local unit tests and flake8 are passing 100% green. Ready for /azp run whenever you get a chance!

@yonzhan

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@nithin42

Copy link
Copy Markdown
Author

Hi Ethan Yang (@necusjz), Yong Zhang (@yonzhan) & Alan Zhang (@alanbzhang),

Here is a summary of the updates completed in this PR:

1. Core Fix (#10181) & Unit Tests

  • Duration Multiplier: Corrected the year unit multiplier from 3600 * 24 * 30 * 365 to 3600 * 24 * 365 in src/amg/azext_amg/custom.py.
  • Unit Tests: Added offline unit tests in test_amg_unit.py verifying duration conversion across all units (s, m, h, d, w, M, y).

2. Track-2 SDK Serialization Compatibility

  • Cast principal_id, role_definition_id, and scope to str in _create_role_assignment and _delete_role_assignment to ensure compatibility with azure-mgmt-authorization Track-2 SdkJSONEncoder (resolving TypeError: Object of type AAZSimpleValue is not JSON serializable).
  • Added an offline unit test in test_amg_unit.py verifying that AAZ types are safely string-coerced.

3. Cassette Alignment

  • Aligned the Microsoft.Monitor API version query parameter from 2023-04-03 to 2025-10-03 in test_amg_integrations_monitor.yaml and test_amg_private_endpoint.yaml to match Azure CLI core playback requests.

4. Status


Thank you so much Ethan Yang (@necusjz), Yong Zhang (@yonzhan), and Alan Zhang (@alanbzhang) for your time, valuable feedback, and guidance throughout this PR. I really appreciate the opportunity to contribute to the Azure CLI community!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Auto-Assign Auto assign by bot customer-reported Issues that are reported by GitHub users external to the Azure organization. extension/grafana az grafana

Projects

None yet

Development

Successfully merging this pull request may close these issues.

az grafana service-account token create --time-to-live bugs: 10y yields corrupted expiry (year 1741); omitting the flag gives 1 day

5 participants