From ae2bec4bd9db3565acb65ceff363b467715b3afd Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Fri, 14 Oct 2022 18:09:07 -0300 Subject: [PATCH] fix(code): clean up EE-4432 (#7865) --- api/cmd/portainer/main.go | 7 +------ api/crypto/aes.go | 2 +- api/exec/swarm_stack.go | 7 +------ api/filesystem/filesystem.go | 2 +- api/hostmanagement/openamt/deviceFeatures.go | 5 +---- api/hostmanagement/openamt/openamt.go | 12 ++--------- .../customtemplates/customtemplate_create.go | 21 +++---------------- .../customtemplates/customtemplate_update.go | 7 +------ api/http/handler/endpoints/endpoint_update.go | 6 +----- api/http/handler/hostmanagement/fdo/fdo.go | 7 +------ .../openamt/amtconfiguration.go | 6 +----- .../handler/hostmanagement/openamt/amtrpc.go | 10 ++------- api/http/handler/kubernetes/ingresses.go | 2 +- api/http/handler/settings/settings_update.go | 7 +------ api/http/handler/websocket/attach.go | 7 +------ api/http/handler/websocket/exec.go | 7 +------ .../proxy/factory/azure/containergroup.go | 5 +---- api/http/security/rate_limiter.go | 2 +- api/internal/ssl/ssl.go | 7 +------ api/kubernetes/cli/access.go | 5 +---- api/stacks/stackutils/validation.go | 2 +- 21 files changed, 25 insertions(+), 111 deletions(-) diff --git a/api/cmd/portainer/main.go b/api/cmd/portainer/main.go index ed503ad65..fd5fa1b72 100644 --- a/api/cmd/portainer/main.go +++ b/api/cmd/portainer/main.go @@ -316,12 +316,7 @@ func updateSettingsFromFlags(dataStore dataservices.DataStore, flags *portainer. sslSettings.HTTPEnabled = true } - err = dataStore.SSLSettings().UpdateSettings(sslSettings) - if err != nil { - return err - } - - return nil + return dataStore.SSLSettings().UpdateSettings(sslSettings) } // enableFeaturesFromFlags turns on or off feature flags diff --git a/api/crypto/aes.go b/api/crypto/aes.go index 5a6f58fe7..6dd273310 100644 --- a/api/crypto/aes.go +++ b/api/crypto/aes.go @@ -13,7 +13,7 @@ import ( // Person with better knowledge is welcomed to improve it. // sourced from https://golang.org/src/crypto/cipher/example_test.go -var emptySalt []byte = make([]byte, 0, 0) +var emptySalt []byte = make([]byte, 0) // AesEncrypt reads from input, encrypts with AES-256 and writes to the output. // passphrase is used to generate an encryption key. diff --git a/api/exec/swarm_stack.go b/api/exec/swarm_stack.go index 78686333c..114ac3fd4 100644 --- a/api/exec/swarm_stack.go +++ b/api/exec/swarm_stack.go @@ -203,12 +203,7 @@ func (manager *SwarmStackManager) updateDockerCLIConfiguration(configPath string headersObject["X-PortainerAgent-Signature"] = signature headersObject["X-PortainerAgent-PublicKey"] = manager.signatureService.EncodedPublicKey() - err = manager.fileService.WriteJSONToFile(configFilePath, config) - if err != nil { - return err - } - - return nil + return manager.fileService.WriteJSONToFile(configFilePath, config) } func (manager *SwarmStackManager) retrieveConfigurationFromDisk(path string) (map[string]interface{}, error) { diff --git a/api/filesystem/filesystem.go b/api/filesystem/filesystem.go index f0202feec..981f1ea97 100644 --- a/api/filesystem/filesystem.go +++ b/api/filesystem/filesystem.go @@ -163,7 +163,7 @@ func (service *Service) Copy(fromFilePath string, toFilePath string, deleteIfExi } if !exists { - return errors.New(fmt.Sprintf("File (%s) doesn't exist", fromFilePath)) + return fmt.Errorf("File (%s) doesn't exist", fromFilePath) } finput, err := os.Open(fromFilePath) diff --git a/api/hostmanagement/openamt/deviceFeatures.go b/api/hostmanagement/openamt/deviceFeatures.go index e71d96684..cce18a8db 100644 --- a/api/hostmanagement/openamt/deviceFeatures.go +++ b/api/hostmanagement/openamt/deviceFeatures.go @@ -21,9 +21,6 @@ func (service *Service) enableDeviceFeatures(configuration portainer.OpenAMTConf jsonValue, _ := json.Marshal(payload) _, err := service.executeSaveRequest(http.MethodPost, url, configuration.MPSToken, jsonValue) - if err != nil { - return err - } - return nil + return err } diff --git a/api/hostmanagement/openamt/openamt.go b/api/hostmanagement/openamt/openamt.go index ca03c999e..863410d14 100644 --- a/api/hostmanagement/openamt/openamt.go +++ b/api/hostmanagement/openamt/openamt.go @@ -83,11 +83,8 @@ func (service *Service) Configure(configuration portainer.OpenAMTConfiguration) } _, err = service.createOrUpdateDomain(configuration) - if err != nil { - return err - } - return nil + return err } func (service *Service) executeSaveRequest(method string, url string, token string, payload []byte) ([]byte, error) { @@ -229,12 +226,7 @@ func (service *Service) ExecuteDeviceAction(configuration portainer.OpenAMTConfi } configuration.MPSToken = token - err = service.executeDeviceAction(configuration, deviceGUID, int(parsedAction)) - if err != nil { - return err - } - - return nil + return service.executeDeviceAction(configuration, deviceGUID, int(parsedAction)) } func (service *Service) EnableDeviceFeatures(configuration portainer.OpenAMTConfiguration, deviceGUID string, features portainer.OpenAMTDeviceEnabledFeatures) (string, error) { diff --git a/api/http/handler/customtemplates/customtemplate_create.go b/api/http/handler/customtemplates/customtemplate_create.go index c026a540e..34a291fb5 100644 --- a/api/http/handler/customtemplates/customtemplate_create.go +++ b/api/http/handler/customtemplates/customtemplate_create.go @@ -142,12 +142,7 @@ func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) e return errors.New("Invalid note. tag is not supported") } - err := validateVariablesDefinitions(payload.Variables) - if err != nil { - return err - } - - return nil + return validateVariablesDefinitions(payload.Variables) } func isValidNote(note string) bool { @@ -251,12 +246,7 @@ func (payload *customTemplateFromGitRepositoryPayload) Validate(r *http.Request) return errors.New("Invalid note. tag is not supported") } - err := validateVariablesDefinitions(payload.Variables) - if err != nil { - return err - } - - return nil + return validateVariablesDefinitions(payload.Variables) } func (handler *Handler) createCustomTemplateFromGitRepository(r *http.Request) (*portainer.CustomTemplate, error) { @@ -395,12 +385,7 @@ func (payload *customTemplateFromFileUploadPayload) Validate(r *http.Request) er return errors.New("Invalid variables. Ensure that the variables are valid JSON") } - err = validateVariablesDefinitions(payload.Variables) - if err != nil { - return err - } - - return nil + return validateVariablesDefinitions(payload.Variables) } func (handler *Handler) createCustomTemplateFromFileUpload(r *http.Request) (*portainer.CustomTemplate, error) { diff --git a/api/http/handler/customtemplates/customtemplate_update.go b/api/http/handler/customtemplates/customtemplate_update.go index 7b0a5e750..38e10fe9a 100644 --- a/api/http/handler/customtemplates/customtemplate_update.go +++ b/api/http/handler/customtemplates/customtemplate_update.go @@ -55,12 +55,7 @@ func (payload *customTemplateUpdatePayload) Validate(r *http.Request) error { return errors.New("Invalid note. tag is not supported") } - err := validateVariablesDefinitions(payload.Variables) - if err != nil { - return err - } - - return nil + return validateVariablesDefinitions(payload.Variables) } // @id CustomTemplateUpdate diff --git a/api/http/handler/endpoints/endpoint_update.go b/api/http/handler/endpoints/endpoint_update.go index a783a1b01..a35f383ab 100644 --- a/api/http/handler/endpoints/endpoint_update.go +++ b/api/http/handler/endpoints/endpoint_update.go @@ -190,12 +190,8 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * switch *payload.Status { case 1: endpoint.Status = portainer.EndpointStatusUp - break case 2: endpoint.Status = portainer.EndpointStatusDown - break - default: - break } } @@ -328,7 +324,7 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * err = handler.SnapshotService.FillSnapshotData(endpoint) if err != nil { - return &httperror.HandlerError{http.StatusInternalServerError, "Unable to add snapshot data", err} + return httperror.InternalServerError("Unable to add snapshot data", err) } return response.JSON(w, endpoint) diff --git a/api/http/handler/hostmanagement/fdo/fdo.go b/api/http/handler/hostmanagement/fdo/fdo.go index ef846a648..69851f454 100644 --- a/api/http/handler/hostmanagement/fdo/fdo.go +++ b/api/http/handler/hostmanagement/fdo/fdo.go @@ -128,12 +128,7 @@ func (handler *Handler) addDefaultProfile() error { profile.FilePath = filePath profile.DateCreated = time.Now().Unix() - err = handler.DataStore.FDOProfile().Create(profile) - if err != nil { - return err - } - - return nil + return handler.DataStore.FDOProfile().Create(profile) } const defaultProfileFileContent = ` diff --git a/api/http/handler/hostmanagement/openamt/amtconfiguration.go b/api/http/handler/hostmanagement/openamt/amtconfiguration.go index 6ffe5667f..ff8db308b 100644 --- a/api/http/handler/hostmanagement/openamt/amtconfiguration.go +++ b/api/http/handler/hostmanagement/openamt/amtconfiguration.go @@ -169,12 +169,8 @@ func (handler *Handler) saveConfiguration(configuration portainer.OpenAMTConfigu configuration.MPSToken = "" settings.OpenAMTConfiguration = configuration - err = handler.DataStore.Settings().UpdateSettings(settings) - if err != nil { - return err - } - return nil + return handler.DataStore.Settings().UpdateSettings(settings) } func (handler *Handler) disableOpenAMT() error { diff --git a/api/http/handler/hostmanagement/openamt/amtrpc.go b/api/http/handler/hostmanagement/openamt/amtrpc.go index a4922d276..643e5dd65 100644 --- a/api/http/handler/hostmanagement/openamt/amtrpc.go +++ b/api/http/handler/hostmanagement/openamt/amtrpc.go @@ -295,11 +295,8 @@ func (handler *Handler) activateDevice(endpoint *portainer.Endpoint, settings po } _, err := handler.PullAndRunContainer(ctx, endpoint, rpcGoImageName, rpcGoContainerName, cmdLine) - if err != nil { - return err - } - return nil + return err } func (handler *Handler) deactivateDevice(endpoint *portainer.Endpoint, settings portainer.Settings) error { @@ -315,9 +312,6 @@ func (handler *Handler) deactivateDevice(endpoint *portainer.Endpoint, settings } _, err := handler.PullAndRunContainer(ctx, endpoint, rpcGoImageName, rpcGoContainerName, cmdLine) - if err != nil { - return err - } - return nil + return err } diff --git a/api/http/handler/kubernetes/ingresses.go b/api/http/handler/kubernetes/ingresses.go index 0dfb8fadb..741436f82 100644 --- a/api/http/handler/kubernetes/ingresses.go +++ b/api/http/handler/kubernetes/ingresses.go @@ -328,7 +328,7 @@ PayloadLoop: updatedClass.GloballyBlocked = existingClass.GloballyBlocked // Handle "allow" - if p.Availability == true { + if p.Availability { // remove the namespace from the list of blocked namespaces // in the existingClass. for _, blockedNS := range existingClass.BlockedNamespaces { diff --git a/api/http/handler/settings/settings_update.go b/api/http/handler/settings/settings_update.go index 49a005207..da5ee2d0c 100644 --- a/api/http/handler/settings/settings_update.go +++ b/api/http/handler/settings/settings_update.go @@ -251,12 +251,7 @@ func (handler *Handler) settingsUpdate(w http.ResponseWriter, r *http.Request) * func (handler *Handler) updateSnapshotInterval(settings *portainer.Settings, snapshotInterval string) error { settings.SnapshotInterval = snapshotInterval - err := handler.SnapshotService.SetSnapshotInterval(snapshotInterval) - if err != nil { - return err - } - - return nil + return handler.SnapshotService.SetSnapshotInterval(snapshotInterval) } func (handler *Handler) updateTLS(settings *portainer.Settings) *httperror.HandlerError { diff --git a/api/http/handler/websocket/attach.go b/api/http/handler/websocket/attach.go index b4a5afb48..fcd9dcc8f 100644 --- a/api/http/handler/websocket/attach.go +++ b/api/http/handler/websocket/attach.go @@ -116,12 +116,7 @@ func hijackAttachStartOperation(websocketConn *websocket.Conn, endpoint *portain return err } - err = hijackRequest(websocketConn, httpConn, attachStartRequest) - if err != nil { - return err - } - - return nil + return hijackRequest(websocketConn, httpConn, attachStartRequest) } func createAttachStartRequest(attachID string) (*http.Request, error) { diff --git a/api/http/handler/websocket/exec.go b/api/http/handler/websocket/exec.go index fb50403eb..ae95e99a0 100644 --- a/api/http/handler/websocket/exec.go +++ b/api/http/handler/websocket/exec.go @@ -121,12 +121,7 @@ func hijackExecStartOperation(websocketConn *websocket.Conn, endpoint *portainer return err } - err = hijackRequest(websocketConn, httpConn, execStartRequest) - if err != nil { - return err - } - - return nil + return hijackRequest(websocketConn, httpConn, execStartRequest) } func createExecStartRequest(execID string) (*http.Request, error) { diff --git a/api/http/proxy/factory/azure/containergroup.go b/api/http/proxy/factory/azure/containergroup.go index d5eeb7a10..1f8ca581c 100644 --- a/api/http/proxy/factory/azure/containergroup.go +++ b/api/http/proxy/factory/azure/containergroup.go @@ -85,11 +85,8 @@ func (transport *Transport) proxyContainerGroupPutRequest(request *http.Request) responseObject = decorateObject(responseObject, resourceControl) err = utils.RewriteResponse(response, responseObject, http.StatusOK) - if err != nil { - return response, err - } - return response, nil + return response, err } func (transport *Transport) proxyContainerGroupGetRequest(request *http.Request) (*http.Response, error) { diff --git a/api/http/security/rate_limiter.go b/api/http/security/rate_limiter.go index 65eea5d4a..58ad63315 100644 --- a/api/http/security/rate_limiter.go +++ b/api/http/security/rate_limiter.go @@ -29,7 +29,7 @@ func NewRateLimiter(maxRequests int, duration time.Duration, banDuration time.Du func (limiter *RateLimiter) LimitAccess(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ip := StripAddrPort(r.RemoteAddr) - if banned := limiter.Inc(ip); banned == true { + if banned := limiter.Inc(ip); banned { httperror.WriteError(w, http.StatusForbidden, "Access denied", errors.ErrResourceAccessDenied) return } diff --git a/api/internal/ssl/ssl.go b/api/internal/ssl/ssl.go index 419b54ad0..f52b6bec5 100644 --- a/api/internal/ssl/ssl.go +++ b/api/internal/ssl/ssl.go @@ -166,10 +166,5 @@ func (service *Service) cacheInfo(certPath string, keyPath string, selfSigned bo settings.KeyPath = keyPath settings.SelfSigned = selfSigned - err = service.dataStore.SSLSettings().UpdateSettings(settings) - if err != nil { - return err - } - - return nil + return service.dataStore.SSLSettings().UpdateSettings(settings) } diff --git a/api/kubernetes/cli/access.go b/api/kubernetes/cli/access.go index a510c73d8..729e3c801 100644 --- a/api/kubernetes/cli/access.go +++ b/api/kubernetes/cli/access.go @@ -117,9 +117,6 @@ func (kcl *KubeClient) UpdateNamespaceAccessPolicies(accessPolicies map[string]p configMap.Data[portainerConfigMapAccessPoliciesKey] = string(data) _, err = kcl.cli.CoreV1().ConfigMaps(portainerNamespace).Update(context.TODO(), configMap, metav1.UpdateOptions{}) - if err != nil { - return err - } - return nil + return err } diff --git a/api/stacks/stackutils/validation.go b/api/stacks/stackutils/validation.go index e5ffe564d..daace6e60 100644 --- a/api/stacks/stackutils/validation.go +++ b/api/stacks/stackutils/validation.go @@ -43,7 +43,7 @@ func IsValidStackFile(stackFileContent []byte, securitySettings *portainer.Endpo } } - if !securitySettings.AllowPrivilegedModeForRegularUsers && service.Privileged == true { + if !securitySettings.AllowPrivilegedModeForRegularUsers && service.Privileged { return errors.New("privileged mode disabled for non administrator users") }