Microsoft365DSC icon indicating copy to clipboard operation
Microsoft365DSC copied to clipboard

[AADConditionalAccessPolicy] Configuration drifts: false positives when using empty arrays

Open SebastianGoers opened this issue 5 years ago • 15 comments

Details of the scenario you tried and the problem that is occurring

When using an empty array for settings that take arrays as input, the comparison with the tenant setting will report a configuration drift. This is because the tenant value will be reported as $null whereas the acutal value in the config is an empty array @(). Not quite sure whether this is only true for this resource or a general issue.

Verbose logs showing the problem

Get-TargetResource Result: 
... IncludeGroups=$null; ...

Target Values: 
... IncludeGroups=(); ...
Test-TargetResource returned False
Completed processing compare operation. The operation returned 
False.

Suggested solution to the issue

Extend the check for diffs to ensure that $null and @() will be considered equal settings.

The DSC configuration that is used to reproduce the issue (as detailed as possible)

AADConditionalAccessPolicy 
        {
           IncludeGroups         = @();

The operating system the target node is running

Version of the DSC module that was used ('dev' if using current dev branch)

SebastianGoers avatar Jan 28 '21 06:01 SebastianGoers

@peterabele can you have a look?

NikCharlebois avatar Jan 28 '21 22:01 NikCharlebois

I can check&fix it this week

Feladó: Nik Charleboismailto:[email protected] Elküldve: Thursday, January 28, 2021 11:02 PM Címzett: microsoft/Microsoft365DSCmailto:[email protected] Másolatot kap: Peter Abelemailto:[email protected]; Assignmailto:[email protected] Tárgy: Re: [microsoft/Microsoft365DSC] [AADConditionalAccessPolicy] Configuration drifts: false positives when using empty arrays (#1036)

@peterabelehttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fpeterabele&data=04%7C01%7C%7C3357d1ea4cab4e1c415c08d8c3d8626a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637474681415988657%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=AD5gdeXiMI4iywk8Q6i5hJzVpatIDFY21BdhaNtZ8Ms%3D&reserved=0 can you have a look?

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHubhttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FMicrosoft365DSC%2Fissues%2F1036%23issuecomment-769428281&data=04%7C01%7C%7C3357d1ea4cab4e1c415c08d8c3d8626a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637474681415988657%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=3xx6f1bYmZxBXLSPeeshT29Db%2FNtPdibZ6uEYarOgDE%3D&reserved=0, or unsubscribehttps://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAREDK3POX5DZJAF2GGZUUF3S4HNGXANCNFSM4WWO7C6A&data=04%7C01%7C%7C3357d1ea4cab4e1c415c08d8c3d8626a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637474681415998617%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=ktmCQhWGib%2FbIZ6%2BHAVNVRY5kG%2FBHi5h73ZakxUGtVg%3D&reserved=0.

peterabele avatar Feb 01 '21 08:02 peterabele

@peterabele : Has this problem been solved? We also see that applying an empty array to ExcludeGroups is not having any effect (maybe this is related to the issue in this incident?). It's worth noting that no errors are being reported.

SebastianGoers avatar Feb 10 '21 09:02 SebastianGoers

@SebastianGoers , not it is not fixed yet, thanks for the additional details.

peterabele avatar Feb 10 '21 11:02 peterabele

Same seems to apply for IncludeLocations ... presumably all actions where an empty array is being used to set an attribute are affected.

SebastianGoers avatar Feb 11 '21 06:02 SebastianGoers

@peterabele , we believe that the issue is in line https://github.com/microsoft/Microsoft365DSC/blob/8a5ac8ac386659bb03ce80714aa734525c3b0ae5/Modules/Microsoft365DSC/DSCResources/MSFT_AADConditionalAccessPolicy/MSFT_AADConditionalAccessPolicy.psm1#L320 of your code.

We assume that line https://github.com/microsoft/Microsoft365DSC/blob/8a5ac8ac386659bb03ce80714aa734525c3b0ae5/Modules/Microsoft365DSC/DSCResources/MSFT_AADConditionalAccessPolicy/MSFT_AADConditionalAccessPolicy.psm1#L323 needs to move to https://github.com/microsoft/Microsoft365DSC/blob/8a5ac8ac386659bb03ce80714aa734525c3b0ae5/Modules/Microsoft365DSC/DSCResources/MSFT_AADConditionalAccessPolicy/MSFT_AADConditionalAccessPolicy.psm1#L320 and replace it.

Otherwise it will causes issues when converting the result in https://github.com/microsoft/Microsoft365DSC/blob/8a5ac8ac386659bb03ce80714aa734525c3b0ae5/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1#L137.

If there are no IncludeGroups defined on the tenant, then the check with https://github.com/microsoft/Microsoft365DSC/blob/8a5ac8ac386659bb03ce80714aa734525c3b0ae5/Modules/Microsoft365DSC/DSCResources/MSFT_AADConditionalAccessPolicy/MSFT_AADConditionalAccessPolicy.psm1#L188 will return an empty list. However your code will return a $null value.

Same applies to ExcludeGroups.

SebastianGoers avatar Feb 17 '21 16:02 SebastianGoers

Hi @peterabele, how is it going with the bugfix?

EricHindrichsen avatar Mar 01 '21 12:03 EricHindrichsen

Hi @peterabele and @NikCharlebois, what's the status here?

EricHindrichsen avatar Apr 07 '21 06:04 EricHindrichsen

Hi All, There was an AAD PS bug related to device conditions, that has been fixed now. Now I will update the resource using the new device condition attributes, and fix the false positive issue the same time.

peterabele avatar Apr 07 '21 10:04 peterabele

Updated code is ready, works fine in my lab, sent the PR.

peterabele avatar Apr 08 '21 08:04 peterabele

@peterabele @NikCharlebois Most issues seem to be fixed now, but IncludeLocations (tried with value "$null" as well as "@()") still brings up the config drift:

2021-04-14T15:29:21.7405170Z VERBOSE: [fv-az33-367]:                            [[AADConditionalAccessPolicy]ddca2ebc-c399-43b4-9362-221e67ec8ac1] 
2021-04-14T15:29:21.7412841Z Get-TargetResource Result: 
(...)
2021-04-14T15:29:21.7459576Z IncludeGroups=(); IncludeLocations=$null; IncludePlatforms=$null; IncludeRoles=(Global Administrator,Guest Inviter,User
(...)
2021-04-14T15:29:21.7571578Z VERBOSE: [fv-az33-367]:                            [[AADConditionalAccessPolicy]ddca2ebc-c399-43b4-9362-221e67ec8ac1] 
2021-04-14T15:29:21.7575447Z Target Values: BuiltInControls=(Mfa); ClientAppTypes=(All); CloudAppSecurityType=; 
(...)
2021-04-14T15:29:21.7597108Z GrantControlOperator=OR; IncludeApplications=(All); IncludeGroups=(); IncludeLocations=(); IncludeRoles=(Global 
(...)
2021-04-14T15:29:22.1405206Z VERBOSE: [fv-az33-367]:                            [[AADConditionalAccessPolicy]ddca2ebc-c399-43b4-9362-221e67ec8ac1] 
2021-04-14T15:29:22.1415894Z Test-TargetResource returned False
2021-04-14T15:29:22.1420758Z VERBOSE: [fv-az33-367]: LCM:  [ End    Test     ]  [[AADConditionalAccessPolicy]ddca2ebc-c399-43b4-9362-221e67ec8ac1] 

EricHindrichsen avatar Apr 14 '21 15:04 EricHindrichsen

@EricHindrichsen , if includelocations / excludelocations is not defined in a CA policy, it's not presented at all (check with Get-AzureADMSConditionalAccessPolicy), hence it's not added to the DSC representation either. You should not add these attributes to a policy in a DSC config, if they are not defined. Just try exporting your actual config and reimporting it, you won't have config drifts afterwards.

peterabele avatar Apr 14 '21 16:04 peterabele

Hi @peterabele, I've tried your approach and it doesn't work either. If I remove includeLocations from our config, then DSC is no longer enforcing that value to be empty! It reports no drift, although I have configured an included location now via the admin portal (for testing purposes) and the value is empty in the config. In my understanding we have to explicitly specify empty values in the config for Test-DscConfig to work correctly.

@NikCharlebois can you please reopen this issue?

EricHindrichsen avatar Apr 15 '21 07:04 EricHindrichsen

Thanks @EricHindrichsen for testing this scenario. I'll do extensive testing on resetting all attributes, and fix where needed.

peterabele avatar Apr 15 '21 09:04 peterabele

@EricHindrichsen @NikCharlebois
Just sent the PR with the new version Configuration drift from undefined to defined is now signaled for each attribute. Changing attributes to undefined is now possible for all attributes, except:         $IncludePlatforms         $IncludeLocations         $IncludeDevices         $ApplicationEnforcedRestrictionsIsEnabled         $CloudAppSecurityIsEnabled         $CloudAppSecurityType         $SignInFrequencyValue         $SignInFrequencyType         $SignInFrequencyIsEnabled         $PersistentBrowserMode         $PersistentBrowserIsEnabled (Set-AzureADMSConditionalAccessPolicy doesn't allow resetting these attributes unfortunately)

peterabele avatar Apr 19 '21 19:04 peterabele