test(fileserver): 내용 해시 선조건 거절과 읽기 정책 배선을 시험한다 #81
+103
-6
@@ -54,6 +54,15 @@ import org.junit.jupiter.api.Test;
|
||||
class FileLifecycleUseCasesTest {
|
||||
|
||||
private static final String DIGEST = "f".repeat(64);
|
||||
|
||||
/**
|
||||
* A well-formed SHA-256 of some other content: what a client holds after the file changed.
|
||||
*
|
||||
* <p>Shaped like a real digest so a refusal can only come from the comparison, never from the
|
||||
* value failing to parse.
|
||||
*/
|
||||
private static final String STALE_DIGEST = "0".repeat(64);
|
||||
|
||||
private static final long SIZE = 10;
|
||||
private static final long MAX_FILE = 1024;
|
||||
private static final StorageNamespace TARGET = StorageNamespace.of("tenant-b");
|
||||
@@ -187,18 +196,26 @@ class FileLifecycleUseCasesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("내용 해시가 맞지 않는 삭제는 PRECONDITION_FAILED로 거절되고 레코드는 READY로 남는다")
|
||||
void aStaleValidatorPreconditionIsRejectedBeforeAnyTransition() {
|
||||
// given
|
||||
FileRecord ready = ReadyFileFixture.ready(metadata, SIZE, DIGEST);
|
||||
|
||||
assertThatThrownBy(
|
||||
// when
|
||||
Throwable failure =
|
||||
catchThrowable(
|
||||
() ->
|
||||
delete.handle(
|
||||
new DeleteFileCommand(
|
||||
ready.fileId(), Optional.of("\"stale\""), FileserverFixtures.context())))
|
||||
.isInstanceOf(FileserverException.class);
|
||||
ready.fileId(), Optional.of(STALE_DIGEST), FileserverFixtures.context())));
|
||||
|
||||
assertThat(metadata.find(ready.fileId()).orElseThrow().state()).isEqualTo(FileState.READY);
|
||||
assertThat(cleanup.queued()).isEmpty();
|
||||
// then
|
||||
assertThat(
|
||||
List.of(
|
||||
codeOf(failure),
|
||||
metadata.find(ready.fileId()).orElseThrow().state(),
|
||||
cleanup.queued()))
|
||||
.containsExactly(FileserverErrorCode.PRECONDITION_FAILED, FileState.READY, List.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -274,6 +291,46 @@ class FileLifecycleUseCasesTest {
|
||||
assertThat(copyGateway.copies()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("내용 해시가 맞지 않는 복사는 PRECONDITION_FAILED로 거절되고 레코드도 바이트도 쿼터도 만들지 않는다")
|
||||
void copyWithAStaleDigestIsRefusedBeforeAnythingIsWritten() {
|
||||
// given
|
||||
FileRecord source = ReadyFileFixture.ready(metadata, SIZE, DIGEST);
|
||||
copyGateway.copiesContentOf(SIZE, DIGEST);
|
||||
|
||||
// when
|
||||
Throwable failure =
|
||||
catchThrowable(() -> copy.handle(copyOf(source, Optional.of(STALE_DIGEST))));
|
||||
|
||||
// then
|
||||
assertThat(
|
||||
List.of(
|
||||
codeOf(failure),
|
||||
copyGateway.copies(),
|
||||
metadata.find(nextCopyId()).isPresent(),
|
||||
quota.reservationCount()))
|
||||
.containsExactly(FileserverErrorCode.PRECONDITION_FAILED, List.of(), false, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("내용 해시가 맞는 복사는 진행되어 대상 namespace에 READY 파일을 만들고 그 크기를 쿼터에 확정한다")
|
||||
void copyWithTheCurrentDigestProceeds() {
|
||||
// given
|
||||
FileRecord source = ReadyFileFixture.ready(metadata, SIZE, DIGEST);
|
||||
copyGateway.copiesContentOf(SIZE, DIGEST);
|
||||
|
||||
// when
|
||||
FileView copied = copy.handle(copyOf(source, Optional.of(DIGEST)));
|
||||
|
||||
// then
|
||||
assertThat(
|
||||
List.of(
|
||||
copied.state(),
|
||||
copied.descriptor().namespace(),
|
||||
quota.usedBytes(QuotaScope.ofNamespace(TARGET.value()))))
|
||||
.containsExactly(FileState.READY, TARGET, SIZE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("저장소가 hard high-water를 넘으면 복사는 레코드도 바이트도 만들지 않고 STORAGE_FULL로 거절된다")
|
||||
void copyAtTheHardHighWaterMarkIsRefusedBeforeAnyRecordOrByteIsWritten() {
|
||||
@@ -462,6 +519,42 @@ class FileLifecycleUseCasesTest {
|
||||
.containsExactly(FileserverErrorCode.ACCESS_DENIED, ready.namespace());
|
||||
}
|
||||
|
||||
/**
|
||||
* A move under a digest the file no longer has is refused and changes nothing.
|
||||
*
|
||||
* <p>The matching side — a current digest lets the move through — is {@link
|
||||
* #moveChangesOnlyTheNamespaceAndNeverTheContentKey}.
|
||||
*/
|
||||
@Test
|
||||
@DisplayName("내용 해시가 맞지 않는 이동은 PRECONDITION_FAILED로 거절되고 파일과 사용량은 그대로다")
|
||||
void moveWithAStaleDigestIsRefusedAndTheFileStays() {
|
||||
// given
|
||||
FileRecord ready = ReadyFileFixture.ready(metadata, SIZE, DIGEST);
|
||||
QuotaScope source = QuotaScope.ofNamespace(ready.namespace().value());
|
||||
QuotaScope target = QuotaScope.ofNamespace(TARGET.value());
|
||||
quota.alreadyHolds(source, SIZE);
|
||||
|
||||
// when
|
||||
Throwable failure =
|
||||
catchThrowable(
|
||||
() ->
|
||||
move.handle(
|
||||
new MoveFileCommand(
|
||||
ready.fileId(),
|
||||
TARGET,
|
||||
Optional.of(STALE_DIGEST),
|
||||
FileserverFixtures.context())));
|
||||
|
||||
// then
|
||||
assertThat(
|
||||
List.of(
|
||||
codeOf(failure),
|
||||
metadata.find(ready.fileId()).orElseThrow().namespace(),
|
||||
quota.usedBytes(source),
|
||||
quota.usedBytes(target)))
|
||||
.containsExactly(FileserverErrorCode.PRECONDITION_FAILED, ready.namespace(), SIZE, 0L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("이동은 파일의 바이트를 출발 namespace 사용량에서 대상 namespace 사용량으로 옮긴다")
|
||||
void moveTransfersTheFilesBytesFromTheSourceNamespaceToTheTarget() {
|
||||
@@ -520,8 +613,12 @@ class FileLifecycleUseCasesTest {
|
||||
}
|
||||
|
||||
private static CopyFileCommand copyOf(FileRecord source) {
|
||||
return copyOf(source, Optional.empty());
|
||||
}
|
||||
|
||||
private static CopyFileCommand copyOf(FileRecord source, Optional<String> expectedSha256) {
|
||||
return new CopyFileCommand(
|
||||
source.fileId(), TARGET, Optional.empty(), Optional.empty(), FileserverFixtures.context());
|
||||
source.fileId(), TARGET, Optional.empty(), expectedSha256, FileserverFixtures.context());
|
||||
}
|
||||
|
||||
/** The id the copy would give its target: the identifier factory's first file id. */
|
||||
|
||||
+68
-6
@@ -36,6 +36,7 @@ import org.flywaydb.core.Flyway;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
@@ -58,6 +59,7 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.postgresql.PostgreSQLContainer;
|
||||
import tools.jackson.core.JacksonException;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
@@ -124,7 +126,11 @@ import tools.jackson.databind.ObjectMapper;
|
||||
// The fileserver's tiers grant their permissions, as security.yml ships them. This
|
||||
// environment loads no configuration file, so the mapping is restated rather than read.
|
||||
"app.authz.role-permissions[ROLE_FILE_READ]=file:read",
|
||||
"app.authz.role-permissions[ROLE_FILE_WRITE]=file:upload,file:delete,file:copy," + "file:move"
|
||||
"app.authz.role-permissions[ROLE_FILE_WRITE]=file:upload,file:delete,file:copy,"
|
||||
+ "file:move",
|
||||
// A tier outside the fileserver's read roles that another mapping grants file:read: the
|
||||
// permission gate lets it through, so a refusal can only come from the fileserver's policy.
|
||||
"app.authz.role-permissions[ROLE_READ_ELSEWHERE]=file:read"
|
||||
})
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
class FileserverCompositionIntegrationTest {
|
||||
@@ -253,19 +259,52 @@ class FileserverCompositionIntegrationTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* A caller without the read role is refused.
|
||||
* A caller no mapping grants {@code file:read} is refused by the permission gate.
|
||||
*
|
||||
* <p>The authorization decision belongs to the injected policy, and this proves the wiring
|
||||
* actually reaches it: the same request that succeeds above fails here on the roles alone.
|
||||
* <p>The download use case's {@code @RequiresPermission("file:read")} answers before the
|
||||
* fileserver's policy is consulted, so this pins the gate's code, not the policy's. That the
|
||||
* wiring reaches the injected policy is {@link
|
||||
* #downloadByAWriterWhoseReadPermissionComesFromAnotherTierIsRefusedByTheFileserverPolicy}.
|
||||
*/
|
||||
@Test
|
||||
void downloadWithoutTheReadRoleIsRefused() throws Exception {
|
||||
@DisplayName("file:read 권한이 없는 호출자의 다운로드는 권한 gate가 AUTHZ_INSUFFICIENT_PERMISSION으로 거절한다")
|
||||
void downloadWithoutTheReadPermissionIsRefusedByThePermissionGate() throws Exception {
|
||||
// given
|
||||
String fileId = knownFileId();
|
||||
actAs(Set.of("ROLE_UNRELATED"));
|
||||
|
||||
// when
|
||||
MvcResult refused = mockMvc.perform(get("/v1/files/{fileId}/content", fileId)).andReturn();
|
||||
|
||||
assertThat(refused.getResponse().getStatus()).isEqualTo(403);
|
||||
// then
|
||||
assertThat(List.<Object>of(refused.getResponse().getStatus(), errorCodeOf(refused)))
|
||||
.containsExactly(403, "AUTHZ_INSUFFICIENT_PERMISSION");
|
||||
}
|
||||
|
||||
/**
|
||||
* A caller the permission gate admits is still refused by the fileserver's own read tier.
|
||||
*
|
||||
* <p>{@code ROLE_READ_ELSEWHERE} carries {@code file:read}, so the gate lets the request through,
|
||||
* and the caller owns the file. What it lacks is a role from {@code security.read-roles}: the
|
||||
* refusal, and its {@code ACCESS_DENIED} code, can only come from the policy the composition
|
||||
* injected. The caller also holds a write tier on purpose — wiring that handed the policy the
|
||||
* write roles as its read roles would let this caller download, and this test would fail.
|
||||
*/
|
||||
@Test
|
||||
@DisplayName(
|
||||
"file:read 권한은 있지만 fileserver 읽기 역할이 없는 작성자의 다운로드는 fileserver 정책이 ACCESS_DENIED로 거절한다")
|
||||
void downloadByAWriterWhoseReadPermissionComesFromAnotherTierIsRefusedByTheFileserverPolicy()
|
||||
throws Exception {
|
||||
// given
|
||||
String fileId = knownFileId();
|
||||
actAs(Set.of("ROLE_FILE_WRITE", "ROLE_READ_ELSEWHERE"));
|
||||
|
||||
// when
|
||||
MvcResult refused = mockMvc.perform(get("/v1/files/{fileId}/content", fileId)).andReturn();
|
||||
|
||||
// then
|
||||
assertThat(List.<Object>of(refused.getResponse().getStatus(), errorCodeOf(refused)))
|
||||
.containsExactly(403, "ACCESS_DENIED");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -311,6 +350,29 @@ class FileserverCompositionIntegrationTest {
|
||||
List.of()));
|
||||
}
|
||||
|
||||
/**
|
||||
* The stable code the shared error envelope carries, or the raw body when it is not one.
|
||||
*
|
||||
* <p>A request that should have been refused may succeed, and then the body is the file's bytes.
|
||||
* Returning them, rather than failing to parse them, lets the assertion print the status and body
|
||||
* that came back instead.
|
||||
*/
|
||||
private String errorCodeOf(MvcResult result) throws Exception {
|
||||
try {
|
||||
JsonNode code =
|
||||
objectMapper
|
||||
.readTree(result.getResponse().getContentAsByteArray())
|
||||
.path("error")
|
||||
.path("code");
|
||||
if (!code.isMissingNode()) {
|
||||
return code.stringValue();
|
||||
}
|
||||
} catch (JacksonException ignored) {
|
||||
// Not JSON at all: the raw body below says what came back.
|
||||
}
|
||||
return result.getResponse().getContentAsString();
|
||||
}
|
||||
|
||||
/** Uploads one file over the raw streaming endpoint and returns the parsed answer. */
|
||||
private JsonNode upload(byte[] content, String filename) throws Exception {
|
||||
MvcResult result =
|
||||
|
||||
Reference in New Issue
Block a user