-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support max bytes quota for requests
`s3proxy.sts.request.quota.byte-qty` Closes #95
- Loading branch information
Showing
7 changed files
with
286 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
170 changes: 170 additions & 0 deletions
170
trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/rest/LimitStreamController.java
This file contains 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,170 @@ | ||
/* | ||
* 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.trino.aws.proxy.server.rest; | ||
|
||
import com.google.common.io.CountingInputStream; | ||
import com.google.common.io.CountingOutputStream; | ||
import com.google.inject.Inject; | ||
import io.airlift.units.DataSize; | ||
import io.trino.aws.proxy.server.TrinoAwsProxyConfig; | ||
import jakarta.ws.rs.WebApplicationException; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.Optional; | ||
|
||
import static jakarta.ws.rs.core.Response.Status.REQUEST_ENTITY_TOO_LARGE; | ||
|
||
public class LimitStreamController | ||
{ | ||
private final Optional<DataSize> quota; | ||
|
||
@Inject | ||
public LimitStreamController(TrinoAwsProxyConfig trinoAwsProxyConfig) | ||
{ | ||
quota = trinoAwsProxyConfig.getMaxPayloadSize(); | ||
} | ||
|
||
public InputStream wrap(InputStream inputStream) | ||
{ | ||
return quota.map(q -> internalWrap(inputStream, q.toBytes())).orElse(inputStream); | ||
} | ||
|
||
private static InputStream internalWrap(InputStream inputStream, long quota) | ||
{ | ||
CountingInputStream delegate = new CountingInputStream(inputStream); | ||
return new InputStream() | ||
{ | ||
@Override | ||
public int read() | ||
throws IOException | ||
{ | ||
return validate(delegate.read()); | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) | ||
throws IOException | ||
{ | ||
return validate(delegate.read(b, off, len)); | ||
} | ||
|
||
@Override | ||
public long skip(long n) | ||
throws IOException | ||
{ | ||
return validate(delegate.skip(n)); | ||
} | ||
|
||
@Override | ||
public void mark(int readlimit) | ||
{ | ||
delegate.mark(readlimit); | ||
validate(); | ||
} | ||
|
||
@Override | ||
public void reset() | ||
throws IOException | ||
{ | ||
delegate.reset(); | ||
validate(); | ||
} | ||
|
||
@Override | ||
public boolean markSupported() | ||
{ | ||
return validate(delegate.markSupported()); | ||
} | ||
|
||
@Override | ||
public void close() | ||
throws IOException | ||
{ | ||
delegate.close(); | ||
} | ||
|
||
private void validate() | ||
{ | ||
validate(null); | ||
} | ||
|
||
private <T> T validate(T value) | ||
{ | ||
if (delegate.getCount() > quota) { | ||
throw new WebApplicationException(REQUEST_ENTITY_TOO_LARGE); | ||
} | ||
return value; | ||
} | ||
}; | ||
} | ||
|
||
public OutputStream wrap(OutputStream outputStream) | ||
{ | ||
return quota.map(q -> internalWrap(outputStream, q.toBytes())).orElse(outputStream); | ||
} | ||
|
||
private OutputStream internalWrap(OutputStream outputStream, long quota) | ||
{ | ||
CountingOutputStream delegate = new CountingOutputStream(outputStream); | ||
|
||
return new OutputStream() | ||
{ | ||
@Override | ||
public void write(byte[] b) | ||
throws IOException | ||
{ | ||
delegate.write(b); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) | ||
throws IOException | ||
{ | ||
delegate.write(b, off, len); | ||
validate(); | ||
} | ||
|
||
@Override | ||
public void flush() | ||
throws IOException | ||
{ | ||
delegate.flush(); | ||
} | ||
|
||
@Override | ||
public void close() | ||
throws IOException | ||
{ | ||
delegate.close(); | ||
} | ||
|
||
@Override | ||
public void write(int b) | ||
throws IOException | ||
{ | ||
delegate.write(b); | ||
validate(); | ||
} | ||
|
||
private void validate() | ||
{ | ||
if (delegate.getCount() > quota) { | ||
throw new WebApplicationException(REQUEST_ENTITY_TOO_LARGE); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains 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
This file contains 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
84 changes: 84 additions & 0 deletions
84
trino-aws-proxy/src/test/java/io/trino/aws/proxy/server/TestMaxPayloadSize.java
This file contains 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,84 @@ | ||
/* | ||
* 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.trino.aws.proxy.server; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.inject.Inject; | ||
import io.trino.aws.proxy.server.testing.TestingTrinoAwsProxyServer; | ||
import io.trino.aws.proxy.server.testing.containers.S3Container.ForS3Container; | ||
import io.trino.aws.proxy.server.testing.harness.TrinoAwsProxyTest; | ||
import io.trino.aws.proxy.server.testing.harness.TrinoAwsProxyTestCommonModules.WithConfiguredBuckets; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.awssdk.core.exception.SdkServiceException; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
|
||
import java.util.List; | ||
|
||
import static io.airlift.http.client.HttpStatus.REQUEST_ENTITY_TOO_LARGE; | ||
import static java.util.Objects.requireNonNull; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.InstanceOfAssertFactories.type; | ||
|
||
@TrinoAwsProxyTest(filters = TestMaxPayloadSize.Filter.class) | ||
public class TestMaxPayloadSize | ||
{ | ||
private final S3Client s3Client; | ||
private final S3Client storageClient; | ||
private final List<String> configuredBuckets; | ||
|
||
public static class Filter | ||
extends WithConfiguredBuckets | ||
{ | ||
@Override | ||
public TestingTrinoAwsProxyServer.Builder filter(TestingTrinoAwsProxyServer.Builder builder) | ||
{ | ||
return super.filter(builder).withProperty("aws.proxy.request.payload.max-size", "1B"); | ||
} | ||
} | ||
|
||
@Inject | ||
public TestMaxPayloadSize(S3Client s3Client, @ForS3Container S3Client storageClient, @ForS3Container List<String> configuredBuckets) | ||
{ | ||
this.s3Client = requireNonNull(s3Client, "s3Client is null"); | ||
this.storageClient = requireNonNull(storageClient, "storageClient is null"); | ||
this.configuredBuckets = ImmutableList.copyOf(configuredBuckets); | ||
} | ||
|
||
@Test | ||
public void testLimitOnPut() | ||
{ | ||
PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(configuredBuckets.getFirst()).key("put").build(); | ||
assertThatThrownBy(() -> s3Client.putObject(putObjectRequest, RequestBody.fromString("this is too big"))) | ||
.asInstanceOf(type(S3Exception.class)) | ||
.extracting(SdkServiceException::statusCode) | ||
.isEqualTo(REQUEST_ENTITY_TOO_LARGE.code()); | ||
} | ||
|
||
@Test | ||
public void testLimitOnGet() | ||
{ | ||
PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(configuredBuckets.getFirst()).key("get").build(); | ||
storageClient.putObject(putObjectRequest, RequestBody.fromString("this is too big for get")); | ||
|
||
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(configuredBuckets.getFirst()).key("get").build(); | ||
assertThatThrownBy(() -> s3Client.getObject(getObjectRequest).readAllBytes()) | ||
.asInstanceOf(type(S3Exception.class)) | ||
.extracting(SdkServiceException::statusCode) | ||
.isEqualTo(REQUEST_ENTITY_TOO_LARGE.code()); | ||
} | ||
} |
This file contains 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