-
Notifications
You must be signed in to change notification settings - Fork 2
feat: sync app user profile attributes as grant metadata #146
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
Open
c1-dev-bot
wants to merge
3
commits into
main
Choose a base branch
from
feat/app-user-profile-sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||
| "github.com/okta/okta-sdk-golang/v2/okta" | ||||||||||||||||||||||||||||||||||||||||||
| "github.com/okta/okta-sdk-golang/v2/okta/query" | ||||||||||||||||||||||||||||||||||||||||||
| "go.uber.org/zap" | ||||||||||||||||||||||||||||||||||||||||||
| "google.golang.org/protobuf/types/known/structpb" | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type appResourceType struct { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -184,13 +185,21 @@ func (o *appResourceType) listAppGroupGrants( | |||||||||||||||||||||||||||||||||||||||||
| for _, applicationGroupAssignment := range applicationGroupAssignments { | ||||||||||||||||||||||||||||||||||||||||||
| groupID := applicationGroupAssignment.Id | ||||||||||||||||||||||||||||||||||||||||||
| principalID := &v2.ResourceId{ResourceType: resourceTypeGroup.Id, Resource: groupID} | ||||||||||||||||||||||||||||||||||||||||||
| rv = append(rv, sdkGrant.NewGrant(resource, "access", principalID, | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| grantOptions := []sdkGrant.GrantOption{ | ||||||||||||||||||||||||||||||||||||||||||
| sdkGrant.WithAnnotation( | ||||||||||||||||||||||||||||||||||||||||||
| &v2.V1Identifier{ | ||||||||||||||||||||||||||||||||||||||||||
| Id: fmtGrantIdV1(V1MembershipEntitlementID(resource.Id.Resource), groupID), | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Include group assignment profile attributes as grant metadata if available | ||||||||||||||||||||||||||||||||||||||||||
| if profileMetadata := appGroupAssignmentProfileToMetadata(applicationGroupAssignment); profileMetadata != nil { | ||||||||||||||||||||||||||||||||||||||||||
| grantOptions = append(grantOptions, sdkGrant.WithGrantMetadata(profileMetadata)) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| rv = append(rv, sdkGrant.NewGrant(resource, "access", principalID, grantOptions...)) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return rv, annos, bag, nil | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -228,13 +237,21 @@ func (o *appResourceType) listAppUsersGrants( | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| userID := applicationUser.Id | ||||||||||||||||||||||||||||||||||||||||||
| principalID := &v2.ResourceId{ResourceType: resourceTypeUser.Id, Resource: userID} | ||||||||||||||||||||||||||||||||||||||||||
| rv = append(rv, sdkGrant.NewGrant(resource, "access", principalID, | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| grantOptions := []sdkGrant.GrantOption{ | ||||||||||||||||||||||||||||||||||||||||||
| sdkGrant.WithAnnotation( | ||||||||||||||||||||||||||||||||||||||||||
| &v2.V1Identifier{ | ||||||||||||||||||||||||||||||||||||||||||
| Id: fmtGrantIdV1(V1MembershipEntitlementID(resource.Id.Resource), userID), | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Include app user profile attributes as grant metadata if available | ||||||||||||||||||||||||||||||||||||||||||
| if profileMetadata := appUserProfileToMetadata(applicationUser); profileMetadata != nil { | ||||||||||||||||||||||||||||||||||||||||||
| grantOptions = append(grantOptions, sdkGrant.WithGrantMetadata(profileMetadata)) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| rv = append(rv, sdkGrant.NewGrant(resource, "access", principalID, grantOptions...)) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return rv, annos, bag, nil | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -587,6 +604,118 @@ func (o *appResourceType) Get(ctx context.Context, resourceId *v2.ResourceId, pa | |||||||||||||||||||||||||||||||||||||||||
| return resource, annos, nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // appUserProfileToMetadata converts an Okta AppUser's profile into a metadata map | ||||||||||||||||||||||||||||||||||||||||||
| // suitable for attaching to grants. This includes app-specific attributes like | ||||||||||||||||||||||||||||||||||||||||||
| // assigned scopes, app roles, and custom profile fields that are set on the user's | ||||||||||||||||||||||||||||||||||||||||||
| // app assignment in Okta. | ||||||||||||||||||||||||||||||||||||||||||
| func appUserProfileToMetadata(appUser *okta.AppUser) map[string]interface{} { | ||||||||||||||||||||||||||||||||||||||||||
| if appUser == nil || appUser.Profile == nil { | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| profile, ok := appUser.Profile.(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if len(profile) == 0 { | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| metadata := make(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||
| for k, v := range profile { | ||||||||||||||||||||||||||||||||||||||||||
| metadata[k] = toStructpbCompatibleValue(v) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Include scope and status from the app user assignment itself. | ||||||||||||||||||||||||||||||||||||||||||
| if appUser.Scope != "" { | ||||||||||||||||||||||||||||||||||||||||||
| metadata["_scope"] = appUser.Scope | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| if appUser.Status != "" { | ||||||||||||||||||||||||||||||||||||||||||
| metadata["_status"] = appUser.Status | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| if appUser.ExternalId != "" { | ||||||||||||||||||||||||||||||||||||||||||
| metadata["_externalId"] = appUser.ExternalId | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Validate that the metadata can be converted to a structpb.Struct | ||||||||||||||||||||||||||||||||||||||||||
| // to avoid panics in WithGrantMetadata/NewGrant. | ||||||||||||||||||||||||||||||||||||||||||
| if _, err := structpb.NewStruct(metadata); err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return metadata | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // appGroupAssignmentProfileToMetadata converts an Okta ApplicationGroupAssignment's | ||||||||||||||||||||||||||||||||||||||||||
| // profile into a metadata map suitable for attaching to grants. | ||||||||||||||||||||||||||||||||||||||||||
| func appGroupAssignmentProfileToMetadata(assignment *okta.ApplicationGroupAssignment) map[string]interface{} { | ||||||||||||||||||||||||||||||||||||||||||
| if assignment == nil || assignment.Profile == nil { | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| profile, ok := assignment.Profile.(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if len(profile) == 0 { | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| metadata := make(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||
| for k, v := range profile { | ||||||||||||||||||||||||||||||||||||||||||
| metadata[k] = toStructpbCompatibleValue(v) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Validate that the metadata can be converted to a structpb.Struct | ||||||||||||||||||||||||||||||||||||||||||
| // to avoid panics in WithGrantMetadata/NewGrant. | ||||||||||||||||||||||||||||||||||||||||||
| if _, err := structpb.NewStruct(metadata); err != nil { | ||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return metadata | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+663
to
+677
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same panic risk as
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // toStructpbCompatibleValue converts a value to a type compatible with | ||||||||||||||||||||||||||||||||||||||||||
| // structpb.NewStruct. structpb supports: nil, bool, int/uint/float (as float64), | ||||||||||||||||||||||||||||||||||||||||||
| // string, []interface{}, and map[string]interface{}. For unsupported types, | ||||||||||||||||||||||||||||||||||||||||||
| // we fall back to fmt.Sprintf to produce a string representation. | ||||||||||||||||||||||||||||||||||||||||||
| func toStructpbCompatibleValue(v interface{}) interface{} { | ||||||||||||||||||||||||||||||||||||||||||
| switch val := v.(type) { | ||||||||||||||||||||||||||||||||||||||||||
| case nil, bool, float64, string: | ||||||||||||||||||||||||||||||||||||||||||
| return val | ||||||||||||||||||||||||||||||||||||||||||
| case int: | ||||||||||||||||||||||||||||||||||||||||||
| return float64(val) | ||||||||||||||||||||||||||||||||||||||||||
| case int32: | ||||||||||||||||||||||||||||||||||||||||||
| return float64(val) | ||||||||||||||||||||||||||||||||||||||||||
| case int64: | ||||||||||||||||||||||||||||||||||||||||||
| return float64(val) | ||||||||||||||||||||||||||||||||||||||||||
| case uint: | ||||||||||||||||||||||||||||||||||||||||||
| return float64(val) | ||||||||||||||||||||||||||||||||||||||||||
| case uint32: | ||||||||||||||||||||||||||||||||||||||||||
| return float64(val) | ||||||||||||||||||||||||||||||||||||||||||
| case uint64: | ||||||||||||||||||||||||||||||||||||||||||
| return float64(val) | ||||||||||||||||||||||||||||||||||||||||||
| case float32: | ||||||||||||||||||||||||||||||||||||||||||
| return float64(val) | ||||||||||||||||||||||||||||||||||||||||||
| case []interface{}: | ||||||||||||||||||||||||||||||||||||||||||
| result := make([]interface{}, len(val)) | ||||||||||||||||||||||||||||||||||||||||||
| for i, item := range val { | ||||||||||||||||||||||||||||||||||||||||||
| result[i] = toStructpbCompatibleValue(item) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return result | ||||||||||||||||||||||||||||||||||||||||||
| case map[string]interface{}: | ||||||||||||||||||||||||||||||||||||||||||
| result := make(map[string]interface{}) | ||||||||||||||||||||||||||||||||||||||||||
| for k, item := range val { | ||||||||||||||||||||||||||||||||||||||||||
| result[k] = toStructpbCompatibleValue(item) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return result | ||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||
| return fmt.Sprintf("%v", val) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func getApp(ctx context.Context, client *okta.Client, appID string) (*okta.Application, *responseContext, error) { | ||||||||||||||||||||||||||||||||||||||||||
| app, resp, err := client.Application.GetApplication(ctx, appID, okta.NewApplication(), nil) | ||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential panic risk:
WithGrantMetadatacallsstructpb.NewStruct(metadata), and if that returns an error,NewGrantwill panic (seegrant.go:90). While JSON-deserialized profiles should contain only structpb-compatible types, Okta app profiles are arbitrary and could theoretically contain values thatstructpbcan't handle (e.g., deeply nested structures or unexpected types from custom schema extensions).Consider either:
structpb.NewStruct()before passing it, logging and skipping on error, orThis is a low-probability issue since JSON-unmarshaled maps typically contain only compatible types, but since the failure mode is a panic (not a returned error), it's worth hardening.