1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-21 14:29:40 +02:00

fix(code): clean up EE-4432 (#7865)

This commit is contained in:
andres-portainer 2022-10-14 18:09:07 -03:00 committed by GitHub
parent 367f3dd6d4
commit ae2bec4bd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 25 additions and 111 deletions

View file

@ -316,12 +316,7 @@ func updateSettingsFromFlags(dataStore dataservices.DataStore, flags *portainer.
sslSettings.HTTPEnabled = true sslSettings.HTTPEnabled = true
} }
err = dataStore.SSLSettings().UpdateSettings(sslSettings) return dataStore.SSLSettings().UpdateSettings(sslSettings)
if err != nil {
return err
}
return nil
} }
// enableFeaturesFromFlags turns on or off feature flags // enableFeaturesFromFlags turns on or off feature flags

View file

@ -13,7 +13,7 @@ import (
// Person with better knowledge is welcomed to improve it. // Person with better knowledge is welcomed to improve it.
// sourced from https://golang.org/src/crypto/cipher/example_test.go // 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. // AesEncrypt reads from input, encrypts with AES-256 and writes to the output.
// passphrase is used to generate an encryption key. // passphrase is used to generate an encryption key.

View file

@ -203,12 +203,7 @@ func (manager *SwarmStackManager) updateDockerCLIConfiguration(configPath string
headersObject["X-PortainerAgent-Signature"] = signature headersObject["X-PortainerAgent-Signature"] = signature
headersObject["X-PortainerAgent-PublicKey"] = manager.signatureService.EncodedPublicKey() headersObject["X-PortainerAgent-PublicKey"] = manager.signatureService.EncodedPublicKey()
err = manager.fileService.WriteJSONToFile(configFilePath, config) return manager.fileService.WriteJSONToFile(configFilePath, config)
if err != nil {
return err
}
return nil
} }
func (manager *SwarmStackManager) retrieveConfigurationFromDisk(path string) (map[string]interface{}, error) { func (manager *SwarmStackManager) retrieveConfigurationFromDisk(path string) (map[string]interface{}, error) {

View file

@ -163,7 +163,7 @@ func (service *Service) Copy(fromFilePath string, toFilePath string, deleteIfExi
} }
if !exists { 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) finput, err := os.Open(fromFilePath)

View file

@ -21,9 +21,6 @@ func (service *Service) enableDeviceFeatures(configuration portainer.OpenAMTConf
jsonValue, _ := json.Marshal(payload) jsonValue, _ := json.Marshal(payload)
_, err := service.executeSaveRequest(http.MethodPost, url, configuration.MPSToken, jsonValue) _, err := service.executeSaveRequest(http.MethodPost, url, configuration.MPSToken, jsonValue)
if err != nil {
return err return err
} }
return nil
}

View file

@ -83,11 +83,8 @@ func (service *Service) Configure(configuration portainer.OpenAMTConfiguration)
} }
_, err = service.createOrUpdateDomain(configuration) _, 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) { 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 configuration.MPSToken = token
err = service.executeDeviceAction(configuration, deviceGUID, int(parsedAction)) return service.executeDeviceAction(configuration, deviceGUID, int(parsedAction))
if err != nil {
return err
}
return nil
} }
func (service *Service) EnableDeviceFeatures(configuration portainer.OpenAMTConfiguration, deviceGUID string, features portainer.OpenAMTDeviceEnabledFeatures) (string, error) { func (service *Service) EnableDeviceFeatures(configuration portainer.OpenAMTConfiguration, deviceGUID string, features portainer.OpenAMTDeviceEnabledFeatures) (string, error) {

View file

@ -142,12 +142,7 @@ func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) e
return errors.New("Invalid note. <img> tag is not supported") return errors.New("Invalid note. <img> tag is not supported")
} }
err := validateVariablesDefinitions(payload.Variables) return validateVariablesDefinitions(payload.Variables)
if err != nil {
return err
}
return nil
} }
func isValidNote(note string) bool { func isValidNote(note string) bool {
@ -251,12 +246,7 @@ func (payload *customTemplateFromGitRepositoryPayload) Validate(r *http.Request)
return errors.New("Invalid note. <img> tag is not supported") return errors.New("Invalid note. <img> tag is not supported")
} }
err := validateVariablesDefinitions(payload.Variables) return validateVariablesDefinitions(payload.Variables)
if err != nil {
return err
}
return nil
} }
func (handler *Handler) createCustomTemplateFromGitRepository(r *http.Request) (*portainer.CustomTemplate, error) { 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") return errors.New("Invalid variables. Ensure that the variables are valid JSON")
} }
err = validateVariablesDefinitions(payload.Variables) return validateVariablesDefinitions(payload.Variables)
if err != nil {
return err
}
return nil
} }
func (handler *Handler) createCustomTemplateFromFileUpload(r *http.Request) (*portainer.CustomTemplate, error) { func (handler *Handler) createCustomTemplateFromFileUpload(r *http.Request) (*portainer.CustomTemplate, error) {

View file

@ -55,12 +55,7 @@ func (payload *customTemplateUpdatePayload) Validate(r *http.Request) error {
return errors.New("Invalid note. <img> tag is not supported") return errors.New("Invalid note. <img> tag is not supported")
} }
err := validateVariablesDefinitions(payload.Variables) return validateVariablesDefinitions(payload.Variables)
if err != nil {
return err
}
return nil
} }
// @id CustomTemplateUpdate // @id CustomTemplateUpdate

View file

@ -190,12 +190,8 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) *
switch *payload.Status { switch *payload.Status {
case 1: case 1:
endpoint.Status = portainer.EndpointStatusUp endpoint.Status = portainer.EndpointStatusUp
break
case 2: case 2:
endpoint.Status = portainer.EndpointStatusDown 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) err = handler.SnapshotService.FillSnapshotData(endpoint)
if err != nil { 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) return response.JSON(w, endpoint)

View file

@ -128,12 +128,7 @@ func (handler *Handler) addDefaultProfile() error {
profile.FilePath = filePath profile.FilePath = filePath
profile.DateCreated = time.Now().Unix() profile.DateCreated = time.Now().Unix()
err = handler.DataStore.FDOProfile().Create(profile) return handler.DataStore.FDOProfile().Create(profile)
if err != nil {
return err
}
return nil
} }
const defaultProfileFileContent = ` const defaultProfileFileContent = `

View file

@ -169,12 +169,8 @@ func (handler *Handler) saveConfiguration(configuration portainer.OpenAMTConfigu
configuration.MPSToken = "" configuration.MPSToken = ""
settings.OpenAMTConfiguration = configuration 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 { func (handler *Handler) disableOpenAMT() error {

View file

@ -295,11 +295,8 @@ func (handler *Handler) activateDevice(endpoint *portainer.Endpoint, settings po
} }
_, err := handler.PullAndRunContainer(ctx, endpoint, rpcGoImageName, rpcGoContainerName, cmdLine) _, 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 { 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) _, err := handler.PullAndRunContainer(ctx, endpoint, rpcGoImageName, rpcGoContainerName, cmdLine)
if err != nil {
return err return err
} }
return nil
}

View file

@ -328,7 +328,7 @@ PayloadLoop:
updatedClass.GloballyBlocked = existingClass.GloballyBlocked updatedClass.GloballyBlocked = existingClass.GloballyBlocked
// Handle "allow" // Handle "allow"
if p.Availability == true { if p.Availability {
// remove the namespace from the list of blocked namespaces // remove the namespace from the list of blocked namespaces
// in the existingClass. // in the existingClass.
for _, blockedNS := range existingClass.BlockedNamespaces { for _, blockedNS := range existingClass.BlockedNamespaces {

View file

@ -251,12 +251,7 @@ func (handler *Handler) settingsUpdate(w http.ResponseWriter, r *http.Request) *
func (handler *Handler) updateSnapshotInterval(settings *portainer.Settings, snapshotInterval string) error { func (handler *Handler) updateSnapshotInterval(settings *portainer.Settings, snapshotInterval string) error {
settings.SnapshotInterval = snapshotInterval settings.SnapshotInterval = snapshotInterval
err := handler.SnapshotService.SetSnapshotInterval(snapshotInterval) return handler.SnapshotService.SetSnapshotInterval(snapshotInterval)
if err != nil {
return err
}
return nil
} }
func (handler *Handler) updateTLS(settings *portainer.Settings) *httperror.HandlerError { func (handler *Handler) updateTLS(settings *portainer.Settings) *httperror.HandlerError {

View file

@ -116,12 +116,7 @@ func hijackAttachStartOperation(websocketConn *websocket.Conn, endpoint *portain
return err return err
} }
err = hijackRequest(websocketConn, httpConn, attachStartRequest) return hijackRequest(websocketConn, httpConn, attachStartRequest)
if err != nil {
return err
}
return nil
} }
func createAttachStartRequest(attachID string) (*http.Request, error) { func createAttachStartRequest(attachID string) (*http.Request, error) {

View file

@ -121,12 +121,7 @@ func hijackExecStartOperation(websocketConn *websocket.Conn, endpoint *portainer
return err return err
} }
err = hijackRequest(websocketConn, httpConn, execStartRequest) return hijackRequest(websocketConn, httpConn, execStartRequest)
if err != nil {
return err
}
return nil
} }
func createExecStartRequest(execID string) (*http.Request, error) { func createExecStartRequest(execID string) (*http.Request, error) {

View file

@ -85,11 +85,8 @@ func (transport *Transport) proxyContainerGroupPutRequest(request *http.Request)
responseObject = decorateObject(responseObject, resourceControl) responseObject = decorateObject(responseObject, resourceControl)
err = utils.RewriteResponse(response, responseObject, http.StatusOK) 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) { func (transport *Transport) proxyContainerGroupGetRequest(request *http.Request) (*http.Response, error) {

View file

@ -29,7 +29,7 @@ func NewRateLimiter(maxRequests int, duration time.Duration, banDuration time.Du
func (limiter *RateLimiter) LimitAccess(next http.Handler) http.Handler { func (limiter *RateLimiter) LimitAccess(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip := StripAddrPort(r.RemoteAddr) 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) httperror.WriteError(w, http.StatusForbidden, "Access denied", errors.ErrResourceAccessDenied)
return return
} }

View file

@ -166,10 +166,5 @@ func (service *Service) cacheInfo(certPath string, keyPath string, selfSigned bo
settings.KeyPath = keyPath settings.KeyPath = keyPath
settings.SelfSigned = selfSigned settings.SelfSigned = selfSigned
err = service.dataStore.SSLSettings().UpdateSettings(settings) return service.dataStore.SSLSettings().UpdateSettings(settings)
if err != nil {
return err
}
return nil
} }

View file

@ -117,9 +117,6 @@ func (kcl *KubeClient) UpdateNamespaceAccessPolicies(accessPolicies map[string]p
configMap.Data[portainerConfigMapAccessPoliciesKey] = string(data) configMap.Data[portainerConfigMapAccessPoliciesKey] = string(data)
_, err = kcl.cli.CoreV1().ConfigMaps(portainerNamespace).Update(context.TODO(), configMap, metav1.UpdateOptions{}) _, err = kcl.cli.CoreV1().ConfigMaps(portainerNamespace).Update(context.TODO(), configMap, metav1.UpdateOptions{})
if err != nil {
return err return err
} }
return nil
}

View file

@ -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") return errors.New("privileged mode disabled for non administrator users")
} }