Skip to content

Commit

Permalink
Fixes for workflow automations (#214)
Browse files Browse the repository at this point in the history
* Addressing issues from golint
  • Loading branch information
ChrisPates authored Aug 28, 2024
1 parent 4b3661b commit c7a866c
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 82 deletions.
47 changes: 24 additions & 23 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 Down Expand Up @@ -107,31 +108,31 @@ 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
} else {
log.Info("Notifying CodePipeline and mark its job execution as Success")
jobID := event.CodePipelineJob.ID
if len(jobID) == 0 {
panic("CodePipeline Job ID is not set")
}
// mark the job as Success.
cplSuccess := &codepipeline.PutJobSuccessResultInput{
JobId: aws.String(jobID),
}
_, cplErr := cpl.PutJobSuccessResult(cplSuccess)
if cplErr != nil {
log.Fatalf(errors.Wrap(err, "Failed to update CodePipeline jobID status").Error())
}
return "Success", nil
return "Failure", err
}
} else {
if err != nil {
log.Fatalf(errors.Wrap(err, "Notifying Lambda and mark this execution as Failure").Error())
return "Failure", err
} else {
return "Success", nil

log.Info("Notifying CodePipeline and mark its job execution as Success")
jobID := event.CodePipelineJob.ID
if len(jobID) == 0 {
panic("CodePipeline Job ID is not set")
}
// mark the job as Success.
cplSuccess := &codepipeline.PutJobSuccessResultInput{
JobId: aws.String(jobID),
}
_, cplErr := cpl.PutJobSuccessResult(cplSuccess)
if cplErr != nil {
log.Fatalf(errors.Wrap(err, "Failed to update CodePipeline jobID status").Error())
}

return "Success", nil
}

if err != nil {
log.Fatalf(errors.Wrap(err, "Notifying Lambda and mark this execution as Failure").Error())
return "Failure", err
}
return "Success", nil
}

func init() {
Expand Down Expand Up @@ -215,7 +216,7 @@ func configLambda() {
}
cfg.SCIMAccessToken = unwrap

unwrap, err = secrets.SCIMEndpointUrl(os.Getenv("SCIM_ENDPOINT"))
unwrap, err = secrets.SCIMEndpointURL(os.Getenv("SCIM_ENDPOINT"))
if err != nil {
log.Fatalf(errors.Wrap(err, "cannot read config: SCIM_ENDPOINT").Error())
}
Expand Down
14 changes: 9 additions & 5 deletions internal/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ import (
)

var (
// ErrUserNotFound

Check failure on line 31 in internal/aws/client.go

View workflow job for this annotation

GitHub Actions / test

comment on exported var ErrUserNotFound should be of the form "ErrUserNotFound ..."
ErrUserNotFound = errors.New("user not found")
// ErrGroupNotFound

Check failure on line 33 in internal/aws/client.go

View workflow job for this annotation

GitHub Actions / test

comment on exported var ErrGroupNotFound should be of the form "ErrGroupNotFound ..."
ErrGroupNotFound = errors.New("group not found")
// ErrUserNotSpecified

Check failure on line 35 in internal/aws/client.go

View workflow job for this annotation

GitHub Actions / test

comment on exported var ErrUserNotSpecified should be of the form "ErrUserNotSpecified ..."
ErrUserNotSpecified = errors.New("user not specified")
)

type ErrHttpNotOK struct {
// ErrHTTPNotOK

Check failure on line 39 in internal/aws/client.go

View workflow job for this annotation

GitHub Actions / test

comment on exported type ErrHTTPNotOK should be of the form "ErrHTTPNotOK ..." (with optional leading article)
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 +66,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 +122,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
19 changes: 9 additions & 10 deletions internal/google/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,17 @@ 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
Loading

0 comments on commit c7a866c

Please sign in to comment.