1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-22 23:09:41 +02:00

task(code): remove unnecessary uses of govalidator BE-11181 (#12156)
Some checks are pending
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run

This commit is contained in:
andres-portainer 2024-08-28 19:37:20 -03:00 committed by GitHub
parent eb3e367ba8
commit 5353570721
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 79 additions and 106 deletions

View file

@ -6,7 +6,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
) )
type MultiFilterArgs []struct { type MultiFilterArgs []struct {

View file

@ -1,9 +1,10 @@
package filesystem package filesystem
import ( import (
"testing"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
func TestMultiFilterDirForPerDevConfigs(t *testing.T) { func TestMultiFilterDirForPerDevConfigs(t *testing.T) {

View file

@ -8,7 +8,7 @@ import (
) )
func ValidateRepoConfig(repoConfig *gittypes.RepoConfig) error { func ValidateRepoConfig(repoConfig *gittypes.RepoConfig) error {
if govalidator.IsNull(repoConfig.URL) || !govalidator.IsURL(repoConfig.URL) { if len(repoConfig.URL) == 0 || !govalidator.IsURL(repoConfig.URL) {
return httperrors.NewInvalidPayloadError("Invalid repository URL. Must correspond to a valid URL format") return httperrors.NewInvalidPayloadError("Invalid repository URL. Must correspond to a valid URL format")
} }
@ -17,7 +17,7 @@ func ValidateRepoConfig(repoConfig *gittypes.RepoConfig) error {
} }
func ValidateRepoAuthentication(auth *gittypes.GitAuthentication) error { func ValidateRepoAuthentication(auth *gittypes.GitAuthentication) error {
if auth != nil && govalidator.IsNull(auth.Password) && auth.GitCredentialID == 0 { if auth != nil && len(auth.Password) == 0 && auth.GitCredentialID == 0 {
return httperrors.NewInvalidPayloadError("Invalid repository credentials. Password or GitCredentialID must be specified when authentication is enabled") return httperrors.NewInvalidPayloadError("Invalid repository credentials. Password or GitCredentialID must be specified when authentication is enabled")
} }

View file

@ -12,7 +12,6 @@ import (
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -30,11 +29,11 @@ type authenticateResponse struct {
} }
func (payload *authenticatePayload) Validate(r *http.Request) error { func (payload *authenticatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Username) { if len(payload.Username) == 0 {
return errors.New("Invalid username") return errors.New("Invalid username")
} }
if govalidator.IsNull(payload.Password) { if len(payload.Password) == 0 {
return errors.New("Invalid password") return errors.New("Invalid password")
} }

View file

@ -9,7 +9,6 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/asaskevich/govalidator"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -19,7 +18,7 @@ type oauthPayload struct {
} }
func (payload *oauthPayload) Validate(r *http.Request) error { func (payload *oauthPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Code) { if len(payload.Code) == 0 {
return errors.New("Invalid OAuth authorization code") return errors.New("Invalid OAuth authorization code")
} }

View file

@ -108,13 +108,13 @@ type customTemplateFromFileContentPayload struct {
} }
func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) error { func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Title) { if len(payload.Title) == 0 {
return errors.New("Invalid custom template title") return errors.New("Invalid custom template title")
} }
if govalidator.IsNull(payload.Description) { if len(payload.Description) == 0 {
return errors.New("Invalid custom template description") return errors.New("Invalid custom template description")
} }
if govalidator.IsNull(payload.FileContent) { if len(payload.FileContent) == 0 {
return errors.New("Invalid file content") return errors.New("Invalid file content")
} }
if payload.Type != portainer.KubernetesStack && payload.Platform != portainer.CustomTemplatePlatformLinux && payload.Platform != portainer.CustomTemplatePlatformWindows { if payload.Type != portainer.KubernetesStack && payload.Platform != portainer.CustomTemplatePlatformLinux && payload.Platform != portainer.CustomTemplatePlatformWindows {
@ -132,7 +132,7 @@ func (payload *customTemplateFromFileContentPayload) Validate(r *http.Request) e
} }
func isValidNote(note string) bool { func isValidNote(note string) bool {
if govalidator.IsNull(note) { if len(note) == 0 {
return true return true
} }
match, _ := regexp.MatchString("<img", note) match, _ := regexp.MatchString("<img", note)
@ -226,19 +226,19 @@ type customTemplateFromGitRepositoryPayload struct {
} }
func (payload *customTemplateFromGitRepositoryPayload) Validate(r *http.Request) error { func (payload *customTemplateFromGitRepositoryPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Title) { if len(payload.Title) == 0 {
return errors.New("Invalid custom template title") return errors.New("Invalid custom template title")
} }
if govalidator.IsNull(payload.Description) { if len(payload.Description) == 0 {
return errors.New("Invalid custom template description") return errors.New("Invalid custom template description")
} }
if govalidator.IsNull(payload.RepositoryURL) || !govalidator.IsURL(payload.RepositoryURL) { if len(payload.RepositoryURL) == 0 || !govalidator.IsURL(payload.RepositoryURL) {
return errors.New("Invalid repository URL. Must correspond to a valid URL format") return errors.New("Invalid repository URL. Must correspond to a valid URL format")
} }
if payload.RepositoryAuthentication && (govalidator.IsNull(payload.RepositoryUsername) || govalidator.IsNull(payload.RepositoryPassword)) { if payload.RepositoryAuthentication && (len(payload.RepositoryUsername) == 0 || len(payload.RepositoryPassword) == 0) {
return errors.New("Invalid repository credentials. Username and password must be specified when authentication is enabled") return errors.New("Invalid repository credentials. Username and password must be specified when authentication is enabled")
} }
if govalidator.IsNull(payload.ComposeFilePathInRepository) { if len(payload.ComposeFilePathInRepository) == 0 {
payload.ComposeFilePathInRepository = filesystem.ComposeFileDefaultName payload.ComposeFilePathInRepository = filesystem.ComposeFileDefaultName
} }

View file

@ -64,11 +64,11 @@ type customTemplateUpdatePayload struct {
} }
func (payload *customTemplateUpdatePayload) Validate(r *http.Request) error { func (payload *customTemplateUpdatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Title) { if len(payload.Title) == 0 {
return errors.New("Invalid custom template title") return errors.New("Invalid custom template title")
} }
if govalidator.IsNull(payload.FileContent) && govalidator.IsNull(payload.RepositoryURL) { if len(payload.FileContent) == 0 && len(payload.RepositoryURL) == 0 {
return errors.New("Either file content or git repository url need to be provided") return errors.New("Either file content or git repository url need to be provided")
} }
@ -80,7 +80,7 @@ func (payload *customTemplateUpdatePayload) Validate(r *http.Request) error {
return errors.New("Invalid custom template type") return errors.New("Invalid custom template type")
} }
if govalidator.IsNull(payload.Description) { if len(payload.Description) == 0 {
return errors.New("Invalid custom template description") return errors.New("Invalid custom template description")
} }
@ -88,11 +88,11 @@ 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")
} }
if payload.RepositoryAuthentication && (govalidator.IsNull(payload.RepositoryUsername) || govalidator.IsNull(payload.RepositoryPassword)) { if payload.RepositoryAuthentication && (len(payload.RepositoryUsername) == 0 || len(payload.RepositoryPassword) == 0) {
return errors.New("Invalid repository credentials. Username and password must be specified when authentication is enabled") return errors.New("Invalid repository credentials. Username and password must be specified when authentication is enabled")
} }
if govalidator.IsNull(payload.ComposeFilePathInRepository) { if len(payload.ComposeFilePathInRepository) == 0 {
payload.ComposeFilePathInRepository = filesystem.ComposeFileDefaultName payload.ComposeFilePathInRepository = filesystem.ComposeFileDefaultName
} }

View file

@ -9,8 +9,6 @@ import (
"github.com/portainer/portainer/api/internal/endpointutils" "github.com/portainer/portainer/api/internal/endpointutils"
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/asaskevich/govalidator"
) )
type edgeGroupCreatePayload struct { type edgeGroupCreatePayload struct {
@ -22,7 +20,7 @@ type edgeGroupCreatePayload struct {
} }
func (payload *edgeGroupCreatePayload) Validate(r *http.Request) error { func (payload *edgeGroupCreatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("invalid Edge group name") return errors.New("invalid Edge group name")
} }

View file

@ -12,8 +12,6 @@ import (
"github.com/portainer/portainer/api/slicesx" "github.com/portainer/portainer/api/slicesx"
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/asaskevich/govalidator"
) )
type edgeGroupUpdatePayload struct { type edgeGroupUpdatePayload struct {
@ -25,7 +23,7 @@ type edgeGroupUpdatePayload struct {
} }
func (payload *edgeGroupUpdatePayload) Validate(r *http.Request) error { func (payload *edgeGroupUpdatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("invalid Edge group name") return errors.New("invalid Edge group name")
} }

View file

@ -49,7 +49,7 @@ type edgeJobCreateFromFileContentPayload struct {
} }
func (payload *edgeJobCreateFromFileContentPayload) Validate(r *http.Request) error { func (payload *edgeJobCreateFromFileContentPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("invalid Edge job name") return errors.New("invalid Edge job name")
} }
@ -57,7 +57,7 @@ func (payload *edgeJobCreateFromFileContentPayload) Validate(r *http.Request) er
return errors.New("invalid Edge job name format. Allowed characters are: [a-zA-Z0-9_.-]") return errors.New("invalid Edge job name format. Allowed characters are: [a-zA-Z0-9_.-]")
} }
if govalidator.IsNull(payload.CronExpression) { if len(payload.CronExpression) == 0 {
return errors.New("invalid cron expression") return errors.New("invalid cron expression")
} }
@ -65,7 +65,7 @@ func (payload *edgeJobCreateFromFileContentPayload) Validate(r *http.Request) er
return errors.New("no environments or groups have been provided") return errors.New("no environments or groups have been provided")
} }
if govalidator.IsNull(payload.FileContent) { if len(payload.FileContent) == 0 {
return errors.New("invalid script file content") return errors.New("invalid script file content")
} }

View file

@ -46,15 +46,15 @@ type edgeStackFromGitRepositoryPayload struct {
} }
func (payload *edgeStackFromGitRepositoryPayload) Validate(r *http.Request) error { func (payload *edgeStackFromGitRepositoryPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return httperrors.NewInvalidPayloadError("Invalid stack name") return httperrors.NewInvalidPayloadError("Invalid stack name")
} }
if govalidator.IsNull(payload.RepositoryURL) || !govalidator.IsURL(payload.RepositoryURL) { if len(payload.RepositoryURL) == 0 || !govalidator.IsURL(payload.RepositoryURL) {
return httperrors.NewInvalidPayloadError("Invalid repository URL. Must correspond to a valid URL format") return httperrors.NewInvalidPayloadError("Invalid repository URL. Must correspond to a valid URL format")
} }
if payload.RepositoryAuthentication && govalidator.IsNull(payload.RepositoryPassword) { if payload.RepositoryAuthentication && len(payload.RepositoryPassword) == 0 {
return httperrors.NewInvalidPayloadError("Invalid repository credentials. Password must be specified when authentication is enabled") return httperrors.NewInvalidPayloadError("Invalid repository credentials. Password must be specified when authentication is enabled")
} }
@ -62,7 +62,7 @@ func (payload *edgeStackFromGitRepositoryPayload) Validate(r *http.Request) erro
return httperrors.NewInvalidPayloadError("Invalid deployment type") return httperrors.NewInvalidPayloadError("Invalid deployment type")
} }
if govalidator.IsNull(payload.FilePathInRepository) { if len(payload.FilePathInRepository) == 0 {
switch payload.DeploymentType { switch payload.DeploymentType {
case portainer.EdgeStackDeploymentCompose: case portainer.EdgeStackDeploymentCompose:
payload.FilePathInRepository = filesystem.ComposeFileDefaultName payload.FilePathInRepository = filesystem.ComposeFileDefaultName

View file

@ -10,7 +10,6 @@ import (
httperrors "github.com/portainer/portainer/api/http/errors" httperrors "github.com/portainer/portainer/api/http/errors"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/asaskevich/govalidator"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -33,11 +32,11 @@ type edgeStackFromStringPayload struct {
} }
func (payload *edgeStackFromStringPayload) Validate(r *http.Request) error { func (payload *edgeStackFromStringPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return httperrors.NewInvalidPayloadError("Invalid stack name") return httperrors.NewInvalidPayloadError("Invalid stack name")
} }
if govalidator.IsNull(payload.StackFileContent) { if len(payload.StackFileContent) == 0 {
return httperrors.NewInvalidPayloadError("Invalid stack file content") return httperrors.NewInvalidPayloadError("Invalid stack file content")
} }

View file

@ -11,7 +11,6 @@ import (
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -31,7 +30,7 @@ func (payload *updateStatusPayload) Validate(r *http.Request) error {
return errors.New("invalid EnvironmentID") return errors.New("invalid EnvironmentID")
} }
if *payload.Status == portainer.EdgeStackStatusError && govalidator.IsNull(payload.Error) { if *payload.Status == portainer.EdgeStackStatusError && len(payload.Error) == 0 {
return errors.New("error message is mandatory when status is error") return errors.New("error message is mandatory when status is error")
} }

View file

@ -9,8 +9,6 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
) )
type endpointGroupCreatePayload struct { type endpointGroupCreatePayload struct {
@ -25,7 +23,7 @@ type endpointGroupCreatePayload struct {
} }
func (payload *endpointGroupCreatePayload) Validate(r *http.Request) error { func (payload *endpointGroupCreatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("invalid environment group name") return errors.New("invalid environment group name")
} }

View file

@ -29,15 +29,15 @@ type repositoryFilePreviewPayload struct {
} }
func (payload *repositoryFilePreviewPayload) Validate(r *http.Request) error { func (payload *repositoryFilePreviewPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Repository) || !govalidator.IsURL(payload.Repository) { if len(payload.Repository) == 0 || !govalidator.IsURL(payload.Repository) {
return errors.New("invalid repository URL. Must correspond to a valid URL format") return errors.New("invalid repository URL. Must correspond to a valid URL format")
} }
if govalidator.IsNull(payload.Reference) { if len(payload.Reference) == 0 {
payload.Reference = "refs/heads/main" payload.Reference = "refs/heads/main"
} }
if govalidator.IsNull(payload.TargetFile) { if len(payload.TargetFile) == 0 {
return errors.New("invalid target filename") return errors.New("invalid target filename")
} }

View file

@ -11,8 +11,6 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
) )
type registryCreatePayload struct { type registryCreatePayload struct {
@ -46,19 +44,19 @@ type registryCreatePayload struct {
} }
func (payload *registryCreatePayload) Validate(_ *http.Request) error { func (payload *registryCreatePayload) Validate(_ *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("Invalid registry name") return errors.New("Invalid registry name")
} }
if govalidator.IsNull(payload.URL) { if len(payload.URL) == 0 {
return errors.New("Invalid registry URL") return errors.New("Invalid registry URL")
} }
if payload.Authentication { if payload.Authentication {
if govalidator.IsNull(payload.Username) || govalidator.IsNull(payload.Password) { if len(payload.Username) == 0 || len(payload.Password) == 0 {
return errors.New("Invalid credentials. Username and password must be specified when authentication is enabled") return errors.New("Invalid credentials. Username and password must be specified when authentication is enabled")
} }
if payload.Type == portainer.EcrRegistry { if payload.Type == portainer.EcrRegistry {
if govalidator.IsNull(payload.Ecr.Region) { if len(payload.Ecr.Region) == 0 {
return errors.New("invalid credentials: access key ID, secret access key and region must be specified when authentication is enabled") return errors.New("invalid credentials: access key ID, secret access key and region must be specified when authentication is enabled")
} }
} }

View file

@ -8,8 +8,6 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
) )
type resourceControlCreatePayload struct { type resourceControlCreatePayload struct {
@ -33,7 +31,7 @@ type resourceControlCreatePayload struct {
var errResourceControlAlreadyExists = errors.New("A resource control is already applied on this resource") //http/resourceControl var errResourceControlAlreadyExists = errors.New("A resource control is already applied on this resource") //http/resourceControl
func (payload *resourceControlCreatePayload) Validate(r *http.Request) error { func (payload *resourceControlCreatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.ResourceID) { if len(payload.ResourceID) == 0 {
return errors.New("invalid payload: invalid resource identifier") return errors.New("invalid payload: invalid resource identifier")
} }

View file

@ -32,11 +32,11 @@ type composeStackFromFileContentPayload struct {
} }
func (payload *composeStackFromFileContentPayload) Validate(r *http.Request) error { func (payload *composeStackFromFileContentPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("Invalid stack name") return errors.New("Invalid stack name")
} }
if govalidator.IsNull(payload.StackFileContent) { if len(payload.StackFileContent) == 0 {
return errors.New("Invalid stack file content") return errors.New("Invalid stack file content")
} }
return nil return nil
@ -202,13 +202,13 @@ func createStackPayloadFromComposeGitPayload(name, repoUrl, repoReference, repoU
} }
func (payload *composeStackFromGitRepositoryPayload) Validate(r *http.Request) error { func (payload *composeStackFromGitRepositoryPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("Invalid stack name") return errors.New("Invalid stack name")
} }
if govalidator.IsNull(payload.RepositoryURL) || !govalidator.IsURL(payload.RepositoryURL) { if len(payload.RepositoryURL) == 0 || !govalidator.IsURL(payload.RepositoryURL) {
return errors.New("Invalid repository URL. Must correspond to a valid URL format") return errors.New("Invalid repository URL. Must correspond to a valid URL format")
} }
if payload.RepositoryAuthentication && govalidator.IsNull(payload.RepositoryPassword) { if payload.RepositoryAuthentication && len(payload.RepositoryPassword) == 0 {
return errors.New("Invalid repository credentials. Password must be specified when authentication is enabled") return errors.New("Invalid repository credentials. Password must be specified when authentication is enabled")
} }
if err := update.ValidateAutoUpdateSettings(payload.AutoUpdate); err != nil { if err := update.ValidateAutoUpdateSettings(payload.AutoUpdate); err != nil {

View file

@ -88,7 +88,7 @@ func createStackPayloadFromK8sUrlPayload(name, namespace, manifestUrl string, co
} }
func (payload *kubernetesStringDeploymentPayload) Validate(r *http.Request) error { func (payload *kubernetesStringDeploymentPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.StackFileContent) { if len(payload.StackFileContent) == 0 {
return errors.New("Invalid stack file content") return errors.New("Invalid stack file content")
} }
@ -96,15 +96,15 @@ func (payload *kubernetesStringDeploymentPayload) Validate(r *http.Request) erro
} }
func (payload *kubernetesGitDeploymentPayload) Validate(r *http.Request) error { func (payload *kubernetesGitDeploymentPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.RepositoryURL) || !govalidator.IsURL(payload.RepositoryURL) { if len(payload.RepositoryURL) == 0 || !govalidator.IsURL(payload.RepositoryURL) {
return errors.New("Invalid repository URL. Must correspond to a valid URL format") return errors.New("Invalid repository URL. Must correspond to a valid URL format")
} }
if payload.RepositoryAuthentication && govalidator.IsNull(payload.RepositoryPassword) { if payload.RepositoryAuthentication && len(payload.RepositoryPassword) == 0 {
return errors.New("Invalid repository credentials. Password must be specified when authentication is enabled") return errors.New("Invalid repository credentials. Password must be specified when authentication is enabled")
} }
if govalidator.IsNull(payload.ManifestFile) { if len(payload.ManifestFile) == 0 {
return errors.New("Invalid manifest file in repository") return errors.New("Invalid manifest file in repository")
} }
@ -112,7 +112,7 @@ func (payload *kubernetesGitDeploymentPayload) Validate(r *http.Request) error {
} }
func (payload *kubernetesManifestURLDeploymentPayload) Validate(r *http.Request) error { func (payload *kubernetesManifestURLDeploymentPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.ManifestURL) || !govalidator.IsURL(payload.ManifestURL) { if len(payload.ManifestURL) == 0 || !govalidator.IsURL(payload.ManifestURL) {
return errors.New("Invalid manifest URL") return errors.New("Invalid manifest URL")
} }

View file

@ -30,13 +30,13 @@ type swarmStackFromFileContentPayload struct {
} }
func (payload *swarmStackFromFileContentPayload) Validate(r *http.Request) error { func (payload *swarmStackFromFileContentPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("Invalid stack name") return errors.New("Invalid stack name")
} }
if govalidator.IsNull(payload.SwarmID) { if len(payload.SwarmID) == 0 {
return errors.New("Invalid Swarm ID") return errors.New("Invalid Swarm ID")
} }
if govalidator.IsNull(payload.StackFileContent) { if len(payload.StackFileContent) == 0 {
return errors.New("Invalid stack file content") return errors.New("Invalid stack file content")
} }
return nil return nil
@ -136,16 +136,16 @@ type swarmStackFromGitRepositoryPayload struct {
} }
func (payload *swarmStackFromGitRepositoryPayload) Validate(r *http.Request) error { func (payload *swarmStackFromGitRepositoryPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("Invalid stack name") return errors.New("Invalid stack name")
} }
if govalidator.IsNull(payload.SwarmID) { if len(payload.SwarmID) == 0 {
return errors.New("Invalid Swarm ID") return errors.New("Invalid Swarm ID")
} }
if govalidator.IsNull(payload.RepositoryURL) || !govalidator.IsURL(payload.RepositoryURL) { if len(payload.RepositoryURL) == 0 || !govalidator.IsURL(payload.RepositoryURL) {
return errors.New("Invalid repository URL. Must correspond to a valid URL format") return errors.New("Invalid repository URL. Must correspond to a valid URL format")
} }
if payload.RepositoryAuthentication && govalidator.IsNull(payload.RepositoryPassword) { if payload.RepositoryAuthentication && len(payload.RepositoryPassword) == 0 {
return errors.New("Invalid repository credentials. Password must be specified when authentication is enabled") return errors.New("Invalid repository credentials. Password must be specified when authentication is enabled")
} }
if err := update.ValidateAutoUpdateSettings(payload.AutoUpdate); err != nil { if err := update.ValidateAutoUpdateSettings(payload.AutoUpdate); err != nil {

View file

@ -14,7 +14,6 @@ import (
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -29,7 +28,7 @@ type updateComposeStackPayload struct {
} }
func (payload *updateComposeStackPayload) Validate(r *http.Request) error { func (payload *updateComposeStackPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.StackFileContent) { if len(payload.StackFileContent) == 0 {
return errors.New("Invalid stack file content") return errors.New("Invalid stack file content")
} }
@ -48,7 +47,7 @@ type updateSwarmStackPayload struct {
} }
func (payload *updateSwarmStackPayload) Validate(r *http.Request) error { func (payload *updateSwarmStackPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.StackFileContent) { if len(payload.StackFileContent) == 0 {
return errors.New("Invalid stack file content") return errors.New("Invalid stack file content")
} }

View file

@ -16,7 +16,6 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/asaskevich/govalidator"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -37,7 +36,7 @@ type kubernetesGitStackUpdatePayload struct {
} }
func (payload *kubernetesFileStackUpdatePayload) Validate(r *http.Request) error { func (payload *kubernetesFileStackUpdatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.StackFileContent) { if len(payload.StackFileContent) == 0 {
return errors.New("Invalid stack file content") return errors.New("Invalid stack file content")
} }

View file

@ -8,8 +8,6 @@ import (
"github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/dataservices"
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/asaskevich/govalidator"
) )
type tagCreatePayload struct { type tagCreatePayload struct {
@ -17,7 +15,7 @@ type tagCreatePayload struct {
} }
func (payload *tagCreatePayload) Validate(r *http.Request) error { func (payload *tagCreatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("invalid tag name") return errors.New("invalid tag name")
} }

View file

@ -8,8 +8,6 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
) )
type teamCreatePayload struct { type teamCreatePayload struct {
@ -20,7 +18,7 @@ type teamCreatePayload struct {
} }
func (payload *teamCreatePayload) Validate(r *http.Request) error { func (payload *teamCreatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Name) { if len(payload.Name) == 0 {
return errors.New("Invalid team name") return errors.New("Invalid team name")
} }
return nil return nil

View file

@ -8,8 +8,6 @@ import (
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/asaskevich/govalidator"
) )
type filePayload struct { type filePayload struct {
@ -20,11 +18,11 @@ type filePayload struct {
} }
func (payload *filePayload) Validate(r *http.Request) error { func (payload *filePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.RepositoryURL) { if len(payload.RepositoryURL) == 0 {
return errors.New("Invalid repository url") return errors.New("Invalid repository url")
} }
if govalidator.IsNull(payload.ComposeFilePathInRepository) { if len(payload.ComposeFilePathInRepository) == 0 {
return errors.New("Invalid file path") return errors.New("Invalid file path")
} }

View file

@ -3,13 +3,12 @@ package users
import ( import (
"errors" "errors"
"net/http" "net/http"
"strings"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
) )
type adminInitPayload struct { type adminInitPayload struct {
@ -20,10 +19,10 @@ type adminInitPayload struct {
} }
func (payload *adminInitPayload) Validate(r *http.Request) error { func (payload *adminInitPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Username) || govalidator.Contains(payload.Username, " ") { if len(payload.Username) == 0 || strings.Contains(payload.Username, " ") {
return errors.New("Invalid username. Must not contain any whitespace") return errors.New("Invalid username. Must not contain any whitespace")
} }
if govalidator.IsNull(payload.Password) { if len(payload.Password) == 0 {
return errors.New("Invalid password") return errors.New("Invalid password")
} }
return nil return nil

View file

@ -3,13 +3,12 @@ package users
import ( import (
"errors" "errors"
"net/http" "net/http"
"strings"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
) )
type userCreatePayload struct { type userCreatePayload struct {
@ -20,7 +19,7 @@ type userCreatePayload struct {
} }
func (payload *userCreatePayload) Validate(r *http.Request) error { func (payload *userCreatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Username) || govalidator.Contains(payload.Username, " ") { if len(payload.Username) == 0 || strings.Contains(payload.Username, " ") {
return errors.New("Invalid username. Must not contain any whitespace") return errors.New("Invalid username. Must not contain any whitespace")
} }

View file

@ -21,7 +21,7 @@ type userAccessTokenCreatePayload struct {
} }
func (payload *userAccessTokenCreatePayload) Validate(r *http.Request) error { func (payload *userAccessTokenCreatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Description) { if len(payload.Description) == 0 {
return errors.New("invalid description: cannot be empty") return errors.New("invalid description: cannot be empty")
} }
if govalidator.HasWhitespaceOnly(payload.Description) { if govalidator.HasWhitespaceOnly(payload.Description) {
@ -100,7 +100,7 @@ func (handler *Handler) userCreateAccessToken(w http.ResponseWriter, r *http.Req
if internalAuth { if internalAuth {
// Internal auth requires the password field and must not be empty // Internal auth requires the password field and must not be empty
if govalidator.IsNull(payload.Password) { if len(payload.Password) == 0 {
return httperror.BadRequest("Invalid request payload", errors.New("invalid password: cannot be empty")) return httperror.BadRequest("Invalid request payload", errors.New("invalid password: cannot be empty"))
} }

View file

@ -4,6 +4,7 @@ import (
"cmp" "cmp"
"errors" "errors"
"net/http" "net/http"
"strings"
"time" "time"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
@ -12,8 +13,6 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
) )
type themePayload struct { type themePayload struct {
@ -33,7 +32,7 @@ type userUpdatePayload struct {
} }
func (payload *userUpdatePayload) Validate(r *http.Request) error { func (payload *userUpdatePayload) Validate(r *http.Request) error {
if govalidator.Contains(payload.Username, " ") { if strings.Contains(payload.Username, " ") {
return errors.New("invalid username. Must not contain any whitespace") return errors.New("invalid username. Must not contain any whitespace")
} }

View file

@ -11,8 +11,6 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
) )
type userUpdatePasswordPayload struct { type userUpdatePasswordPayload struct {
@ -23,10 +21,10 @@ type userUpdatePasswordPayload struct {
} }
func (payload *userUpdatePasswordPayload) Validate(r *http.Request) error { func (payload *userUpdatePasswordPayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.Password) { if len(payload.Password) == 0 {
return errors.New("Invalid current password") return errors.New("Invalid current password")
} }
if govalidator.IsNull(payload.NewPassword) { if len(payload.NewPassword) == 0 {
return errors.New("Invalid new password") return errors.New("Invalid new password")
} }
return nil return nil

View file

@ -11,7 +11,6 @@ import (
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/asaskevich/govalidator"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
) )
@ -24,7 +23,7 @@ type webhookCreatePayload struct {
} }
func (payload *webhookCreatePayload) Validate(r *http.Request) error { func (payload *webhookCreatePayload) Validate(r *http.Request) error {
if govalidator.IsNull(payload.ResourceID) { if len(payload.ResourceID) == 0 {
return errors.New("Invalid ResourceID") return errors.New("Invalid ResourceID")
} }
if payload.EndpointID == 0 { if payload.EndpointID == 0 {