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

[Draft] [Bug] azure_role_definition - Better error handling when importing azure role definitions #27082

Conversation

cdituri
Copy link
Contributor

@cdituri cdituri commented Aug 17, 2024

Community Note

  • Please vote on this PR by adding a 👍 reaction to the original PR to help the community and maintainers prioritize for review
  • Please do not leave comments along the lines of "+1", "me too" or "any updates", they generate extra noise for PR followers and do not help prioritize for review

Description

Fixes a bug, in the parsing of azure role definition strings into RoleDefinitionIds, which leads to a crash. Also added a unit test.

Update: I added !fixup commits which make the parsing adhere to the Azure api docs more strictly, but which allow for case insensitivity. Also enhanced the unit test methodology and added additional test data for better coverage. Lastly, parse.RoleDefinitionIds() no longer relies on strings.Spilt.

PR Checklist

  • I have followed the guidelines in our Contributing Documentation.
  • I have checked to ensure there aren't other open Pull Requests for the same update/change.
  • I have checked if my changes close any open issues. If so please include appropriate closing keywords below.
  • I have updated/added Documentation as required written in a helpful and kind way to assist users that may be unfamiliar with the resource / data source.
  • I have used a meaningful PR title to help maintainers and other users understand this change and help prevent duplicate work.
    For example: “resource_name_here - description of change e.g. adding property new_property_name_here

Changes to existing Resource / Data Source

  • I have added an explanation of what my changes do and why I'd like you to include them (This may be covered by linking to an issue above, but may benefit from additional explanation).
  • I have written new tests for my resource or datasource changes & updated any relevent documentation.
  • I have successfully run tests with my changes locally. If not, please provide details on testing challenges that prevented you running the tests.
  • (For changes that include a state migration only). I have manually tested the migration path between relevant versions of the provider.

Testing

  • My submission includes Test coverage as described in the Contribution Guide and the tests pass. (if this is not possible for any reason, please include details of why you did or could not add test coverage)
Running tool: C:\Program Files\Go\bin\go.exe test -timeout 30s -run ^TestParseRoleDefinitionID$ github.com/hashicorp/terraform-provider-azurerm/internal/services/authorization/parse

=== RUN   TestParseRoleDefinitionID
Running tool: C:\Program Files\Go\bin\go.exe test -timeout 30s -run ^TestParseRoleDefinitionID$ github.com/hashicorp/terraform-provider-azurerm/internal/services/authorization/parse

=== RUN   TestParseRoleDefinitionID
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/12345678-1234-1234-1234-1234567890ab|/subscriptions/00000000-0000-0000-0000-000000000000", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/12345678-1234-1234-1234-1234567890ab"
        Scope: "/subscriptions/00000000-0000-0000-0000-000000000000"
        Role Definition: "12345678-1234-1234-1234-1234567890ab")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/12345678-1234-1234-1234-1234567890ab|/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/valid.resource()-_group", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/12345678-1234-1234-1234-1234567890ab"
        Scope: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/valid.resource()-_group"
        Role Definition: "12345678-1234-1234-1234-1234567890ab")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/providers/Microsoft.Authorization/roleDefinitions/12345678-1234-1234-1234-1234567890ab|/subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/providers/Microsoft.Authorization/roleDefinitions/12345678-1234-1234-1234-1234567890ab"
        Scope: "/subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM"
        Role Definition: "12345678-1234-1234-1234-1234567890ab")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/providers/Microsoft.Authorization/rOlEdEfiNiTiOns/AB65ee35-Dc39-43aC-86a0-aCCaaDf4abcd|/subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/providers/Microsoft.Authorization/rOlEdEfiNiTiOns/AB65ee35-Dc39-43aC-86a0-aCCaaDf4abcd"
        Scope: "/subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM"
        Role Definition: "AB65ee35-Dc39-43aC-86a0-aCCaaDf4abcd")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/00000000-0000-0000-0000-000000000000|/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/invalid.resource()-_group.", ShouldParse false
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:153: [DEBUG] parse failed as expected, got "could not parse scope from Role Definition ID, invalid format \"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/invalid.resource()-_group.\""
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-dc39-43ac-86a0-accaadf4abcd|/providers/Microsoft.Management/managementGroups/Some-Management-Group", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-dc39-43ac-86a0-accaadf4abcd"
        Scope: "/providers/Microsoft.Management/managementGroups/Some-Management-Group"
        Role Definition: "ab65ee35-dc39-43ac-86a0-accaadf4abcd")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/pRoVidErs/MiCrOsOft.AuThORiZaTiOn/RoLeDeFinItiOns/ab65ee35-DC39-43AC-86a0-accaadf4abcd|/PrOviDeRs/MiCrOsOfT.MaNaGeMeNt/maNAGementGroups/Some-Management-Group", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/pRoVidErs/MiCrOsOft.AuThORiZaTiOn/RoLeDeFinItiOns/ab65ee35-DC39-43AC-86a0-accaadf4abcd"
        Scope: "/PrOviDeRs/MiCrOsOfT.MaNaGeMeNt/maNAGementGroups/Some-Management-Group"
        Role Definition: "ab65ee35-DC39-43AC-86a0-accaadf4abcd")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-dc39-43ac-86a0-accaadf4abcd|/providers/Microsoft.Management/managementGroups/Some-Management-Group/subscriptions/12345678-1234-1234-1234-1234567890ab", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-dc39-43ac-86a0-accaadf4abcd"
        Scope: "/providers/Microsoft.Management/managementGroups/Some-Management-Group/subscriptions/12345678-1234-1234-1234-1234567890ab"
        Role Definition: "ab65ee35-dc39-43ac-86a0-accaadf4abcd")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-dc39-43ac-86a0-accaadf4abcd|/providers/Microsoft.Management/managementGroups/Some-Management-Group/subscriptions/12345678-1234-1234-1234-1234567890ab/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-dc39-43ac-86a0-accaadf4abcd"
        Scope: "/providers/Microsoft.Management/managementGroups/Some-Management-Group/subscriptions/12345678-1234-1234-1234-1234567890ab/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM"
        Role Definition: "ab65ee35-dc39-43ac-86a0-accaadf4abcd")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-dc39-43ac-86a0-accaadf4abcd|/", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-dc39-43ac-86a0-accaadf4abcd"
        Scope: "/"
        Role Definition: "ab65ee35-dc39-43ac-86a0-accaadf4abcd")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/providers/Microsoft.Authorization/rOlEdEfiNiTiOns/AB65ee35-Dc39-43aC-86a0-aCCaaDf4abcd|/", ShouldParse true
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:147: [DEBUG] parse succeeded as expected, got:
        Role Definition (Resource ID: "/providers/Microsoft.Authorization/rOlEdEfiNiTiOns/AB65ee35-Dc39-43aC-86a0-aCCaaDf4abcd"
        Scope: "/"
        Role Definition: "AB65ee35-Dc39-43aC-86a0-aCCaaDf4abcd")
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "12345678-1234-1234-1234-1234567890ab|/providers/Microsoft.Management/managementGroups/Some-Management-Group", ShouldParse false
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:153: [DEBUG] parse failed as expected, got "could not parse Role Definition ID, invalid format \"12345678-1234-1234-1234-1234567890ab\""
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "12345678-1234-inva-lid1-|/providers/Microsoft.Management/managementGroups/Some-Management-Group", ShouldParse false
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:153: [DEBUG] parse failed as expected, got "could not parse Role Definition ID, invalid format \"12345678-1234-inva-lid1-\""
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "12345678-nonsense", ShouldParse false
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:153: [DEBUG] parse failed as expected, got "could not parse Role Definition ID, invalid format \"12345678-nonsense\""
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:136: [DEBUG] parsing "/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-INVALID-86a0-accaadf4abcd|/", ShouldParse false
    c:\Go\src\github.com\hashicorp\terraform-provider-azurerm\internal\services\authorization\parse\role_definition_test.go:153: [DEBUG] parse failed as expected, got "could not parse Role Definition ID, invalid format \"/providers/Microsoft.Authorization/roleDefinitions/ab65ee35-INVALID-86a0-accaadf4abcd\""
--- PASS: TestParseRoleDefinitionID (0.00s)
PASS
ok      github.com/hashicorp/terraform-provider-azurerm/internal/services/authorization/parse   0.132s

For state migrations please test the changes locally and provide details here, such as the versions involved in testing the migration path. For further details on testing state migration changes please see our guide on state migrations in the contributor documentation. -->

Change Log

Below please provide what should go into the changelog (if anything) conforming to the Changelog Format documented here.

This is a (please select all that apply):

  • Bug Fix
  • New Feature (ie adding a service, resource, or data source)
  • Enhancement
  • Breaking Change

Related Issue(s)

Fixes #26682

@cdituri cdituri force-pushed the cdituri/better-error-handling-when-importing-azure-role-definitions branch from 1966d46 to da091a2 Compare August 17, 2024 18:17
@github-actions github-actions bot added size/M and removed size/S labels Aug 17, 2024
@cdituri cdituri changed the title azure_role_definition - Better error handling when importing azure role definitions [Bug] azure_role_definition - Better error handling when importing azure role definitions Aug 17, 2024
@cdituri
Copy link
Contributor Author

cdituri commented Aug 17, 2024

I've marked this PR as [Draft] and will refrain from further commits to give maintainers a chance to comment and provide guidance.

Part of the difficultly in testing has been that, previously, the range of allowable user-supplied values was wider and there was no unit test. So there's no way to be certain what values users may have used in the field. Thus, if this PR is acceptable it should probably be considered a breaking change.

I've also been hitting additional cases which I had not originally considered. This can be seen in the evolution of the commit history. The first commit addresses the reporter's immediate issue, and further !fixups make the parsing more strict and address additional cases. Thank you for the consideration. Cheers.

@cdituri cdituri force-pushed the cdituri/better-error-handling-when-importing-azure-role-definitions branch from 0606d8a to b40bddf Compare August 18, 2024 18:23
@cdituri cdituri force-pushed the cdituri/better-error-handling-when-importing-azure-role-definitions branch from b40bddf to 4a5c3c3 Compare August 21, 2024 15:19
@cdituri cdituri changed the title [Bug] azure_role_definition - Better error handling when importing azure role definitions [WIP] [Bug] azure_role_definition - Better error handling when importing azure role definitions Aug 21, 2024
@cdituri cdituri changed the title [WIP] [Bug] azure_role_definition - Better error handling when importing azure role definitions [Draft] [Bug] azure_role_definition - Better error handling when importing azure role definitions Aug 21, 2024
@cdituri cdituri marked this pull request as draft August 22, 2024 19:34
@rcskosir rcskosir added the bug label Oct 14, 2024
@katbyte
Copy link
Collaborator

katbyte commented Oct 17, 2024

@cdituri - is this PR ready for review?

@stephybun
Copy link
Member

Since this has been sitting in draft for a while and we haven't heard back I'm going to close this PR for the moment. Let us know once you're ready to pick this back up again for us to review, we can reopen this then. Thanks!

@stephybun stephybun closed this Dec 9, 2024
Copy link

github-actions bot commented Jan 9, 2025

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for better error handling when importing azure role definitions
4 participants