Skip to content

Commit da83be6

Browse files
authored
[deckhouse-cli] fix: mirror: correct URL parsing for VEX images with port in registry address (#253)
Signed-off-by: Timur Tuktamyshev <[email protected]>
1 parent 1b61ae9 commit da83be6

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

pkg/libmirror/images/digests.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,32 @@ func FindVexImage(
195195
return "", fmt.Errorf("parse reference: %w", err)
196196
}
197197

198-
split := strings.SplitN(vexImageName, ":", 2)
199-
imagePath := split[0]
200-
tag := split[1]
198+
// Use LastIndex to correctly handle URLs with port (e.g., localhost:443/repo:tag)
199+
splitIndex := strings.LastIndex(vexImageName, ":")
200+
if splitIndex == -1 {
201+
return "", fmt.Errorf("invalid vex image name format: %s", vexImageName)
202+
}
203+
imagePath := vexImageName[:splitIndex]
204+
tag := vexImageName[splitIndex+1:]
201205

206+
// Add missing path segments to client if VEX image is in a subpath
202207
imageSegmentsRaw := strings.TrimPrefix(imagePath, client.GetRegistry())
203-
imageSegments := strings.Split(imageSegmentsRaw, "/")
204-
205-
for i, segment := range imageSegments {
206-
client = client.WithSegment(segment)
207-
logger.Debugf("Segment %d: %s", i, segment)
208+
imageSegmentsRaw = strings.TrimPrefix(imageSegmentsRaw, "/")
209+
if imageSegmentsRaw != "" {
210+
for _, segment := range strings.Split(imageSegmentsRaw, "/") {
211+
client = client.WithSegment(segment)
212+
logger.Debugf("Segment: %s", segment)
213+
}
208214
}
209215

210216
err = client.CheckImageExists(context.TODO(), tag)
211217
if errors.Is(err, regclient.ErrImageNotFound) {
212218
// Image not found, which is expected for non-vulnerable images
213219
return "", nil
214220
}
221+
if err != nil {
222+
return "", fmt.Errorf("check VEX image exists: %w", err)
223+
}
215224

216225
return vexImageName, nil
217226
}

pkg/libmirror/layouts/layouts.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,23 +493,32 @@ func FindVexImage(
493493
return "", fmt.Errorf("parse reference: %w", err)
494494
}
495495

496-
split := strings.SplitN(vexImageName, ":", 2)
497-
imagePath := split[0]
498-
tag := split[1]
496+
// Use LastIndex to correctly handle URLs with port (e.g., localhost:443/repo:tag)
497+
splitIndex := strings.LastIndex(vexImageName, ":")
498+
if splitIndex == -1 {
499+
return "", fmt.Errorf("invalid vex image name format: %s", vexImageName)
500+
}
501+
imagePath := vexImageName[:splitIndex]
502+
tag := vexImageName[splitIndex+1:]
499503

504+
// Add missing path segments to client if VEX image is in a subpath
500505
imageSegmentsRaw := strings.TrimPrefix(imagePath, client.GetRegistry())
501-
imageSegments := strings.Split(imageSegmentsRaw, "/")
502-
503-
for i, segment := range imageSegments {
504-
client = client.WithSegment(segment)
505-
logger.Debugf("Segment %d: %s", i, segment)
506+
imageSegmentsRaw = strings.TrimPrefix(imageSegmentsRaw, "/")
507+
if imageSegmentsRaw != "" {
508+
for _, segment := range strings.Split(imageSegmentsRaw, "/") {
509+
client = client.WithSegment(segment)
510+
logger.Debugf("Segment: %s", segment)
511+
}
506512
}
507513

508514
err = client.CheckImageExists(context.TODO(), tag)
509515
if errors.Is(err, regclient.ErrImageNotFound) {
510516
// Image not found, which is expected for non-vulnerable images
511517
return "", nil
512518
}
519+
if err != nil {
520+
return "", fmt.Errorf("check VEX image exists: %w", err)
521+
}
513522

514523
return vexImageName, nil
515524
}

0 commit comments

Comments
 (0)