refactor: 백엔드 리펙토링 진행 중
This commit is contained in:
+5
-1
@@ -206,8 +206,12 @@ public final class DefaultFileserverAdminService implements FileserverAdminServi
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Management-plane reads are an admin operation, not a metadata read: a data-plane reader must
|
||||
* not learn storage health or list other callers' incomplete uploads.
|
||||
*/
|
||||
private void authorizeRead(RequestContext context) {
|
||||
accessPolicy.authorize(FileOperation.READ_METADATA, context.subject(), Optional.empty());
|
||||
accessPolicy.authorize(FileOperation.ADMIN_READ, context.subject(), Optional.empty());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+3
@@ -5,6 +5,7 @@ import dev.caskeleton.application.fileserver.api.FileState;
|
||||
import dev.caskeleton.application.fileserver.api.StorageNamespace;
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Public metadata view of a file, without transport-specific validators. */
|
||||
public record FileDescriptor(
|
||||
@@ -16,6 +17,7 @@ public record FileDescriptor(
|
||||
long size,
|
||||
String sha256,
|
||||
Instant publishedAt,
|
||||
Optional<FileOwner> owner,
|
||||
long version) {
|
||||
|
||||
public FileDescriptor {
|
||||
@@ -23,5 +25,6 @@ public record FileDescriptor(
|
||||
Objects.requireNonNull(namespace, "namespace");
|
||||
Objects.requireNonNull(state, "state");
|
||||
Objects.requireNonNull(originalFilename, "originalFilename");
|
||||
Objects.requireNonNull(owner, "owner");
|
||||
}
|
||||
}
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package dev.caskeleton.application.fileserver.api.metadata;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Who a file belongs to: the principal that uploaded it, in the tenant it was uploaded in.
|
||||
*
|
||||
* <p>Recorded once, when the record is created, from the caller the transport authenticated. The
|
||||
* access policy compares it with the caller of every later operation on the file, so two callers
|
||||
* holding the same role tier no longer see each other's files.
|
||||
*
|
||||
* @param principalId the uploading principal
|
||||
* @param tenantId the uploading caller's tenant, empty when the request was not tenant scoped
|
||||
*/
|
||||
public record FileOwner(String principalId, Optional<String> tenantId) {
|
||||
|
||||
public FileOwner {
|
||||
Objects.requireNonNull(principalId, "principalId");
|
||||
Objects.requireNonNull(tenantId, "tenantId");
|
||||
if (principalId.isBlank()) {
|
||||
throw new IllegalArgumentException("an owner needs a principal");
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
@@ -14,6 +14,9 @@ import java.util.OptionalLong;
|
||||
*
|
||||
* <p>The relational record — not the filesystem — decides whether a file is publicly readable.
|
||||
* {@code version} backs every optimistic transition.
|
||||
*
|
||||
* <p>{@code owner} is empty only for a row created before ownership was recorded; the default
|
||||
* access policy grants such a row to nobody on the data plane.
|
||||
*/
|
||||
public record FileRecord(
|
||||
FileId fileId,
|
||||
@@ -28,6 +31,7 @@ public record FileRecord(
|
||||
Optional<String> sha256,
|
||||
Optional<Instant> publishedAt,
|
||||
Optional<String> lastErrorCode,
|
||||
Optional<FileOwner> owner,
|
||||
long version,
|
||||
Instant createdAt,
|
||||
Instant updatedAt) {
|
||||
@@ -45,6 +49,7 @@ public record FileRecord(
|
||||
Objects.requireNonNull(sha256, "sha256");
|
||||
Objects.requireNonNull(publishedAt, "publishedAt");
|
||||
Objects.requireNonNull(lastErrorCode, "lastErrorCode");
|
||||
Objects.requireNonNull(owner, "owner");
|
||||
Objects.requireNonNull(createdAt, "createdAt");
|
||||
Objects.requireNonNull(updatedAt, "updatedAt");
|
||||
if (version < 0) {
|
||||
@@ -68,6 +73,7 @@ public record FileRecord(
|
||||
actualSize.orElse(0L),
|
||||
sha256.orElse(""),
|
||||
publishedAt.orElse(null),
|
||||
owner,
|
||||
version);
|
||||
}
|
||||
}
|
||||
|
||||
+5
-2
@@ -10,14 +10,16 @@ import java.util.OptionalLong;
|
||||
* Insert payload for a new file record in {@code CREATED}.
|
||||
*
|
||||
* <p>{@code originalName} is already sanitized display text and {@code claimedMediaType} is the
|
||||
* untrusted client assertion; neither is used to build a physical key.
|
||||
* untrusted client assertion; neither is used to build a physical key. {@code owner} is the caller
|
||||
* that asked for the file, and it never changes afterwards.
|
||||
*/
|
||||
public record FileRecordDraft(
|
||||
FileId fileId,
|
||||
StorageNamespace namespace,
|
||||
String originalName,
|
||||
Optional<String> claimedMediaType,
|
||||
OptionalLong expectedSize) {
|
||||
OptionalLong expectedSize,
|
||||
FileOwner owner) {
|
||||
|
||||
public FileRecordDraft {
|
||||
Objects.requireNonNull(fileId, "fileId");
|
||||
@@ -25,5 +27,6 @@ public record FileRecordDraft(
|
||||
Objects.requireNonNull(originalName, "originalName");
|
||||
Objects.requireNonNull(claimedMediaType, "claimedMediaType");
|
||||
Objects.requireNonNull(expectedSize, "expectedSize");
|
||||
Objects.requireNonNull(owner, "owner");
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -62,5 +62,11 @@ public interface UploadSessionStore {
|
||||
*/
|
||||
boolean claimForCleanup(UploadId uploadId, Instant now);
|
||||
|
||||
/**
|
||||
* Sessions whose expiry is before {@code cutoff} and that are still open, oldest first.
|
||||
*
|
||||
* <p>Open only: a terminal session has already been ended and its cleanup queued, and listing it
|
||||
* again would let finished uploads crowd out the ones still waiting to be expired.
|
||||
*/
|
||||
List<UploadSession> findExpired(Instant cutoff, int limit);
|
||||
}
|
||||
|
||||
+18
-2
@@ -1,18 +1,24 @@
|
||||
package dev.caskeleton.application.fileserver.api.security;
|
||||
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileOwner;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Framework-free caller identity handed to the access policy.
|
||||
*
|
||||
* <p>The Fileserver never interprets these values: it only passes them to the injected policy. No
|
||||
* Spring Security type appears here.
|
||||
* <p>The Fileserver interprets only two of these values: the principal and the {@link
|
||||
* #TENANT_ATTRIBUTE tenant} are stamped on a new file as its owner. Everything else is passed to
|
||||
* the injected policy untouched. No Spring Security type appears here.
|
||||
*/
|
||||
public record FileAccessSubject(
|
||||
String principalId, Set<String> roles, Map<String, String> attributes) {
|
||||
|
||||
/** Attribute a transport sets when it resolved the caller's tenant. */
|
||||
public static final String TENANT_ATTRIBUTE = "tenant";
|
||||
|
||||
public FileAccessSubject {
|
||||
Objects.requireNonNull(principalId, "principalId");
|
||||
Objects.requireNonNull(roles, "roles");
|
||||
@@ -29,4 +35,14 @@ public record FileAccessSubject(
|
||||
public static FileAccessSubject of(String principalId, Set<String> roles) {
|
||||
return new FileAccessSubject(principalId, roles, Map.of());
|
||||
}
|
||||
|
||||
/** The caller's tenant, empty when the request was not tenant scoped. */
|
||||
public Optional<String> tenant() {
|
||||
return Optional.ofNullable(attributes.get(TENANT_ATTRIBUTE));
|
||||
}
|
||||
|
||||
/** This caller as the owner of a file it creates. */
|
||||
public FileOwner asOwner() {
|
||||
return new FileOwner(principalId, tenant());
|
||||
}
|
||||
}
|
||||
|
||||
+4
@@ -14,6 +14,10 @@ public enum FileOperation {
|
||||
DELETE,
|
||||
COPY,
|
||||
MOVE,
|
||||
/**
|
||||
* Management-plane reads: storage health, capabilities, orphan and incomplete-upload listings.
|
||||
*/
|
||||
ADMIN_READ,
|
||||
ADMIN_REVERIFY,
|
||||
ADMIN_FORCE_DELETE
|
||||
}
|
||||
|
||||
+55
@@ -10,6 +10,7 @@ import dev.caskeleton.application.fileserver.api.metadata.FileMetadataStore;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileRecord;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileRecordMutation;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.QuotaScope;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.UploadSession;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.UploadSessionStore;
|
||||
import dev.caskeleton.application.fileserver.observability.FileserverMetricsPort;
|
||||
import dev.caskeleton.application.transaction.TransactionPort;
|
||||
@@ -67,6 +68,9 @@ public final class DefaultCleanupService implements CleanupService {
|
||||
throw new IllegalArgumentException("batch bounds must be positive");
|
||||
}
|
||||
Instant now = clock.instant();
|
||||
// Expired uploads are turned into work before the queue is read, so their staging items are
|
||||
// claimed in this same pass.
|
||||
expireUploads(now, maxItems);
|
||||
// Abandoned claims come back first. A claim takes an item out of the due set, so an item whose
|
||||
// worker performed the physical delete and then died is invisible to claimDue — nothing ever
|
||||
// reclaimed it, and the file's quota and lifecycle stayed unsettled for good. Reclaiming before
|
||||
@@ -108,6 +112,50 @@ public final class DefaultCleanupService implements CleanupService {
|
||||
deleted, skippedActiveLease, skippedStateChanged, failed, reclaimed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends every upload whose expiry has passed and schedules its staged bytes for removal.
|
||||
*
|
||||
* <p>Nothing else ever did: an abandoned upload refused further appends once it expired, but its
|
||||
* record stayed UPLOADING and its staged bytes stayed on the volume for good. The session is made
|
||||
* terminal first, in the same transaction as the rest, so no writer can take a lease on bytes the
|
||||
* cleanup is about to delete. Only a record the upload still owns — UPLOADING, or FAILED before
|
||||
* it was ever published — becomes EXPIRED; any other state belongs to finalize, verification or
|
||||
* delete, and only its session is closed.
|
||||
*/
|
||||
private void expireUploads(Instant now, int limit) {
|
||||
for (UploadSession session : sessionStore.findExpired(now, limit)) {
|
||||
try {
|
||||
transactions.inWrite(() -> expire(session));
|
||||
} catch (RuntimeException raced) {
|
||||
// A concurrent finalize, cancel or another node's sweep changed the record first; the
|
||||
// session stays open and the next batch decides again from the new state.
|
||||
metrics.recordCleanup("expire", "conflict");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void expire(UploadSession session) {
|
||||
if (!sessionStore.terminate(session.uploadId())) {
|
||||
return;
|
||||
}
|
||||
metadataStore
|
||||
.find(session.fileId())
|
||||
.filter(
|
||||
record -> record.state() == FileState.UPLOADING || record.state() == FileState.FAILED)
|
||||
.ifPresent(
|
||||
record -> {
|
||||
metadataStore.transition(
|
||||
record.fileId(),
|
||||
record.version(),
|
||||
record.state(),
|
||||
FileState.EXPIRED,
|
||||
FileRecordMutation.failure("UPLOAD_EXPIRED"));
|
||||
queue.enqueue(
|
||||
CleanupRequest.forStaging(
|
||||
CleanupType.EXPIRED_UPLOAD, record.fileId(), session.uploadId()));
|
||||
});
|
||||
}
|
||||
|
||||
private Outcome process(CleanupItem item, Instant now) {
|
||||
try {
|
||||
if (item.request().uploadId().isPresent()) {
|
||||
@@ -132,6 +180,13 @@ public final class DefaultCleanupService implements CleanupService {
|
||||
* upload that is mid-flight, so the item is deferred rather than executed.
|
||||
*/
|
||||
private Outcome cleanStaging(CleanupItem item, UploadId uploadId, Instant now) {
|
||||
// A quarantined file's staged bytes are the evidence a later decision needs, whatever the queue
|
||||
// item says about why it was queued.
|
||||
Optional<FileRecord> record = item.request().fileId().flatMap(metadataStore::find);
|
||||
if (record.filter(found -> found.state() == FileState.QUARANTINED).isPresent()) {
|
||||
markDone(item);
|
||||
return Outcome.of(OutcomeKind.SKIPPED_STATE_CHANGED, 0);
|
||||
}
|
||||
// Claimed in the database rather than decided from a lease read a moment ago. Between the read
|
||||
// and the delete a writer could acquire that very lease, and the bytes removed were then the
|
||||
// ones an upload was actively appending to. The claim clears the lease in the same statement
|
||||
|
||||
+3
-1
@@ -124,7 +124,9 @@ public final class DefaultFileLifecycleService implements FileLifecycleService {
|
||||
command.targetNamespace(),
|
||||
filename.value(),
|
||||
source.verifiedMediaType().or(source::claimedMediaType),
|
||||
source.actualSize())));
|
||||
source.actualSize(),
|
||||
// The copy belongs to whoever made it, not to the source's owner.
|
||||
context.subject().asOwner())));
|
||||
|
||||
StoredContent copied;
|
||||
try {
|
||||
|
||||
+18
-6
@@ -18,6 +18,8 @@ import dev.caskeleton.application.fileserver.api.metadata.FileRecord;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileRecordMutation;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.UploadSession;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.UploadSessionStore;
|
||||
import dev.caskeleton.application.fileserver.api.security.FileAccessPolicy;
|
||||
import dev.caskeleton.application.fileserver.api.security.FileOperation;
|
||||
import dev.caskeleton.application.fileserver.api.security.RequestContext;
|
||||
import dev.caskeleton.application.fileserver.api.security.SanitizedFilename;
|
||||
import dev.caskeleton.application.fileserver.api.security.VerificationRequest;
|
||||
@@ -52,6 +54,7 @@ public final class DefaultFinalizeUploadService implements FinalizeUploadService
|
||||
private final QuotaCommitGateway quotaGateway;
|
||||
private final CleanupQueue cleanupQueue;
|
||||
private final RecoveryQueue recoveryQueue;
|
||||
private final FileAccessPolicy accessPolicy;
|
||||
private final PublishMode publishMode;
|
||||
private final TransactionPort transactions;
|
||||
private final Clock clock;
|
||||
@@ -64,6 +67,7 @@ public final class DefaultFinalizeUploadService implements FinalizeUploadService
|
||||
QuotaCommitGateway quotaGateway,
|
||||
CleanupQueue cleanupQueue,
|
||||
RecoveryQueue recoveryQueue,
|
||||
FileAccessPolicy accessPolicy,
|
||||
PublishMode publishMode,
|
||||
TransactionPort transactions,
|
||||
Clock clock) {
|
||||
@@ -74,6 +78,7 @@ public final class DefaultFinalizeUploadService implements FinalizeUploadService
|
||||
this.quotaGateway = quotaGateway;
|
||||
this.cleanupQueue = cleanupQueue;
|
||||
this.recoveryQueue = recoveryQueue;
|
||||
this.accessPolicy = accessPolicy;
|
||||
this.publishMode = publishMode;
|
||||
this.transactions = transactions;
|
||||
this.clock = clock;
|
||||
@@ -98,6 +103,9 @@ public final class DefaultFinalizeUploadService implements FinalizeUploadService
|
||||
public FileView finalizeUpload(
|
||||
UploadSession session, FinalizeUploadRequest request, RequestContext context) {
|
||||
FileRecord record = requireRecord(session);
|
||||
// Before any digest, verification or publish: a denial must have no side effect.
|
||||
accessPolicy.authorize(
|
||||
FileOperation.FINALIZE, context.subject(), Optional.of(record.toDescriptor()));
|
||||
requireDeclaredLengthReached(session);
|
||||
|
||||
UploadHandle handle = contentGateway.reattach(session);
|
||||
@@ -243,13 +251,17 @@ public final class DefaultFinalizeUploadService implements FinalizeUploadService
|
||||
FileState.VERIFYING,
|
||||
target,
|
||||
FileRecordMutation.failure(verdict.code()));
|
||||
// Terminal alongside the queued cleanup: a rejected upload is finished being written
|
||||
// to, and leaving it ACTIVE let a writer acquire a lease on bytes already scheduled
|
||||
// for deletion.
|
||||
// Terminal either way: a rejected or quarantined upload is finished being written
|
||||
// to, and leaving it ACTIVE let a writer acquire a lease on bytes a verdict had
|
||||
// already judged.
|
||||
sessionStore.terminate(session.uploadId());
|
||||
cleanupQueue.enqueue(
|
||||
CleanupRequest.forStaging(
|
||||
CleanupType.FAILED_VERIFICATION_CONTENT, moved.fileId(), session.uploadId()));
|
||||
if (target == FileState.REJECTED) {
|
||||
cleanupQueue.enqueue(
|
||||
CleanupRequest.forStaging(
|
||||
CleanupType.FAILED_VERIFICATION_CONTENT,
|
||||
moved.fileId(),
|
||||
session.uploadId()));
|
||||
}
|
||||
return moved;
|
||||
});
|
||||
if (verdict.verdict() == VerificationVerdict.REJECT
|
||||
|
||||
+2
-1
@@ -133,7 +133,8 @@ public final class DefaultUploadApplicationService implements UploadApplicationS
|
||||
request.namespace(),
|
||||
displayName.value(),
|
||||
request.claimedMediaType(),
|
||||
request.expectedLength()));
|
||||
request.expectedLength(),
|
||||
context.subject().asOwner()));
|
||||
UploadSession session =
|
||||
sessionStore.create(
|
||||
new UploadSessionDraft(
|
||||
|
||||
+16
@@ -2,6 +2,7 @@ package dev.caskeleton.application.fileserver.admin;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
|
||||
import dev.caskeleton.application.fileserver.api.ContentKey;
|
||||
import dev.caskeleton.application.fileserver.api.FileId;
|
||||
@@ -84,6 +85,21 @@ class FileserverAdminServiceTest {
|
||||
assertThat(health.toString()).doesNotContain("/var").doesNotContain("mount");
|
||||
}
|
||||
|
||||
/** A data-plane reader must not reach the management plane by asking for metadata. */
|
||||
@Test
|
||||
void aCallerDeniedAdminReadsCannotSeeStorageHealth() {
|
||||
// given
|
||||
accessPolicy.deny(FileOperation.ADMIN_READ);
|
||||
|
||||
// when
|
||||
Throwable failure = catchThrowable(() -> admin.storageHealth(FileserverFixtures.context()));
|
||||
|
||||
// then
|
||||
assertThat(failure)
|
||||
.isInstanceOf(FileAccessDeniedException.class)
|
||||
.hasMessage("operation denied by policy");
|
||||
}
|
||||
|
||||
@Test
|
||||
void orphanReconcileDefaultsToDryRunAndDeletesNothing() {
|
||||
adminPorts.addOrphan(new OrphanObject(ORPHAN_KEY, 100, FileserverFixtures.NOW, "fp-1"));
|
||||
|
||||
+2
@@ -103,6 +103,7 @@ class MetadataPortContractTest {
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
0,
|
||||
NOW,
|
||||
NOW);
|
||||
@@ -179,6 +180,7 @@ class MetadataPortContractTest {
|
||||
Optional.of("a".repeat(64)),
|
||||
Optional.of(NOW),
|
||||
Optional.empty(),
|
||||
Optional.of(new FileOwner("user-1", Optional.empty())),
|
||||
3,
|
||||
NOW,
|
||||
NOW);
|
||||
|
||||
+73
@@ -9,6 +9,7 @@ import dev.caskeleton.application.fileserver.api.FileState;
|
||||
import dev.caskeleton.application.fileserver.api.UploadId;
|
||||
import dev.caskeleton.application.fileserver.api.error.ConcurrentFileModificationException;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileRecord;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileRecordMutation;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.UploadSession;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.UploadSessionDraft;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.WriterLease;
|
||||
@@ -295,6 +296,78 @@ class CleanupServiceTest {
|
||||
assertThat(cleanup.runBatch(10, 1024).processed()).isZero();
|
||||
}
|
||||
|
||||
/** The queue item is a statement about the past; a quarantined file's bytes must survive it. */
|
||||
@Test
|
||||
void aStagingItemForAQuarantinedFileLeavesTheStagedBytesInPlace() {
|
||||
// given
|
||||
FileRecord verifying = ReadyFileFixture.verifying(metadata, SIZE, DIGEST);
|
||||
metadata.transition(
|
||||
verifying.fileId(),
|
||||
verifying.version(),
|
||||
FileState.VERIFYING,
|
||||
FileState.QUARANTINED,
|
||||
FileRecordMutation.failure("SCRIPTABLE_CONTENT"));
|
||||
UploadId uploadId = UploadId.of(UUID.randomUUID());
|
||||
sessions.create(
|
||||
new UploadSessionDraft(
|
||||
uploadId,
|
||||
verifying.fileId(),
|
||||
UploadMode.SINGLE_SHOT,
|
||||
OptionalLong.of(SIZE),
|
||||
FileserverFixtures.NOW.plusSeconds(3600)));
|
||||
sessions.terminate(uploadId);
|
||||
content.storeStaging(uploadId);
|
||||
queue.enqueue(
|
||||
CleanupRequest.forStaging(
|
||||
CleanupType.FAILED_VERIFICATION_CONTENT, verifying.fileId(), uploadId));
|
||||
|
||||
// when
|
||||
cleanup.runBatch(100, 1L << 30);
|
||||
|
||||
// then
|
||||
assertThat(content.stagingExists(uploadId)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUploadPastItsExpiryIsMovedToExpired() {
|
||||
// given
|
||||
FileRecord uploading = ReadyFileFixture.uploading(metadata, SIZE, DIGEST);
|
||||
expiredSession(uploading.fileId());
|
||||
|
||||
// when
|
||||
cleanup.runBatch(100, 1L << 30);
|
||||
|
||||
// then
|
||||
assertThat(metadata.find(uploading.fileId()).orElseThrow().state())
|
||||
.isEqualTo(FileState.EXPIRED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theStagedBytesOfAnExpiredUploadAreReclaimed() {
|
||||
// given
|
||||
FileRecord uploading = ReadyFileFixture.uploading(metadata, SIZE, DIGEST);
|
||||
UploadId uploadId = expiredSession(uploading.fileId());
|
||||
content.storeStaging(uploadId);
|
||||
|
||||
// when
|
||||
cleanup.runBatch(100, 1L << 30);
|
||||
|
||||
// then
|
||||
assertThat(content.stagingExists(uploadId)).isFalse();
|
||||
}
|
||||
|
||||
private UploadId expiredSession(FileId fileId) {
|
||||
UploadId uploadId = UploadId.of(UUID.randomUUID());
|
||||
sessions.create(
|
||||
new UploadSessionDraft(
|
||||
uploadId,
|
||||
fileId,
|
||||
UploadMode.RESUMABLE,
|
||||
OptionalLong.of(SIZE),
|
||||
FileserverFixtures.NOW.minusSeconds(1)));
|
||||
return uploadId;
|
||||
}
|
||||
|
||||
private FileRecord deletingRecord() {
|
||||
FileRecord ready = ReadyFileFixture.ready(metadata, SIZE, DIGEST);
|
||||
return metadata.markDeleting(ready.fileId(), ready.version());
|
||||
|
||||
+2
-1
@@ -29,7 +29,8 @@ public final class FileserverFixtures {
|
||||
NAMESPACE,
|
||||
"report.bin",
|
||||
Optional.of("application/octet-stream"),
|
||||
OptionalLong.of(expectedSize));
|
||||
OptionalLong.of(expectedSize),
|
||||
context().subject().asOwner());
|
||||
}
|
||||
|
||||
public static UploadSession session(FileId fileId, long expectedLength, long committedOffset) {
|
||||
|
||||
+3
@@ -59,6 +59,7 @@ public final class InMemoryFileMetadataStore implements FileMetadataStore {
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.of(draft.owner()),
|
||||
0,
|
||||
now,
|
||||
now);
|
||||
@@ -128,6 +129,7 @@ public final class InMemoryFileMetadataStore implements FileMetadataStore {
|
||||
mutation.sha256().or(current::sha256),
|
||||
mutation.publishedAt().or(current::publishedAt),
|
||||
mutation.lastErrorCode().or(current::lastErrorCode),
|
||||
current.owner(),
|
||||
current.version() + 1,
|
||||
current.createdAt(),
|
||||
now);
|
||||
@@ -164,6 +166,7 @@ public final class InMemoryFileMetadataStore implements FileMetadataStore {
|
||||
current.sha256(),
|
||||
current.publishedAt(),
|
||||
current.lastErrorCode(),
|
||||
current.owner(),
|
||||
current.version() + 1,
|
||||
current.createdAt(),
|
||||
now);
|
||||
|
||||
+1
-1
@@ -181,7 +181,7 @@ public final class InMemoryUploadSessionStore implements UploadSessionStore {
|
||||
public List<UploadSession> findExpired(Instant cutoff, int limit) {
|
||||
List<UploadSession> expired = new ArrayList<>();
|
||||
for (UploadSession session : sessions.values()) {
|
||||
if (session.isExpiredAt(cutoff) && expired.size() < limit) {
|
||||
if (!session.terminal() && session.isExpiredAt(cutoff) && expired.size() < limit) {
|
||||
expired.add(session);
|
||||
}
|
||||
}
|
||||
|
||||
+42
@@ -2,19 +2,23 @@ package dev.caskeleton.application.fileserver.upload;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
|
||||
import dev.caskeleton.application.fileserver.api.FileId;
|
||||
import dev.caskeleton.application.fileserver.api.FileState;
|
||||
import dev.caskeleton.application.fileserver.api.content.PublishMode;
|
||||
import dev.caskeleton.application.fileserver.api.error.AmbiguousCompletionException;
|
||||
import dev.caskeleton.application.fileserver.api.error.FileAccessDeniedException;
|
||||
import dev.caskeleton.application.fileserver.api.error.IntegrityMismatchException;
|
||||
import dev.caskeleton.application.fileserver.api.error.MalwareDetectedException;
|
||||
import dev.caskeleton.application.fileserver.api.error.PartialWriteException;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileRecord;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileRecordMutation;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.UploadSession;
|
||||
import dev.caskeleton.application.fileserver.api.security.FileOperation;
|
||||
import dev.caskeleton.application.fileserver.api.security.VerificationResult;
|
||||
import dev.caskeleton.application.fileserver.testkit.DirectTransactions;
|
||||
import dev.caskeleton.application.fileserver.testkit.FakeFileAccessPolicy;
|
||||
import dev.caskeleton.application.fileserver.testkit.FakeQuotaCommitGateway;
|
||||
import dev.caskeleton.application.fileserver.testkit.FakeUploadContentGateway;
|
||||
import dev.caskeleton.application.fileserver.testkit.FileserverFixtures;
|
||||
@@ -37,6 +41,7 @@ class FinalizeUploadServiceTest {
|
||||
private final FakeQuotaCommitGateway quota = new FakeQuotaCommitGateway();
|
||||
private final InMemoryCleanupQueue cleanup = new InMemoryCleanupQueue();
|
||||
private final InMemoryRecoveryQueue recovery = new InMemoryRecoveryQueue();
|
||||
private final FakeFileAccessPolicy accessPolicy = new FakeFileAccessPolicy();
|
||||
|
||||
private final DirectTransactions transactions = new DirectTransactions();
|
||||
private VerificationResult verdict = VerificationResult.accept("ALL_CHECKS_PASSED");
|
||||
@@ -53,6 +58,7 @@ class FinalizeUploadServiceTest {
|
||||
quota,
|
||||
cleanup,
|
||||
recovery,
|
||||
accessPolicy,
|
||||
PublishMode.ATOMIC_MOVE_PREFERRED,
|
||||
transactions,
|
||||
Clock.fixed(FileserverFixtures.NOW, ZoneOffset.UTC));
|
||||
@@ -156,6 +162,42 @@ class FinalizeUploadServiceTest {
|
||||
assertThat(content.publishedCount()).isZero();
|
||||
}
|
||||
|
||||
/** A quarantined file may still be released by a later decision, so its bytes must survive. */
|
||||
@Test
|
||||
void aQuarantineVerdictSchedulesNoCleanupOfTheStagedBytes() {
|
||||
// given
|
||||
verdict = VerificationResult.quarantine("SCRIPTABLE_CONTENT");
|
||||
UploadSession session = uploadedSession(10);
|
||||
|
||||
// when
|
||||
service.finalizeUpload(
|
||||
session, FinalizeUploadRequest.synchronousWithoutDigest(), FileserverFixtures.context());
|
||||
|
||||
// then
|
||||
assertThat(cleanup.queued()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCallerTheFinalizeOperationIsDeniedToPublishesNothing() {
|
||||
// given
|
||||
accessPolicy.deny(FileOperation.FINALIZE);
|
||||
UploadSession session = uploadedSession(10);
|
||||
|
||||
// when
|
||||
Throwable failure =
|
||||
catchThrowable(
|
||||
() ->
|
||||
service.finalizeUpload(
|
||||
session,
|
||||
FinalizeUploadRequest.synchronousWithoutDigest(),
|
||||
FileserverFixtures.context()));
|
||||
|
||||
// then
|
||||
assertThat(failure)
|
||||
.isInstanceOf(FileAccessDeniedException.class)
|
||||
.hasMessage("operation denied by policy");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRetryVerdictLeavesTheFileVerifyingWithoutPublishing() {
|
||||
verdict = VerificationResult.retry("SCANNER_TIMEOUT");
|
||||
|
||||
+25
@@ -14,8 +14,11 @@ import dev.caskeleton.application.fileserver.api.error.IntegrityMismatchExceptio
|
||||
import dev.caskeleton.application.fileserver.api.error.StorageUnavailableException;
|
||||
import dev.caskeleton.application.fileserver.api.error.UploadExpiredException;
|
||||
import dev.caskeleton.application.fileserver.api.error.UploadOffsetMismatchException;
|
||||
import dev.caskeleton.application.fileserver.api.metadata.FileOwner;
|
||||
import dev.caskeleton.application.fileserver.api.security.FileAccessSubject;
|
||||
import dev.caskeleton.application.fileserver.api.security.FileOperation;
|
||||
import dev.caskeleton.application.fileserver.api.security.OriginalFilenamePolicy;
|
||||
import dev.caskeleton.application.fileserver.api.security.RequestContext;
|
||||
import dev.caskeleton.application.fileserver.api.transfer.UploadMode;
|
||||
import dev.caskeleton.application.fileserver.concurrency.DefaultWriterLeaseCoordinator;
|
||||
import dev.caskeleton.application.fileserver.observability.FileserverMetricsPort;
|
||||
@@ -40,8 +43,10 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Clock;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalLong;
|
||||
import java.util.Set;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class UploadApplicationServiceTest {
|
||||
@@ -70,6 +75,26 @@ class UploadApplicationServiceTest {
|
||||
assertThat(quota.reservationCount()).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCreatedFileIsOwnedByTheCallerInItsTenant() {
|
||||
// given
|
||||
RequestContext tenantCaller =
|
||||
new RequestContext(
|
||||
new FileAccessSubject(
|
||||
"user-9",
|
||||
Set.of("uploader"),
|
||||
Map.of(FileAccessSubject.TENANT_ATTRIBUTE, "tenant-a")),
|
||||
"trace-1",
|
||||
"node-a");
|
||||
|
||||
// when
|
||||
UploadSessionView created = service.create(createRequest(), tenantCaller);
|
||||
|
||||
// then
|
||||
assertThat(metadata.find(created.fileId()).orElseThrow().owner())
|
||||
.contains(new FileOwner("user-9", Optional.of("tenant-a")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAppendAndCancelMaintainStateAndOffset() {
|
||||
UploadSessionView created = service.create(createRequest(), FileserverFixtures.context());
|
||||
|
||||
Reference in New Issue
Block a user