🎉 Init
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package dev.dinauer.r9t.core.auth;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class SigV4TestSigner
|
||||
{
|
||||
private static final DateTimeFormatter AMZ_DATE = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
|
||||
private static final DateTimeFormatter DATE_STAMP = DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneOffset.UTC);
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String SERVICE = "s3";
|
||||
|
||||
private SigV4TestSigner()
|
||||
{
|
||||
}
|
||||
|
||||
public static void sign(HttpRequest.Builder builder, String method, URI uri, String accessKey, String secretKey, byte[] body)
|
||||
{
|
||||
Instant now = Instant.now();
|
||||
String amzDate = AMZ_DATE.format(now);
|
||||
String dateStamp = DATE_STAMP.format(now);
|
||||
String payloadHash = SigV4Signer.sha256Hex(body);
|
||||
String host = uri.getHost() + ":" + uri.getPort();
|
||||
|
||||
Map<String, String> headers = new LinkedHashMap<>();
|
||||
headers.put("host", host);
|
||||
headers.put("x-amz-content-sha256", payloadHash);
|
||||
headers.put("x-amz-date", amzDate);
|
||||
|
||||
String signedHeaders = String.join(";", headers.keySet());
|
||||
StringBuilder canonicalHeaders = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : headers.entrySet())
|
||||
{
|
||||
canonicalHeaders.append(entry.getKey()).append(":").append(entry.getValue()).append("\n");
|
||||
}
|
||||
|
||||
String path = uri.getRawPath();
|
||||
if (path == null || path.isEmpty())
|
||||
{
|
||||
path = "/";
|
||||
}
|
||||
String query = uri.getRawQuery();
|
||||
if (query == null)
|
||||
{
|
||||
query = "";
|
||||
}
|
||||
|
||||
String canonicalRequest = method + "\n"
|
||||
+ path + "\n"
|
||||
+ query + "\n"
|
||||
+ canonicalHeaders + "\n"
|
||||
+ signedHeaders + "\n"
|
||||
+ payloadHash;
|
||||
|
||||
String credentialScope = dateStamp + "/" + REGION + "/" + SERVICE + "/aws4_request";
|
||||
String stringToSign = SigV4CanonicalRequest.stringToSign(amzDate, credentialScope, canonicalRequest);
|
||||
|
||||
byte[] signingKey = SigV4Signer.signingKey(secretKey, dateStamp, REGION, SERVICE);
|
||||
String signature = SigV4Signer.hex(SigV4Signer.hmacSha256(signingKey, stringToSign));
|
||||
|
||||
String authorizationHeader = "AWS4-HMAC-SHA256 Credential=" + accessKey + "/" + credentialScope
|
||||
+ ", SignedHeaders=" + signedHeaders + ", Signature=" + signature;
|
||||
|
||||
builder.header("X-Amz-Date", amzDate)
|
||||
.header("X-Amz-Content-Sha256", payloadHash)
|
||||
.header("Authorization", authorizationHeader);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package dev.dinauer.r9t.core.object;
|
||||
|
||||
import dev.dinauer.r9t.core.auth.SigV4TestSigner;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
public class ObjectUploadTest
|
||||
{
|
||||
private static final String BUCKET = "upload-test-bucket";
|
||||
private static final String KEY = "upload-test.txt";
|
||||
private static final String ACCESS_KEY = "testkey";
|
||||
private static final String SECRET_KEY = "testsecret";
|
||||
|
||||
@TestHTTPResource
|
||||
URI baseUri;
|
||||
|
||||
private final HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
|
||||
|
||||
@Test
|
||||
public void upload() throws Exception
|
||||
{
|
||||
HttpResponse<Void> createBucket = send("PUT", baseUri.resolve(BUCKET), new byte[0], null);
|
||||
assertEquals(200, createBucket.statusCode());
|
||||
|
||||
byte[] content = "hello upload test".getBytes(StandardCharsets.UTF_8);
|
||||
HttpResponse<Void> put = send("PUT", baseUri.resolve(BUCKET + "/" + KEY), content, "text/plain");
|
||||
assertEquals(200, put.statusCode());
|
||||
assertTrue(put.headers().firstValue("ETag").isPresent());
|
||||
|
||||
HttpResponse<byte[]> get = sendForBytes("GET", baseUri.resolve(BUCKET + "/" + KEY));
|
||||
assertEquals(200, get.statusCode());
|
||||
assertEquals("text/plain", get.headers().firstValue("Content-Type").orElse(""));
|
||||
assertEquals(new String(content, StandardCharsets.UTF_8), new String(get.body(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unsignedRequestIsRejected() throws Exception
|
||||
{
|
||||
HttpResponse<Void> response = client.send(
|
||||
HttpRequest.newBuilder(baseUri.resolve(BUCKET)).PUT(HttpRequest.BodyPublishers.noBody()).build(),
|
||||
HttpResponse.BodyHandlers.discarding());
|
||||
assertEquals(401, response.statusCode());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanup() throws Exception
|
||||
{
|
||||
send("DELETE", baseUri.resolve(BUCKET + "/" + KEY), new byte[0], null);
|
||||
send("DELETE", baseUri.resolve(BUCKET), new byte[0], null);
|
||||
}
|
||||
|
||||
private HttpResponse<Void> send(String method, URI uri, byte[] body, String contentType) throws Exception
|
||||
{
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(uri)
|
||||
.method(method, HttpRequest.BodyPublishers.ofByteArray(body));
|
||||
if (contentType != null)
|
||||
{
|
||||
builder.header("Content-Type", contentType);
|
||||
}
|
||||
SigV4TestSigner.sign(builder, method, uri, ACCESS_KEY, SECRET_KEY, body);
|
||||
return client.send(builder.build(), HttpResponse.BodyHandlers.discarding());
|
||||
}
|
||||
|
||||
private HttpResponse<byte[]> sendForBytes(String method, URI uri) throws Exception
|
||||
{
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(uri)
|
||||
.method(method, HttpRequest.BodyPublishers.ofByteArray(new byte[0]));
|
||||
SigV4TestSigner.sign(builder, method, uri, ACCESS_KEY, SECRET_KEY, new byte[0]);
|
||||
return client.send(builder.build(), HttpResponse.BodyHandlers.ofByteArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
dev.dinauer.r9t.workdir=target/test-r9t
|
||||
dev.dinauer.r9t.credentials.access-key=testkey
|
||||
dev.dinauer.r9t.credentials.secret-key=testsecret
|
||||
Reference in New Issue
Block a user