Skip to content

Commit

Permalink
Addressing issues from golint
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPates committed Aug 28, 2024
1 parent 2df0226 commit 672dc59
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 62 deletions.
11 changes: 7 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Execute() {
}
}

// Handler for when executing as a lambda
func Handler(ctx context.Context, event events.CodePipelineEvent) (string, error) {
log.Debug(event)
err := rootCmd.Execute()
Expand All @@ -87,6 +88,7 @@ func Handler(ctx context.Context, event events.CodePipelineEvent) (string, error

if cfg.IsLambdaRunningInCodePipeline {
log.Info("Lambda has been invoked by CodePipeline")
rtnMessage := ""

if err != nil {
// notify codepipeline and mark its job execution as Failure
Expand All @@ -107,7 +109,7 @@ func Handler(ctx context.Context, event events.CodePipelineEvent) (string, error
if cplErr != nil {
log.Fatalf(errors.Wrap(err, "Failed to update CodePipeline jobID status").Error())
}
return "Failure", err
rtnMessage = "Failure"
} else {
log.Info("Notifying CodePipeline and mark its job execution as Success")
jobID := event.CodePipelineJob.ID
Expand All @@ -122,16 +124,17 @@ func Handler(ctx context.Context, event events.CodePipelineEvent) (string, error
if cplErr != nil {
log.Fatalf(errors.Wrap(err, "Failed to update CodePipeline jobID status").Error())
}
return "Success", nil
rtnMessage = "Success"
}
} else {
if err != nil {
log.Fatalf(errors.Wrap(err, "Notifying Lambda and mark this execution as Failure").Error())
return "Failure", err
rtnMessage = "Failure"
} else {
return "Success", nil
rtnMessage = "Success"
}
}
return rtnMessage, err
}

func init() {
Expand Down
13 changes: 8 additions & 5 deletions internal/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ import (
)

var (
// User not found error
ErrUserNotFound = errors.New("user not found")
// Group not found error
ErrGroupNotFound = errors.New("group not found")
// User not specified error
ErrUserNotSpecified = errors.New("user not specified")
)

type ErrHttpNotOK struct {
type ErrHTTPNotOK struct {
StatusCode int
}

func (e *ErrHttpNotOK) Error() string {
func (e *ErrHTTPNotOK) Error() string {
return fmt.Sprintf("status of http response was %d", e.StatusCode)
}

Expand All @@ -62,15 +65,15 @@ type Client interface {
}

type client struct {
httpClient HttpClient
httpClient HTTPClient
endpointURL *url.URL
bearerToken string
}

// NewClient creates a new client to talk with AWS SSO's SCIM endpoint. It
// requires a http.Client{} as well as the URL and bearer token from the
// console. If the URL is not parsable, an error will be thrown.
func NewClient(c HttpClient, config *Config) (Client, error) {
func NewClient(c HTTPClient, config *Config) (Client, error) {
u, err := url.Parse(config.Endpoint)
if err != nil {
return nil, err
Expand Down Expand Up @@ -118,7 +121,7 @@ func (c *client) sendRequestWithBody(method string, url string, body interface{}

// If we get a non-2xx status code, raise that via an error
if resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusNoContent {
err = &ErrHttpNotOK{resp.StatusCode}
err = &ErrHTTPNotOK{resp.StatusCode}
}

return
Expand Down
18 changes: 9 additions & 9 deletions internal/aws/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestNewClient(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: ":foo",
Expand All @@ -86,7 +86,7 @@ func TestSendRequestBadUrl(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: "https://scim.example.com/",
Expand All @@ -104,7 +104,7 @@ func TestSendRequestBadStatusCode(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: "https://scim.example.com/",
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestSendRequestCheckAuthHeader(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: "https://scim.example.com/",
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestSendRequestWithBodyCheckHeaders(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: "https://scim.example.com/",
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestClient_FindUserByEmail(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: "https://scim.example.com/",
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestClient_FindGroupByDisplayName(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: "https://scim.example.com/",
Expand Down Expand Up @@ -362,7 +362,7 @@ func TestClient_CreateUser(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: "https://scim.example.com/",
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestClient_UpdateUser(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

x := mock.NewMockIHttpClient(ctrl)
x := mock.NewIHTTPClient(ctrl)

c, err := NewClient(x, &Config{
Endpoint: "https://scim.example.com/",
Expand Down
4 changes: 2 additions & 2 deletions internal/aws/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package aws

import "net/http"

// HttpClient is a generic HTTP Do interface
type HttpClient interface {
// HTTPClient is a generic HTTP Do interface
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
28 changes: 14 additions & 14 deletions internal/aws/mock/mock_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ import (
"github.com/golang/mock/gomock"
)

// MockIHttpClient is a mock of IHttpClient interface
type MockIHttpClient struct {
// IHTTPClient is a mock of IHTTPClient interface
type IHTTPClient struct {
ctrl *gomock.Controller
recorder *MockIHttpClientMockRecorder
recorder *IHTTPClientMockRecorder
}

// MockIHttpClientMockRecorder is the mock recorder for MockIHttpClient
type MockIHttpClientMockRecorder struct {
mock *MockIHttpClient
// IHTTPClientMockRecorder is the mock recorder for IHTTPClient
type IHTTPClientMockRecorder struct {
mock *IHTTPClient
}

// NewMockIHttpClient creates a new mock instance
func NewMockIHttpClient(ctrl *gomock.Controller) *MockIHttpClient {
mock := &MockIHttpClient{ctrl: ctrl}
mock.recorder = &MockIHttpClientMockRecorder{mock}
// NewIHTTPClient creates a new mock instance
func NewIHTTPClient(ctrl *gomock.Controller) *IHTTPClient {
mock := &IHTTPClient{ctrl: ctrl}
mock.recorder = &IHTTPClientMockRecorder{mock}
return mock
}

// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockIHttpClient) EXPECT() *MockIHttpClientMockRecorder {
func (m *IHTTPClient) EXPECT() *IHTTPClientMockRecorder {
return m.recorder
}

// Do mocks base method
func (m *MockIHttpClient) Do(req *http.Request) (*http.Response, error) {
func (m *IHTTPClient) Do(req *http.Request) (*http.Response, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Do", req)
ret0, _ := ret[0].(*http.Response)
Expand All @@ -54,7 +54,7 @@ func (m *MockIHttpClient) Do(req *http.Request) (*http.Response, error) {
}

// Do indicates an expected call of Do
func (mr *MockIHttpClientMockRecorder) Do(req interface{}) *gomock.Call {
func (mr *IHTTPClientMockRecorder) Do(req interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*MockIHttpClient)(nil).Do), req)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*IHTTPClient)(nil).Do), req)
}
8 changes: 4 additions & 4 deletions internal/config/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func (s *Secrets) SCIMAccessToken(secretArn string) (string, error) {
return s.getSecret(secretArn)
}

// SCIMEndpointUrl ...
func (s *Secrets) SCIMEndpointUrl(secretArn string) (string, error) {
// SCIMEndpointURL ...
func (s *Secrets) SCIMEndpointURL(secretArn string) (string, error) {
if len([]rune(secretArn)) == 0 {
return s.getSecret("SSOSyncSCIMEndpointUrl")
return s.getSecret("SSOSyncSCIMEndpointURL")
}
return s.getSecret(secretArn)
}
Expand All @@ -58,7 +58,7 @@ func (s *Secrets) Region(secretArn string) (string, error) {
return s.getSecret(secretArn)
}

// Identity Store ID ...
// IdentityStoreID ...
func (s *Secrets) IdentityStoreID(secretArn string) (string, error) {
if len([]rune(secretArn)) == 0 {
return s.getSecret("IdentityStoreID")
Expand Down
18 changes: 8 additions & 10 deletions internal/google/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,16 @@ func (c *client) GetGroups(query string) ([]*admin.Group, error) {
return nil
})
return g, err
} else {

// The Google api doesn't support multi-part queries, but we do so we need to split into an array of query strings
queries := strings.Split(query, ",")
// The Google api doesn't support multi-part queries, but we do so we need to split into an array of query strings
queries := strings.Split(query, ",")

// Then call the api one query at a time, appending to our list
for _, subQuery := range queries {
err = c.service.Groups.List().Customer("my_customer").Query(subQuery).Pages(context.TODO(), func(groups *admin.Groups) error {
g = append(g, groups.Groups...)
return nil
})
}
// Then call the api one query at a time, appending to our list
for _, subQuery := range queries {
err = c.service.Groups.List().Customer("my_customer").Query(subQuery).Pages(context.TODO(), func(groups *admin.Groups) error {
g = append(g, groups.Groups...)
return nil
})
}

// Check we've got some users otherwise something is wrong.
Expand Down
30 changes: 16 additions & 14 deletions internal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ func (s *syncGSuite) SyncGroupsUsers(queryGroups string, queryUsers string) erro
log.Info("creating user")
_, err := s.aws.CreateUser(awsUser)
if err != nil {
errHttp := new(aws.ErrHttpNotOK)
if errors.As(err, &errHttp) && errHttp.StatusCode == 409 {
errHTTP := new(aws.ErrHttpNotOK)
if errors.As(err, &errHTTP) && errHTTP.StatusCode == 409 {
log.WithField("user", awsUser.Username).Warn("user already exists")
continue
}
Expand Down Expand Up @@ -805,10 +805,7 @@ func DoSync(ctx context.Context, cfg *config.Config) error {
if err != nil {
log.WithField("error", err).Warn("Problem performing test query against Identity Store")
return err
} else {
log.WithField("Groups", response).Info("Test call for groups successful")

}
log.WithField("Groups", response).Info("Test call for groups successful")

// Initialize sync client with
// 1. SCIM API client
Expand Down Expand Up @@ -884,6 +881,7 @@ func (s *syncGSuite) GetGroups() ([]*aws.Group, error) {
return awsGroups, nil
}

// Callback handler for paginated List of Groups
func ListGroupsPagesCallbackFn(page *identitystore.ListGroupsOutput, lastPage bool) bool {
// Loop through each Group returned
for _, group := range page.Groups {
Expand Down Expand Up @@ -916,6 +914,7 @@ func (s *syncGSuite) GetUsers() ([]*aws.User, error) {
return awsUsers, nil
}

// Callback handler for paginated List of Users
func ListUsersPagesCallbackFn(page *identitystore.ListUsersOutput, lastPage bool) bool {
// Loop through each User in ListUsersOutput and convert to native User object
for _, user := range page.Users {
Expand All @@ -924,6 +923,7 @@ func ListUsersPagesCallbackFn(page *identitystore.ListUsersOutput, lastPage bool
return !lastPage
}

// Convert SDK user to native user object
func ConvertSdkUserObjToNative(user *identitystore.User) *aws.User {
// Convert emails into native Email object
userEmails := make([]aws.UserEmail, 0)
Expand Down Expand Up @@ -968,6 +968,7 @@ func ConvertSdkUserObjToNative(user *identitystore.User) *aws.User {
}
}

// Create User ID for user object map
func CreateUserIDtoUserObjMap(awsUsers []*aws.User) map[string]*aws.User {
awsUsersMap := make(map[string]*aws.User)

Expand All @@ -978,6 +979,7 @@ func CreateUserIDtoUserObjMap(awsUsers []*aws.User) map[string]*aws.User {
return awsUsersMap
}

// Handler for Paginated Group Membership List
var ListGroupMembershipPagesCallbackFn func(page *identitystore.ListGroupMembershipsOutput, lastPage bool) bool

func (s *syncGSuite) GetGroupMembershipsLists(awsGroups []*aws.Group, awsUsersMap map[string]*aws.User) (map[string][]*aws.User, error) {
Expand All @@ -986,8 +988,8 @@ func (s *syncGSuite) GetGroupMembershipsLists(awsGroups []*aws.Group, awsUsersMa

ListGroupMembershipPagesCallbackFn = func(page *identitystore.ListGroupMembershipsOutput, lastPage bool) bool {
for _, member := range page.GroupMemberships { // For every member in the group
userId := member.MemberId.UserId
user := awsUsersMap[*userId]
userID := member.MemberId.UserId
user := awsUsersMap[*userID]

// Append new user onto existing list of users
awsGroupsUsers[curGroup.DisplayName] = append(awsGroupsUsers[curGroup.DisplayName], user)
Expand Down Expand Up @@ -1034,25 +1036,25 @@ func (s *syncGSuite) IsUserInGroup(user *aws.User, group *aws.Group) (*bool, err
return isUserInGroup, nil
}

func (s *syncGSuite) RemoveUserFromGroup(userId *string, groupId *string) error {
memberIdOutput, err := s.identityStoreClient.GetGroupMembershipId(
func (s *syncGSuite) RemoveUserFromGroup(userID *string, groupID *string) error {
memberIDOutput, err := s.identityStoreClient.GetGroupMembershipId(
&identitystore.GetGroupMembershipIdInput{
IdentityStoreId: &s.cfg.IdentityStoreID,
GroupId: groupId,
MemberId: &identitystore.MemberId{UserId: userId},
GroupId: groupID,
MemberId: &identitystore.MemberId{UserId: userID},
},
)

if err != nil {
return err
}

memberId := memberIdOutput.MembershipId
memberID := memberIDOutput.MembershipId

_, err = s.identityStoreClient.DeleteGroupMembership(
&identitystore.DeleteGroupMembershipInput{
IdentityStoreId: &s.cfg.IdentityStoreID,
MembershipId: memberId,
MembershipId: memberID,
},
)

Expand Down

0 comments on commit 672dc59

Please sign in to comment.