-
Notifications
You must be signed in to change notification settings - Fork 1
[BB-1481] Add grants to iam_roles #91
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
68c68da
Add grants to roles for iam_users
mateovespConductor d0c6452
Make grants more tolerable if access is denied and add nitpick commen…
mateovespConductor 9b89387
Change the values of ci test so that the actions are executed correctly
mateovespConductor f01cf8a
Address review feedback: cleanup ARN parsing logic
mateovespConductor 4324052
Add grant expansion for role grants
mateovespConductor a975e9e
nit: correct func comment
mateovespConductor 70314e2
fix: support wildcard actions for AssumeRole detection
mateovespConductor 4bdfe78
Revert: support wildcard actions for AssumeRole detection
mateovespConductor 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
Some comments aren't visible on the classic Files Changed page.
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
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
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 |
|---|---|---|
|
|
@@ -3,15 +3,20 @@ package connector | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "path" | ||
|
|
||
| awsSdk "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/aws/arn" | ||
| "github.com/aws/aws-sdk-go-v2/service/iam" | ||
| iamTypes "github.com/aws/aws-sdk-go-v2/service/iam/types" | ||
| v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" | ||
| "github.com/conductorone/baton-sdk/pkg/annotations" | ||
| "github.com/conductorone/baton-sdk/pkg/pagination" | ||
| entitlementSdk "github.com/conductorone/baton-sdk/pkg/types/entitlement" | ||
| "github.com/conductorone/baton-sdk/pkg/types/grant" | ||
| resourceSdk "github.com/conductorone/baton-sdk/pkg/types/resource" | ||
| "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -99,15 +104,118 @@ func (o *roleResourceType) Entitlements(_ context.Context, resource *v2.Resource | |
| annos.Update(&v2.V1Identifier{ | ||
| Id: V1MembershipEntitlementID(resource.Id), | ||
| }) | ||
| member := entitlementSdk.NewAssignmentEntitlement(resource, roleAssignmentEntitlement, entitlementSdk.WithGrantableTo(resourceTypeIAMGroup, resourceTypeSSOUser)) | ||
| member := entitlementSdk.NewAssignmentEntitlement(resource, roleAssignmentEntitlement, entitlementSdk.WithGrantableTo( | ||
| resourceTypeIAMUser, | ||
| resourceTypeRole, | ||
| resourceTypeIAMGroup, | ||
| resourceTypeSSOUser, | ||
| )) | ||
| member.Description = fmt.Sprintf("Can assume the %s role in AWS", resource.DisplayName) | ||
| member.Annotations = annos | ||
| member.DisplayName = fmt.Sprintf("%s Role", resource.DisplayName) | ||
| return []*v2.Entitlement{member}, "", nil, nil | ||
| } | ||
|
|
||
| func (o *roleResourceType) Grants(_ context.Context, _ *v2.Resource, _ *pagination.Token) ([]*v2.Grant, string, annotations.Annotations, error) { | ||
| return nil, "", nil, nil | ||
| func (o *roleResourceType) Grants( | ||
| ctx context.Context, | ||
| resource *v2.Resource, | ||
| _ *pagination.Token, | ||
| ) ([]*v2.Grant, string, annotations.Annotations, error) { | ||
| if resource == nil || resource.Id == nil || resource.Id.Resource == "" { | ||
| return nil, "", nil, fmt.Errorf("invalid role resource: missing resource id") | ||
| } | ||
| l := ctxzap.Extract(ctx) | ||
|
|
||
| iamClient := o.iamClient | ||
| if resource.ParentResourceId != nil { | ||
| var err error | ||
| iamClient, err = o.awsClientFactory.GetIAMClient(ctx, resource.ParentResourceId.Resource) | ||
|
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. Just a comment or request for any changes and not any issue. I think this is fine, I don't think these make any API calls to create the clients and they are cached. I don't think this type of cache would be a problem on a lambda, but not 100% sure |
||
| if err != nil { | ||
| return nil, "", nil, fmt.Errorf("aws-connector: GetIAMClient failed: %w", err) | ||
| } | ||
| } | ||
| if iamClient == nil { | ||
| return nil, "", nil, fmt.Errorf("no iam client available") | ||
| } | ||
|
|
||
| parsedARN, err := arn.Parse(resource.Id.Resource) | ||
| if err != nil { | ||
| return nil, "", nil, fmt.Errorf("invalid role ARN: %w", err) | ||
| } | ||
|
|
||
| roleName := path.Base(parsedARN.Resource) | ||
| if roleName == "" || roleName == "/" || roleName == "." { | ||
| return nil, "", nil, fmt.Errorf("invalid role resource in ARN: %s", resource.Id.Resource) | ||
| } | ||
|
|
||
| roleResp, err := iamClient.GetRole(ctx, &iam.GetRoleInput{ | ||
| RoleName: awsSdk.String(roleName), | ||
| }) | ||
| if err != nil { | ||
| l.Error("baton-aws: failed to get role details, skipping grants for this role", | ||
| zap.String("role_name", roleName), | ||
| zap.Error(err), | ||
| ) | ||
| return nil, "", nil, nil | ||
| } | ||
|
|
||
| if roleResp == nil || roleResp.Role == nil { | ||
| l.Warn("baton-aws: GetRole returned empty role", zap.String("role_name", roleName)) | ||
| return nil, "", nil, nil | ||
| } | ||
|
|
||
| if roleResp.Role.AssumeRolePolicyDocument == nil { | ||
| l.Debug("role has no AssumeRolePolicyDocument, returning no grants", | ||
| zap.String("role_name", roleName), | ||
| ) | ||
| return nil, "", nil, nil | ||
| } | ||
|
|
||
| principals, err := extractTrustPrincipals( | ||
| awsSdk.ToString(roleResp.Role.AssumeRolePolicyDocument), | ||
| ) | ||
| if err != nil { | ||
| return nil, "", nil, fmt.Errorf("failed to extract principals for role %s: %w", roleName, err) | ||
| } | ||
|
|
||
| var grants []*v2.Grant | ||
| for _, principalARN := range principals { | ||
| principalResourceType, principalID, ok := detectPrincipalResource(principalARN) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| principal, errCreateResource := resourceSdk.NewResourceID(principalResourceType, principalID) | ||
| if errCreateResource != nil { | ||
| l.Error("baton-aws: failed to create principal resource, skipping grant", | ||
| zap.Error(errCreateResource), | ||
| zap.String("principal_arn", principalARN), | ||
| ) | ||
| continue | ||
| } | ||
|
|
||
| var grantAnnos annotations.Annotations | ||
| if principalResourceType == resourceTypeRole { | ||
| grantAnnos.Update(&v2.GrantExpandable{ | ||
| EntitlementIds: []string{ | ||
| fmt.Sprintf("%s:%s:%s", resourceTypeRole.Id, principalID, roleAssignmentEntitlement), | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| newGrant := grant.NewGrant( | ||
| resource, | ||
| roleAssignmentEntitlement, | ||
| principal, | ||
| ) | ||
|
|
||
| if len(grantAnnos) > 0 { | ||
| newGrant.Annotations = grantAnnos | ||
| } | ||
| grants = append(grants, newGrant) | ||
| } | ||
|
|
||
| return grants, "", nil, nil | ||
| } | ||
|
|
||
| func iamRoleBuilder(iamClient *iam.Client, awsClientFactory *AWSClientFactory) *roleResourceType { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.