diff --git a/auth/auth.go b/auth/auth.go index 5795791a..f9587517 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -42,11 +42,7 @@ type Authenticator struct { } func (a *Authenticator) IsInitialized() bool { - if a.store.HasAdminUser(context.Background()) { - return true - } - - return false + return a.store.HasAdminUser(context.Background()) } func (a *Authenticator) GetJWTToken(ctx context.Context) (string, error) { diff --git a/auth/init_required.go b/auth/init_required.go index cfa7438c..b2e9b797 100644 --- a/auth/init_required.go +++ b/auth/init_required.go @@ -16,6 +16,7 @@ package auth import ( "encoding/json" + "log" "net/http" "garm/apiserver/params" @@ -40,7 +41,9 @@ func (i *initRequired) Middleware(next http.Handler) http.Handler { if err != nil || ctrlInfo.ControllerID.String() == "" { w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusConflict) - json.NewEncoder(w).Encode(params.InitializationRequired) + if err := json.NewEncoder(w).Encode(params.InitializationRequired); err != nil { + log.Printf("failed to encode response: %s", err) + } return } ctx := r.Context() diff --git a/auth/instance_middleware.go b/auth/instance_middleware.go index 8fbd4778..562df07f 100644 --- a/auth/instance_middleware.go +++ b/auth/instance_middleware.go @@ -73,7 +73,6 @@ func NewInstanceJWTToken(instance params.Instance, secret, entity string, poolTy // used with gorilla type instanceMiddleware struct { store dbCommon.Store - auth *Authenticator cfg config.JWTAuth } diff --git a/auth/jwt.go b/auth/jwt.go index 835b5cc7..f96ae01b 100644 --- a/auth/jwt.go +++ b/auth/jwt.go @@ -18,6 +18,7 @@ import ( "context" "encoding/json" "fmt" + "log" "net/http" "strings" @@ -42,7 +43,6 @@ type JWTClaims struct { // used with gorilla type jwtMiddleware struct { store dbCommon.Store - auth *Authenticator cfg config.JWTAuth } @@ -75,10 +75,12 @@ func (amw *jwtMiddleware) claimsToContext(ctx context.Context, claims *JWTClaims func invalidAuthResponse(w http.ResponseWriter) { w.WriteHeader(http.StatusUnauthorized) w.Header().Add("Content-Type", "application/json") - json.NewEncoder(w).Encode( + if err := json.NewEncoder(w).Encode( apiParams.APIErrorResponse{ Error: "Authentication failed", - }) + }); err != nil { + log.Printf("failed to encode response: %s", err) + } } // Middleware implements the middleware interface diff --git a/cmd/garm-cli/cmd/enterprise.go b/cmd/garm-cli/cmd/enterprise.go index 0a58519b..362885d5 100644 --- a/cmd/garm-cli/cmd/enterprise.go +++ b/cmd/garm-cli/cmd/enterprise.go @@ -139,8 +139,8 @@ func init() { enterpriseAddCmd.Flags().StringVar(&enterpriseName, "name", "", "The name of the enterprise") enterpriseAddCmd.Flags().StringVar(&enterpriseWebhookSecret, "webhook-secret", "", "The webhook secret for this enterprise") enterpriseAddCmd.Flags().StringVar(&enterpriseCreds, "credentials", "", "Credentials name. See credentials list.") - enterpriseAddCmd.MarkFlagRequired("credentials") - enterpriseAddCmd.MarkFlagRequired("name") + enterpriseAddCmd.MarkFlagRequired("credentials") //nolint + enterpriseAddCmd.MarkFlagRequired("name") //nolint enterpriseCmd.AddCommand( enterpriseListCmd, diff --git a/cmd/garm-cli/cmd/init.go b/cmd/garm-cli/cmd/init.go index b0b5959e..b96b106a 100644 --- a/cmd/garm-cli/cmd/init.go +++ b/cmd/garm-cli/cmd/init.go @@ -128,8 +128,8 @@ func init() { initCmd.Flags().StringVarP(&loginEmail, "email", "e", "", "Email address") initCmd.Flags().StringVarP(&loginFullName, "full-name", "f", "", "Full name of the user") initCmd.Flags().StringVarP(&loginPassword, "password", "p", "", "The admin password") - initCmd.MarkFlagRequired("name") - initCmd.MarkFlagRequired("url") + initCmd.MarkFlagRequired("name") //nolint + initCmd.MarkFlagRequired("url") //nolint } func renderUserTable(user params.User) { diff --git a/cmd/garm-cli/cmd/org_pool.go b/cmd/garm-cli/cmd/org_pool.go index f4eec97b..68ab620a 100644 --- a/cmd/garm-cli/cmd/org_pool.go +++ b/cmd/garm-cli/cmd/org_pool.go @@ -238,10 +238,10 @@ func init() { orgPoolAddCmd.Flags().UintVar(&poolMaxRunners, "max-runners", 5, "The maximum number of runner this pool will create.") orgPoolAddCmd.Flags().UintVar(&poolMinIdleRunners, "min-idle-runners", 1, "Attempt to maintain a minimum of idle self-hosted runners of this type.") orgPoolAddCmd.Flags().BoolVar(&poolEnabled, "enabled", false, "Enable this pool.") - orgPoolAddCmd.MarkFlagRequired("provider-name") - orgPoolAddCmd.MarkFlagRequired("image") - orgPoolAddCmd.MarkFlagRequired("flavor") - orgPoolAddCmd.MarkFlagRequired("tags") + orgPoolAddCmd.MarkFlagRequired("provider-name") //nolint + orgPoolAddCmd.MarkFlagRequired("image") //nolint + orgPoolAddCmd.MarkFlagRequired("flavor") //nolint + orgPoolAddCmd.MarkFlagRequired("tags") //nolint orgPoolUpdateCmd.Flags().StringVar(&poolImage, "image", "", "The provider-specific image name to use for runners in this pool.") orgPoolUpdateCmd.Flags().StringVar(&poolFlavor, "flavor", "", "The flavor to use for this runner.") diff --git a/cmd/garm-cli/cmd/organization.go b/cmd/garm-cli/cmd/organization.go index 184a9a18..7c7d0ba6 100644 --- a/cmd/garm-cli/cmd/organization.go +++ b/cmd/garm-cli/cmd/organization.go @@ -139,8 +139,8 @@ func init() { orgAddCmd.Flags().StringVar(&orgName, "name", "", "The name of the organization") orgAddCmd.Flags().StringVar(&orgWebhookSecret, "webhook-secret", "", "The webhook secret for this organization") orgAddCmd.Flags().StringVar(&orgCreds, "credentials", "", "Credentials name. See credentials list.") - orgAddCmd.MarkFlagRequired("credentials") - orgAddCmd.MarkFlagRequired("name") + orgAddCmd.MarkFlagRequired("credentials") //nolint + orgAddCmd.MarkFlagRequired("name") //nolint organizationCmd.AddCommand( orgListCmd, diff --git a/cmd/garm-cli/cmd/pool.go b/cmd/garm-cli/cmd/pool.go index b50176e1..435149ad 100644 --- a/cmd/garm-cli/cmd/pool.go +++ b/cmd/garm-cli/cmd/pool.go @@ -85,11 +85,11 @@ Example: } else if cmd.Flags().Changed("all") { pools, err = cli.ListAllPools() } else { - cmd.Help() + cmd.Help() //nolint os.Exit(0) } default: - cmd.Help() + cmd.Help() //nolint os.Exit(0) } @@ -195,7 +195,7 @@ var poolAddCmd = &cobra.Command{ } else if cmd.Flags().Changed("enterprise") { pool, err = cli.CreateEnterprisePool(poolEnterprise, newPoolParams) } else { - cmd.Help() + cmd.Help() //nolint os.Exit(0) } @@ -313,10 +313,10 @@ func init() { poolAddCmd.Flags().UintVar(&poolRunnerBootstrapTimeout, "runner-bootstrap-timeout", 20, "Duration in minutes after which a runner is considered failed if it does not join Github.") poolAddCmd.Flags().UintVar(&poolMinIdleRunners, "min-idle-runners", 1, "Attempt to maintain a minimum of idle self-hosted runners of this type.") poolAddCmd.Flags().BoolVar(&poolEnabled, "enabled", false, "Enable this pool.") - poolAddCmd.MarkFlagRequired("provider-name") - poolAddCmd.MarkFlagRequired("image") - poolAddCmd.MarkFlagRequired("flavor") - poolAddCmd.MarkFlagRequired("tags") + poolAddCmd.MarkFlagRequired("provider-name") //nolint + poolAddCmd.MarkFlagRequired("image") //nolint + poolAddCmd.MarkFlagRequired("flavor") //nolint + poolAddCmd.MarkFlagRequired("tags") //nolint poolAddCmd.Flags().StringVarP(&poolRepository, "repo", "r", "", "Add the new pool within this repository.") poolAddCmd.Flags().StringVarP(&poolOrganization, "org", "o", "", "Add the new pool withing this organization.") diff --git a/cmd/garm-cli/cmd/profile.go b/cmd/garm-cli/cmd/profile.go index 64ca54f7..01cc113e 100644 --- a/cmd/garm-cli/cmd/profile.go +++ b/cmd/garm-cli/cmd/profile.go @@ -218,8 +218,8 @@ func init() { profileAddCmd.Flags().StringVarP(&loginURL, "url", "a", "", "The base URL for the runner manager API") profileAddCmd.Flags().StringVarP(&loginUserName, "username", "u", "", "Username to log in as") profileAddCmd.Flags().StringVarP(&loginPassword, "password", "p", "", "The user passowrd") - profileAddCmd.MarkFlagRequired("name") - profileAddCmd.MarkFlagRequired("url") + profileAddCmd.MarkFlagRequired("name") //nolint + profileAddCmd.MarkFlagRequired("url") //nolint profileCmd.AddCommand( profileListCmd, diff --git a/cmd/garm-cli/cmd/repo_pool.go b/cmd/garm-cli/cmd/repo_pool.go index e754337d..4d221d99 100644 --- a/cmd/garm-cli/cmd/repo_pool.go +++ b/cmd/garm-cli/cmd/repo_pool.go @@ -225,10 +225,10 @@ func init() { repoPoolAddCmd.Flags().UintVar(&poolMaxRunners, "max-runners", 5, "The maximum number of runner this pool will create.") repoPoolAddCmd.Flags().UintVar(&poolMinIdleRunners, "min-idle-runners", 1, "Attempt to maintain a minimum of idle self-hosted runners of this type.") repoPoolAddCmd.Flags().BoolVar(&poolEnabled, "enabled", false, "Enable this pool.") - repoPoolAddCmd.MarkFlagRequired("provider-name") - repoPoolAddCmd.MarkFlagRequired("image") - repoPoolAddCmd.MarkFlagRequired("flavor") - repoPoolAddCmd.MarkFlagRequired("tags") + repoPoolAddCmd.MarkFlagRequired("provider-name") //nolint + repoPoolAddCmd.MarkFlagRequired("image") //nolint + repoPoolAddCmd.MarkFlagRequired("flavor") //nolint + repoPoolAddCmd.MarkFlagRequired("tags") //nolint repoPoolUpdateCmd.Flags().StringVar(&poolImage, "image", "", "The provider-specific image name to use for runners in this pool.") repoPoolUpdateCmd.Flags().StringVar(&poolFlavor, "flavor", "", "The flavor to use for this runner.") diff --git a/cmd/garm-cli/cmd/repository.go b/cmd/garm-cli/cmd/repository.go index 52896ced..bdb208ef 100644 --- a/cmd/garm-cli/cmd/repository.go +++ b/cmd/garm-cli/cmd/repository.go @@ -142,9 +142,9 @@ func init() { repoAddCmd.Flags().StringVar(&repoName, "name", "", "The name of the repository") repoAddCmd.Flags().StringVar(&repoWebhookSecret, "webhook-secret", "", "The webhook secret for this repository") repoAddCmd.Flags().StringVar(&repoCreds, "credentials", "", "Credentials name. See credentials list.") - repoAddCmd.MarkFlagRequired("credentials") - repoAddCmd.MarkFlagRequired("owner") - repoAddCmd.MarkFlagRequired("name") + repoAddCmd.MarkFlagRequired("credentials") //nolint + repoAddCmd.MarkFlagRequired("owner") //nolint + repoAddCmd.MarkFlagRequired("name") //nolint repositoryCmd.AddCommand( repoListCmd, diff --git a/cmd/garm-cli/cmd/runner.go b/cmd/garm-cli/cmd/runner.go index 2f36233b..7b6265cd 100644 --- a/cmd/garm-cli/cmd/runner.go +++ b/cmd/garm-cli/cmd/runner.go @@ -98,11 +98,11 @@ Example: } else if cmd.Flags().Changed("all") { instances, err = cli.ListAllInstances() } else { - cmd.Help() + cmd.Help() //nolint os.Exit(0) } default: - cmd.Help() + cmd.Help() //nolint os.Exit(0) } diff --git a/cmd/garm-cli/config/config.go b/cmd/garm-cli/config/config.go index 15b48d4d..ba6b3be1 100644 --- a/cmd/garm-cli/config/config.go +++ b/cmd/garm-cli/config/config.go @@ -162,7 +162,7 @@ func (c *Config) SaveConfig() error { } cfgHandle, err := os.Create(cfgFile) if err != nil { - errors.Wrap(err, "getting file handle") + return errors.Wrap(err, "getting file handle") } encoder := toml.NewEncoder(cfgHandle) diff --git a/cmd/garm/main.go b/cmd/garm/main.go index 00d6c114..411b735f 100644 --- a/cmd/garm/main.go +++ b/cmd/garm/main.go @@ -86,8 +86,10 @@ func main() { var hub *websocket.Hub if cfg.Default.EnableLogStreamer { hub = websocket.NewHub(ctx) - hub.Start() - defer hub.Stop() + if err := hub.Start(); err != nil { + log.Fatal(err) + } + defer hub.Stop() //nolint writers = append(writers, hub) } diff --git a/runner/enterprises.go b/runner/enterprises.go index 4fdd4b0c..424ae542 100644 --- a/runner/enterprises.go +++ b/runner/enterprises.go @@ -45,7 +45,9 @@ func (r *Runner) CreateEnterprise(ctx context.Context, param params.CreateEnterp defer func() { if err != nil { - r.store.DeleteEnterprise(ctx, enterprise.ID) + if deleteErr := r.store.DeleteEnterprise(ctx, enterprise.ID); deleteErr != nil { + log.Printf("failed to delete enterprise: %s", deleteErr) + } } }() diff --git a/runner/organizations.go b/runner/organizations.go index eef1e65f..8806baf5 100644 --- a/runner/organizations.go +++ b/runner/organizations.go @@ -59,7 +59,9 @@ func (r *Runner) CreateOrganization(ctx context.Context, param params.CreateOrgP defer func() { if err != nil { - r.store.DeleteOrganization(ctx, org.ID) + if deleteErr := r.store.DeleteOrganization(ctx, org.ID); deleteErr != nil { + log.Printf("failed to delete org: %s", deleteErr) + } } }() diff --git a/runner/pool/pool.go b/runner/pool/pool.go index a968a596..2e0bbbdd 100644 --- a/runner/pool/pool.go +++ b/runner/pool/pool.go @@ -1009,7 +1009,7 @@ func (r *basePoolManager) deletePendingInstances() { return errors.Wrap(err, "deleting instance from database") } return - }(instance) + }(instance) //nolint } } diff --git a/runner/providers/lxd/util.go b/runner/providers/lxd/util.go index 941e5107..59bac2b8 100644 --- a/runner/providers/lxd/util.go +++ b/runner/providers/lxd/util.go @@ -18,7 +18,6 @@ import ( "context" "database/sql" "fmt" - "io/ioutil" "log" "net" "net/http" @@ -128,28 +127,28 @@ func getClientFromConfig(ctx context.Context, cfg *config.LXD) (cli lxd.Instance var srvCrtContents, tlsCAContents, clientCertContents, clientKeyContents []byte if cfg.TLSServerCert != "" { - srvCrtContents, err = ioutil.ReadFile(cfg.TLSServerCert) + srvCrtContents, err = os.ReadFile(cfg.TLSServerCert) if err != nil { return nil, errors.Wrap(err, "reading TLSServerCert") } } if cfg.TLSCA != "" { - tlsCAContents, err = ioutil.ReadFile(cfg.TLSCA) + tlsCAContents, err = os.ReadFile(cfg.TLSCA) if err != nil { return nil, errors.Wrap(err, "reading TLSCA") } } if cfg.ClientCertificate != "" { - clientCertContents, err = ioutil.ReadFile(cfg.ClientCertificate) + clientCertContents, err = os.ReadFile(cfg.ClientCertificate) if err != nil { return nil, errors.Wrap(err, "reading ClientCertificate") } } if cfg.ClientKey != "" { - clientKeyContents, err = ioutil.ReadFile(cfg.ClientKey) + clientKeyContents, err = os.ReadFile(cfg.ClientKey) if err != nil { return nil, errors.Wrap(err, "reading ClientKey") } diff --git a/runner/repositories.go b/runner/repositories.go index f9a33137..c511f3ab 100644 --- a/runner/repositories.go +++ b/runner/repositories.go @@ -59,7 +59,9 @@ func (r *Runner) CreateRepository(ctx context.Context, param params.CreateRepoPa defer func() { if err != nil { - r.store.DeleteRepository(ctx, repo.ID) + if deleteErr := r.store.DeleteRepository(ctx, repo.ID); deleteErr != nil { + log.Printf("failed to delete repository: %s", deleteErr) + } } }() diff --git a/util/exec/exec_nix.go b/util/exec/exec_nix.go index 76f8ba46..1525eca6 100644 --- a/util/exec/exec_nix.go +++ b/util/exec/exec_nix.go @@ -8,9 +8,5 @@ import ( ) func IsExecutable(path string) bool { - if unix.Access(path, unix.X_OK) == nil { - return true - } - - return false + return unix.Access(path, unix.X_OK) == nil } diff --git a/websocket/client.go b/websocket/client.go index 962da1e0..28429a3a 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -55,8 +55,15 @@ func (c *Client) clientReader() { c.conn.Close() }() c.conn.SetReadLimit(maxMessageSize) - c.conn.SetReadDeadline(time.Now().Add(pongWait)) - c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil }) + if err := c.conn.SetReadDeadline(time.Now().Add(pongWait)); err != nil { + log.Printf("failed to set read deadline: %s", err) + } + c.conn.SetPongHandler(func(string) error { + if err := c.conn.SetReadDeadline(time.Now().Add(pongWait)); err != nil { + return err + } + return nil + }) for { mt, _, err := c.conn.ReadMessage() if err != nil { @@ -78,10 +85,14 @@ func (c *Client) clientWriter() { for { select { case message, ok := <-c.send: - c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + if err := c.conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil { + log.Printf("failed to set write deadline: %s", err) + } if !ok { // The hub closed the channel. - c.conn.WriteMessage(websocket.CloseMessage, []byte{}) + if err := c.conn.WriteMessage(websocket.CloseMessage, []byte{}); err != nil { + log.Printf("failed to write message: %s", err) + } return } @@ -90,7 +101,9 @@ func (c *Client) clientWriter() { return } case <-ticker.C: - c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + if err := c.conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil { + log.Printf("failed to set write deadline: %s", err) + } if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil { return }