Package: ca-pwt
Conditional Access PowerToys (CA-PowerToys)CA-PowerToys is a set of tools to help you manage Conditional Access policies in your tenant. It is a command line tool that can be used to export and import Conditional Access policies and associated groups, facilitating the editing of the policies in a human readable format. This is particular useful if you are implementing a Policy-as-Code approach, eventually using a Git repository to store your policies and a CI/CD pipeline (like Azure DevOps) to import them into your tenant.Why ?There are several tools to manage Conditional Access policies, such as Graph PowerShell, Microsoft Graph API, Azure AD PowerShell and even M365DSC. Unfortunately, none of these tools can be used to export Conditional Access policies in aformat that can be human readable and editable, and thenimport them back to another tenant. This is where CA-PowerToys can help you, with several commands that can be chained to export, clean up, replace guids with attributes, and import Conditional Access policies and groups.CapabilitiesCA-PowerToys can be used to:Get an access tokento be used in subsequent commands or to be used in other tools, such as Graph PowerShell, using a desired client_id (useful if Graph PowerShell or other tools are blocked in the target tenant)Export/Import Conditional Access policiesto/from a fileExport groupsthat are used in Conditional Access policies to a fileClean up Conditional Access policies and Groups files, removing attributes that are read-only or not allowed in the import processReplace guids with attributes in Conditional Access policies (and vice-versa), making it "human readable" and editable. For example, replace theidattribute with thedisplayNameattribute in a list of excluded groups in a Conditional Access policyThrottle the number of requeststo the Graph API, to avoid hitting the rate limitsZero Trust Sample PoliciesA set of sample policies can be found in theZero Trust Conditional Access Policiesrepository. These policies are based on the samples available athttps://github.com/microsoft/ConditionalAccessforZeroTrustResourcesand therecommended guidelinesand can be used as a starting point to implement a Zero Trust strategy in your organization.InstallationCA-PowerToys is a command line tool that can be used in Windows, Linux, and MacOS. It is written in Python and can be used as a module or as a standalone tool, as long as you have Python >3.7 installed (or Docker).pipTo install it, you can use pip:> pip install ca-pwt> ca-pwt --helpFrom source codeYou can also install it from the source code:> git clone https://github.com/0x6f677548/zerotrust-ca-powertoys.git> cd ca-powertoys> pip install .> ca-pwt --helpDockerAlternatively, you can use the Docker image:> docker pull ghcr.io/0x6f677548/zerotrust-ca-powertoys:latest> docker run -it --rm ghcr.io/0x6f677548/zerotrust-ca-powertoys --helpUsageAll available commands and options can be seen by using the--helpoption.> ca-pwt --helpTo get help on a specific command, use the--helpoption with the command name. For example, to get help on theexport-policiescommand, use the following command:> ca-pwt export-policies --helpUsage ExamplesObtaining an access tokenThere are several ways to obtain an access token. The easiest is to useacquire-tokenin chain with other commands and login interactively. In the example below, the token obtained will be injected in the subsequent command to export the policies.> ca-pwt acquire-token export-policies --output_file policies.jsonExporting conditional access policies...
Obtaining policies from tenant...
Writing policies to file policies.json...Store it in a variable to be used in subsequent commandsYou can also obtain an access token and store it in a variable to be used in subsequent commands. In this case, you need to instruct the command to output the token using the--output_tokenoption.(pwsh)> $token = (ca-pwt acquire-token --output_token)> ca-pwt --access_token $token export-policies --output_file policies.json(bash)> token = $(ca-pwt acquire-token --output_token)> ca-pwt --access_token $token export-policies --output_file policies.jsonExporting conditional access policies...
Obtaining policies from tenant...
Writing policies to file policies.json...If you are in an endpoint where you can't use the interactive login, you can useacquire-tokencommand with--client_idand--client_secretoptions, or, alternatively, using device flow, through the--device_codeoption.> $token = (ca-pwt acquire-token --device_code --output_token)To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code 123456789 to authenticate.Using the tool in a CI/CD pipelineIf you are using the tool in a CI/CD pipeline, CA-PowerToys supports authentication through a service principal, using the--client_idand--client_secretoptions. In this case, you need to instruct the command to output the token using the--output_tokenoption. You should also use the--tenant_idoption to specify the tenant where the service principal was created.> $token = (ca-pwt acquire-token --client_id $client_id --client_secret $client_secret --tenant_id $tenant_id --output_token)> ca-pwt --access_token $token import-policies --input_file policies.json --duplicate_action overwriteExporting policies> ca-pwt --access_token $token export-policies --output_file policies.jsonExporting conditional access policies...
Obtaining policies from tenant...
Writing policies to file policies.json...You can also define oData filters to export only a subset of the policies. In the example below, we export only the policies that have the word "Global" in the DisplayName.> ca-pwt --access_token $token export-policies --output_file policies.json --filter "startswith(displayName,'Global')"Exporting conditional access policies...
Obtaining policies from tenant...
Writing policies to file policies.json...Exporting all Policies and associated Groups> ca-pwt --access_token $token export-policies --output_file policies.json export-policy-groups --output_file groups.jsonExporting ca policies...
Output file: policies.json
Exporting groups found in CA policies...
Input file: policies.json; Output file: groups.json; Lookup cache file: NoneExporting all Policies and associated Groups, replace guids with human-readable attributes in the policies file and make the file ready for import> ca-pwt --access_token $token export-policies --output_file policies.json cleanup-policies replace-guids-with-attrs export-policy-groups --output_file groups.json cleanup-groupsExporting ca policies...
Output file: policies.json
Cleaning up CA policies for import...
Input file: policies.json; Output file: policies.json
Replacing guids with attributes in CA policies...
Input file: policies.json; Output file: policies.json
Exporting groups found in CA policies...
Input file: policies.json; Output file: groups.json
Cleaning up groups for import...
Input file: groups.json; Output file: groups.jsonImporting groups and policies> ca-pwt --access_token $token import-groups --input_file groups.json import-policies --input_file .\policies-humanreadable.jsonImporting groups...
Input file: groups.json
Successfully created groups:
97d90185-6aef-4b84-9051-ed92c3b023a1: CA-Test-Group
Importing CA policies...
Input file: policies-humanreadable.json; Lookup cache file:
Successfully created policies:
896d7a7f-8300-4537-8d39-287b25f7259c: CA-Test-PolicyUsing CA-PowerToys to export policies and import them using Graph PowerShellYou can use CA-PowerToys to export policies in a compatible format with Graph PowerShell. This is useful if you want to export policies from one tenant and import them into another tenant and use Graph PowerShell to import them (you can also use CA-PowerToys to import them, using theimport-policiescommand).This example chains theexport-policiesandcleanup-policiescommands to export the policies and clean them up for import. The output is stored in the policies.json file. Then we read the policies from the file and import them using the New-MgIdentityConditionalAccessPolicy command.> ca-pwt --access_token $token export-policies --output_file policies.json cleanup-policiesExporting conditional access policies...
Obtaining policies from tenant...
Writing policies to file policies.json...
Cleaning up conditional access policies for import...
Reading policies from file policies.json...
Cleaning up policies...
Writing policies to file policies.json...>$policy=Get-Content-Path.\policies.json|ConvertFrom-Json-AsHashtable>$policy.count2>New-MgIdentityConditionalAccessPolicy-BodyParameter$policy[0]Id                                   CreatedDateTime     Description DisplayName
--                                   ---------------     ----------- -----------
7b385aec-569a-470a-980b-8624bfa6332c 14/10/2023 07:52:54             CA001-Global-BaseProtection-All…Note: In the above example, a token was previously obtained using theacquire-tokenand stored in the$tokenvariable.