-
Notifications
You must be signed in to change notification settings - Fork 358
Require instance-level access for FileFetcherHttpHandlerInternal downloads #16191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
herdiyana256
wants to merge
2
commits into
cdapio:develop
Choose a base branch
from
herdiyana256:fix-filefetcher-internal-missing-authz
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...t/java/io/cdap/cdap/gateway/handlers/FileFetcherHttpHandlerInternalAuthorizationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * Copyright © 2026 Cask Data, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
|
|
||
| package io.cdap.cdap.gateway.handlers; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import io.cdap.cdap.common.NotFoundException; | ||
| import io.cdap.cdap.proto.id.InstanceId; | ||
| import io.cdap.cdap.proto.security.Authorizable; | ||
| import io.cdap.cdap.proto.security.Principal; | ||
| import io.cdap.cdap.proto.security.StandardPermission; | ||
| import io.cdap.cdap.security.auth.context.AuthenticationTestContext; | ||
| import io.cdap.cdap.security.authorization.DefaultContextAccessEnforcer; | ||
| import io.cdap.cdap.security.authorization.InMemoryAccessController; | ||
| import io.cdap.cdap.security.spi.authentication.AuthenticationContext; | ||
| import io.cdap.cdap.security.spi.authorization.UnauthorizedException; | ||
| import io.cdap.http.HttpResponder; | ||
| import io.netty.handler.codec.http.HttpRequest; | ||
| import java.io.File; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import org.apache.twill.filesystem.LocalLocationFactory; | ||
| import org.apache.twill.filesystem.LocationFactory; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * FileFetcherHttpHandlerInternal#download() resolves the requested path as an absolute | ||
| * location against the whole storage backend (not scoped to any namespace or | ||
| * caller-owned resource), so it must reject callers without instance-level access, | ||
| * same as ConfigHandler does for its own raw-internal-state endpoints. See | ||
| * ConfigHandlerAuthorizationTest for the sibling coverage this mirrors. | ||
| */ | ||
| public class FileFetcherHttpHandlerInternalAuthorizationTest { | ||
| private static final Principal MASTER_PRINCIPAL = new Principal("master", Principal.PrincipalType.USER); | ||
| private static final Principal UNPRIVILEGED_PRINCIPAL = new Principal("unprivileged", | ||
| Principal.PrincipalType.USER); | ||
|
|
||
| private static FileFetcherHttpHandlerInternal fileFetcherHandler; | ||
|
|
||
| HttpRequest request; | ||
| HttpResponder responder; | ||
| Exception exceptionThrown; | ||
|
|
||
| @BeforeClass | ||
| public static void setup() { | ||
| StandardPermission[] requiredPermissions = new StandardPermission[] {StandardPermission.GET}; | ||
|
|
||
| InMemoryAccessController inMemoryAccessController = new InMemoryAccessController(); | ||
| inMemoryAccessController.grant(Authorizable.fromEntityId(InstanceId.SELF), MASTER_PRINCIPAL, | ||
| Collections.unmodifiableSet(new HashSet<>(Arrays.asList(requiredPermissions)))); | ||
| AuthenticationContext authenticationContext = new AuthenticationTestContext(); | ||
| DefaultContextAccessEnforcer contextAccessEnforcer = new DefaultContextAccessEnforcer(authenticationContext, | ||
| inMemoryAccessController); | ||
|
|
||
| LocationFactory locationFactory = new LocalLocationFactory(new File(System.getProperty("java.io.tmpdir"))); | ||
| fileFetcherHandler = new FileFetcherHttpHandlerInternal(locationFactory, contextAccessEnforcer); | ||
| } | ||
|
|
||
| @Before | ||
| public void initializeVariables() { | ||
| request = mock(HttpRequest.class); | ||
| responder = mock(HttpResponder.class); | ||
| exceptionThrown = null; | ||
| } | ||
|
|
||
| @Test | ||
| public void testDownloadUnauthorized() throws Exception { | ||
| when(request.uri()).thenReturn("/v3Internal/location/does-not-matter"); | ||
| AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL); | ||
| try { | ||
| fileFetcherHandler.download(request, responder); | ||
| } catch (UnauthorizedException e) { | ||
| exceptionThrown = e; | ||
| } | ||
| Assert.assertNotNull("an unprivileged caller must not be able to fetch an arbitrary " | ||
| + "storage-backend path via the internal file-fetch endpoint", exceptionThrown); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDownloadAuthorizedReachesLocationLookup() throws Exception { | ||
| when(request.uri()).thenReturn("/v3Internal/location/does-not-exist"); | ||
| AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL); | ||
| try { | ||
| fileFetcherHandler.download(request, responder); | ||
| } catch (UnauthorizedException e) { | ||
| exceptionThrown = e; | ||
| } catch (NotFoundException e) { | ||
| // Expected: a caller with instance-level access passes the authorization check | ||
| // and proceeds to the real location lookup, which correctly reports the | ||
| // requested (nonexistent) path as not found. This is not an authorization | ||
| // failure. | ||
| } | ||
| Assert.assertNull("a caller with instance-level access must not be rejected by " | ||
| + "the authorization check", exceptionThrown); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.