Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export Azure Policy assignments and linked definitions to a file #1266

Closed
ArmaanMcleod opened this issue Feb 17, 2022 · 8 comments · Fixed by #1337
Closed

Export Azure Policy assignments and linked definitions to a file #1266

ArmaanMcleod opened this issue Feb 17, 2022 · 8 comments · Fixed by #1337
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@ArmaanMcleod
Copy link
Contributor

ArmaanMcleod commented Feb 17, 2022

Related to #181, we should provide a way to export Azure Policy assignments and linked definitions to a file.

Default to subscription scope, and exclude defintions that are not assigned.

@ArmaanMcleod ArmaanMcleod added the enhancement New feature or request label Feb 17, 2022
@ArmaanMcleod ArmaanMcleod self-assigned this Feb 17, 2022
@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Feb 19, 2022

@BernieWhite

Do you think these are appropriate Cmdlet parameters:

function Export-AzPolicyAssignmentData {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory = $False)]
        [String]$Name,

        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
        [String]$AssignmentFile,

        [Parameter(Mandatory = $False)]
        [String]$Scope,

        [Parameter(Mandatory = $False)]
        [String]$OutputPath = $PWD,

        [Parameter(Mandatory = $False)]
        [Switch]$PassThru = $False
    )
}

Where Scope would be defaulted to the current /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx scope.

Might be worth bringing in an Id parameter as well in another parameter set.

@BernieWhite
Copy link
Collaborator

BernieWhite commented Feb 20, 2022

@ArmaanMcleod I'm not super clear on how the additional parameters would be used, Scope I get which is currently how Get-AzPolicyAssignment works.

Also how does AssignmentFile work is it for input or output?

Originally, I though that we would point to a scope and enumerate all assignments at that scope or higher, then find all the policy/ initiative definitions for those assignments. But if we have a different line of thinking then that is fine.

@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Feb 20, 2022

@BernieWhite Yeah that was my thinking as well, was trying to line up this cmdlet with Get-AzPolicyAssignment as much as possible. I will just stick with similar parameters to that cmdlet to keep this simple.

I'll review the other parameters as I'm doing this to see if they need to be added. AssignmentFile is probably not needed since I don't think we need to export assignment data from input. Export-AzPolicyAssignmentData would probably just export in-flight data from policy assignments, similar to Export-AzRuleData.

I think two parameter sets like this would be useful:

Export-AzPolicyAssignmentData -Name <string> [-Scope <string>] [-OutputPath <string>] [-PassThru] [<CommonParameters>]

Export-AzPolicyAssignmentData -Id <string> [-OutputPath <string>] [-PassThru] [<CommonParameters>]

Gives the ability to pass in name & scope together(subscription default scope if not supplied), or a fully qualified resource ID.

@BernieWhite
Copy link
Collaborator

@ArmaanMcleod Ok sounds good. A tweak to that would be to make the Name parameter optional.

@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Feb 21, 2022

@BernieWhite Thanks, will add that in 👍.

Also wondering how we should handle contexts for this cmdlet? Should it just run from the default context or should we give the ability to set contexts with -Subscription, -Tenant parameters? Similar to Export-AzRuleData.

Export-AzPolicyAssignmentData [-Name <string>] [-Scope <string>] [-Subscription <string>] [-Tenant <string>] [-OutputPath <string>] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Export-AzPolicyAssignmentData -Id <string> [-Subscription <string>] [-Tenant <string>] [-OutputPath <string>] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Might be easier just to expect these parameters to be provided.

@BernieWhite
Copy link
Collaborator

@ArmaanMcleod It is a good point.

Using one of No parameter or -Scope or -Id is going to tell us the at a minimum the subscription and management groups, unless the scope is higher at a MG which again will fill this blank.

I adding -tenant is a good idea. We can't really work that out easily.

If -subscription is an array then it might be helpful for bulk exports, but maybe let's not over complicate in the first iteration, we can always add it later.

@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Feb 26, 2022

@BernieWhite Should this cmdlet enforce a naming standard on output files? Like *.assignment.json?

I assume this will be needed for #1278 to visit the exported assignment files.

@BernieWhite
Copy link
Collaborator

@ArmaanMcleod Fine with that. It might make finding these files slightly faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants