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

--include-role-assignment not working #573

Closed
giuliohome opened this issue Nov 10, 2024 · 6 comments · Fixed by #574 or #576
Closed

--include-role-assignment not working #573

giuliohome opened this issue Nov 10, 2024 · 6 comments · Fixed by #574 or #576
Labels
bug Something isn't working

Comments

@giuliohome
Copy link

I have tried

aztfexport rg --include-role-assignment my-rg

and I've found no role assignment in the generated main.tf

while they are present and I can see them from az role assignment list --resource-group my-rg and they are directly assigned to the resource group.

@giuliohome giuliohome changed the title --include-resource-group not working --include-role-assignment not working Nov 10, 2024
@giuliohome
Copy link
Author

Repro:

  • create my-rg resource group
  • assign the owner as shown in the screenshot, from Access control (IAM) > Role assignements
  • run the command reported below
  • check the main.tf

Image

$ aztfexport rg --include-role-assignment --output-dir tf_test -n my-rg 
$ cat tf_test/main.tf 
resource "azurerm_resource_group" "res-0" {
  location = "westeurope"
  name     = "my-rg"
}

@giuliohome
Copy link
Author

Also this command returns all the info about the role assignment

az graph query -q "AuthorizationResources 
    | where type == 'microsoft.authorization/roleassignments' 
    and properties.scope == '/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}'"

@giuliohome
Copy link
Author

Thank you very much for the prompt support!

@giuliohome
Copy link
Author

The role assignment is still missing after the fix, if the resource group is empty, in that case the list of resources contains the rg but not the ra.

$ aztfexport rg --append --include-role-assignment --output-dir tf_test
_fix -n my-rg
⣯  Importing resources...
(1/1) Importing /subscriptions/53695a56-bab1-42e1-b477-56901ef22e79/resourceGroups/my-rg as azurerm_resource_group.res-0

On the other side I confirm the same role assignment is exported when the resource group is not empty, so the root cause has been identified somehow.
I think the issue should be reopened, thanks.

@giuliohome
Copy link
Author

giuliohome commented Nov 11, 2024

I believe the issue is that, when the resource group is empty, azlist currently needs the --authorization-scope-filter flag (e.g. "AtScopeAboveAndBelow" ) and --arg-table "AuthorizationResources" to retrieve role assignments. These flags aren’t included when using rg instead of query.

In fact, the following command works:

$ aztfexport query --arg-authorization-scope-filter "AtScopeAboveAndBelow" --include-role-assignment --arg-table "AuthorizationResources" --output-dir tf_test -n "properties.scope == '/subscriptions/53695a56-bab1-42e1-b477-56901ef22e79/resourcegroups/my-rg'"

The output file tf_test/main.aztfexport.tf contains the expected azurerm_role_assignment resource in that case.

When resources are present, azlist may capture role assignments due to broader enumeration within the resource group. Therefore, instead of programmatically adding the resource group after receiving an empty result (which is too late in the process), a more reliable fix would be to adjust azlist to ensure it includes the resource group scope directly. This would make azlist consistently retrieve role assignments at the group level, regardless of content.

Fundamentally, a resource group belongs to ResourceContainers not to Resources.

$ azlist -s '53695a56-bab1-42e1-b477-56901ef22e79' --extension Microsoft.Authorization/roleAssignments  --table ResourceContainers 'name == "my-rg"'
/subscriptions/53695a56-bab1-42e1-b477-56901ef22e79/resourceGroups/my-rg
/subscriptions/53695a56-bab1-42e1-b477-56901ef22e79/resourceGroups/my-rg/providers/Microsoft.Authorization/roleAssignments/7706404e-2d54-48c6-ba34-08ec3b0d5335

@magodo
Copy link
Collaborator

magodo commented Nov 12, 2024

@giuliohome You're right! I missed the case that an empty resource group case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants