Skip to content

Commit

Permalink
Validate drive ID args as UUIDs (#792)
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <bala@minio.io>
  • Loading branch information
balamurugana committed Jun 6, 2023
1 parent ca9ebb2 commit c0ae169
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/kubectl-directpv/flags.go
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions pkg/installer/migrate.go
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"os"
"regexp"
"strings"

directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/utils.go
Expand Up @@ -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 {
Expand Down

0 comments on commit c0ae169

Please sign in to comment.