Skip to content

Commit

Permalink
Fix an issue with MaxItems
Browse files Browse the repository at this point in the history
  • Loading branch information
bt353 committed Dec 1, 2023
1 parent 87ed734 commit 99716eb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/handlers_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (s *server) UserListHandler(w http.ResponseWriter, r *http.Request) {

// TODO check if bucket exists and fail if it doesn't?
users := []*iam.User{}
foundGroups, err := iamService.ListGroups(r.Context(), &iam.ListGroupsInput{MaxItems: aws.Int64(1000)}, bucket)
foundGroups, err := iamService.ListGroups(r.Context(), &iam.ListGroupsInput{}, bucket)
if err != nil {
log.Errorf("there was an error listing groups %s", err)
}
Expand Down
20 changes: 19 additions & 1 deletion iam/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,29 @@ func (i *IAM) ListGroups(ctx context.Context, input *iam.ListGroupsInput, bucket
if err != nil {
return []*iam.Group{}, apierror.New(apierror.ErrInternalError, "unknown error", err)
}
isTruncated := groups.IsTruncated
groupsList := groups.Groups

if isTruncated != nil && *isTruncated {
for ok := true; ok; ok = *isTruncated {
input.Marker = groups.Marker
groups, err = i.Service.ListGroupsWithContext(ctx, input)
if err != nil {
return []*iam.Group{}, apierror.New(apierror.ErrInternalError, "unknown error", err)
}
if *groups.IsTruncated != false {
isTruncated = groups.IsTruncated
groupsList = append(groupsList, groups.Groups...)
} else {
break
}
}
}

log.Debugf("listing iam groups for account %+v", groups)
var outGroups []*iam.Group

for _, group := range groups.Groups {
for _, group := range groupsList {
log.Debugf("checking if %s contains %s", aws.StringValue(group.GroupName), bucket)
if strings.Contains(aws.StringValue(group.GroupName), bucket) {
outGroups = append(outGroups, group)
Expand Down
73 changes: 72 additions & 1 deletion iam/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ var testFormatGroupNameInputs = []TestFormatGroupNameSet{
},
}

var testListGroupsDataMarkers = []string{
"mymarker1",
"mymarker2",
"mymarker3",
}

var testListGroupsData = []*iam.Group{
{
Arn: aws.String("arn:aws:iam::12345678910:group/testsite.yalepages.org-spinup-BktAdmGrp"),
Expand Down Expand Up @@ -166,7 +172,63 @@ func (m *mockIAMClient) ListGroupsWithContext(ctx context.Context, input *iam.Li
return nil, m.err
}

return &iam.ListGroupsOutput{Groups: testListGroupsData}, nil
if input.Marker == nil && input.MaxItems == nil {
return &iam.ListGroupsOutput{Groups: testListGroupsData}, nil
}

if input.Marker == nil && input.MaxItems != nil {
maxItems := aws.Int64Value(input.MaxItems) << 32
itemsLen := len(testListGroupsData)
g := int(maxItems)

var groups []*iam.Group

if g > itemsLen {
g = itemsLen
}

for i := 0; i < g; i++ {
groups = append(groups, testListGroupsData[i])
}

return &iam.ListGroupsOutput{Groups: groups}, nil
}

if input.Marker != nil && input.MaxItems != nil {
markerIndex := -1
startIndex := -1
itemsLen := len(testListGroupsData)

for i := 0; i < len(testListGroupsDataMarkers); i++ {
m := testListGroupsDataMarkers[i]
if m == *input.Marker {
markerIndex = i
}
}

if markerIndex == -1 {
return &iam.ListGroupsOutput{}, nil
}

startIndex = markerIndex
maxItems := aws.Int64Value(input.MaxItems) << 32
itemsLen = len(testListGroupsData) - startIndex
g := int(maxItems)

var groups []*iam.Group

if g > itemsLen {
g = itemsLen
}

for i := startIndex; i < g; i++ {
groups = append(groups, testListGroupsData[i])
}

return &iam.ListGroupsOutput{Groups: groups}, nil
}

return &iam.ListGroupsOutput{}, nil
}

func (m *mockIAMClient) CreateGroupWithContext(ctx context.Context, input *iam.CreateGroupInput, opts ...request.Option) (*iam.CreateGroupOutput, error) {
Expand Down Expand Up @@ -238,6 +300,15 @@ func TestListGroups(t *testing.T) {
t.Errorf("expected %+v, got %+v", testListGroupsExpected, listResult)
}

listResultMaxItemsConstrain, err := i.ListGroups(context.TODO(), &iam.ListGroupsInput{MaxItems: aws.Int64(1)}, "testsite.yalepages.org")
if err != nil {
t.Errorf("expected nil error, got %s", err)
}

if !reflect.DeepEqual(listResultMaxItemsConstrain, testListGroupsExpected) {
t.Errorf("expected %+v, got %+v", testListGroupsExpected, listResultMaxItemsConstrain)
}

listResult, err = i.ListGroups(context.TODO(), &iam.ListGroupsInput{}, "anothersite.yalepages.org")
if err != nil {
t.Errorf("expected nil error, got %s", err)
Expand Down

0 comments on commit 99716eb

Please sign in to comment.