Skip to content

Commit 607776e

Browse files
committed
refactor: address changes requested by @majst01
1 parent eb6aaa8 commit 607776e

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

cmd/metal-api/internal/service/image-service.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"log/slog"
77
"net/http"
8+
"net/url"
89
"strconv"
9-
"strings"
1010
"time"
1111

1212
"github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore"
@@ -321,7 +321,7 @@ func (r *imageResource) createImage(request *restful.Request, response *restful.
321321
}
322322
}
323323

324-
err = checkImageURL(requestPayload.ID, requestPayload.URL, "", "")
324+
err = checkImageURL(requestPayload.ID, requestPayload.URL, nil)
325325
if err != nil {
326326
r.sendError(request, response, httperrors.BadRequest(err))
327327
return
@@ -350,33 +350,37 @@ func (r *imageResource) createImage(request *restful.Request, response *restful.
350350
r.send(request, response, http.StatusCreated, v1.NewImageResponse(img))
351351
}
352352

353-
func checkImageURL(id, url, username, password string) error {
354-
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
355-
res, err := http.Head(url)
353+
func checkImageURL(id, inputURL string, ociCredentials authn.Authenticator) error {
354+
parsedURL, err := url.Parse(inputURL)
355+
if err != nil {
356+
return fmt.Errorf("image:%s with url:%s could not be parsed. error:%w", id, inputURL, err)
357+
}
358+
359+
switch parsedURL.Scheme {
360+
case "http", "https":
361+
res, err := http.Head(inputURL)
356362
if err != nil {
357-
return fmt.Errorf("image:%s is not accessible under:%s error:%w", id, url, err)
363+
return fmt.Errorf("image:%s is not accessible under:%s error:%w", id, inputURL, err)
358364
}
359365
if res.StatusCode >= 400 {
360-
return fmt.Errorf("image:%s is not accessible under:%s status:%s", id, url, res.Status)
366+
return fmt.Errorf("image:%s is not accessible under:%s status:%s", id, inputURL, res.Status)
361367
}
362-
} else {
363-
ref, err := name.ParseReference(url)
368+
case "oci":
369+
ref, err := name.ParseReference(inputURL)
364370
if err != nil {
365-
return fmt.Errorf("image reference:%s could not be parsed. error:%w", url, err)
371+
return fmt.Errorf("image reference:%s could not be parsed. error:%w", inputURL, err)
366372
}
367373

368-
var auth = authn.Anonymous
369-
if username != "" || password != "" {
370-
auth = &authn.Basic{
371-
Username: username,
372-
Password: password,
373-
}
374+
if ociCredentials == nil {
375+
ociCredentials = authn.Anonymous
374376
}
375377

376-
_, err = remote.Head(ref, remote.WithAuth(auth))
378+
_, err = remote.Head(ref, remote.WithAuth(ociCredentials))
377379
if err != nil {
378-
return fmt.Errorf("image:%s is not accessible under:%s error:%w", id, url, err)
380+
return fmt.Errorf("image:%s is not accessible under:%s error:%w", id, inputURL, err)
379381
}
382+
default:
383+
return fmt.Errorf("image:%s with url:%s has unkown protocol. error:%w", id, inputURL, err)
380384
}
381385

382386
return nil
@@ -435,7 +439,7 @@ func (r *imageResource) updateImage(request *restful.Request, response *restful.
435439
newImage.Description = *requestPayload.Description
436440
}
437441
if requestPayload.URL != nil {
438-
err = checkImageURL(requestPayload.ID, *requestPayload.URL, "", "")
442+
err = checkImageURL(requestPayload.ID, *requestPayload.URL, nil)
439443
if err != nil {
440444
r.sendError(request, response, httperrors.BadRequest(err))
441445
return

cmd/metal-api/internal/service/partition-service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (r *partitionResource) createPartition(request *restful.Request, response *
175175
imageURL = *requestPayload.PartitionBootConfiguration.ImageURL
176176
}
177177

178-
err = checkImageURL("image", imageURL, "", "")
178+
err = checkImageURL("image", imageURL, nil)
179179
if err != nil {
180180
r.sendError(request, response, httperrors.BadRequest(err))
181181
return
@@ -186,7 +186,7 @@ func (r *partitionResource) createPartition(request *restful.Request, response *
186186
kernelURL = *requestPayload.PartitionBootConfiguration.KernelURL
187187
}
188188

189-
err = checkImageURL("kernel", kernelURL, "", "")
189+
err = checkImageURL("kernel", kernelURL, nil)
190190
if err != nil {
191191
r.sendError(request, response, httperrors.BadRequest(err))
192192
return
@@ -306,7 +306,7 @@ func (r *partitionResource) updatePartition(request *restful.Request, response *
306306
newPartition.Labels = requestPayload.Labels
307307
}
308308
if requestPayload.PartitionBootConfiguration.ImageURL != nil {
309-
err = checkImageURL("image", *requestPayload.PartitionBootConfiguration.ImageURL, "", "")
309+
err = checkImageURL("image", *requestPayload.PartitionBootConfiguration.ImageURL, nil)
310310
if err != nil {
311311
r.sendError(request, response, httperrors.BadRequest(err))
312312
return
@@ -316,7 +316,7 @@ func (r *partitionResource) updatePartition(request *restful.Request, response *
316316
}
317317

318318
if requestPayload.PartitionBootConfiguration.KernelURL != nil {
319-
err = checkImageURL("image", *requestPayload.PartitionBootConfiguration.KernelURL, "", "")
319+
err = checkImageURL("kernel", *requestPayload.PartitionBootConfiguration.KernelURL, nil)
320320
if err != nil {
321321
r.sendError(request, response, httperrors.BadRequest(err))
322322
return

0 commit comments

Comments
 (0)