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

Add Resources for Claims Mapping Policy Management #733

Closed
wants to merge 2 commits into from

Conversation

computeracer
Copy link
Contributor

Adds support for the claims mapping policy and claims mapping policy assignment resources so these can be
managed with Terraform. This depends on the hamilton sdk support being merged first and then this branch can be updated with the correct vendor dependencies.

Related to:

@webframp
Copy link

Very open to redesigning the interface of this one based on feedback, we're beginning to test this already with some internal use cases

Copy link
Member

@manicminer manicminer left a comment

Choose a reason for hiding this comment

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

Hi @computeracer, thanks again for your work on this and in the SDK. Your upstream PR is now merged, released and updated in the provider, so you can rebase this onto main and it should then be able to pass.

I've made a preliminary review, if you can take a look at the items below and rebase the branch, I'll be happy to take another look with a view to merging. Thanks!

docs/resources/claims_mapping_policy.md Outdated Show resolved Hide resolved
docs/resources/claims_mapping_policy_assignment.md Outdated Show resolved Hide resolved
docs/resources/claims_mapping_policy.md Outdated Show resolved Hide resolved
internal/services/serviceprincipals/registration.go Outdated Show resolved Hide resolved
Comment on lines +88 to +83
resource "azuread_claims_mapping_policy" "test2" {
definition = [
"{\"ClaimsMappingPolicy\":{\"Version\":1,\"IncludeBasicClaimSet\":\"false\",\"ClaimsSchema\": [{\"Source\":\"user\",\"ID\":\"employeeid\",\"SamlClaimType\":\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name\",\"JwtClaimType\":\"name\"},{\"Source\":\"company\",\"ID\":\"tenantcountry\",\"SamlClaimType\":\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country\",\"JwtClaimType\":\"country\"}]}}"
]
description = "%[1]s"
display_name = "integration-%[1]s"
}
Copy link
Member

Choose a reason for hiding this comment

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

To avoid repetition, we can substitute a config here from ServicePrincipalClaimsMappingPolicy (e.g. basicClaimsMappingPolicy)

Choose a reason for hiding this comment

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

In principle I completely understand the need to dedupe stuff like this for future maintainability, so to make sure I understand correctly here, you want to re-use the test definition from here https://github.com/hashicorp/terraform-provider-azuread/pull/733/files#diff-673ecbaaaf4f1fe043eda4ebc250325ec41d602d5992220d3b2db8f6bf2fa1d4R42 ?

Only asking to clarify because in the basicClaimsMappingPolicy case it's used for the basic test while here in updateClaimsMappingPolicyAssignment it's used for the update test.

Should we extract out the single resource block for re-use as a constant string (or similar) or did you have some other way to re-use in mind? Do you know of other places in the tests where this has been done so we'd have an example? We want to make sure this gets updated the way you're thinking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the good feedback. I updated the other concerns. I'll try to look at this one Monday.

Copy link
Contributor Author

@computeracer computeracer Mar 17, 2022

Choose a reason for hiding this comment

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

When working through the refactoring I found our tests were broken.
Seems they were broken even before I began to work through the suggestions.
After doing some debugging I found the odata.ID is nil
when it goes to post to msgraph.

(dlv) locals
status = 0
policy = github.com/manicminer/hamilton/msgraph.ClaimsMappingPolicy {DirectoryObject: (*"github.com/manicminer/hamilton/msgraph.DirectoryObject")(0xc000270268), Definition: *[]string nil, Description: *string nil,...+2 more}
checkPolicyAlreadyExists = github.com/manicminer/hamilton/msgraph.(*ServicePrincipalsClient).AssignClaimsMappingPolicy.func1
(dlv) print policy.ID
*"ssssssss-ssss-ssss-ssss-ssssssssss"
(dlv) ls
> github.com/manicminer/hamilton/msgraph.(*ServicePrincipalsClient).AssignClaimsMappingPolicy() ./vendor/github.com/manicminer/hamilton/msgraph/serviceprincipals.go:371 (PC: 0xbddb07)
   366:                                 return o.Error.Match(odata.ErrorAddedObjectReferencesAlreadyExist)
   367:                         }
   368:                         return false
   369:                 }
   370:
=> 371:                 body, err := json.Marshal(DirectoryObject{ODataId: policy.ODataId})
   372:                 if err != nil {
   373:                         return status, fmt.Errorf("json.Marshal(): %v", err)
   374:                 }
   375:
   376:                 _, status, _, err = c.BaseClient.Post(ctx, PostHttpRequestInput{

This field is needed according to
https://docs.microsoft.com/en-us/graph/api/serviceprincipal-post-claimsmappingpolicies?view=graph-rest-1.0&tabs=http#request-body

I wonder if we missed something when we applied this suggestion
manicminer/hamilton#147 (comment)

Any ideas on what we may have missed @manicminer?

I think we found the issue. The original code referenced in the previous discussion was building the odata.ID based on the ID manually, where the applied suggestion no longer did that. By reading the claims mapping policy in the terraform assignment create function, we get a full claims mapping policy id with the odata id properly set. Now to try the refactor 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@manicminer I eliminated duplication with the create function for the assignment, but kept the 2nd one so simulate the update. Please let me know what you think at this point. Thank you again for your reviews.

Adds support for the claims mapping policy resource so these can be
managed with Terraform.

Related to:
- manicminer/hamilton#147
- hashicorp#644
- https://docs.microsoft.com/en-us/graph/api/resources/claimsmappingpolicy?view=graph-rest-1.0
@computeracer computeracer force-pushed the claimsmappingpolicy branch 2 times, most recently from 0f98bbc to e970f75 Compare March 17, 2022 20:03
Adds support for the claims mapping policy assignment resource so
claims mapping policies can be assigned to a service principle
with Terraform.

Related to:
- manicminer/hamilton#147
- hashicorp#644
- https://docs.microsoft.com/en-us/graph/api/serviceprincipal-post-claimsmappingpolicies?view=graph-rest-1.0&tabs=http
@manicminer
Copy link
Member

manicminer commented Apr 7, 2022

Hey @computeracer, @webframp, sorry for the delay in circling back on this. Thanks again for your work on this and for making the changes. This is mostly looking good; I've refactored it a little to split the azuread_claims_mapping_policy resource out into its own service package, and renamed the assignment resource to reflect that it's in the serviceprincipals package, along with some test fixes.

I was unable to push to your fork, so in the interest of expedience I reopened this as #766, preserving your original commits. This will go out in a release later today. Thanks again!

@manicminer manicminer closed this Apr 7, 2022
manicminer added a commit that referenced this pull request Apr 8, 2022
@webframp
Copy link

webframp commented Apr 8, 2022

You're awesome @manicminer, thanks for the work! If we can improve anything about the way we contribute here I'd love to chat about it on slack sometime

@manicminer
Copy link
Member

manicminer commented Apr 14, 2022

@webframp Thanks, this was a really great contribution. Feel free to open PRs early and often for feedback, and reach out any time on Slack :)

@github-actions
Copy link

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 May 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants