Skip to content

Commit

Permalink
Added ImageGet message with tests, got importer compiling again
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Nemere committed Oct 31, 2023
1 parent 01d46bd commit a8befe0
Show file tree
Hide file tree
Showing 6 changed files with 1,842 additions and 1,606 deletions.
41 changes: 41 additions & 0 deletions api/ws/handlers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package wsHandler
import (
"context"
"errors"
"fmt"

"github.com/pixlise/core/v3/api/dbCollections"
"github.com/pixlise/core/v3/api/ws/wsHelpers"
"github.com/pixlise/core/v3/core/errorwithstatus"
protos "github.com/pixlise/core/v3/generated-protos"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down Expand Up @@ -51,6 +53,45 @@ func HandleImageListReq(req *protos.ImageListReq, hctx wsHelpers.HandlerContext)
}, nil
}

func HandleImageGetReq(req *protos.ImageGetReq, hctx wsHelpers.HandlerContext) (*protos.ImageGetResp, error) {
if err := wsHelpers.CheckStringField(&req.ImageName, "ImageName", 1, 255); err != nil {
return nil, err
}

// Look up the image in DB to determine scan IDs, then determine ownership
ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.ImagesName)

filter := bson.M{"_id": req.ImageName}
result := coll.FindOne(ctx, filter)
if result.Err() != nil {
return nil, result.Err()
}

img := protos.ScanImage{}
err := result.Decode(&img)
if err != nil {
return nil, err
}

// Now look up any associated ids
if len(img.AssociatedScanIds) <= 0 {
return nil, fmt.Errorf("Failed to find scan associated with image: %v", req.ImageName)
}

// Check that the user has access to all the scans in question
for _, scanId := range img.AssociatedScanIds {
_, err := wsHelpers.CheckObjectAccess(false, scanId, protos.ObjectType_OT_SCAN, hctx)
if err != nil {
return nil, errorwithstatus.MakeUnauthorisedError(fmt.Errorf("User cannot access scan %v associated with image %v. Error: %v", scanId, req.ImageName, err))
}
}

return &protos.ImageGetResp{
Image: &img,
}, nil
}

func HandleImageGetDefaultReq(req *protos.ImageGetDefaultReq, hctx wsHelpers.HandlerContext) (*protos.ImageGetDefaultResp, error) {
if err := wsHelpers.CheckFieldLength(req.ScanIds, "ScanIds", 1, 10); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion data-formats
Loading

0 comments on commit a8befe0

Please sign in to comment.