From 137ce370966c0dd7aa659ccc501dadf08e156686 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Fri, 30 Dec 2022 15:26:46 -0300 Subject: [PATCH] chore(nil): remove unnecessary nil checks EE-4847 (#8254) --- api/exec/compose_stack.go | 2 +- api/http/handler/edgegroups/edgegroup_create.go | 4 ++-- api/http/handler/edgegroups/edgegroup_update.go | 4 ++-- api/http/handler/edgejobs/edgejob_create.go | 4 ++-- api/http/handler/edgestacks/edgestack_create.go | 4 ++-- api/http/handler/edgestacks/edgestack_update.go | 2 +- api/http/security/bouncer.go | 8 ++++---- api/internal/authorization/endpint_role_with_override.go | 5 +---- 8 files changed, 15 insertions(+), 18 deletions(-) diff --git a/api/exec/compose_stack.go b/api/exec/compose_stack.go index 875235f09..f7bf3c872 100644 --- a/api/exec/compose_stack.go +++ b/api/exec/compose_stack.go @@ -139,7 +139,7 @@ func (manager *ComposeStackManager) fetchEndpointProxy(endpoint *portainer.Endpo // createEnvFile creates a file that would hold both "in-place" and default environment variables. // It will return the name of the file if the stack has "in-place" env vars, otherwise empty string. func createEnvFile(stack *portainer.Stack) (string, error) { - if stack.Env == nil || len(stack.Env) == 0 { + if len(stack.Env) == 0 { return "", nil } diff --git a/api/http/handler/edgegroups/edgegroup_create.go b/api/http/handler/edgegroups/edgegroup_create.go index 2d095cc03..11e839372 100644 --- a/api/http/handler/edgegroups/edgegroup_create.go +++ b/api/http/handler/edgegroups/edgegroup_create.go @@ -25,10 +25,10 @@ func (payload *edgeGroupCreatePayload) Validate(r *http.Request) error { if govalidator.IsNull(payload.Name) { return errors.New("invalid Edge group name") } - if payload.Dynamic && (payload.TagIDs == nil || len(payload.TagIDs) == 0) { + if payload.Dynamic && len(payload.TagIDs) == 0 { return errors.New("tagIDs is mandatory for a dynamic Edge group") } - if !payload.Dynamic && (payload.Endpoints == nil || len(payload.Endpoints) == 0) { + if !payload.Dynamic && len(payload.Endpoints) == 0 { return errors.New("environment is mandatory for a static Edge group") } return nil diff --git a/api/http/handler/edgegroups/edgegroup_update.go b/api/http/handler/edgegroups/edgegroup_update.go index 944dc885e..a0ce97860 100644 --- a/api/http/handler/edgegroups/edgegroup_update.go +++ b/api/http/handler/edgegroups/edgegroup_update.go @@ -27,10 +27,10 @@ func (payload *edgeGroupUpdatePayload) Validate(r *http.Request) error { if govalidator.IsNull(payload.Name) { return errors.New("invalid Edge group name") } - if payload.Dynamic && (payload.TagIDs == nil || len(payload.TagIDs) == 0) { + if payload.Dynamic && len(payload.TagIDs) == 0 { return errors.New("tagIDs is mandatory for a dynamic Edge group") } - if !payload.Dynamic && (payload.Endpoints == nil || len(payload.Endpoints) == 0) { + if !payload.Dynamic && len(payload.Endpoints) == 0 { return errors.New("environments is mandatory for a static Edge group") } return nil diff --git a/api/http/handler/edgejobs/edgejob_create.go b/api/http/handler/edgejobs/edgejob_create.go index e2525f855..691a57691 100644 --- a/api/http/handler/edgejobs/edgejob_create.go +++ b/api/http/handler/edgejobs/edgejob_create.go @@ -69,7 +69,7 @@ func (payload *edgeJobCreateFromFileContentPayload) Validate(r *http.Request) er return errors.New("invalid cron expression") } - if (payload.Endpoints == nil || len(payload.Endpoints) == 0) && (payload.EdgeGroups == nil || len(payload.EdgeGroups) == 0) { + if len(payload.Endpoints) == 0 && len(payload.EdgeGroups) == 0 { return errors.New("no environments or groups have been provided") } @@ -146,7 +146,7 @@ func (payload *edgeJobCreateFromFilePayload) Validate(r *http.Request) error { } payload.EdgeGroups = edgeGroups - if (payload.Endpoints == nil || len(payload.Endpoints) == 0) && (payload.EdgeGroups == nil || len(payload.EdgeGroups) == 0) { + if len(payload.Endpoints) == 0 && len(payload.EdgeGroups) == 0 { return errors.New("no environments or groups have been provided") } diff --git a/api/http/handler/edgestacks/edgestack_create.go b/api/http/handler/edgestacks/edgestack_create.go index b06b412e4..c2f338ac2 100644 --- a/api/http/handler/edgestacks/edgestack_create.go +++ b/api/http/handler/edgestacks/edgestack_create.go @@ -103,7 +103,7 @@ func (payload *swarmStackFromFileContentPayload) Validate(r *http.Request) error if govalidator.IsNull(payload.StackFileContent) { return &InvalidPayloadError{msg: "Invalid stack file content"} } - if payload.EdgeGroups == nil || len(payload.EdgeGroups) == 0 { + if len(payload.EdgeGroups) == 0 { return &InvalidPayloadError{msg: "Edge Groups are mandatory for an Edge stack"} } return nil @@ -221,7 +221,7 @@ func (payload *swarmStackFromGitRepositoryPayload) Validate(r *http.Request) err payload.FilePathInRepository = filesystem.ManifestFileDefaultName } } - if payload.EdgeGroups == nil || len(payload.EdgeGroups) == 0 { + if len(payload.EdgeGroups) == 0 { return &InvalidPayloadError{msg: "Edge Groups are mandatory for an Edge stack"} } return nil diff --git a/api/http/handler/edgestacks/edgestack_update.go b/api/http/handler/edgestacks/edgestack_update.go index 3dcf2b752..168de1395 100644 --- a/api/http/handler/edgestacks/edgestack_update.go +++ b/api/http/handler/edgestacks/edgestack_update.go @@ -27,7 +27,7 @@ func (payload *updateEdgeStackPayload) Validate(r *http.Request) error { if payload.StackFileContent == "" { return errors.New("Invalid stack file content") } - if payload.EdgeGroups != nil && len(payload.EdgeGroups) == 0 { + if len(payload.EdgeGroups) == 0 { return errors.New("Edge Groups are mandatory for an Edge stack") } return nil diff --git a/api/http/security/bouncer.go b/api/http/security/bouncer.go index e9980d6cf..c66d49715 100644 --- a/api/http/security/bouncer.go +++ b/api/http/security/bouncer.go @@ -81,9 +81,9 @@ func (bouncer *RequestBouncer) RestrictedAccess(h http.Handler) http.Handler { // TeamLeaderAccess defines a security check for APIs require team leader privilege // // Bouncer operations are applied backwards: -// - Parse the JWT from the request and stored in context, user has to be authenticated -// - Upgrade to the restricted request -// - User is admin or team leader +// - Parse the JWT from the request and stored in context, user has to be authenticated +// - Upgrade to the restricted request +// - User is admin or team leader func (bouncer *RequestBouncer) TeamLeaderAccess(h http.Handler) http.Handler { h = bouncer.mwIsTeamLeader(h) h = bouncer.mwUpgradeToRestrictedRequest(h) @@ -199,7 +199,7 @@ func (bouncer *RequestBouncer) mwCheckPortainerAuthorizations(next http.Handler, } _, err = bouncer.dataStore.User().User(tokenData.ID) - if err != nil && bouncer.dataStore.IsErrObjectNotFound(err) { + if bouncer.dataStore.IsErrObjectNotFound(err) { httperror.WriteError(w, http.StatusUnauthorized, "Unauthorized", httperrors.ErrUnauthorized) return } else if err != nil { diff --git a/api/internal/authorization/endpint_role_with_override.go b/api/internal/authorization/endpint_role_with_override.go index c26913947..14d004487 100644 --- a/api/internal/authorization/endpint_role_with_override.go +++ b/api/internal/authorization/endpint_role_with_override.go @@ -44,10 +44,7 @@ func (service *Service) CleanNAPWithOverridePolicies( } if hasChange { - err = kubecli.UpdateNamespaceAccessPolicies(accessPolicies) - if err != nil { - return err - } + return kubecli.UpdateNamespaceAccessPolicies(accessPolicies) } return nil