-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): support IAM Groups (#368)
* without tests and testdata * change testdata * mockgen * change testdata sh * remove unused processes * operation tests * unit tests * typo in tests * typo * fix deploy.sh
- Loading branch information
Showing
13 changed files
with
1,290 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package operation | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types" | ||
"github.com/go-to-k/delstack/pkg/client" | ||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/sync/semaphore" | ||
) | ||
|
||
var _ IOperator = (*IamGroupOperator)(nil) | ||
|
||
type IamGroupOperator struct { | ||
client client.IIam | ||
resources []*types.StackResourceSummary | ||
} | ||
|
||
func NewIamGroupOperator(client client.IIam) *IamGroupOperator { | ||
return &IamGroupOperator{ | ||
client: client, | ||
resources: []*types.StackResourceSummary{}, | ||
} | ||
} | ||
|
||
func (o *IamGroupOperator) AddResource(resource *types.StackResourceSummary) { | ||
o.resources = append(o.resources, resource) | ||
} | ||
|
||
func (o *IamGroupOperator) GetResourcesLength() int { | ||
return len(o.resources) | ||
} | ||
|
||
func (o *IamGroupOperator) DeleteResources(ctx context.Context) error { | ||
eg, ctx := errgroup.WithContext(ctx) | ||
sem := semaphore.NewWeighted(int64(runtime.NumCPU())) | ||
|
||
for _, Group := range o.resources { | ||
Group := Group | ||
if err := sem.Acquire(ctx, 1); err != nil { | ||
return err | ||
} | ||
eg.Go(func() error { | ||
defer sem.Release(1) | ||
|
||
return o.DeleteIamGroup(ctx, Group.PhysicalResourceId) | ||
}) | ||
} | ||
|
||
return eg.Wait() | ||
} | ||
|
||
func (o *IamGroupOperator) DeleteIamGroup(ctx context.Context, GroupName *string) error { | ||
exists, err := o.client.CheckGroupExists(ctx, GroupName) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
return nil | ||
} | ||
|
||
users, err := o.client.GetGroupUsers(ctx, GroupName) | ||
if err != nil { | ||
return err | ||
} | ||
if len(users) > 0 { | ||
if err := o.client.RemoveUsersFromGroup(ctx, GroupName, users); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := o.client.DeleteGroup(ctx, GroupName); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,251 @@ | ||
package operation | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
cfnTypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types" | ||
"github.com/aws/aws-sdk-go-v2/service/iam/types" | ||
"github.com/go-to-k/delstack/internal/io" | ||
"github.com/go-to-k/delstack/pkg/client" | ||
gomock "github.com/golang/mock/gomock" | ||
) | ||
|
||
/* | ||
Test Cases | ||
*/ | ||
|
||
func TestIamGroupOperator_DeleteIamGroup(t *testing.T) { | ||
io.NewLogger(false) | ||
|
||
type args struct { | ||
ctx context.Context | ||
groupName *string | ||
} | ||
|
||
cases := []struct { | ||
name string | ||
args args | ||
prepareMockFn func(m *client.MockIIam) | ||
want error | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "delete group successfully", | ||
args: args{ | ||
ctx: context.Background(), | ||
groupName: aws.String("test"), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("test")).Return(true, nil) | ||
m.EXPECT().GetGroupUsers(gomock.Any(), aws.String("test")).Return( | ||
[]types.User{ | ||
{ | ||
UserName: aws.String("UserName1"), | ||
}, | ||
{ | ||
UserName: aws.String("UserName2"), | ||
}, | ||
}, nil) | ||
m.EXPECT().RemoveUsersFromGroup(gomock.Any(), aws.String("test"), gomock.Any()).Return(nil) | ||
m.EXPECT().DeleteGroup(gomock.Any(), aws.String("test")).Return(nil) | ||
}, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "delete group failure for CheckGroupExists errors", | ||
args: args{ | ||
ctx: context.Background(), | ||
groupName: aws.String("test"), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("test")).Return(false, fmt.Errorf("GetGroupError")) | ||
}, | ||
want: fmt.Errorf("GetGroupError"), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "delete group failure for CheckGroupExists (not exists)", | ||
args: args{ | ||
ctx: context.Background(), | ||
groupName: aws.String("test"), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("test")).Return(false, nil) | ||
}, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "delete group failure for GetGroupUsers errors", | ||
args: args{ | ||
ctx: context.Background(), | ||
groupName: aws.String("test"), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("test")).Return(true, nil) | ||
m.EXPECT().GetGroupUsers(gomock.Any(), aws.String("test")).Return(nil, fmt.Errorf("GetGroupUsersError")) | ||
}, | ||
want: fmt.Errorf("GetGroupUsersError"), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "delete group failure for RemoveUsersFromGroup errors", | ||
args: args{ | ||
ctx: context.Background(), | ||
groupName: aws.String("test"), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("test")).Return(true, nil) | ||
m.EXPECT().GetGroupUsers(gomock.Any(), aws.String("test")).Return( | ||
[]types.User{ | ||
{ | ||
UserName: aws.String("UserName1"), | ||
}, | ||
{ | ||
UserName: aws.String("UserName2"), | ||
}, | ||
}, nil) | ||
m.EXPECT().RemoveUsersFromGroup(gomock.Any(), aws.String("test"), gomock.Any()).Return(fmt.Errorf("RemoveUsersFromGroupError")) | ||
}, | ||
want: fmt.Errorf("RemoveUsersFromGroupError"), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "delete group successfully for GetGroupUsers with zero length", | ||
args: args{ | ||
ctx: context.Background(), | ||
groupName: aws.String("test"), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("test")).Return(true, nil) | ||
m.EXPECT().GetGroupUsers(gomock.Any(), aws.String("test")).Return([]types.User{}, nil) | ||
m.EXPECT().DeleteGroup(gomock.Any(), aws.String("test")).Return(nil) | ||
}, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "delete group failure for DeleteGroup errors", | ||
args: args{ | ||
ctx: context.Background(), | ||
groupName: aws.String("test"), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("test")).Return(true, nil) | ||
m.EXPECT().GetGroupUsers(gomock.Any(), aws.String("test")).Return( | ||
[]types.User{ | ||
{ | ||
UserName: aws.String("UserName1"), | ||
}, | ||
{ | ||
UserName: aws.String("UserName2"), | ||
}, | ||
}, nil) | ||
m.EXPECT().RemoveUsersFromGroup(gomock.Any(), aws.String("test"), gomock.Any()).Return(nil) | ||
m.EXPECT().DeleteGroup(gomock.Any(), aws.String("test")).Return(fmt.Errorf("DeleteGroupError")) | ||
}, | ||
want: fmt.Errorf("DeleteGroupError"), | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
iamMock := client.NewMockIIam(ctrl) | ||
tt.prepareMockFn(iamMock) | ||
|
||
iamGroupOperator := NewIamGroupOperator(iamMock) | ||
|
||
err := iamGroupOperator.DeleteIamGroup(tt.args.ctx, tt.args.groupName) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("error = %#v, wantErr %#v", err.Error(), tt.wantErr) | ||
return | ||
} | ||
if tt.wantErr && err.Error() != tt.want.Error() { | ||
t.Errorf("err = %#v, want %#v", err.Error(), tt.want.Error()) | ||
return | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIamGroupOperator_DeleteResourcesForIamGroup(t *testing.T) { | ||
io.NewLogger(false) | ||
|
||
type args struct { | ||
ctx context.Context | ||
} | ||
|
||
cases := []struct { | ||
name string | ||
args args | ||
prepareMockFn func(m *client.MockIIam) | ||
want error | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "delete resources successfully", | ||
args: args{ | ||
ctx: context.Background(), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("PhysicalResourceId1")).Return(true, nil) | ||
m.EXPECT().GetGroupUsers(gomock.Any(), aws.String("PhysicalResourceId1")).Return( | ||
[]types.User{ | ||
{ | ||
UserName: aws.String("UserName1"), | ||
}, | ||
{ | ||
UserName: aws.String("UserName2"), | ||
}, | ||
}, nil) | ||
m.EXPECT().RemoveUsersFromGroup(gomock.Any(), aws.String("PhysicalResourceId1"), gomock.Any()).Return(nil) | ||
m.EXPECT().DeleteGroup(gomock.Any(), aws.String("PhysicalResourceId1")).Return(nil) | ||
}, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "delete resources failure", | ||
args: args{ | ||
ctx: context.Background(), | ||
}, | ||
prepareMockFn: func(m *client.MockIIam) { | ||
m.EXPECT().CheckGroupExists(gomock.Any(), aws.String("PhysicalResourceId1")).Return(false, fmt.Errorf("GetGroupError")) | ||
}, | ||
want: fmt.Errorf("GetGroupError"), | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
iamMock := client.NewMockIIam(ctrl) | ||
tt.prepareMockFn(iamMock) | ||
|
||
iamGroupOperator := NewIamGroupOperator(iamMock) | ||
iamGroupOperator.AddResource(&cfnTypes.StackResourceSummary{ | ||
LogicalResourceId: aws.String("LogicalResourceId1"), | ||
ResourceStatus: "DELETE_FAILED", | ||
ResourceType: aws.String("AWS::IAM::Group"), | ||
PhysicalResourceId: aws.String("PhysicalResourceId1"), | ||
}) | ||
|
||
err := iamGroupOperator.DeleteResources(tt.args.ctx) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("error = %#v, wantErr %#v", err.Error(), tt.wantErr) | ||
return | ||
} | ||
if tt.wantErr && err.Error() != tt.want.Error() { | ||
t.Errorf("err = %#v, want %#v", err.Error(), tt.want.Error()) | ||
return | ||
} | ||
}) | ||
} | ||
} |
This file contains 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.