From fc43624b3a8d3beb85f880fccca57a2238d23474 Mon Sep 17 00:00:00 2001 From: Vishnu Jayadevan Date: Fri, 28 Jul 2023 17:18:33 +0530 Subject: [PATCH] refactor: fixing DeepSource issues --- artifact/handler.go | 2 +- auth/oauth/github_test.go | 2 +- auth/oauth/handler.go | 4 +++- auth/saml/backend.go | 6 ------ auth/saml/handler.go | 26 +++++++++++++++++++++----- cmd/runner/runner.go | 2 +- provider/github/api_test.go | 4 ++-- 7 files changed, 29 insertions(+), 17 deletions(-) diff --git a/artifact/handler.go b/artifact/handler.go index 1f6fb48..59d4559 100644 --- a/artifact/handler.go +++ b/artifact/handler.go @@ -121,6 +121,6 @@ func (h *Handler) HandleAutofix(c echo.Context) error { return c.JSON(200, autofixArtifactsResponse) } -func (h *Handler) HandleOptions(c echo.Context) error { +func (*Handler) HandleOptions(c echo.Context) error { return c.NoContent(http.StatusOK) } diff --git a/auth/oauth/github_test.go b/auth/oauth/github_test.go index 8f2f74c..3a4485f 100644 --- a/auth/oauth/github_test.go +++ b/auth/oauth/github_test.go @@ -35,7 +35,7 @@ func TestGithub_GetToken(t *testing.T) { return } w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"access_token":"token","token_type":"bearer","scope":"repo,gist"}`)) + _, _ = w.Write([]byte(`{"access_token":"token","token_type":"bearer","scope":"repo,gist"}`)) })) defer server.Close() serverURL, _ := url.Parse(server.URL) diff --git a/auth/oauth/handler.go b/auth/oauth/handler.go index b771a9d..105687a 100644 --- a/auth/oauth/handler.go +++ b/auth/oauth/handler.go @@ -144,7 +144,9 @@ func (h *Handler) HandleSession(c echo.Context) error { } code := ksuid.New().String() - h.store.SetAccessCode(code, user) + if err := h.store.SetAccessCode(code, user); err != nil { + return c.JSON(500, err.Error()) + } u := h.deepsource.Host.JoinPath(fmt.Sprintf("/accounts/runner/apps/%s/login/callback/bifrost/", req.AppID)) q := u.Query() diff --git a/auth/saml/backend.go b/auth/saml/backend.go index 04ad1f4..5f169f6 100644 --- a/auth/saml/backend.go +++ b/auth/saml/backend.go @@ -40,9 +40,3 @@ func NewSAMLMiddleware(ctx context.Context, opts *Opts, client *http.Client) (*s return sp, err } - -func SAMLHandler() http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("hello world")) - }) -} diff --git a/auth/saml/handler.go b/auth/saml/handler.go index 9c1897b..cb4d405 100644 --- a/auth/saml/handler.go +++ b/auth/saml/handler.go @@ -10,6 +10,7 @@ import ( "github.com/deepsourcecorp/runner/auth/token" "github.com/labstack/echo/v4" "github.com/segmentio/ksuid" + "golang.org/x/exp/slog" "golang.org/x/oauth2" ) @@ -55,7 +56,10 @@ func (h *Handler) AuthorizationHandler() echo.HandlerFunc { request.Parse(r) if !h.runner.IsValidClientID(request.ClientID) { w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("invalid client_id")) + if _, err := w.Write([]byte("invalid client_id")); err != nil { + slog.Error("error writing response", slog.Any("err", err)) + return + } return } @@ -72,7 +76,11 @@ func (h *Handler) AuthorizationHandler() echo.HandlerFunc { session, ok := s.(samlsp.SessionWithAttributes) if !ok { w.WriteHeader(http.StatusUnauthorized) - w.Write([]byte("unauthorized")) + if _, err := w.Write([]byte("unauthorized")); err != nil { + slog.Error("error writing response", slog.Any("err", err)) + return + } + return } attr := session.GetAttributes() @@ -85,7 +93,10 @@ func (h *Handler) AuthorizationHandler() echo.HandlerFunc { accessToken, err := h.tokenService.GetAccessToken(user) if err != nil { w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) + if _, err := w.Write([]byte(err.Error())); err != nil { + slog.Error("error writing response", slog.Any("err", err)) + return + } return } @@ -101,7 +112,10 @@ func (h *Handler) AuthorizationHandler() echo.HandlerFunc { refreshToken, err := h.tokenService.GetRefreshToken(user) if err != nil { w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) + if _, err := w.Write([]byte(err.Error())); err != nil { + slog.Error("error writing response", slog.Any("err", err)) + return + } return } http.SetCookie(w, &http.Cookie{ @@ -140,7 +154,9 @@ func (h *Handler) HandleSession(c echo.Context) error { } code := ksuid.New().String() - h.store.SetAccessCode(code, user) + if err := h.store.SetAccessCode(code, user); err != nil { + return c.JSON(400, err.Error()) + } u := h.deepsource.Host.JoinPath("/accounts/runner/apps/saml/login/callback/bifrost/") q := u.Query() diff --git a/cmd/runner/runner.go b/cmd/runner/runner.go index a8fa64f..e6dcb62 100644 --- a/cmd/runner/runner.go +++ b/cmd/runner/runner.go @@ -59,7 +59,7 @@ func (s *Server) Start() error { return nil } -func (s *Server) PrintBanner() { +func (*Server) PrintBanner() { fmt.Println(fmt.Sprintf(Banner, Version)) } diff --git a/provider/github/api_test.go b/provider/github/api_test.go index fb78dc8..d3fed56 100644 --- a/provider/github/api_test.go +++ b/provider/github/api_test.go @@ -77,7 +77,7 @@ func TestAPIProxyFactory_NewClient(t *testing.T) { } func TestAPIProxy_GenerateJWT(t *testing.T) { - privateKey, _ := rsa.GenerateKey(rand.Reader, 512) + privateKey, _ := rsa.GenerateKey(rand.Reader, 2048) app := &App{ID: "test-app-id", PrivateKey: privateKey} proxy := &APIProxy{app: app} token, err := proxy.GenerateJWT() @@ -136,7 +136,7 @@ func TestAPIProxy_GenerateAccessToken(t *testing.T) { t.Run(tc.name, func(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(tc.responseStatus) - w.Write([]byte(tc.responseBody)) + _, _ = w.Write([]byte(tc.responseBody)) })) defer server.Close()