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

Cache Azure Policy Aliases #1277

Closed
ArmaanMcleod opened this issue Feb 25, 2022 · 7 comments · Fixed by #1337
Closed

Cache Azure Policy Aliases #1277

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

Comments

@ArmaanMcleod
Copy link
Contributor

ArmaanMcleod commented Feb 25, 2022

Related to #181, we should cache Azure Policy Aliases in a JSON file

The format could be something like below:

"Microsoft.AlertsManagement": {
    "smartDetectorAlertRules": {
      "locations": "global",
      "aliasMappings": {
        "Microsoft.AlertsManagement/smartDetectorAlertRules/actionGroups": "properties.actionGroups",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/actionGroups.customEmailSubject": "properties.actionGroups.customEmailSubject",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/actionGroups.customWebhookPayload": "properties.actionGroups.customWebhookPayload",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/actionGroups.groupIds": "properties.actionGroups.groupIds",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/actionGroups.groupIds[*]": "properties.actionGroups.groupIds[*]",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/description": "properties.description",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector": "properties.detector",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector.description": "properties.detector.description",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector.id": "properties.detector.id",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector.imagePaths": "properties.detector.imagePaths",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector.imagePaths[*]": "properties.detector.imagePaths[*]",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector.name": "properties.detector.name",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector.parameters": "properties.detector.parameters",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector.supportedResourceTypes": "properties.detector.supportedResourceTypes",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/detector.supportedResourceTypes[*]": "properties.detector.supportedResourceTypes[*]",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/frequency": "properties.frequency",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/scope": "properties.scope",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/scope[*]": "properties.scope[*]",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/severity": "properties.severity",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/state": "properties.state",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/throttling": "properties.throttling",
        "Microsoft.AlertsManagement/smartDetectorAlertRules/throttling.duration": "properties.throttling.duration"
      },
      "apiVersions": [
        "2018-02-01-privatepreview",
        "2019-03-01",
        "2019-06-01",
        "2021-04-01"
      ]
    }

Which would allow us to quickly lookup property paths for a given alias.

The other option was to use Get-AzPolicyAlias, but this would require an API call everytime an alias is needed, which would lead to slower performance.

@ArmaanMcleod ArmaanMcleod added the enhancement New feature or request label Feb 25, 2022
@ArmaanMcleod ArmaanMcleod self-assigned this Feb 25, 2022
@BernieWhite
Copy link
Collaborator

@ArmaanMcleod Let's be aware of any performance cost of the index. We don't really want to do any iteration down resource types to find the aliases.

@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Feb 26, 2022

@ArmaanMcleod Let's be aware of any performance cost of the index. We don't really want to do any iteration down resource types to find the aliases.

@BernieWhite I think performance cost should be fine, since we can get namespace & resource type from the alias, then index the full alias to get the path. This should be a constant time lookup since no iteration is involved.

Another idea is not even caring about the namespace and resource types, and just dumping aliasMappings as a simple key:value object in the file. That would make things even simpler in terms of lookup and serialization/deserialization. Just not sure if we need to keep the other information.

What do you think?

@BernieWhite
Copy link
Collaborator

@ArmaanMcleod Yes, that might reduce memory and allocations.

@ArmaanMcleod
Copy link
Contributor Author

@BernieWhite The issue with it is some aliases like Microsoft.Compute/imageId are duplicated across resources. Wouldn't reliably be able to keep alias name as a key for everything.

I think I will keep the first approach for now and think of a better way down the line.

@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Feb 26, 2022

I guess another thing to consider is if we should replace the aliases with the path when assignments are exported, or if the aliases should be expanded when the assignments are being visited.

To me it would be easier to just convert them when JSON rules are being emitted after the policy rule is visited.

@BernieWhite
Copy link
Collaborator

I guess another thing to consider is if we should replace the aliases with the path when assignments are exported, or if the aliases should be expanded when the assignments are being visited.

To me it would be easier to just convert them when JSON rules are being emitted after the policy rule is visited.

Agreed.

@ArmaanMcleod
Copy link
Contributor Author

Will also add a github actions workflow for this.

This was referenced Mar 30, 2022
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