Skip to content

Commit 3504a8f

Browse files
fix(api): enforce import_enabled flag for local storage (#8844)
1 parent 2164ca0 commit 3504a8f

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

pkg/api/controller.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,20 @@ func (c *Controller) ImportStart(w http.ResponseWriter, r *http.Request, body ap
31293129
if !c.authorize(w, r, perm) {
31303130
return
31313131
}
3132-
3132+
repo, err := c.Catalog.GetRepository(r.Context(), repository)
3133+
if err != nil {
3134+
_ = c.handleAPIError(r.Context(), w, r, err)
3135+
return
3136+
}
3137+
storageInfo := c.BlockAdapter.GetStorageNamespaceInfo(repo.StorageNamespace)
3138+
if storageInfo == nil {
3139+
_ = c.handleAPIError(r.Context(), w, r, graveler.ErrNotFound)
3140+
return
3141+
}
3142+
if !storageInfo.ImportSupport {
3143+
writeError(w, r, http.StatusForbidden, "import is not supported for this storage namespace")
3144+
return
3145+
}
31333146
ctx := r.Context()
31343147
c.LogAction(ctx, "import", r, repository, branch, "")
31353148

pkg/api/controller_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6628,3 +6628,43 @@ func TestController_GetLicense(t *testing.T) {
66286628
require.NotNil(t, resp.JSON501, "expected HTTP-501 response, got %v", resp.StatusCode())
66296629
})
66306630
}
6631+
6632+
func TestController_ImportStart_Disabled(t *testing.T) {
6633+
// set up a server with local imports disabled
6634+
storageLocation := t.TempDir()
6635+
viper.Set(config.BlockstoreTypeKey, block.BlockstoreTypeLocal)
6636+
viper.Set("blockstore.local.path", storageLocation)
6637+
viper.Set("blockstore.local.import_enabled", false)
6638+
6639+
clt, deps := setupClientWithAdmin(t)
6640+
ctx := t.Context()
6641+
6642+
// Create a repository with a unique name for the test
6643+
repo := testUniqueRepoName()
6644+
createRepoResp, err := clt.CreateRepositoryWithResponse(ctx, &apigen.CreateRepositoryParams{}, apigen.CreateRepositoryJSONRequestBody{
6645+
Name: repo,
6646+
StorageNamespace: onBlock(deps, "bucket/prefix"),
6647+
})
6648+
require.NoError(t, err)
6649+
require.NotNil(t, createRepoResp.JSON201, "expected status 201 Created when creating repo")
6650+
6651+
// attempt to start an import
6652+
resp, err := clt.ImportStartWithResponse(ctx, repo, "main", apigen.ImportStartJSONRequestBody{
6653+
Commit: apigen.CommitCreation{
6654+
Message: "test import",
6655+
},
6656+
Paths: []apigen.ImportLocation{
6657+
{
6658+
Path: "some/local/path",
6659+
Type: "common_prefix",
6660+
Destination: "/",
6661+
},
6662+
},
6663+
})
6664+
6665+
// verify the request is forbidden
6666+
require.NoError(t, err)
6667+
require.NotNil(t, resp, "response should not be nil")
6668+
require.NotNil(t, resp.JSON403, "expected a 403 forbidden response, but got none")
6669+
require.Contains(t, resp.JSON403.Message, "import is not supported for this storage namespace")
6670+
}

0 commit comments

Comments
 (0)