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

DXCDT-432: auth0_role_permissions resource #583

Merged
merged 9 commits into from
May 18, 2023

Conversation

willvedd
Copy link
Contributor

@willvedd willvedd commented May 16, 2023

🔧 Changes

Introducing the auth0_role_permissions resource which enables management of all role permissions in a single block.

Unlike the auth0_role_permission resource (#582) which manages on a per-permission basis, this resource manages all of a role's permissions. In addition to offering a flexible way of expressing role permissions, out-of-band changes can be observed with a single import command rather than multiple imports per permission.

Remaining a draft until split into two smaller PRs

📚 References

🔬 Testing

Added suite of integration tests.

📝 Checklist

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

@willvedd willvedd requested a review from a team as a code owner May 16, 2023 23:22
@willvedd willvedd marked this pull request as draft May 16, 2023 23:28
@codecov-commenter
Copy link

codecov-commenter commented May 16, 2023

Codecov Report

Merging #583 (9f7a550) into main (ac04ae9) will decrease coverage by 0.04%.
The diff coverage is 84.44%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #583      +/-   ##
==========================================
- Coverage   87.28%   87.24%   -0.04%     
==========================================
  Files          69       70       +1     
  Lines       10559    10693     +134     
==========================================
+ Hits         9216     9329     +113     
- Misses       1021     1037      +16     
- Partials      322      327       +5     
Impacted Files Coverage Δ
internal/provider/provider.go 100.00% <ø> (ø)
internal/auth0/role/resource_permissions.go 84.32% <84.32%> (ø)
internal/auth0/role/resource.go 81.86% <100.00%> (ø)

@@ -113,11 +113,11 @@ resource auth0_role the_one {
}
`

func TestAccRolePermissions(t *testing.T) {
func TestAccRoleResourcePermissions(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necessary to rename this test to not collide with the new resource.

@willvedd willvedd marked this pull request as ready for review May 17, 2023 17:36
mutex.Lock(roleID)
defer mutex.Unlock(roleID)

permissions, err := api.Role.Permissions(roleID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... 🤔 This will remove all the permissions, not just the ones assigned through this resource. I think it should be okay, however in the auth0_user_roles we do

userRolesToRemove := data.Get("roles").(*schema.Set).List()
	var rmRoles []*management.Role
	for _, rmRole := range userRolesToRemove {
		role := &management.Role{ID: auth0.String(rmRole.(string))}
		rmRoles = append(rmRoles, role)
	}

The two approaches technically should be exactly the same as we should manage all the permissions through this and have nothing left on the API.

However, there is one edge case where:

  1. You attach permissions to a role. (but the role has actually more permissions set on the API)
  2. You do a read in another terraform plan and you're gonna see the diff between what you have in the config and what's on remote.
  3. If you don't add those extra permissions to the config and immediately do a terraform destroy, with the logic we have now it will remove even the others that are untracked, whereas the solution above to fetch them again from the config will only remove the ones that we set in the config and leave the others that were untracked and shown in the diff.

What do you think we should do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think it would be an anti-pattern for folks to adopt that workflow, it's a safer and low-cost to only delete the ones within management purview.

Config: acctest.ParseTestName(testAccRolePermissionNoneAssigned, strings.ToLower(t.Name())),
},
{
RefreshState: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impostor!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary otherwise the data source doesn't get the change reflected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see that now 👍🏻 , indeed the execution flow on a terraform apply is:

  1. read and refresh state
  2. creations
  3. updates
  4. deletions

so we need another refresh state after the deletion.

@willvedd willvedd requested a review from sergiught May 17, 2023 22:51
Base automatically changed from DXCDT-431-role-permission-resource to main May 18, 2023 09:27
@sergiught sergiught force-pushed the DXCDT-432-role-permissions-resource branch from 86ab1f8 to 9f7a550 Compare May 18, 2023 09:46
@sergiught sergiught merged commit f26a446 into main May 18, 2023
@sergiught sergiught deleted the DXCDT-432-role-permissions-resource branch May 18, 2023 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants