From c0ae169c3b169219a4672bb753872c35381410b6 Mon Sep 17 00:00:00 2001 From: Bala FA Date: Tue, 6 Jun 2023 11:04:42 +0530 Subject: [PATCH] Validate drive ID args as UUIDs (#792) Signed-off-by: Bala.FA --- cmd/kubectl-directpv/flags.go | 3 +++ pkg/installer/migrate.go | 5 +---- pkg/utils/utils.go | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/kubectl-directpv/flags.go b/cmd/kubectl-directpv/flags.go index 09adbd8c6..f887591ec 100644 --- a/cmd/kubectl-directpv/flags.go +++ b/cmd/kubectl-directpv/flags.go @@ -186,6 +186,9 @@ func validateDriveIDArgs() error { if driveIDArgs[i] == "" { return fmt.Errorf("empty drive ID") } + if !utils.IsUUID(driveIDArgs[i]) { + return fmt.Errorf("invalid drive ID %v", driveIDArgs[i]) + } driveIDSelectors = append(driveIDSelectors, directpvtypes.DriveID(driveIDArgs[i])) } return nil diff --git a/pkg/installer/migrate.go b/pkg/installer/migrate.go index 1d9ca82e5..bc53f77b2 100644 --- a/pkg/installer/migrate.go +++ b/pkg/installer/migrate.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "os" - "regexp" "strings" directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types" @@ -48,8 +47,6 @@ const ( legacyPurgeProtection = legacyclient.GroupName + "/purge-protection" ) -var uuidRegex = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$") - type migrateTask struct{} func (migrateTask) Name() string { @@ -99,7 +96,7 @@ func migrateDrives(ctx context.Context, dryRun bool, progressCh chan<- Message) continue // ignore other than Ready/InUse drives } - if !uuidRegex.MatchString(result.Drive.Status.FilesystemUUID) { + if !utils.IsUUID(result.Drive.Status.FilesystemUUID) { legacyDriveErrors[result.Drive.Name] = fmt.Errorf( "invalid filesystem UUID %v in legacy drive %v", result.Drive.Status.FilesystemUUID, result.Drive.Name, diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index c206a298d..0e12f0b14 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -21,12 +21,20 @@ import ( "fmt" "os" "path/filepath" + "regexp" "strings" "github.com/fatih/color" "sigs.k8s.io/yaml" ) +var uuidRegex = regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$") + +// IsUUID checks whether value is UUID string. +func IsUUID(value string) bool { + return uuidRegex.MatchString(value) +} + // Contains checks whether value in the slice. func Contains[ctype comparable](slice []ctype, value ctype) bool { for _, s := range slice {