Skip to content

Fix missing permission check#1465

Open
nicolonsky wants to merge 1 commit intomaester365:mainfrom
nicolonsky:main
Open

Fix missing permission check#1465
nicolonsky wants to merge 1 commit intomaester365:mainfrom
nicolonsky:main

Conversation

@nicolonsky
Copy link
Contributor

Description

Fixes #1422.

Contribution Checklist

Before submitting this PR, please confirm you have completed the following:

  • 📖 Read the guidelines for contributing to this repository.
  • 🧪 Ensure the build and unit tests pass by running /powershell/tests/pester.ps1 on your local system.

@nicolonsky nicolonsky requested a review from a team as a code owner February 25, 2026 22:08
@SamErde SamErde requested a review from Copilot February 26, 2026 02:44
@SamErde SamErde added maester-test Related to a Maester test intune Microsoft Intune labels Feb 26, 2026
Copy link
Contributor

Copilot AI left a comment

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 addresses issue #1422 by adding a permission check to the MT.1100 test (Test-MtIntuneDiagnosticSettings) to gracefully handle cases where the user lacks Azure RBAC permissions to read Intune diagnostic settings. When a 403 (Forbidden) response is received from the Azure ARM API, the test now skips with an appropriate message instead of failing.

Changes:

  • Added permission check that detects 403 status code and throws UnauthorizedAccessException
  • Added catch block to handle UnauthorizedAccessException and skip test with custom reason
  • Added documentation explaining the Azure RBAC prerequisites and providing PowerShell snippets to create a custom role

Reviewed changes

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

File Description
powershell/public/maester/intune/Test-MtIntuneDiagnosticSettings.ps1 Added 403 status code check and exception handling to skip test when Azure RBAC permissions are insufficient
powershell/public/maester/intune/Test-MtIntuneDiagnosticSettings.md Added prerequisites section documenting required Azure RBAC permissions and remediation steps

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

$diagnosticSettingsRequest = Invoke-AzRestMethod -Method GET -Path "/providers/microsoft.intune/diagnosticSettings?api-version=2017-04-01-preview"

# check whether the user has permissions to read diagnostic settings
if ($diagnosticSettingsRequest.StatusCode -eq 403) {
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The status code check uses an integer comparison (403) but should use a string comparison ('403') to match the type returned by Invoke-AzRestMethod. The StatusCode property returns a string value, not an integer. This can be seen in the similar function Test-MtCisaDiagnosticSettings which uses '$settings.StatusCode -ne '200'' with a string comparison.

Additionally, checking only for 403 is too narrow. A more robust approach would be to check if StatusCode is not '200', which handles 403 (forbidden), 401 (unauthorized), and other error status codes. This matches the pattern used in Test-MtCisaDiagnosticSettings.

Suggested change
if ($diagnosticSettingsRequest.StatusCode -eq 403) {
if ($diagnosticSettingsRequest.StatusCode -ne '200') {

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

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

@nicolonsky, this suggestion makes sense, but it also dilutes the specificity of your error handling. You might do if '403' {report the permissions issue } else {report a general error}.

Comment on lines 36 to +73
@@ -62,6 +68,9 @@ function Test-MtIntuneDiagnosticSettings {
}
Add-MtTestResultDetail -Result $testResultMarkdown
return [bool]($diagnosticSettings | Where-Object { $_.properties.logs | Where-Object { $_.category -eq 'AuditLogs' -and $_.enabled -eq $true } })
} catch [System.UnauthorizedAccessException] {
Add-MtTestResultDetail -SkippedBecause Custom -SkippedCustomReason 'Insufficient permissions to read Intune diagnostic settings in Azure.'
return $null
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The approach of throwing and catching UnauthorizedAccessException is inconsistent with how similar diagnostic settings tests handle authorization failures. The existing Test-MtCisaDiagnosticSettings function (lines 56-61) checks the status code and directly calls Add-MtTestResultDetail -SkippedBecause NotAuthorized without throwing or catching exceptions.

For consistency with the codebase, this code should check the status code and directly skip the test using Add-MtTestResultDetail -SkippedBecause NotAuthorized, rather than throwing and catching an exception. This approach is simpler, more direct, and follows the established pattern for Azure diagnostic settings tests.

Copilot uses AI. Check for mistakes.

# check whether the user has permissions to read diagnostic settings
if ($diagnosticSettingsRequest.StatusCode -eq 403) {
throw [System.UnauthorizedAccessException]::new('No Azure RBAC permissions to read Intune diagnostic settings.')
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The line parsing of ConvertFrom-Json will fail if the API returns a non-200 status code, because the Content property will contain an error message in JSON format rather than the expected structure with a 'value' property.

With the permission check moved earlier (before line 40), this issue is mitigated. However, if the check is modified to use the pattern from Test-MtCisaDiagnosticSettings (checking for -ne '200' instead of specifically checking for 403), this line will be protected from all error status codes, not just 403.

Suggested change
throw [System.UnauthorizedAccessException]::new('No Azure RBAC permissions to read Intune diagnostic settings.')
throw [System.UnauthorizedAccessException]::new('No Azure RBAC permissions to read Intune diagnostic settings.')
} elseif ($diagnosticSettingsRequest.StatusCode -ne 200) {
throw [System.Exception]::new(("Failed to retrieve Intune diagnostic settings. HTTP status code: {0}" -f $diagnosticSettingsRequest.StatusCode))

Copilot uses AI. Check for mistakes.

#### Test Prerequisites

For this test to run, the executing principal must have permissions to read Intune diagnostic settings in Azure (`microsoft.intune/diagnosticSettings/read` action). This typically requires at least the 'Monitoring Reader' or 'Reader' Azure role assigned at the subscription where the target Intune Log Analytics workspace or storage account resides.
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The documentation states that the Azure role should be "assigned at the subscription where the target Intune Log Analytics workspace or storage account resides," but the API call in the test is querying provider-level diagnostic settings (/providers/microsoft.intune/diagnosticSettings), not subscription-level resources.

According to the issue description, the role assignment needs to be at the subscription level (the scope used in the example is "/subscriptions/$SubscriptionId"), but the wording "where the target Intune Log Analytics workspace or storage account resides" is potentially misleading. It would be clearer to state that the role should be assigned at the subscription level, since the diagnostic settings themselves are a provider-level resource that requires subscription-level permissions to read.

Suggested change
For this test to run, the executing principal must have permissions to read Intune diagnostic settings in Azure (`microsoft.intune/diagnosticSettings/read` action). This typically requires at least the 'Monitoring Reader' or 'Reader' Azure role assigned at the subscription where the target Intune Log Analytics workspace or storage account resides.
For this test to run, the executing principal must have permissions to read Intune diagnostic settings in Azure (`microsoft.intune/diagnosticSettings/read` action). This typically requires at least the 'Monitoring Reader' or 'Reader' Azure role assigned at the subscription level (for example, with scope `/subscriptions/$SubscriptionId`), which provides access to the provider-level Intune diagnostic settings.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Double check. Looks like a valid suggestion here as well.

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

Labels

intune Microsoft Intune maester-test Related to a Maester test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🪲 MT.1100 (Intune Diagnostic Settings) fails without showing Azure ARM permissions error

3 participants