Skip to content

Commit

Permalink
Merge pull request #102 from scireum/aha/SignerFixx
Browse files Browse the repository at this point in the history
Aha/signer fixx
  • Loading branch information
sabieber committed Dec 11, 2018
2 parents b3e8c82 + a658e26 commit 66e84dd
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 65 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>com.scireum</groupId>
<artifactId>sirius-parent</artifactId>
<version>5.7</version>
<version>5.8</version>
</parent>
<artifactId>s3ninja</artifactId>
<version>DEVELOPMENT-SNAPSHOT</version>
Expand All @@ -24,8 +24,8 @@
<url>http://s3ninja.net</url>

<properties>
<sirius.kernel>12.0-rc8</sirius.kernel>
<sirius.web>19.0-rc7</sirius.web>
<sirius.kernel>12.0</sirius.kernel>
<sirius.web>19.0</sirius.web>
</properties>

<dependencies>
Expand Down
34 changes: 16 additions & 18 deletions src/main/java/ninja/APILog.java
Expand Up @@ -29,9 +29,7 @@ public class APILog {
/**
* Used to describe if a call was successful or why if failed.
*/
public enum Result {
OK, REJECTED, ERROR
}
public enum Result {OK, REJECTED, ERROR}

/**
* Represents a log entry.
Expand All @@ -43,6 +41,21 @@ public static class Entry {
private String result;
private String duration;

/**
* Creates a new log entry.
*
* @param function name or method of the function which was invoked
* @param description description of the call
* @param result outcome of the call
* @param duration duration of the call
*/
protected Entry(String function, String description, String result, String duration) {
this.function = function;
this.description = description;
this.result = result;
this.duration = duration;
}

/**
* Returns the method or function which was called.
*
Expand Down Expand Up @@ -103,21 +116,6 @@ public String getCSS() {

return "";
}

/**
* Creates a new log entry.
*
* @param function name or method of the function which was invoked
* @param description description of the call
* @param result outcome of the call
* @param duration duration of the call
*/
protected Entry(String function, String description, String result, String duration) {
this.function = function;
this.description = description;
this.result = result;
this.duration = duration;
}
}

private final List<Entry> entries = Lists.newArrayList();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ninja/Aws4HashCalculator.java
Expand Up @@ -55,7 +55,9 @@ public boolean supports(final WebContext ctx) {
/**
* Computes the authentication hash as specified by the AWS SDK for verification purposes.
*
* @param ctx the current request to fetch parameters from
* @param ctx the current request to fetch parameters from
* @param pathPrefix the path prefix to preped to the {@link S3Dispatcher#getEffectiveURI(WebContext) effective URI}
* of the request
* @return the computes hash value
* @throws Exception when hashing fails
*/
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/ninja/AwsHashCalculator.java
Expand Up @@ -14,9 +14,6 @@
import sirius.web.http.WebContext;
import sirius.web.security.UserContext;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -51,12 +48,11 @@ public String computeHash(WebContext ctx, String pathPrefix) {
}
}

private String doComputeHash(final WebContext ctx, final String pathPrefix)
throws Exception {
private String doComputeHash(final WebContext ctx, final String pathPrefix) throws Exception {
if (aws4HashCalculator.supports(ctx)) {
return aws4HashCalculator.computeHash(ctx, pathPrefix);
} else {
return legacyHashCalculator.computeHash(ctx);
return legacyHashCalculator.computeHash(ctx, pathPrefix);
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/ninja/AwsLegacyHashCalculator.java
Expand Up @@ -63,10 +63,12 @@ public class AwsLegacyHashCalculator {
* Computes the authentication hash as specified by the AWS SDK for verification purposes.
*
* @param ctx the current request to fetch parameters from
* @param pathPrefix the path prefix to preped to the {@link S3Dispatcher#getEffectiveURI(WebContext) effective URI}
* of the request
* @return the computes hash value
* @throws Exception when hashing fails
*/
public String computeHash(WebContext ctx) throws Exception {
public String computeHash(WebContext ctx, String pathPrefix) throws Exception {
StringBuilder stringToSign = new StringBuilder(ctx.getRequest().method().name());
stringToSign.append("\n");
stringToSign.append(ctx.getHeaderValue("Content-MD5").asString(""));
Expand All @@ -93,7 +95,7 @@ public String computeHash(WebContext ctx) throws Exception {
stringToSign.append("\n");
}

stringToSign.append(ctx.getRequestedURI());
stringToSign.append(pathPrefix + "/" + S3Dispatcher.getEffectiveURI(ctx));

char separator = '?';
for (String parameterName : ctx.getParameterNames().stream().sorted().collect(Collectors.toList())) {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/ninja/Bucket.java
Expand Up @@ -35,10 +35,8 @@
*/
public class Bucket {

private static final int PAGE_SIZE = 25;

private File file;
private static Cache<String, Boolean> publicAccessCache = CacheManager.createCache("public-bucket-access");
private static Cache<String, Boolean> publicAccessCache = CacheManager.createLocalCache("public-bucket-access");

/**
* Creates a new bucket based on the given directory.
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/ninja/S3Dispatcher.java
Expand Up @@ -8,7 +8,6 @@

package ninja;

import com.google.common.base.Charsets;
import com.google.common.collect.Maps;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
Expand Down Expand Up @@ -132,7 +131,7 @@ public Callback<WebContext> preparePreDispatch(WebContext ctx) {
}

Bucket bucket = storage.getBucket(bucketAndObject.getFirst());
if (!bucket.exists()) {
if (!bucket.exists() && !storage.isAutocreateBuckets()) {
return null;
}

Expand All @@ -149,7 +148,16 @@ private InputStreamHandler createInputStreamHandler(WebContext ctx) {
}
}

private String getEffectiveURI(WebContext ctx) {
/**
* Returns the effective URI.
* <p>
* As we have to support legacy URIs which have an <tt>/s3</tt> prefix, we cut this here, and
* also the first "/" and only return the effective URI to process.
*
* @param ctx the current request
* @return the effective URI to process
*/
public static String getEffectiveURI(WebContext ctx) {
String uri = ctx.getRequestedURI();
if (uri.startsWith("/s3")) {
uri = uri.substring(3);
Expand All @@ -174,7 +182,7 @@ public boolean dispatch(WebContext ctx) throws Exception {
}

Bucket bucket = storage.getBucket(bucketAndObject.getFirst());
if (!bucket.exists()) {
if (!bucket.exists() && !storage.isAutocreateBuckets()) {
return false;
}

Expand Down Expand Up @@ -489,8 +497,6 @@ private void putObject(WebContext ctx, Bucket bucket, String id, InputStreamHand
ByteStreams.copy(inputStream, out);
}

System.out.println(Files.toString(object.getFile(), Charsets.UTF_8));

Map<String, String> properties = Maps.newTreeMap();
for (String name : ctx.getRequest().headers().names()) {
String nameLower = name.toLowerCase();
Expand Down Expand Up @@ -750,20 +756,24 @@ private File combineParts(String id, String uploadId, List<File> parts) {
file.getAbsolutePath());
}
try (FileChannel out = new FileOutputStream(file).getChannel()) {
for (File part : parts) {
try (RandomAccessFile raf = new RandomAccessFile(part, "r")) {
FileChannel channel = raf.getChannel();
out.write(channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length()));
}
}
combine(parts, out);
}
} catch (IOException e) {
Exceptions.handle(e);
throw Exceptions.handle(e);
}

return file;
}

private void combine(List<File> parts, FileChannel out) throws IOException {
for (File part : parts) {
try (RandomAccessFile raf = new RandomAccessFile(part, "r")) {
FileChannel channel = raf.getChannel();
out.write(channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length()));
}
}
}

/**
* Handles DELETE /bucket/id?uploadId=X
*
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/ninja/SignedChunkHandler.java
Expand Up @@ -9,7 +9,6 @@
package ninja;

import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

Expand Down Expand Up @@ -42,7 +41,6 @@ private void skipSignature(ByteBuf content) {
}
}

@NotNull
private String readChunkLengthHex(ByteBuf content) {
StringBuilder lengthString = new StringBuilder();
while (content.isReadable()) {
Expand Down
10 changes: 2 additions & 8 deletions src/test/java/AWS4SignerAWSSpec.groovy
Expand Up @@ -12,12 +12,6 @@ import com.amazonaws.auth.AWSCredentials
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.S3ClientOptions
import com.amazonaws.services.s3.model.AmazonS3Exception
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.transfer.TransferManagerBuilder
import com.google.common.base.Charsets
import com.google.common.io.ByteStreams
import com.google.common.io.Files

class AWS4SignerAWSSpec extends BaseAWSSpec {

Expand All @@ -27,8 +21,8 @@ class AWS4SignerAWSSpec extends BaseAWSSpec {
"AKIAIOSFODNN7EXAMPLE",
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
AmazonS3Client newClient = new AmazonS3Client(credentials,
new ClientConfiguration())
newClient.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true))
new ClientConfiguration())
newClient.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build())
newClient.setEndpoint("http://localhost:9999")

return newClient
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/AWS4SignerWithPathSuffixAWSSpec.groovy
Expand Up @@ -21,8 +21,8 @@ class AWS4SignerWithPathSuffixAWSSpec extends BaseAWSSpec {
"AKIAIOSFODNN7EXAMPLE",
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
AmazonS3Client newClient = new AmazonS3Client(credentials,
new ClientConfiguration())
newClient.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true))
new ClientConfiguration())
newClient.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build())
newClient.setEndpoint("http://localhost:9999/s3")

return newClient
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/S3SignerAWSSpec.groovy
Expand Up @@ -17,12 +17,13 @@ class S3SignerAWSSpec extends BaseAWSSpec {

@Override
AmazonS3Client getClient() {
AWSCredentials credentials = new BasicAWSCredentials(
"AKIAIOSFODNN7EXAMPLE",
AWSCredentials credentials = new BasicAWSCredentials("AKIAIOSFODNN7EXAMPLE",
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
AmazonS3Client newClient = new AmazonS3Client(credentials,
new ClientConfiguration().withSignerOverride("S3SignerType"))
newClient.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true))
ClientConfiguration config = new ClientConfiguration().withSignerOverride("S3SignerType")

AmazonS3Client newClient = new AmazonS3Client(credentials, config)
newClient.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build())

newClient.setEndpoint("http://localhost:9999")

return newClient
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/S3SignerWithPathSuffixAWSSpec.groovy
Expand Up @@ -21,8 +21,8 @@ class S3SignerWithPathSuffixAWSSpec extends BaseAWSSpec {
"AKIAIOSFODNN7EXAMPLE",
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
AmazonS3Client newClient = new AmazonS3Client(credentials,
new ClientConfiguration().withSignerOverride("S3SignerType"))
newClient.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true))
new ClientConfiguration().withSignerOverride("S3SignerType"))
newClient.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build())
newClient.setEndpoint("http://localhost:9999/s3")

return newClient
Expand Down

0 comments on commit 66e84dd

Please sign in to comment.