package api import ( "fmt" "regexp" "strings" ) var ( iqnRegex = regexp.MustCompile(`^iqn\.\d{4}-\d{2}\.[^:]+:[^:]+$`) ) func validateIQN(iqn string) error { if !iqnRegex.MatchString(iqn) { return fmt.Errorf("invalid IQN format: %s (expected format: iqn.YYYY-MM.reversed.domain:identifier)", iqn) } return nil } func validateRepositoryName(name string) error { if name == "" { return fmt.Errorf("repository name cannot be empty") } if len(name) > 64 { return fmt.Errorf("repository name too long (max 64 characters)") } // Allow alphanumeric, dash, underscore matched, _ := regexp.MatchString(`^[a-zA-Z0-9_-]+$`, name) if !matched { return fmt.Errorf("repository name contains invalid characters (only alphanumeric, dash, underscore allowed)") } return nil } func validateSize(size string) error { if size == "" { return fmt.Errorf("size cannot be empty") } // Basic validation for size format (e.g., "10G", "500M") matched, _ := regexp.MatchString(`^\d+[KMGT]?$`, strings.ToUpper(size)) if !matched { return fmt.Errorf("invalid size format: %s (expected format: 10G, 500M, etc.)", size) } return nil } func validateIP(ip string) error { // Basic IP validation parts := strings.Split(ip, ":") if len(parts) != 2 { return fmt.Errorf("invalid IP:port format: %s", ip) } ipParts := strings.Split(parts[0], ".") if len(ipParts) != 4 { return fmt.Errorf("invalid IP address: %s", parts[0]) } return nil } func validatePortal(portal string) error { if portal == "" { return fmt.Errorf("portal cannot be empty") } // Format: IP:port or hostname:port parts := strings.Split(portal, ":") if len(parts) != 2 { return fmt.Errorf("invalid portal format: %s (expected IP:port or hostname:port)", portal) } // If it looks like an IP, validate it if matched, _ := regexp.MatchString(`^\d+\.\d+\.\d+\.\d+$`, parts[0]); matched { return validateIP(portal) } return nil }