use of software.amazon.eventstream.HeaderValue in project aws-sdk-java-v2 by aws.
the class Aws4EventStreamSignerTest method generateTestVector.
TestVector generateTestVector() {
return new TestVector() {
List<String> requestBody = Lists.newArrayList("A", "B", "C");
@Override
public List<String> requestBody() {
return requestBody;
}
@Override
public SdkHttpFullRequest.Builder httpFullRequest() {
// Signing key: "29dc0a760fed568677d74136ad02d315a07d31b8f321f5c43350f284dac892c";
return SdkHttpFullRequest.builder().method(SdkHttpMethod.POST).putHeader("Host", "demo.us-east-1.amazonaws.com").putHeader("content-encoding", "application/vnd.amazon.eventstream").putHeader("x-amz-content-sha256", "STREAMING-AWS4-HMAC-SHA256-EVENTS").encodedPath("/streaming").protocol("https").host("demo.us-east-1.amazonaws.com");
}
@Override
public AsyncRequestBody requestBodyPublisher() {
List<ByteBuffer> bodyBytes = requestBody.stream().map(s -> ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8))).collect(Collectors.toList());
Publisher<ByteBuffer> bodyPublisher = Flowable.fromIterable(bodyBytes);
return AsyncRequestBody.fromPublisher(bodyPublisher);
}
@Override
public Flowable<Message> expectedMessagePublisher() {
Flowable<String> sigsHex = Flowable.just("7aabf85b765e6a4d0d500b6e968657b14726fa3e1eb7e839302728ffd77629a5", "f72aa9642f571d24a6e1ae42f10f073ad9448d8a028b6bcd82da081335adda02", "632af120435b57ec241d8bfbb12e496dfd5e2730a1a02ac0ab6eaa230ae02e9a", "c6f679ddb3af68f5e82f0cf6761244cb2338cf11e7d01a24130aea1b7c17e53e");
// The Last data frame is empty
Flowable<String> payloads = Flowable.fromIterable(requestBody).concatWith(Flowable.just(""));
return sigsHex.zipWith(payloads, new BiFunction<String, String, Message>() {
// The first Instant was used to sign the request
private int idx = 1;
@Override
public Message apply(String sig, String payload) throws Exception {
Map<String, HeaderValue> headers = new HashMap<>();
headers.put(EVENT_STREAM_DATE, HeaderValue.fromTimestamp(SIGNING_INSTANTS.get(idx++)));
headers.put(EVENT_STREAM_SIGNATURE, HeaderValue.fromByteArray(BinaryUtils.fromHex(sig)));
return new Message(headers, payload.getBytes(StandardCharsets.UTF_8));
}
});
}
};
}
use of software.amazon.eventstream.HeaderValue in project aws-sdk-java-v2 by aws.
the class AwsClientHandlerUtils method encodeEventStreamRequestToByteBuffer.
/**
* Encodes the request into a flow message and then returns bytebuffer from the message.
*
* @param request The request to encode
* @return A bytebuffer representing the given request
*/
public static ByteBuffer encodeEventStreamRequestToByteBuffer(SdkHttpFullRequest request) {
Map<String, HeaderValue> headers = request.headers().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> HeaderValue.fromString(firstIfPresent(e.getValue()))));
byte[] payload = null;
if (request.contentStreamProvider().isPresent()) {
try {
payload = IoUtils.toByteArray(request.contentStreamProvider().get().newStream());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return new Message(headers, payload).toByteBuffer();
}
use of software.amazon.eventstream.HeaderValue in project aws-sdk-java-v2 by aws.
the class EventStreamAsyncResponseTransformerTest method verifyExceptionThrown.
private void verifyExceptionThrown(Map<String, HeaderValue> headers) {
SdkServiceException exception = SdkServiceException.builder().build();
Message exceptionMessage = new Message(headers, new byte[0]);
Flowable<ByteBuffer> bytePublisher = Flowable.just(exceptionMessage.toByteBuffer());
SubscribingResponseHandler handler = new SubscribingResponseHandler();
AsyncResponseTransformer<SdkResponse, Void> transformer = EventStreamAsyncResponseTransformer.builder().eventStreamResponseHandler(handler).exceptionResponseHandler((response, executionAttributes) -> exception).executor(Executors.newSingleThreadExecutor()).future(new CompletableFuture<>()).build();
CompletableFuture<Void> cf = transformer.prepare();
transformer.onResponse(null);
transformer.onStream(SdkPublisher.adapt(bytePublisher));
assertThatThrownBy(() -> {
try {
cf.join();
} catch (CompletionException e) {
if (e.getCause() instanceof SdkServiceException) {
throw e.getCause();
}
}
}).isSameAs(exception);
assertThat(handler.exceptionOccurredCalled).isTrue();
}
use of software.amazon.eventstream.HeaderValue in project aws-sdk-java-v2 by aws.
the class EventStreamInitialRequestInterceptor method modifyAsyncHttpContent.
@Override
public Optional<AsyncRequestBody> modifyAsyncHttpContent(ModifyHttpRequest context, ExecutionAttributes executionAttributes) {
if (!Boolean.TRUE.equals(executionAttributes.getAttribute(HAS_INITIAL_REQUEST_EVENT))) {
return context.asyncRequestBody();
}
/*
* At this point in the request execution, the requestBody contains the serialized initial request,
* and the asyncRequestBody contains the event stream proper. We will prepend the former to the
* latter.
*/
byte[] payload = getInitialRequestPayload(context);
String contentType = context.httpRequest().headers().get(CONTENT_TYPE).get(0);
Map<String, HeaderValue> initialRequestEventHeaders = new HashMap<>();
initialRequestEventHeaders.put(":message-type", HeaderValue.fromString("event"));
initialRequestEventHeaders.put(":event-type", HeaderValue.fromString("initial-request"));
initialRequestEventHeaders.put(":content-type", HeaderValue.fromString(contentType));
ByteBuffer initialRequest = new Message(initialRequestEventHeaders, payload).toByteBuffer();
Publisher<ByteBuffer> asyncRequestBody = context.asyncRequestBody().orElseThrow(() -> new IllegalStateException("This request is an event streaming request and thus " + "should have an asyncRequestBody"));
Publisher<ByteBuffer> withInitialRequest = new AsyncStreamPrepender<>(asyncRequestBody, initialRequest);
return Optional.of(AsyncRequestBody.fromPublisher(withInitialRequest));
}
use of software.amazon.eventstream.HeaderValue in project aws-sdk-java-v2 by aws.
the class EventStreamAws4SignerTest method openStreamEventSignaturesCanRollOverBetweenDays.
/**
* Verify that when an event stream is open from one day to the next, the signature is properly signed for the day of the
* event.
*/
@Test
public void openStreamEventSignaturesCanRollOverBetweenDays() {
EventStreamAws4Signer signer = EventStreamAws4Signer.create();
Region region = Region.US_WEST_2;
AwsCredentials credentials = AwsBasicCredentials.create("a", "s");
String signingName = "name";
AdjustableClock clock = new AdjustableClock();
clock.time = Instant.parse("2020-01-01T23:59:59Z");
SdkHttpFullRequest initialRequest = SdkHttpFullRequest.builder().uri(URI.create("http://localhost:8080")).method(SdkHttpMethod.GET).build();
SdkHttpFullRequest signedRequest = SignerTestUtils.signRequest(signer, initialRequest, credentials, signingName, clock, region.id());
ByteBuffer event = new Message(Collections.emptyMap(), "foo".getBytes(UTF_8)).toByteBuffer();
Callable<ByteBuffer> lastEvent = () -> {
clock.time = Instant.parse("2020-01-02T00:00:00Z");
return event;
};
AsyncRequestBody requestBody = AsyncRequestBody.fromPublisher(Flowable.concatArray(Flowable.just(event), Flowable.fromCallable(lastEvent)));
AsyncRequestBody signedBody = SignerTestUtils.signAsyncRequest(signer, signedRequest, requestBody, credentials, signingName, clock, region.id());
List<Message> signedMessages = readMessages(signedBody);
assertThat(signedMessages.size()).isEqualTo(3);
Map<String, HeaderValue> firstMessageHeaders = signedMessages.get(0).getHeaders();
assertThat(firstMessageHeaders.get(":date").getTimestamp()).isEqualTo("2020-01-01T23:59:59Z");
assertThat(Base64.getEncoder().encodeToString(firstMessageHeaders.get(":chunk-signature").getByteArray())).isEqualTo("EFt7ZU043r/TJE8U+1GxJXscmNxoqmIdGtUIl8wE9u0=");
Map<String, HeaderValue> lastMessageHeaders = signedMessages.get(2).getHeaders();
assertThat(lastMessageHeaders.get(":date").getTimestamp()).isEqualTo("2020-01-02T00:00:00Z");
assertThat(Base64.getEncoder().encodeToString(lastMessageHeaders.get(":chunk-signature").getByteArray())).isEqualTo("UTRGo0D7BQytiVkH1VofR/8f3uFsM4V5QR1A8grr1+M=");
}
Aggregations