/
AwsRequestSigningFactory.java
100 lines (85 loc) · 4.09 KB
/
AwsRequestSigningFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.httppush;
import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.ditto.connectivity.model.HmacCredentials;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import akka.actor.ActorSystem;
/**
* Creator of the signing process {@code AWS4-HMAC-SHA256}.
*/
public final class AwsRequestSigningFactory implements RequestSigningFactory {
/**
* Token timeout to evaluate the body of outgoing requests, which should take very little time as it does not
* depend on IO.
*/
private static final Duration TIMEOUT = Duration.ofSeconds(10);
/**
* Which header to sign by default.
*/
private static final List<String> DEFAULT_CANONICAL_HEADERS = List.of("host");
@Override
public RequestSigning create(final ActorSystem actorSystem, final HmacCredentials credentials) {
final JsonObject parameters = credentials.getParameters();
final String region = parameters.getValueOrThrow(JsonFields.REGION);
final String service = parameters.getValueOrThrow(JsonFields.SERVICE);
final String accessKey = parameters.getValueOrThrow(JsonFields.ACCESS_KEY);
final String secretKey = parameters.getValueOrThrow(JsonFields.SECRET_KEY);
final boolean doubleEncode = parameters.getValue(JsonFields.DOUBLE_ENCODE).orElse(true);
final List<String> canonicalHeaders = parameters.getValue(JsonFields.CANONICAL_HEADERS)
.map(array -> array.stream().map(JsonValue::asString).collect(Collectors.toList()))
.orElse(DEFAULT_CANONICAL_HEADERS);
final var xAmzContentSha256 = parameters.getValue(JsonFields.X_AMZ_CONTENT_SHA256)
.map(AwsRequestSigning.XAmzContentSha256::forName)
.orElse(AwsRequestSigning.XAmzContentSha256.EXCLUDED);
return new AwsRequestSigning(actorSystem, canonicalHeaders, region, service, accessKey, secretKey, doubleEncode,
TIMEOUT, xAmzContentSha256);
}
/**
* JSON fields of algorithm parameters.
*/
public static final class JsonFields {
/**
* Obligatory: The AWS region of the signed requests.
*/
public static final JsonFieldDefinition<String> REGION = JsonFieldDefinition.ofString("region");
/**
* Obligatory: The service for which the signed requests are intended.
*/
public static final JsonFieldDefinition<String> SERVICE = JsonFieldDefinition.ofString("service");
/**
* Obligatory: Access key to sign requests with.
*/
public static final JsonFieldDefinition<String> ACCESS_KEY = JsonFieldDefinition.ofString("accessKey");
/**
* Obligatory: Secret key to sign requests with.
*/
public static final JsonFieldDefinition<String> SECRET_KEY = JsonFieldDefinition.ofString("secretKey");
/**
* Optional: Whether to double-encode and normalize path segments. True by default. Set to false for S3.
*/
public static final JsonFieldDefinition<Boolean> DOUBLE_ENCODE = JsonFieldDefinition.ofBoolean("doubleEncode");
/**
* Optional: Which headers to sign. They differ for each AWS service. By default only "host" is signed.
*/
public static final JsonFieldDefinition<JsonArray> CANONICAL_HEADERS =
JsonFieldDefinition.ofJsonArray("canonicalHeaders");
public static final JsonFieldDefinition<String> X_AMZ_CONTENT_SHA256 =
JsonFieldDefinition.ofString("xAmzContentSha256");
}
}