Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/api/handlers/cloning_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,14 @@ func (ch *CloningHandler) GetPodsHandler(c *gin.Context) {

// Loop through the user's deployed pods and add template information
for i := range pods {
templateName := strings.Replace(strings.ToLower(pods[i].Name[5:]), fmt.Sprintf("_%s", strings.ToLower(username)), "", 1)
deploymentName, ok := cloning.PodPoolDeploymentName(pods[i].Name)
if !ok {
log.Printf("Error parsing pod name %s", pods[i].Name)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to parse pod name", "details": fmt.Sprintf("Pod %s has an invalid name", pods[i].Name)})
return
}

templateName := strings.Replace(strings.ToLower(deploymentName), fmt.Sprintf("_%s", strings.ToLower(username)), "", 1)
templateInfo, err := ch.Service.DatabaseService.GetTemplateInfo(templateName)
if err != nil {
log.Printf("Error retrieving template info for pod %s: %v", pods[i].Name, err)
Expand Down
10 changes: 9 additions & 1 deletion internal/api/handlers/dashboard_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"

"github.com/cpp-cyber/proclone/internal/cloning"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -91,7 +92,14 @@ func (dh *DashboardHandler) GetUserDashboardStatsHandler(c *gin.Context) {

// Loop through the user's deployed pods and add template information
for i := range pods {
templateName := strings.Replace(strings.ToLower(pods[i].Name[5:]), fmt.Sprintf("_%s", strings.ToLower(username)), "", 1)
deploymentName, ok := cloning.PodPoolDeploymentName(pods[i].Name)
if !ok {
log.Printf("Error parsing pod name %s", pods[i].Name)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to parse pod name", "details": fmt.Sprintf("Pod %s has an invalid name", pods[i].Name)})
return
}

templateName := strings.Replace(strings.ToLower(deploymentName), fmt.Sprintf("_%s", strings.ToLower(username)), "", 1)
templateInfo, err := dh.cloningHandler.Service.DatabaseService.GetTemplateInfo(templateName)
if err != nil {
log.Printf("Error retrieving template info for pod %s: %v", pods[i].Name, err)
Expand Down
28 changes: 25 additions & 3 deletions internal/cloning/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@ package cloning
import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/cpp-cyber/proclone/internal/proxmox"
)

func PodPoolDeploymentName(poolName string) (string, bool) {
name := strings.TrimPrefix(poolName, "pod_")
if len(name) < 5 || name[4] != '_' {
return "", false
}

if _, err := strconv.Atoi(name[:4]); err != nil {
return "", false
}

return name[5:], true
}

func (cs *CloningService) GetPods(username string) ([]Pod, error) {
// Get User DN
userDN, err := cs.LDAPService.GetUserDN(username)
Expand All @@ -23,7 +37,10 @@ func (cs *CloningService) GetPods(username string) ([]Pod, error) {

// Build regex pattern to match username or any of their group names
groupsWithUser := append(groups, username)
regexPattern := fmt.Sprintf(`(?i)1[0-9]{3}_.*_(%s)$`, strings.Join(groupsWithUser, "|"))
for i := range groupsWithUser {
groupsWithUser[i] = regexp.QuoteMeta(groupsWithUser[i])
}
regexPattern := fmt.Sprintf(`(?i)^(?:pod_)?1[0-9]{3}_.*_(%s)$`, strings.Join(groupsWithUser, "|"))

// Get pods based on regex pattern
pods, err := cs.MapVirtualResourcesToPods(regexPattern)
Expand All @@ -34,7 +51,7 @@ func (cs *CloningService) GetPods(username string) ([]Pod, error) {
}

func (cs *CloningService) AdminGetPods() ([]Pod, error) {
pods, err := cs.MapVirtualResourcesToPods(`1[0-9]{3}_.*`)
pods, err := cs.MapVirtualResourcesToPods(`(?i)^(?:pod_)?1[0-9]{3}_.*`)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,7 +104,12 @@ func (cs *CloningService) ValidateCloneRequest(templateName string, username str

for _, pod := range podPools {
// Remove the Pod ID number and _ to compare
if !alreadyDeployed && strings.EqualFold(pod.Name[5:], templateName) {
deploymentName, ok := PodPoolDeploymentName(pod.Name)
if !ok {
continue
}

if !alreadyDeployed && strings.EqualFold(deploymentName, templateName) {
alreadyDeployed = true
}

Expand Down
30 changes: 18 additions & 12 deletions internal/proxmox/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ func (s *ProxmoxService) GetTemplatePools() ([]string, error) {
return templatePools, nil
}

func parsePodIDFromPoolName(poolName string) (int, bool) {
name := strings.TrimPrefix(poolName, "pod_")
if len(name) < 4 {
return 0, false
}

id, err := strconv.Atoi(name[:4])
if err != nil {
return 0, false
}

return id, true
}

func (s *ProxmoxService) IsPoolEmpty(poolName string) (bool, error) {
poolVMs, err := s.GetPoolVMs(poolName)
if err != nil {
Expand Down Expand Up @@ -187,12 +201,8 @@ func (s *ProxmoxService) GetNextPodID(minPodID int, maxPodID int) (string, int,
// Extract pod IDs from existing pools
var usedIDs []int
for _, pool := range poolsResponse {
if len(pool.PoolID) >= 4 {
if id, err := strconv.Atoi(pool.PoolID[:4]); err == nil {
if id >= minPodID && id <= maxPodID {
usedIDs = append(usedIDs, id)
}
}
if id, ok := parsePodIDFromPoolName(pool.PoolID); ok && id >= minPodID && id <= maxPodID {
usedIDs = append(usedIDs, id)
}
}

Expand Down Expand Up @@ -226,12 +236,8 @@ func (s *ProxmoxService) GetNextPodIDs(minPodID int, maxPodID int, num int) ([]s
// Extract pod IDs from existing pools
var usedIDs []int
for _, pool := range poolsResponse {
if len(pool.PoolID) >= 4 {
if id, err := strconv.Atoi(pool.PoolID[:4]); err == nil {
if id >= minPodID && id <= maxPodID {
usedIDs = append(usedIDs, id)
}
}
if id, ok := parsePodIDFromPoolName(pool.PoolID); ok && id >= minPodID && id <= maxPodID {
usedIDs = append(usedIDs, id)
}
}

Expand Down
Loading