feat(graph): allow client-supplied drive IDs when creating spaces

This commit is contained in:
Sara Ahmadi committed 2026-11-10 13:41:51 +01:00
1 parent 83615f23e5
commit 5ea6b25e22
4 files changed
+192

No files matched your search

+17
View File
@@ -20,6 +20,7 @@ import (
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/go-chi/render"
"github.com/google/uuid"
libregraph "github.com/opencloud-eu/libre-graph-api-go"
revactx "github.com/opencloud-eu/reva/v2/pkg/ctx"
"github.com/opencloud-eu/reva/v2/pkg/storagespace"
@@ -429,6 +430,17 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
Quota: getQuota(drive.Quota, g.config.Spaces.DefaultQuota),
}
if id := drive.GetId(); id != "" {
// the id ends up in storage paths and in "storageid$spaceid!opaqueid" references,
// so only accept the canonical form of the uuids the storage drivers generate themselves
if u, err := uuid.Parse(id); err != nil || u.String() != id {
log.Debug().Str("id", id).Msg("could not create drive: id is not a canonical uuid")
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "invalid drive id: must be a canonical UUID")
return
}
csr.Opaque = utils.AppendPlainToOpaque(csr.Opaque, "spaceid", id)
}
if drive.Description != nil {
csr.Opaque = utils.AppendPlainToOpaque(csr.Opaque, "description", *drive.Description)
}
@@ -463,6 +475,11 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, resp.GetStatus().GetMessage())
return
}
if resp.GetStatus().GetCode() == cs3rpc.Code_CODE_ALREADY_EXISTS {
log.Debug().Str("grpcmessage", resp.GetStatus().GetMessage()).Msg("could not create drive: already exists")
errorcode.NameAlreadyExists.Render(w, r, http.StatusConflict, "drive already exists")
return
}
log.Debug().Interface("grpcmessage", csr).Str("grpc", resp.GetStatus().GetMessage()).Msg("could not create drive: grpc error")
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, resp.GetStatus().GetMessage())
return
+110
View File
@@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strings"
"time"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
@@ -843,6 +844,115 @@ var _ = Describe("Graph", func() {
Expect(*response.Description).To(Equal("This space is for testing"))
Expect(*response.LibreGraphContentType).To(Equal("application/vnd.opencloud.test"))
})
Context("drive id handling", func() {
const spaceID = "0b9a7b84-3e71-4a4e-9c34-0e6a4c3f5f21"
// csr captures the request the handler sends to the gateway
var csr *provider.CreateStorageSpaceRequest
mockSuccessfulCreate := func(createdSpaceID string) {
gatewayClient.On("CreateStorageSpace", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
csr = args.Get(1).(*provider.CreateStorageSpaceRequest)
}).Return(&provider.CreateStorageSpaceResponse{
Status: status.NewOK(ctx),
StorageSpace: &provider.StorageSpace{
Id: &provider.StorageSpaceId{OpaqueId: "pro-1$" + createdSpaceID},
Name: "Test Space",
SpaceType: "project",
Root: &provider.ResourceId{StorageId: "pro-1", SpaceId: createdSpaceID, OpaqueId: createdSpaceID},
},
}, nil)
gatewayClient.On("GetQuota", mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{
Status: status.NewOK(ctx),
TotalBytes: 500,
}, nil)
}
BeforeEach(func() {
csr = nil
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settingssvc.GetPermissionByIDResponse{
Permission: &v0.Permission{
Operation: v0.Permission_OPERATION_READWRITE,
Constraint: v0.Permission_CONSTRAINT_ALL,
},
}, nil)
})
DescribeTable("lets the storage provider generate the id when none is supplied",
func(jsonBody string) {
mockSuccessfulCreate("generated-id")
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/drives", bytes.NewBufferString(jsonBody)).WithContext(ctx)
rr := httptest.NewRecorder()
svc.CreateDrive(rr, r)
Expect(rr.Code).To(Equal(http.StatusCreated))
Expect(csr).ToNot(BeNil())
Expect(csr.GetOpaque().GetMap()).ToNot(HaveKey("spaceid"))
},
Entry("absent id", `{"name": "Test Space"}`),
Entry("empty id", `{"name": "Test Space", "id": ""}`),
)
It("forwards the id to the storage provider", func() {
mockSuccessfulCreate(spaceID)
jsonBody := []byte(`{"name": "Test Space", "id": "` + spaceID + `"}`)
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/drives", bytes.NewBuffer(jsonBody)).WithContext(ctx)
rr := httptest.NewRecorder()
svc.CreateDrive(rr, r)
Expect(rr.Code).To(Equal(http.StatusCreated))
Expect(utils.ReadPlainFromOpaque(csr.GetOpaque(), "spaceid")).To(Equal(spaceID))
var response libregraph.Drive
Expect(json.Unmarshal(rr.Body.Bytes(), &response)).To(Succeed())
Expect(response.GetId()).To(Equal("pro-1$" + spaceID))
})
DescribeTable("rejects ids that are not canonical UUIDs before calling the storage provider",
func(id string) {
gatewayClient.On("CreateStorageSpace", mock.Anything, mock.Anything).Return(&provider.CreateStorageSpaceResponse{
Status: status.NewInternal(ctx, "storage provider must not be called"),
}, nil).Maybe()
body, err := json.Marshal(map[string]string{"name": "Test Space", "id": id})
Expect(err).ToNot(HaveOccurred())
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/drives", bytes.NewBuffer(body)).WithContext(ctx)
rr := httptest.NewRecorder()
svc.CreateDrive(rr, r)
Expect(rr.Code).To(Equal(http.StatusBadRequest))
var libreError libregraph.OdataError
Expect(json.Unmarshal(rr.Body.Bytes(), &libreError)).To(Succeed())
Expect(libreError.Error.Code).To(Equal(errorcode.InvalidRequest.String()))
Expect(libreError.Error.Message).To(Equal("invalid drive id: must be a canonical UUID"))
gatewayClient.AssertNotCalled(GinkgoT(), "CreateStorageSpace", mock.Anything, mock.Anything)
},
Entry("path traversal", "../../x"),
Entry("storage id delimiter", "pro-1$"+spaceID),
Entry("opaque id delimiter", spaceID+"!"+spaceID),
Entry("arbitrary string", "not-a-uuid"),
Entry("uppercase", strings.ToUpper(spaceID)),
Entry("braces", "{"+spaceID+"}"),
Entry("urn", "urn:uuid:"+spaceID),
Entry("no hyphens", strings.ReplaceAll(spaceID, "-", "")),
)
It("returns 409 Conflict when the storage provider reports that the space already exists", func() {
gatewayClient.On("CreateStorageSpace", mock.Anything, mock.Anything).Return(&provider.CreateStorageSpaceResponse{
Status: status.NewAlreadyExists(ctx, nil, "decomposedfs: spaces: space already exists"),
}, nil)
jsonBody := []byte(`{"name": "Test Space", "id": "` + spaceID + `"}`)
r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/drives", bytes.NewBuffer(jsonBody)).WithContext(ctx)
rr := httptest.NewRecorder()
svc.CreateDrive(rr, r)
Expect(rr.Code).To(Equal(http.StatusConflict))
var libreError libregraph.OdataError
Expect(json.Unmarshal(rr.Body.Bytes(), &libreError)).To(Succeed())
Expect(libreError.Error.Code).To(Equal(errorcode.NameAlreadyExists.String()))
Expect(libreError.Error.Message).To(Equal("drive already exists"))
})
})
It("Incomplete space", func() {
gatewayClient.On("CreateStorageSpace", mock.Anything, mock.Anything).Return(&provider.CreateStorageSpaceResponse{
Status: status.NewOK(ctx),
@@ -805,6 +805,30 @@ class SpacesContext implements Context {
);
}
/**
* @param string $user
* @param string $spaceName
* @param string $spaceType
* @param string $spaceId
*
* @return void
*
* @throws GuzzleException
* @throws Exception
*/
#[When('/^user "([^"]*)" (?:creates|tries to create) a space "([^"]*)" of type "([^"]*)" with id "([^"]*)" using the Graph API$/')]
public function theUserCreatesASpaceWithIdUsingTheGraphApi(
string $user,
string $spaceName,
string $spaceType,
string $spaceId
): void {
$space = ["name" => $spaceName, "driveType" => $spaceType, "id" => $spaceId];
$this->featureContext->setResponse(
$this->createSpace($user, $space)
);
}
/**
* Remember the available Spaces
*
@@ -153,3 +153,44 @@ Feature: create space
| user-role |
| Admin |
| Space Admin |
Scenario: space admin creates a project space with a client-supplied id via the Graph API
Given the administrator has assigned the role "Space Admin" to user "Alice" using the Graph API
When user "Alice" creates a space "Project Mars" of type "project" with id "4e3b1a52-8f0d-4c6e-9a71-2d5f6b8c9e10" using the Graph API
Then the HTTP status code should be "201"
And the JSON response should contain space called "Project Mars" and match
"""
{
"type": "object",
"required": ["id"],
"properties": {
"id": {
"type": "string",
"pattern": "\\$4e3b1a52-8f0d-4c6e-9a71-2d5f6b8c9e10$"
}
}
}
"""
When user "Alice" looks up the single space "Project Mars" via the Graph API by using its id
Then the HTTP status code should be "200"
And the JSON response should contain space called "Project Mars" and match
"""
{
"type": "object",
"required": ["id", "name"],
"properties": {
"id": {
"type": "string",
"pattern": "\\$4e3b1a52-8f0d-4c6e-9a71-2d5f6b8c9e10$"
},
"name": {
"type": "string",
"enum": ["Project Mars"]
}
}
}
"""
When user "Alice" tries to create a space "Project Venus" of type "project" with id "4e3b1a52-8f0d-4c6e-9a71-2d5f6b8c9e10" using the Graph API
Then the HTTP status code should be "409"
And the user "Alice" should not have a space called "Project Venus"