diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 134992b59a..a35c3158ea 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -148,6 +148,6 @@ #/avm/ptn/avd-lza/management-plane/ @Azure/avm-ptn-avd-lza-managementplane-module-owners-bicep @Azure/avm-core-team-technical-bicep #/avm/ptn/avd-lza/networking/ @Azure/avm-ptn-avd-lza-networking-module-owners-bicep @Azure/avm-core-team-technical-bicep #/avm/ptn/avd-lza/session-hosts/ @Azure/avm-ptn-avd-lza-sessionhosts-module-owners-bicep @Azure/avm-core-team-technical-bicep +/avm/ptn/policy-insights/remediation/ @Azure/avm-ptn-policyinsights-remediation-module-owners-bicep @Azure/avm-core-team-technical-bicep #/avm/ptn/security/security-center/ @Azure/avm-ptn-securitycenter-module-owners-bicep @Azure/avm-core-team-technical-bicep - *avm.core.team.tests.ps1 @Azure/avm-core-team-technical-bicep diff --git a/.github/ISSUE_TEMPLATE/avm_module_issue.yml b/.github/ISSUE_TEMPLATE/avm_module_issue.yml index bcfcbaa7ed..459c075404 100644 --- a/.github/ISSUE_TEMPLATE/avm_module_issue.yml +++ b/.github/ISSUE_TEMPLATE/avm_module_issue.yml @@ -44,6 +44,7 @@ body: # - "avm/ptn/avd-lza/management-plane" # - "avm/ptn/avd-lza/networking" # - "avm/ptn/avd-lza/session-hosts" + - "avm/ptn/policy-insights/remediation" - "avm/ptn/security/security-center" - "avm/res/aad/domain-service" - "avm/res/analysis-services/server" diff --git a/.github/workflows/avm.ptn.policy-insights.remediation.yml b/.github/workflows/avm.ptn.policy-insights.remediation.yml new file mode 100644 index 0000000000..eecddf314c --- /dev/null +++ b/.github/workflows/avm.ptn.policy-insights.remediation.yml @@ -0,0 +1,86 @@ +name: "avm.ptn.policy-insights.remediation" + +on: + schedule: + - cron: "0 12 1/15 * *" # Bi-Weekly Test (on 1st & 15th of month) + workflow_dispatch: + inputs: + staticValidation: + type: boolean + description: "Execute static validation" + required: false + default: true + deploymentValidation: + type: boolean + description: "Execute deployment validation" + required: false + default: true + removeDeployment: + type: boolean + description: "Remove deployed module" + required: false + default: true + push: + branches: + - main + paths: + - ".github/actions/templates/avm-**" + - ".github/workflows/avm.template.module.yml" + - ".github/workflows/avm.ptn.policy-insights.remediation.yml" + - "avm.ptn.policy-insights.remediation/**" + - "avm/utilities/pipelines/**" + - "!avm/utilities/pipelines/platform/**" + - "!*/**/README.md" + +env: + modulePath: "avm/ptn/policy-insights/remediation" + workflowPath: ".github/workflows/avm.ptn.policy-insights.remediation.yml" + +concurrency: + group: ${{ github.workflow }} + +jobs: + ########################### + # Initialize pipeline # + ########################### + job_initialize_pipeline: + runs-on: ubuntu-latest + name: "Initialize pipeline" + steps: + - name: "Checkout" + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: "Set input parameters to output variables" + id: get-workflow-param + uses: ./.github/actions/templates/avm-getWorkflowInput + with: + workflowPath: "${{ env.workflowPath}}" + - name: "Get module test file paths" + id: get-module-test-file-paths + uses: ./.github/actions/templates/avm-getModuleTestFiles + with: + modulePath: "${{ env.modulePath }}" + outputs: + workflowInput: ${{ steps.get-workflow-param.outputs.workflowInput }} + moduleTestFilePaths: ${{ steps.get-module-test-file-paths.outputs.moduleTestFilePaths }} + psRuleModuleTestFilePaths: ${{ steps.get-module-test-file-paths.outputs.psRuleModuleTestFilePaths }} + modulePath: "${{ env.modulePath }}" + + ############################## + # Call reusable workflow # + ############################## + call-workflow-passing-data: + name: "Run" + permissions: + id-token: write # For OIDC + contents: write # For release tags + needs: + - job_initialize_pipeline + uses: ./.github/workflows/avm.template.module.yml + with: + workflowInput: "${{ needs.job_initialize_pipeline.outputs.workflowInput }}" + moduleTestFilePaths: "${{ needs.job_initialize_pipeline.outputs.moduleTestFilePaths }}" + psRuleModuleTestFilePaths: "${{ needs.job_initialize_pipeline.outputs.psRuleModuleTestFilePaths }}" + modulePath: "${{ needs.job_initialize_pipeline.outputs.modulePath}}" + secrets: inherit \ No newline at end of file diff --git a/avm/ptn/policy-insights/remediation/README.md b/avm/ptn/policy-insights/remediation/README.md new file mode 100644 index 0000000000..b5fbd56ee5 --- /dev/null +++ b/avm/ptn/policy-insights/remediation/README.md @@ -0,0 +1,601 @@ +# Policy Insights Remediations `[Microsoft.PolicyInsights/remediations]` + +This module deploys a Policy Insights Remediation. + +## Navigation + +- [Resource Types](#Resource-Types) +- [Usage examples](#Usage-examples) +- [Parameters](#Parameters) +- [Outputs](#Outputs) +- [Cross-referenced modules](#Cross-referenced-modules) +- [Data Collection](#Data-Collection) + +## Resource Types + +| Resource Type | API Version | +| :-- | :-- | +| `Microsoft.PolicyInsights/remediations` | [2021-10-01](https://learn.microsoft.com/en-us/azure/templates/Microsoft.PolicyInsights/2021-10-01/remediations) | + +## Usage examples + +The following section provides usage examples for the module, which were used to validate and deploy the module successfully. For a full reference, please review the module's test folder in its repository. + +>**Note**: Each example lists all the required parameters first, followed by the rest - each in alphabetical order. + +>**Note**: To reference the module, please use the following syntax `br/public:avm/ptn/policy-insights/remediation:`. + +- [Policy Remediation (Management Group scope)](#example-1-policy-remediation-management-group-scope) +- [Policy Remediation (Management Group scope)](#example-2-policy-remediation-management-group-scope) +- [Policy Remediation (Resource Group scope)](#example-3-policy-remediation-resource-group-scope) +- [Policy Remediation (Resource Group scope)](#example-4-policy-remediation-resource-group-scope) +- [Policy Remediation (Subscription scope)](#example-5-policy-remediation-subscription-scope) +- [Policy Remediation (Subscription scope)](#example-6-policy-remediation-subscription-scope) + +### Example 1: _Policy Remediation (Management Group scope)_ + +This module runs a Policy remediation task at Management Group scope using minimal parameters. + + +
+ +via Bicep module + +```bicep +module remediation 'br/public:avm/ptn/policy-insights/remediation:' = { + name: 'remediationDeployment' + params: { + // Required parameters + name: 'pirmgmin001' + policyAssignmentId: '' + // Non-required parameters + location: '' + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + } +} +``` + +
+

+ +

+ +via JSON Parameter file + +```json +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + // Required parameters + "name": { + "value": "pirmgmin001" + }, + "policyAssignmentId": { + "value": "" + }, + // Non-required parameters + "location": { + "value": "" + }, + "policyDefinitionReferenceId": { + "value": "Prerequisite_DeployExtensionWindows" + } + } +} +``` + +
+

+ +### Example 2: _Policy Remediation (Management Group scope)_ + +This module runs a Policy remediation task at Management Group scope using common parameters. + + +

+ +via Bicep module + +```bicep +module remediation 'br/public:avm/ptn/policy-insights/remediation:' = { + name: 'remediationDeployment' + params: { + // Required parameters + name: 'pirmgmax001' + policyAssignmentId: '' + // Non-required parameters + failureThresholdPercentage: '0.5' + filtersLocations: [] + location: '' + parallelDeployments: 1 + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + resourceCount: 10 + } +} +``` + +
+

+ +

+ +via JSON Parameter file + +```json +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + // Required parameters + "name": { + "value": "pirmgmax001" + }, + "policyAssignmentId": { + "value": "" + }, + // Non-required parameters + "failureThresholdPercentage": { + "value": "0.5" + }, + "filtersLocations": { + "value": [] + }, + "location": { + "value": "" + }, + "parallelDeployments": { + "value": 1 + }, + "policyDefinitionReferenceId": { + "value": "Prerequisite_DeployExtensionWindows" + }, + "resourceCount": { + "value": 10 + } + } +} +``` + +
+

+ +### Example 3: _Policy Remediation (Resource Group scope)_ + +This module runs a Policy remediation task at Resource Group scope using minimal parameters. + + +

+ +via Bicep module + +```bicep +module remediation 'br/public:avm/ptn/policy-insights/remediation:' = { + name: 'remediationDeployment' + params: { + // Required parameters + name: 'pirrgmin001' + policyAssignmentId: '' + // Non-required parameters + location: '' + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + resourceGroupName: '' + subscriptionId: '' + } +} +``` + +
+

+ +

+ +via JSON Parameter file + +```json +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + // Required parameters + "name": { + "value": "pirrgmin001" + }, + "policyAssignmentId": { + "value": "" + }, + // Non-required parameters + "location": { + "value": "" + }, + "policyDefinitionReferenceId": { + "value": "Prerequisite_DeployExtensionWindows" + }, + "resourceGroupName": { + "value": "" + }, + "subscriptionId": { + "value": "" + } + } +} +``` + +
+

+ +### Example 4: _Policy Remediation (Resource Group scope)_ + +This module runs a Policy remediation task at Resource Group scope using common parameters. + + +

+ +via Bicep module + +```bicep +module remediation 'br/public:avm/ptn/policy-insights/remediation:' = { + name: 'remediationDeployment' + params: { + // Required parameters + name: 'pirrgmax001' + policyAssignmentId: '' + // Non-required parameters + failureThresholdPercentage: '0.5' + filtersLocations: [] + location: '' + parallelDeployments: 1 + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + resourceCount: 10 + resourceDiscoveryMode: 'ReEvaluateCompliance' + resourceGroupName: '' + subscriptionId: '' + } +} +``` + +
+

+ +

+ +via JSON Parameter file + +```json +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + // Required parameters + "name": { + "value": "pirrgmax001" + }, + "policyAssignmentId": { + "value": "" + }, + // Non-required parameters + "failureThresholdPercentage": { + "value": "0.5" + }, + "filtersLocations": { + "value": [] + }, + "location": { + "value": "" + }, + "parallelDeployments": { + "value": 1 + }, + "policyDefinitionReferenceId": { + "value": "Prerequisite_DeployExtensionWindows" + }, + "resourceCount": { + "value": 10 + }, + "resourceDiscoveryMode": { + "value": "ReEvaluateCompliance" + }, + "resourceGroupName": { + "value": "" + }, + "subscriptionId": { + "value": "" + } + } +} +``` + +
+

+ +### Example 5: _Policy Remediation (Subscription scope)_ + +This module runs a Policy remediation task at subscription scope using minimal parameters. + + +

+ +via Bicep module + +```bicep +module remediation 'br/public:avm/ptn/policy-insights/remediation:' = { + name: 'remediationDeployment' + params: { + // Required parameters + name: 'pirsubmin001' + policyAssignmentId: '' + // Non-required parameters + location: '' + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + subscriptionId: '' + } +} +``` + +
+

+ +

+ +via JSON Parameter file + +```json +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + // Required parameters + "name": { + "value": "pirsubmin001" + }, + "policyAssignmentId": { + "value": "" + }, + // Non-required parameters + "location": { + "value": "" + }, + "policyDefinitionReferenceId": { + "value": "Prerequisite_DeployExtensionWindows" + }, + "subscriptionId": { + "value": "" + } + } +} +``` + +
+

+ +### Example 6: _Policy Remediation (Subscription scope)_ + +This module runs a Policy remediation task at subscription scope using common parameters. + + +

+ +via Bicep module + +```bicep +module remediation 'br/public:avm/ptn/policy-insights/remediation:' = { + name: 'remediationDeployment' + params: { + // Required parameters + name: 'pirsubmax001' + policyAssignmentId: '' + // Non-required parameters + failureThresholdPercentage: '0.5' + filtersLocations: [] + location: '' + parallelDeployments: 1 + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + resourceCount: 10 + resourceDiscoveryMode: 'ReEvaluateCompliance' + subscriptionId: '' + } +} +``` + +
+

+ +

+ +via JSON Parameter file + +```json +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + // Required parameters + "name": { + "value": "pirsubmax001" + }, + "policyAssignmentId": { + "value": "" + }, + // Non-required parameters + "failureThresholdPercentage": { + "value": "0.5" + }, + "filtersLocations": { + "value": [] + }, + "location": { + "value": "" + }, + "parallelDeployments": { + "value": 1 + }, + "policyDefinitionReferenceId": { + "value": "Prerequisite_DeployExtensionWindows" + }, + "resourceCount": { + "value": 10 + }, + "resourceDiscoveryMode": { + "value": "ReEvaluateCompliance" + }, + "subscriptionId": { + "value": "" + } + } +} +``` + +
+

+ + +## Parameters + +**Required parameters** + +| Parameter | Type | Description | +| :-- | :-- | :-- | +| [`name`](#parameter-name) | string | Specifies the name of the policy remediation. | +| [`policyAssignmentId`](#parameter-policyassignmentid) | string | The resource ID of the policy assignment that should be remediated. | + +**Optional parameters** + +| Parameter | Type | Description | +| :-- | :-- | :-- | +| [`enableTelemetry`](#parameter-enabletelemetry) | bool | Enable/Disable usage telemetry for module. | +| [`failureThresholdPercentage`](#parameter-failurethresholdpercentage) | string | The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail. | +| [`filtersLocations`](#parameter-filterslocations) | array | The filters that will be applied to determine which resources to remediate. | +| [`location`](#parameter-location) | string | Location deployment metadata. | +| [`managementGroupId`](#parameter-managementgroupid) | string | The target scope for the remediation. The name of the management group for the policy assignment. If not provided, will use the current scope for deployment. | +| [`parallelDeployments`](#parameter-paralleldeployments) | int | Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used. | +| [`policyDefinitionReferenceId`](#parameter-policydefinitionreferenceid) | string | The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition. | +| [`resourceCount`](#parameter-resourcecount) | int | Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used. | +| [`resourceDiscoveryMode`](#parameter-resourcediscoverymode) | string | The way resources to remediate are discovered. Defaults to ExistingNonCompliant if not specified. | +| [`resourceGroupName`](#parameter-resourcegroupname) | string | The target scope for the remediation. The name of the resource group for the policy assignment. | +| [`subscriptionId`](#parameter-subscriptionid) | string | The target scope for the remediation. The subscription ID of the subscription for the policy assignment. | + +### Parameter: `name` + +Specifies the name of the policy remediation. + +- Required: Yes +- Type: string + +### Parameter: `policyAssignmentId` + +The resource ID of the policy assignment that should be remediated. + +- Required: Yes +- Type: string + +### Parameter: `enableTelemetry` + +Enable/Disable usage telemetry for module. + +- Required: No +- Type: bool +- Default: `True` + +### Parameter: `failureThresholdPercentage` + +The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail. + +- Required: No +- Type: string +- Default: `'1'` + +### Parameter: `filtersLocations` + +The filters that will be applied to determine which resources to remediate. + +- Required: No +- Type: array +- Default: `[]` + +### Parameter: `location` + +Location deployment metadata. + +- Required: No +- Type: string +- Default: `[deployment().location]` + +### Parameter: `managementGroupId` + +The target scope for the remediation. The name of the management group for the policy assignment. If not provided, will use the current scope for deployment. + +- Required: No +- Type: string +- Default: `[managementGroup().name]` + +### Parameter: `parallelDeployments` + +Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used. + +- Required: No +- Type: int +- Default: `10` + +### Parameter: `policyDefinitionReferenceId` + +The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition. + +- Required: No +- Type: string +- Default: `''` + +### Parameter: `resourceCount` + +Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used. + +- Required: No +- Type: int +- Default: `500` + +### Parameter: `resourceDiscoveryMode` + +The way resources to remediate are discovered. Defaults to ExistingNonCompliant if not specified. + +- Required: No +- Type: string +- Default: `'ExistingNonCompliant'` +- Allowed: + ```Bicep + [ + 'ExistingNonCompliant' + 'ReEvaluateCompliance' + ] + ``` + +### Parameter: `resourceGroupName` + +The target scope for the remediation. The name of the resource group for the policy assignment. + +- Required: No +- Type: string +- Default: `''` + +### Parameter: `subscriptionId` + +The target scope for the remediation. The subscription ID of the subscription for the policy assignment. + +- Required: No +- Type: string +- Default: `''` + + +## Outputs + +| Output | Type | Description | +| :-- | :-- | :-- | +| `location` | string | The location the resource was deployed into. | +| `name` | string | The name of the remediation. | +| `resourceId` | string | The resource ID of the remediation. | + +## Cross-referenced modules + +_None_ + +## Data Collection + +The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the [repository](https://aka.ms/avm/telemetry). There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at . You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices. diff --git a/avm/ptn/policy-insights/remediation/main.bicep b/avm/ptn/policy-insights/remediation/main.bicep new file mode 100644 index 0000000000..3e6e369e9b --- /dev/null +++ b/avm/ptn/policy-insights/remediation/main.bicep @@ -0,0 +1,156 @@ +metadata name = 'Policy Insights Remediations' +metadata description = 'This module deploys a Policy Insights Remediation.' +metadata owner = 'Azure/module-maintainers' + +targetScope = 'managementGroup' + +// ================ // +// Parameters // +// ================ // + +@sys.description('Required. Specifies the name of the policy remediation.') +param name string + +@sys.description('Optional. The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail.') +param failureThresholdPercentage string = '1' + +@sys.description('Optional. The filters that will be applied to determine which resources to remediate.') +param filtersLocations array = [] + +@sys.description('Optional. Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used.') +@minValue(1) +@maxValue(30) +param parallelDeployments int = 10 + +@sys.description('Optional. Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used.') +@minValue(1) +@maxValue(50000) +param resourceCount int = 500 + +@sys.description('Optional. The way resources to remediate are discovered. Defaults to ExistingNonCompliant if not specified.') +@allowed([ + 'ExistingNonCompliant' + 'ReEvaluateCompliance' +]) +param resourceDiscoveryMode string = 'ExistingNonCompliant' + +@sys.description('Required. The resource ID of the policy assignment that should be remediated.') +param policyAssignmentId string + +@sys.description('Optional. The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition.') +param policyDefinitionReferenceId string = '' + +@sys.description('Optional. The target scope for the remediation. The name of the management group for the policy assignment. If not provided, will use the current scope for deployment.') +param managementGroupId string = managementGroup().name + +@sys.description('Optional. The target scope for the remediation. The subscription ID of the subscription for the policy assignment.') +param subscriptionId string = '' + +@sys.description('Optional. The target scope for the remediation. The name of the resource group for the policy assignment.') +param resourceGroupName string = '' + +@sys.description('Optional. Location deployment metadata.') +param location string = deployment().location + +@sys.description('Optional. Enable/Disable usage telemetry for module.') +param enableTelemetry bool = true + +// ================ // +// Resources // +// ================ // + +resource avmTelemetry 'Microsoft.Resources/deployments@2023-07-01' = + if (enableTelemetry) { + name: take( + '46d3xbcp.ptn.policyinsights-remediation.${replace('-..--..-', '.', '-')}.${substring(uniqueString(deployment().name, location), 0, 4)}', + 64 + ) + location: location + properties: { + mode: 'Incremental' + template: { + '$schema': 'https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#' + contentVersion: '1.0.0.0' + resources: [] + outputs: { + telemetry: { + type: 'String' + value: 'For more information, see https://aka.ms/avm/TelemetryInfo' + } + } + } + } + } + +module remediation_mg 'modules/management-group.bicep' = + if (empty(subscriptionId) && empty(resourceGroupName)) { + name: '${uniqueString(deployment().name, location)}-Remediation-MG-Module' + scope: managementGroup(managementGroupId) + params: { + name: name + location: location + policyAssignmentId: policyAssignmentId + policyDefinitionReferenceId: policyDefinitionReferenceId + filtersLocations: filtersLocations + resourceCount: resourceCount + parallelDeployments: parallelDeployments + failureThresholdPercentage: failureThresholdPercentage + } + } + +module remediation_sub 'modules/subscription.bicep' = + if (!empty(subscriptionId) && empty(resourceGroupName)) { + name: '${uniqueString(deployment().name, location)}-Remediation-Sub-Module' + scope: subscription(subscriptionId) + params: { + name: name + location: location + policyAssignmentId: policyAssignmentId + policyDefinitionReferenceId: policyDefinitionReferenceId + filtersLocations: filtersLocations + resourceCount: resourceCount + resourceDiscoveryMode: resourceDiscoveryMode + parallelDeployments: parallelDeployments + failureThresholdPercentage: failureThresholdPercentage + } + } + +module remediation_rg 'modules/resource-group.bicep' = + if (!empty(resourceGroupName) && !empty(subscriptionId)) { + name: '${uniqueString(deployment().name, location)}-Remediation-RG-Module' + scope: resourceGroup(subscriptionId, resourceGroupName) + params: { + name: name + location: location + policyAssignmentId: policyAssignmentId + policyDefinitionReferenceId: policyDefinitionReferenceId + filtersLocations: filtersLocations + resourceCount: resourceCount + resourceDiscoveryMode: resourceDiscoveryMode + parallelDeployments: parallelDeployments + failureThresholdPercentage: failureThresholdPercentage + } + } + +// ================ // +// Outputs // +// ================ // + +@sys.description('The name of the remediation.') +output name string = empty(subscriptionId) && empty(resourceGroupName) + ? remediation_mg.outputs.name + : (!empty(subscriptionId) && empty(resourceGroupName) ? remediation_sub.outputs.name : remediation_rg.outputs.name) + +@sys.description('The resource ID of the remediation.') +output resourceId string = empty(subscriptionId) && empty(resourceGroupName) + ? remediation_mg.outputs.resourceId + : (!empty(subscriptionId) && empty(resourceGroupName) + ? remediation_sub.outputs.resourceId + : remediation_rg.outputs.resourceId) + +@sys.description('The location the resource was deployed into.') +output location string = empty(subscriptionId) && empty(resourceGroupName) + ? remediation_mg.outputs.location + : (!empty(subscriptionId) && empty(resourceGroupName) + ? remediation_sub.outputs.location + : remediation_rg.outputs.location) diff --git a/avm/ptn/policy-insights/remediation/main.json b/avm/ptn/policy-insights/remediation/main.json new file mode 100644 index 0000000000..ce2990904d --- /dev/null +++ b/avm/ptn/policy-insights/remediation/main.json @@ -0,0 +1,678 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "metadata": { + "_generator": { + "name": "bicep", + "version": "0.26.170.59819", + "templateHash": "9938615640421486630" + }, + "name": "Policy Insights Remediations", + "description": "This module deploys a Policy Insights Remediation.", + "owner": "Azure/module-maintainers" + }, + "parameters": { + "name": { + "type": "string", + "metadata": { + "description": "Required. Specifies the name of the policy remediation." + } + }, + "failureThresholdPercentage": { + "type": "string", + "defaultValue": "1", + "metadata": { + "description": "Optional. The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail." + } + }, + "filtersLocations": { + "type": "array", + "defaultValue": [], + "metadata": { + "description": "Optional. The filters that will be applied to determine which resources to remediate." + } + }, + "parallelDeployments": { + "type": "int", + "defaultValue": 10, + "minValue": 1, + "maxValue": 30, + "metadata": { + "description": "Optional. Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used." + } + }, + "resourceCount": { + "type": "int", + "defaultValue": 500, + "minValue": 1, + "maxValue": 50000, + "metadata": { + "description": "Optional. Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used." + } + }, + "resourceDiscoveryMode": { + "type": "string", + "defaultValue": "ExistingNonCompliant", + "allowedValues": [ + "ExistingNonCompliant", + "ReEvaluateCompliance" + ], + "metadata": { + "description": "Optional. The way resources to remediate are discovered. Defaults to ExistingNonCompliant if not specified." + } + }, + "policyAssignmentId": { + "type": "string", + "metadata": { + "description": "Required. The resource ID of the policy assignment that should be remediated." + } + }, + "policyDefinitionReferenceId": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Optional. The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition." + } + }, + "managementGroupId": { + "type": "string", + "defaultValue": "[managementGroup().name]", + "metadata": { + "description": "Optional. The target scope for the remediation. The name of the management group for the policy assignment. If not provided, will use the current scope for deployment." + } + }, + "subscriptionId": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Optional. The target scope for the remediation. The subscription ID of the subscription for the policy assignment." + } + }, + "resourceGroupName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Optional. The target scope for the remediation. The name of the resource group for the policy assignment." + } + }, + "location": { + "type": "string", + "defaultValue": "[deployment().location]", + "metadata": { + "description": "Optional. Location deployment metadata." + } + }, + "enableTelemetry": { + "type": "bool", + "defaultValue": true, + "metadata": { + "description": "Optional. Enable/Disable usage telemetry for module." + } + } + }, + "resources": [ + { + "condition": "[parameters('enableTelemetry')]", + "type": "Microsoft.Resources/deployments", + "apiVersion": "2023-07-01", + "name": "[take(format('46d3xbcp.ptn.policyinsights-remediation.{0}.{1}', replace('-..--..-', '.', '-'), substring(uniqueString(deployment().name, parameters('location')), 0, 4)), 64)]", + "location": "[parameters('location')]", + "properties": { + "mode": "Incremental", + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "resources": [], + "outputs": { + "telemetry": { + "type": "String", + "value": "For more information, see https://aka.ms/avm/TelemetryInfo" + } + } + } + } + }, + { + "condition": "[and(empty(parameters('subscriptionId')), empty(parameters('resourceGroupName')))]", + "type": "Microsoft.Resources/deployments", + "apiVersion": "2022-09-01", + "name": "[format('{0}-Remediation-MG-Module', uniqueString(deployment().name, parameters('location')))]", + "scope": "[format('Microsoft.Management/managementGroups/{0}', parameters('managementGroupId'))]", + "location": "[deployment().location]", + "properties": { + "expressionEvaluationOptions": { + "scope": "inner" + }, + "mode": "Incremental", + "parameters": { + "name": { + "value": "[parameters('name')]" + }, + "location": { + "value": "[parameters('location')]" + }, + "policyAssignmentId": { + "value": "[parameters('policyAssignmentId')]" + }, + "policyDefinitionReferenceId": { + "value": "[parameters('policyDefinitionReferenceId')]" + }, + "filtersLocations": { + "value": "[parameters('filtersLocations')]" + }, + "resourceCount": { + "value": "[parameters('resourceCount')]" + }, + "parallelDeployments": { + "value": "[parameters('parallelDeployments')]" + }, + "failureThresholdPercentage": { + "value": "[parameters('failureThresholdPercentage')]" + } + }, + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "metadata": { + "_generator": { + "name": "bicep", + "version": "0.26.170.59819", + "templateHash": "12299861631299116532" + }, + "name": "Policy Insights Remediations (Management Group scope)", + "description": "This module starts a Policy Remediation task at a Management Group scope.", + "owner": "Azure/module-maintainers" + }, + "parameters": { + "name": { + "type": "string", + "metadata": { + "description": "Required. Specifies the name of the policy remediation." + } + }, + "failureThresholdPercentage": { + "type": "string", + "defaultValue": "1", + "metadata": { + "description": "Optional. The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail." + } + }, + "filtersLocations": { + "type": "array", + "defaultValue": [], + "metadata": { + "description": "Optional. The filters that will be applied to determine which resources to remediate." + } + }, + "parallelDeployments": { + "type": "int", + "defaultValue": 10, + "minValue": 1, + "maxValue": 30, + "metadata": { + "description": "Optional. Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used." + } + }, + "resourceCount": { + "type": "int", + "defaultValue": 500, + "minValue": 1, + "maxValue": 50000, + "metadata": { + "description": "Optional. Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used." + } + }, + "policyAssignmentId": { + "type": "string", + "metadata": { + "description": "Required. The resource ID of the policy assignment that should be remediated." + } + }, + "policyDefinitionReferenceId": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Optional. The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition." + } + }, + "location": { + "type": "string", + "defaultValue": "[deployment().location]", + "metadata": { + "description": "Optional. Location deployment metadata." + } + } + }, + "resources": [ + { + "type": "Microsoft.PolicyInsights/remediations", + "apiVersion": "2021-10-01", + "name": "[parameters('name')]", + "properties": { + "failureThreshold": { + "percentage": "[json(parameters('failureThresholdPercentage'))]" + }, + "filters": { + "locations": "[parameters('filtersLocations')]" + }, + "parallelDeployments": "[parameters('parallelDeployments')]", + "policyAssignmentId": "[parameters('policyAssignmentId')]", + "policyDefinitionReferenceId": "[parameters('policyDefinitionReferenceId')]", + "resourceCount": "[parameters('resourceCount')]" + } + } + ], + "outputs": { + "name": { + "type": "string", + "metadata": { + "description": "The name of the remediation." + }, + "value": "[parameters('name')]" + }, + "resourceId": { + "type": "string", + "metadata": { + "description": "The resource ID of the remediation." + }, + "value": "[extensionResourceId(managementGroup().id, 'Microsoft.PolicyInsights/remediations', parameters('name'))]" + }, + "managementGroupName": { + "type": "string", + "metadata": { + "description": "The managmeent group of the deployed remediation." + }, + "value": "[managementGroup().name]" + }, + "location": { + "type": "string", + "metadata": { + "description": "The location the resource was deployed into." + }, + "value": "[parameters('location')]" + } + } + } + } + }, + { + "condition": "[and(not(empty(parameters('subscriptionId'))), empty(parameters('resourceGroupName')))]", + "type": "Microsoft.Resources/deployments", + "apiVersion": "2022-09-01", + "name": "[format('{0}-Remediation-Sub-Module', uniqueString(deployment().name, parameters('location')))]", + "subscriptionId": "[parameters('subscriptionId')]", + "location": "[deployment().location]", + "properties": { + "expressionEvaluationOptions": { + "scope": "inner" + }, + "mode": "Incremental", + "parameters": { + "name": { + "value": "[parameters('name')]" + }, + "location": { + "value": "[parameters('location')]" + }, + "policyAssignmentId": { + "value": "[parameters('policyAssignmentId')]" + }, + "policyDefinitionReferenceId": { + "value": "[parameters('policyDefinitionReferenceId')]" + }, + "filtersLocations": { + "value": "[parameters('filtersLocations')]" + }, + "resourceCount": { + "value": "[parameters('resourceCount')]" + }, + "resourceDiscoveryMode": { + "value": "[parameters('resourceDiscoveryMode')]" + }, + "parallelDeployments": { + "value": "[parameters('parallelDeployments')]" + }, + "failureThresholdPercentage": { + "value": "[parameters('failureThresholdPercentage')]" + } + }, + "template": { + "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "metadata": { + "_generator": { + "name": "bicep", + "version": "0.26.170.59819", + "templateHash": "1015297291864726810" + }, + "name": "Policy Insights Remediations (Subscription scope)", + "description": "This module deploys a Policy Insights Remediation on a Subscription scope.", + "owner": "Azure/module-maintainers" + }, + "parameters": { + "name": { + "type": "string", + "metadata": { + "description": "Required. Specifies the name of the policy remediation." + } + }, + "failureThresholdPercentage": { + "type": "string", + "defaultValue": "1", + "metadata": { + "description": "Optional. The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail." + } + }, + "filtersLocations": { + "type": "array", + "defaultValue": [], + "metadata": { + "description": "Optional. The filters that will be applied to determine which resources to remediate." + } + }, + "parallelDeployments": { + "type": "int", + "defaultValue": 10, + "minValue": 1, + "maxValue": 30, + "metadata": { + "description": "Optional. Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used." + } + }, + "resourceCount": { + "type": "int", + "defaultValue": 500, + "minValue": 1, + "maxValue": 50000, + "metadata": { + "description": "Optional. Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used." + } + }, + "resourceDiscoveryMode": { + "type": "string", + "defaultValue": "ExistingNonCompliant", + "allowedValues": [ + "ExistingNonCompliant", + "ReEvaluateCompliance" + ], + "metadata": { + "description": "Optional. The way resources to remediate are discovered. Defaults to ExistingNonCompliant if not specified." + } + }, + "policyAssignmentId": { + "type": "string", + "metadata": { + "description": "Required. The resource ID of the policy assignment that should be remediated." + } + }, + "policyDefinitionReferenceId": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Optional. The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition." + } + }, + "location": { + "type": "string", + "defaultValue": "[deployment().location]", + "metadata": { + "description": "Optional. Location deployment metadata." + } + } + }, + "resources": [ + { + "type": "Microsoft.PolicyInsights/remediations", + "apiVersion": "2021-10-01", + "name": "[parameters('name')]", + "properties": { + "failureThreshold": { + "percentage": "[json(parameters('failureThresholdPercentage'))]" + }, + "filters": { + "locations": "[parameters('filtersLocations')]" + }, + "parallelDeployments": "[parameters('parallelDeployments')]", + "policyAssignmentId": "[parameters('policyAssignmentId')]", + "policyDefinitionReferenceId": "[parameters('policyDefinitionReferenceId')]", + "resourceCount": "[parameters('resourceCount')]", + "resourceDiscoveryMode": "[parameters('resourceDiscoveryMode')]" + } + } + ], + "outputs": { + "name": { + "type": "string", + "metadata": { + "description": "The name of the remediation." + }, + "value": "[parameters('name')]" + }, + "resourceId": { + "type": "string", + "metadata": { + "description": "The resource ID of the remediation." + }, + "value": "[subscriptionResourceId('Microsoft.PolicyInsights/remediations', parameters('name'))]" + }, + "subscriptionName": { + "type": "string", + "metadata": { + "description": "The subscription name of the deployed remediation." + }, + "value": "[subscription().displayName]" + }, + "location": { + "type": "string", + "metadata": { + "description": "The location the resource was deployed into." + }, + "value": "[parameters('location')]" + } + } + } + } + }, + { + "condition": "[and(not(empty(parameters('resourceGroupName'))), not(empty(parameters('subscriptionId'))))]", + "type": "Microsoft.Resources/deployments", + "apiVersion": "2022-09-01", + "name": "[format('{0}-Remediation-RG-Module', uniqueString(deployment().name, parameters('location')))]", + "subscriptionId": "[parameters('subscriptionId')]", + "resourceGroup": "[parameters('resourceGroupName')]", + "properties": { + "expressionEvaluationOptions": { + "scope": "inner" + }, + "mode": "Incremental", + "parameters": { + "name": { + "value": "[parameters('name')]" + }, + "location": { + "value": "[parameters('location')]" + }, + "policyAssignmentId": { + "value": "[parameters('policyAssignmentId')]" + }, + "policyDefinitionReferenceId": { + "value": "[parameters('policyDefinitionReferenceId')]" + }, + "filtersLocations": { + "value": "[parameters('filtersLocations')]" + }, + "resourceCount": { + "value": "[parameters('resourceCount')]" + }, + "resourceDiscoveryMode": { + "value": "[parameters('resourceDiscoveryMode')]" + }, + "parallelDeployments": { + "value": "[parameters('parallelDeployments')]" + }, + "failureThresholdPercentage": { + "value": "[parameters('failureThresholdPercentage')]" + } + }, + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "metadata": { + "_generator": { + "name": "bicep", + "version": "0.26.170.59819", + "templateHash": "10110336327374487357" + }, + "name": "Policy Insights Remediations (Resource Group scope)", + "description": "This module deploys a Policy Insights Remediation on a Resource Group scope.", + "owner": "Azure/module-maintainers" + }, + "parameters": { + "name": { + "type": "string", + "metadata": { + "description": "Required. Specifies the name of the policy remediation." + } + }, + "failureThresholdPercentage": { + "type": "string", + "defaultValue": "1", + "metadata": { + "description": "Optional. The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail." + } + }, + "filtersLocations": { + "type": "array", + "defaultValue": [], + "metadata": { + "description": "Optional. The filters that will be applied to determine which resources to remediate." + } + }, + "parallelDeployments": { + "type": "int", + "defaultValue": 10, + "minValue": 1, + "maxValue": 30, + "metadata": { + "description": "Optional. Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used." + } + }, + "resourceCount": { + "type": "int", + "defaultValue": 500, + "minValue": 1, + "maxValue": 50000, + "metadata": { + "description": "Optional. Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used." + } + }, + "resourceDiscoveryMode": { + "type": "string", + "defaultValue": "ExistingNonCompliant", + "allowedValues": [ + "ExistingNonCompliant", + "ReEvaluateCompliance" + ], + "metadata": { + "description": "Optional. The way resources to remediate are discovered. Defaults to ExistingNonCompliant if not specified." + } + }, + "policyAssignmentId": { + "type": "string", + "metadata": { + "description": "Required. The resource ID of the policy assignment that should be remediated." + } + }, + "policyDefinitionReferenceId": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Optional. The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition." + } + }, + "location": { + "type": "string", + "defaultValue": "[resourceGroup().location]", + "metadata": { + "description": "Optional. Location deployment metadata." + } + } + }, + "resources": [ + { + "type": "Microsoft.PolicyInsights/remediations", + "apiVersion": "2021-10-01", + "name": "[parameters('name')]", + "properties": { + "failureThreshold": { + "percentage": "[json(parameters('failureThresholdPercentage'))]" + }, + "filters": { + "locations": "[parameters('filtersLocations')]" + }, + "parallelDeployments": "[parameters('parallelDeployments')]", + "policyAssignmentId": "[parameters('policyAssignmentId')]", + "policyDefinitionReferenceId": "[parameters('policyDefinitionReferenceId')]", + "resourceCount": "[parameters('resourceCount')]", + "resourceDiscoveryMode": "[parameters('resourceDiscoveryMode')]" + } + } + ], + "outputs": { + "name": { + "type": "string", + "metadata": { + "description": "The name of the remediation." + }, + "value": "[parameters('name')]" + }, + "resourceId": { + "type": "string", + "metadata": { + "description": "The resource ID of the remediation." + }, + "value": "[resourceId('Microsoft.PolicyInsights/remediations', parameters('name'))]" + }, + "resourceGroupName": { + "type": "string", + "metadata": { + "description": "The resource group of the deployed remediation." + }, + "value": "[resourceGroup().name]" + }, + "location": { + "type": "string", + "metadata": { + "description": "The location the resource was deployed into." + }, + "value": "[parameters('location')]" + } + } + } + } + } + ], + "outputs": { + "name": { + "type": "string", + "metadata": { + "description": "The name of the remediation." + }, + "value": "[if(and(empty(parameters('subscriptionId')), empty(parameters('resourceGroupName'))), reference(extensionResourceId(tenantResourceId('Microsoft.Management/managementGroups', parameters('managementGroupId')), 'Microsoft.Resources/deployments', format('{0}-Remediation-MG-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.name.value, if(and(not(empty(parameters('subscriptionId'))), empty(parameters('resourceGroupName'))), reference(subscriptionResourceId(parameters('subscriptionId'), 'Microsoft.Resources/deployments', format('{0}-Remediation-Sub-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.name.value, reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', parameters('subscriptionId'), parameters('resourceGroupName')), 'Microsoft.Resources/deployments', format('{0}-Remediation-RG-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.name.value))]" + }, + "resourceId": { + "type": "string", + "metadata": { + "description": "The resource ID of the remediation." + }, + "value": "[if(and(empty(parameters('subscriptionId')), empty(parameters('resourceGroupName'))), reference(extensionResourceId(tenantResourceId('Microsoft.Management/managementGroups', parameters('managementGroupId')), 'Microsoft.Resources/deployments', format('{0}-Remediation-MG-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.resourceId.value, if(and(not(empty(parameters('subscriptionId'))), empty(parameters('resourceGroupName'))), reference(subscriptionResourceId(parameters('subscriptionId'), 'Microsoft.Resources/deployments', format('{0}-Remediation-Sub-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.resourceId.value, reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', parameters('subscriptionId'), parameters('resourceGroupName')), 'Microsoft.Resources/deployments', format('{0}-Remediation-RG-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.resourceId.value))]" + }, + "location": { + "type": "string", + "metadata": { + "description": "The location the resource was deployed into." + }, + "value": "[if(and(empty(parameters('subscriptionId')), empty(parameters('resourceGroupName'))), reference(extensionResourceId(tenantResourceId('Microsoft.Management/managementGroups', parameters('managementGroupId')), 'Microsoft.Resources/deployments', format('{0}-Remediation-MG-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.location.value, if(and(not(empty(parameters('subscriptionId'))), empty(parameters('resourceGroupName'))), reference(subscriptionResourceId(parameters('subscriptionId'), 'Microsoft.Resources/deployments', format('{0}-Remediation-Sub-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.location.value, reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', parameters('subscriptionId'), parameters('resourceGroupName')), 'Microsoft.Resources/deployments', format('{0}-Remediation-RG-Module', uniqueString(deployment().name, parameters('location')))), '2022-09-01').outputs.location.value))]" + } + } +} \ No newline at end of file diff --git a/avm/ptn/policy-insights/remediation/modules/management-group.bicep b/avm/ptn/policy-insights/remediation/modules/management-group.bicep new file mode 100644 index 0000000000..4a97aeba14 --- /dev/null +++ b/avm/ptn/policy-insights/remediation/modules/management-group.bicep @@ -0,0 +1,73 @@ +metadata name = 'Policy Insights Remediations (Management Group scope)' +metadata description = 'This module starts a Policy Remediation task at a Management Group scope.' +metadata owner = 'Azure/module-maintainers' + +targetScope = 'managementGroup' + +// ================ // +// Parameters // +// ================ // + +@sys.description('Required. Specifies the name of the policy remediation.') +param name string + +@sys.description('Optional. The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail.') +param failureThresholdPercentage string = '1' + +@sys.description('Optional. The filters that will be applied to determine which resources to remediate.') +param filtersLocations array = [] + +@sys.description('Optional. Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used.') +@minValue(1) +@maxValue(30) +param parallelDeployments int = 10 + +@sys.description('Optional. Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used.') +@minValue(1) +@maxValue(50000) +param resourceCount int = 500 + +@sys.description('Required. The resource ID of the policy assignment that should be remediated.') +param policyAssignmentId string + +@sys.description('Optional. The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition.') +param policyDefinitionReferenceId string = '' + +@sys.description('Optional. Location deployment metadata.') +param location string = deployment().location + +// ================ // +// Resources // +// ================ // + +resource remediation 'Microsoft.PolicyInsights/remediations@2021-10-01' = { + name: name + properties: { + failureThreshold: { + percentage: json(failureThresholdPercentage) // The json() function is used to allow specifying a decimal value. + } + filters: { + locations: filtersLocations + } + parallelDeployments: parallelDeployments + policyAssignmentId: policyAssignmentId + policyDefinitionReferenceId: policyDefinitionReferenceId + resourceCount: resourceCount + } +} + +// ================ // +// Outputs // +// ================ // + +@sys.description('The name of the remediation.') +output name string = remediation.name + +@sys.description('The resource ID of the remediation.') +output resourceId string = remediation.id + +@sys.description('The managmeent group of the deployed remediation.') +output managementGroupName string = managementGroup().name + +@sys.description('The location the resource was deployed into.') +output location string = location diff --git a/avm/ptn/policy-insights/remediation/modules/resource-group.bicep b/avm/ptn/policy-insights/remediation/modules/resource-group.bicep new file mode 100644 index 0000000000..7ca140514b --- /dev/null +++ b/avm/ptn/policy-insights/remediation/modules/resource-group.bicep @@ -0,0 +1,81 @@ +metadata name = 'Policy Insights Remediations (Resource Group scope)' +metadata description = 'This module deploys a Policy Insights Remediation on a Resource Group scope.' +metadata owner = 'Azure/module-maintainers' + +targetScope = 'resourceGroup' + +// ================ // +// Parameters // +// ================ // + +@sys.description('Required. Specifies the name of the policy remediation.') +param name string + +@sys.description('Optional. The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail.') +param failureThresholdPercentage string = '1' + +@sys.description('Optional. The filters that will be applied to determine which resources to remediate.') +param filtersLocations array = [] + +@sys.description('Optional. Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used.') +@minValue(1) +@maxValue(30) +param parallelDeployments int = 10 + +@sys.description('Optional. Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used.') +@minValue(1) +@maxValue(50000) +param resourceCount int = 500 + +@sys.description('Optional. The way resources to remediate are discovered. Defaults to ExistingNonCompliant if not specified.') +@allowed([ + 'ExistingNonCompliant' + 'ReEvaluateCompliance' +]) +param resourceDiscoveryMode string = 'ExistingNonCompliant' + +@sys.description('Required. The resource ID of the policy assignment that should be remediated.') +param policyAssignmentId string + +@sys.description('Optional. The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition.') +param policyDefinitionReferenceId string = '' + +@sys.description('Optional. Location deployment metadata.') +param location string = resourceGroup().location + +// ================ // +// Resources // +// ================ // + +resource remediation 'Microsoft.PolicyInsights/remediations@2021-10-01' = { + name: name + properties: { + failureThreshold: { + percentage: json(failureThresholdPercentage) // The json() function is used to allow specifying a decimal value. + } + filters: { + locations: filtersLocations + } + parallelDeployments: parallelDeployments + policyAssignmentId: policyAssignmentId + policyDefinitionReferenceId: policyDefinitionReferenceId + resourceCount: resourceCount + resourceDiscoveryMode: resourceDiscoveryMode + } +} + +// ================ // +// Outputs // +// ================ // + +@sys.description('The name of the remediation.') +output name string = remediation.name + +@sys.description('The resource ID of the remediation.') +output resourceId string = remediation.id + +@sys.description('The resource group of the deployed remediation.') +output resourceGroupName string = resourceGroup().name + +@sys.description('The location the resource was deployed into.') +output location string = location diff --git a/avm/ptn/policy-insights/remediation/modules/subscription.bicep b/avm/ptn/policy-insights/remediation/modules/subscription.bicep new file mode 100644 index 0000000000..17468d8d4e --- /dev/null +++ b/avm/ptn/policy-insights/remediation/modules/subscription.bicep @@ -0,0 +1,81 @@ +metadata name = 'Policy Insights Remediations (Subscription scope)' +metadata description = 'This module deploys a Policy Insights Remediation on a Subscription scope.' +metadata owner = 'Azure/module-maintainers' + +targetScope = 'subscription' + +// ================ // +// Parameters // +// ================ // + +@sys.description('Required. Specifies the name of the policy remediation.') +param name string + +@sys.description('Optional. The remediation failure threshold settings. A number between 0.0 to 1.0 representing the percentage failure threshold. The remediation will fail if the percentage of failed remediation operations (i.e. failed deployments) exceeds this threshold. 0 means that the remediation will stop after the first failure. 1 means that the remediation will not stop even if all deployments fail.') +param failureThresholdPercentage string = '1' + +@sys.description('Optional. The filters that will be applied to determine which resources to remediate.') +param filtersLocations array = [] + +@sys.description('Optional. Determines how many resources to remediate at any given time. Can be used to increase or reduce the pace of the remediation. Can be between 1-30. Higher values will cause the remediation to complete more quickly, but increase the risk of throttling. If not provided, the default parallel deployments value is used.') +@minValue(1) +@maxValue(30) +param parallelDeployments int = 10 + +@sys.description('Optional. Determines the max number of resources that can be remediated by the remediation job. Can be between 1-50000. If not provided, the default resource count is used.') +@minValue(1) +@maxValue(50000) +param resourceCount int = 500 + +@sys.description('Optional. The way resources to remediate are discovered. Defaults to ExistingNonCompliant if not specified.') +@allowed([ + 'ExistingNonCompliant' + 'ReEvaluateCompliance' +]) +param resourceDiscoveryMode string = 'ExistingNonCompliant' + +@sys.description('Required. The resource ID of the policy assignment that should be remediated.') +param policyAssignmentId string + +@sys.description('Optional. The policy definition reference ID of the individual definition that should be remediated. Required when the policy assignment being remediated assigns a policy set definition.') +param policyDefinitionReferenceId string = '' + +@sys.description('Optional. Location deployment metadata.') +param location string = deployment().location + +// ================ // +// Resources // +// ================ // + +resource remediation 'Microsoft.PolicyInsights/remediations@2021-10-01' = { + name: name + properties: { + failureThreshold: { + percentage: json(failureThresholdPercentage) // The json() function is used to allow specifying a decimal value. + } + filters: { + locations: filtersLocations + } + parallelDeployments: parallelDeployments + policyAssignmentId: policyAssignmentId + policyDefinitionReferenceId: policyDefinitionReferenceId + resourceCount: resourceCount + resourceDiscoveryMode: resourceDiscoveryMode + } +} + +// ================ // +// Outputs // +// ================ // + +@sys.description('The name of the remediation.') +output name string = remediation.name + +@sys.description('The resource ID of the remediation.') +output resourceId string = remediation.id + +@sys.description('The subscription name of the deployed remediation.') +output subscriptionName string = subscription().displayName + +@sys.description('The location the resource was deployed into.') +output location string = location diff --git a/avm/ptn/policy-insights/remediation/tests/e2e/mg.defaults/main.test.bicep b/avm/ptn/policy-insights/remediation/tests/e2e/mg.defaults/main.test.bicep new file mode 100644 index 0000000000..eae75d89ef --- /dev/null +++ b/avm/ptn/policy-insights/remediation/tests/e2e/mg.defaults/main.test.bicep @@ -0,0 +1,49 @@ +targetScope = 'managementGroup' + +metadata name = 'Policy Remediation (Management Group scope)' +metadata description = 'This module runs a Policy remediation task at Management Group scope using minimal parameters.' + +// ========== // +// Parameters // +// ========== // + +@sys.description('Optional. The location to deploy resources to.') +param resourceLocation string = deployment().location + +@sys.description('Optional. A short identifier for the kind of deployment. Should be kept short to not run into resource-name length-constraints.') +param serviceShort string = 'pirmgmin' + +@sys.description('Optional. A token to inject into the name of each resource.') +param namePrefix string = '#_namePrefix_#' + +// ============ // +// Dependencies // +// ============ // + +// General resources +// ================= + +resource policySetAssignments 'Microsoft.Authorization/policyAssignments@2022-06-01' = { + name: 'dep-${namePrefix}-psa-${serviceShort}' + identity: { + type: 'SystemAssigned' + } + location: resourceLocation + properties: { + policyDefinitionId: '/providers/Microsoft.Authorization/policySetDefinitions/12794019-7a00-42cf-95c2-882eed337cc8' + } +} + +// ============== // +// Test Execution // +// ============== // + +module testDeployment '../../../main.bicep' = { + name: '${uniqueString(deployment().name)}-test-${serviceShort}' + params: { + name: '${namePrefix}${serviceShort}001' + location: resourceLocation + policyAssignmentId: policySetAssignments.id + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + } +} diff --git a/avm/ptn/policy-insights/remediation/tests/e2e/mg.max/main.test.bicep b/avm/ptn/policy-insights/remediation/tests/e2e/mg.max/main.test.bicep new file mode 100644 index 0000000000..bd89bdf9c0 --- /dev/null +++ b/avm/ptn/policy-insights/remediation/tests/e2e/mg.max/main.test.bicep @@ -0,0 +1,53 @@ +targetScope = 'managementGroup' + +metadata name = 'Policy Remediation (Management Group scope)' +metadata description = 'This module runs a Policy remediation task at Management Group scope using common parameters.' + +// ========== // +// Parameters // +// ========== // + +@sys.description('Optional. The location to deploy resources to.') +param resourceLocation string = deployment().location + +@sys.description('Optional. A short identifier for the kind of deployment. Should be kept short to not run into resource-name length-constraints.') +param serviceShort string = 'pirmgmax' + +@sys.description('Optional. A token to inject into the name of each resource.') +param namePrefix string = '#_namePrefix_#' + +// ============ // +// Dependencies // +// ============ // + +// General resources +// ================= + +resource policySetAssignments 'Microsoft.Authorization/policyAssignments@2022-06-01' = { + name: 'dep-${namePrefix}-psa-${serviceShort}' + identity: { + type: 'SystemAssigned' + } + location: resourceLocation + properties: { + policyDefinitionId: '/providers/Microsoft.Authorization/policySetDefinitions/12794019-7a00-42cf-95c2-882eed337cc8' + } +} + +// ============== // +// Test Execution // +// ============== // + +module testDeployment '../../../main.bicep' = { + name: '${uniqueString(deployment().name)}-test-${serviceShort}' + params: { + name: '${namePrefix}${serviceShort}001' + location: resourceLocation + policyAssignmentId: policySetAssignments.id + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + filtersLocations: [] + resourceCount: 10 + parallelDeployments: 1 + failureThresholdPercentage: '0.5' + } +} diff --git a/avm/ptn/policy-insights/remediation/tests/e2e/rg.defaults/main.test.bicep b/avm/ptn/policy-insights/remediation/tests/e2e/rg.defaults/main.test.bicep new file mode 100644 index 0000000000..d28faf5b67 --- /dev/null +++ b/avm/ptn/policy-insights/remediation/tests/e2e/rg.defaults/main.test.bicep @@ -0,0 +1,63 @@ +targetScope = 'managementGroup' + +metadata name = 'Policy Remediation (Resource Group scope)' +metadata description = 'This module runs a Policy remediation task at Resource Group scope using minimal parameters.' + +// ========== // +// Parameters // +// ========== // + +@sys.description('Optional. The location to deploy resources to.') +param resourceLocation string = deployment().location + +@sys.description('Optional. A short identifier for the kind of deployment. Should be kept short to not run into resource-name length-constraints.') +param serviceShort string = 'pirrgmin' + +@sys.description('Optional. A token to inject into the name of each resource.') +param namePrefix string = '#_namePrefix_#' + +@description('Optional. Subscription ID of the subscription to deploy the resource group.') +param subscriptionId string = '#_subscriptionId_#' + +// ============ // +// Dependencies // +// ============ // + +// General resources +// ================= + +module resourceGroupDeploy 'br/public:avm/res/resources/resource-group:0.2.3' = { + scope: subscription(subscriptionId) + name: '${uniqueString(deployment().name, resourceLocation)}-resourceGroup' + params: { + name: 'dep-${namePrefix}-policy-remediation-${serviceShort}-rg' + location: resourceLocation + } +} + +module policySetAssignments 'br/public:avm/ptn/authorization/policy-assignment:0.1.0' = { + name: '${uniqueString(deployment().name, resourceLocation)}-policySetAssignment-rg' + params: { + name: 'dep-${namePrefix}-psa-${serviceShort}' + subscriptionId: subscriptionId + resourceGroupName: resourceGroupDeploy.outputs.name + location: resourceLocation + policyDefinitionId: '/providers/Microsoft.Authorization/policySetDefinitions/12794019-7a00-42cf-95c2-882eed337cc8' + } +} + +// ============== // +// Test Execution // +// ============== // + +module testDeployment '../../../main.bicep' = { + name: '${uniqueString(deployment().name)}-test-${serviceShort}' + params: { + name: '${namePrefix}${serviceShort}001' + location: resourceLocation + subscriptionId: subscriptionId + resourceGroupName: resourceGroupDeploy.outputs.name + policyAssignmentId: policySetAssignments.outputs.resourceId + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + } +} diff --git a/avm/ptn/policy-insights/remediation/tests/e2e/rg.max/main.test.bicep b/avm/ptn/policy-insights/remediation/tests/e2e/rg.max/main.test.bicep new file mode 100644 index 0000000000..736296908e --- /dev/null +++ b/avm/ptn/policy-insights/remediation/tests/e2e/rg.max/main.test.bicep @@ -0,0 +1,68 @@ +targetScope = 'managementGroup' + +metadata name = 'Policy Remediation (Resource Group scope)' +metadata description = 'This module runs a Policy remediation task at Resource Group scope using common parameters.' + +// ========== // +// Parameters // +// ========== // + +@sys.description('Optional. The location to deploy resources to.') +param resourceLocation string = deployment().location + +@sys.description('Optional. A short identifier for the kind of deployment. Should be kept short to not run into resource-name length-constraints.') +param serviceShort string = 'pirrgmax' + +@sys.description('Optional. A token to inject into the name of each resource.') +param namePrefix string = '#_namePrefix_#' + +@description('Optional. Subscription ID of the subscription to deploy the resource group.') +param subscriptionId string = '#_subscriptionId_#' + +// ============ // +// Dependencies // +// ============ // + +// General resources +// ================= + +module resourceGroupDeploy 'br/public:avm/res/resources/resource-group:0.2.3' = { + scope: subscription(subscriptionId) + name: '${uniqueString(deployment().name, resourceLocation)}-resourceGroup' + params: { + name: 'dep-${namePrefix}-policy-remediation-${serviceShort}-rg' + location: resourceLocation + } +} + +module policySetAssignments 'br/public:avm/ptn/authorization/policy-assignment:0.1.0' = { + name: '${uniqueString(deployment().name, resourceLocation)}-policySetAssignment-rg' + params: { + name: 'dep-${namePrefix}-psa-${serviceShort}' + subscriptionId: subscriptionId + resourceGroupName: resourceGroupDeploy.outputs.name + location: resourceLocation + policyDefinitionId: '/providers/Microsoft.Authorization/policySetDefinitions/12794019-7a00-42cf-95c2-882eed337cc8' + } +} + +// ============== // +// Test Execution // +// ============== // + +module testDeployment '../../../main.bicep' = { + name: '${uniqueString(deployment().name)}-test-${serviceShort}' + params: { + name: '${namePrefix}${serviceShort}001' + location: resourceLocation + subscriptionId: subscriptionId + resourceGroupName: resourceGroupDeploy.outputs.name + policyAssignmentId: policySetAssignments.outputs.resourceId + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + filtersLocations: [] + resourceCount: 10 + resourceDiscoveryMode: 'ReEvaluateCompliance' + parallelDeployments: 1 + failureThresholdPercentage: '0.5' + } +} diff --git a/avm/ptn/policy-insights/remediation/tests/e2e/sub.defaults/main.test.bicep b/avm/ptn/policy-insights/remediation/tests/e2e/sub.defaults/main.test.bicep new file mode 100644 index 0000000000..f82fc9f281 --- /dev/null +++ b/avm/ptn/policy-insights/remediation/tests/e2e/sub.defaults/main.test.bicep @@ -0,0 +1,52 @@ +targetScope = 'managementGroup' + +metadata name = 'Policy Remediation (Subscription scope)' +metadata description = 'This module runs a Policy remediation task at subscription scope using minimal parameters.' + +// ========== // +// Parameters // +// ========== // + +@sys.description('Optional. The location to deploy resources to.') +param resourceLocation string = deployment().location + +@sys.description('Optional. A short identifier for the kind of deployment. Should be kept short to not run into resource-name length-constraints.') +param serviceShort string = 'pirsubmin' + +@sys.description('Optional. A token to inject into the name of each resource.') +param namePrefix string = '#_namePrefix_#' + +@description('Optional. Subscription ID of the target subscription.') +param subscriptionId string = '#_subscriptionId_#' + +// ============ // +// Dependencies // +// ============ // + +// General resources +// ================= + +module policySetAssignments 'br/public:avm/ptn/authorization/policy-assignment:0.1.0' = { + name: '${uniqueString(deployment().name, resourceLocation)}-policySetAssignment-sub' + params: { + name: 'dep-${namePrefix}-psa-${serviceShort}' + subscriptionId: subscriptionId + location: resourceLocation + policyDefinitionId: '/providers/Microsoft.Authorization/policySetDefinitions/12794019-7a00-42cf-95c2-882eed337cc8' + } +} + +// ============== // +// Test Execution // +// ============== // + +module testDeployment '../../../main.bicep' = { + name: '${uniqueString(deployment().name)}-test-${serviceShort}' + params: { + name: '${namePrefix}${serviceShort}001' + location: resourceLocation + subscriptionId: subscriptionId + policyAssignmentId: policySetAssignments.outputs.resourceId + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + } +} diff --git a/avm/ptn/policy-insights/remediation/tests/e2e/sub.max/main.test.bicep b/avm/ptn/policy-insights/remediation/tests/e2e/sub.max/main.test.bicep new file mode 100644 index 0000000000..77979a6c48 --- /dev/null +++ b/avm/ptn/policy-insights/remediation/tests/e2e/sub.max/main.test.bicep @@ -0,0 +1,57 @@ +targetScope = 'managementGroup' + +metadata name = 'Policy Remediation (Subscription scope)' +metadata description = 'This module runs a Policy remediation task at subscription scope using common parameters.' + +// ========== // +// Parameters // +// ========== // + +@sys.description('Optional. The location to deploy resources to.') +param resourceLocation string = deployment().location + +@sys.description('Optional. A short identifier for the kind of deployment. Should be kept short to not run into resource-name length-constraints.') +param serviceShort string = 'pirsubmax' + +@sys.description('Optional. A token to inject into the name of each resource.') +param namePrefix string = '#_namePrefix_#' + +@description('Optional. Subscription ID of the target subscription.') +param subscriptionId string = '#_subscriptionId_#' + +// ============ // +// Dependencies // +// ============ // + +// General resources +// ================= + +module policySetAssignments 'br/public:avm/ptn/authorization/policy-assignment:0.1.0' = { + name: '${uniqueString(deployment().name, resourceLocation)}-policySetAssignment-sub' + params: { + name: 'dep-${namePrefix}-psa-${serviceShort}' + subscriptionId: subscriptionId + location: resourceLocation + policyDefinitionId: '/providers/Microsoft.Authorization/policySetDefinitions/12794019-7a00-42cf-95c2-882eed337cc8' + } +} + +// ============== // +// Test Execution // +// ============== // + +module testDeployment '../../../main.bicep' = { + name: '${uniqueString(deployment().name)}-test-${serviceShort}' + params: { + name: '${namePrefix}${serviceShort}001' + location: resourceLocation + subscriptionId: subscriptionId + policyAssignmentId: policySetAssignments.outputs.resourceId + policyDefinitionReferenceId: 'Prerequisite_DeployExtensionWindows' + filtersLocations: [] + resourceCount: 10 + resourceDiscoveryMode: 'ReEvaluateCompliance' + parallelDeployments: 1 + failureThresholdPercentage: '0.5' + } +} diff --git a/avm/ptn/policy-insights/remediation/version.json b/avm/ptn/policy-insights/remediation/version.json new file mode 100644 index 0000000000..4a115b2fd5 --- /dev/null +++ b/avm/ptn/policy-insights/remediation/version.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://aka.ms/bicep-registry-module-version-file-schema#", + "version": "0.1", + "pathFilters": [ + "./main.json" + ] + }