Skip to content

Commit 28ddc60

Browse files
authored
HttpSigner interface changes (#4018)
* Fix generics related warning in Signer interfaces * Remove <T> for HttpAuthOption.Builder.schemeId * Remove wildcards from sign methods * Make Identity explicit in HttpSignRequest * Make IdentityT type extend Identity in HttpSigner * Add consumer builder pattern to HttpSigner methods They have default implementation that rely on new DefaultHttpSignRequest.BuilderImpl constructor that relies on the generic IdentityT without knowing the Class of that type. This class is @SdkInternalApi so this constructor is considered private. Also, make HttpSignRequest.builder take Class<IdentityT> as parameter. Also, make IdentityT type extends Identity in HttpSignRequest. * Add test to show HttpSigner usage * Note identityType parameter is ignored in HttpSignRequest.builder * Use separate interfaces for sync and async sign requests This fixes HttpSignerTest.signAsync_usingRequest_works test. * Use abstract DefaultHttpSignRequest to avoid duplication * Split sync/async interfaces for SignedHttpRequest And removed `payloadType()` accessor. Also, HttpSignRequest builders take Identity as parameter instead of Class<IdentityT>. * Minor fixes * Added missing Builder method override for identity * Fixed javadocs * Fixed toString * Make protected properties Map an unmodifiableMap * Remove overriding in HttpSignRequest.Builder sub-interfaces * Remove overriding in SignedHttpRequest.Builder sub-interfaces * Allow sign methods take request with subtype of the IdentityT * Make HttpSignRequest @SdkProtectedApi * Make HttpSignRequest @SdkPublicApi
1 parent 800b41a commit 28ddc60

19 files changed

+649
-126
lines changed

core/http-auth-spi/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<artifactId>equalsverifier</artifactId>
7171
<scope>test</scope>
7272
</dependency>
73+
<dependency>
74+
<groupId>org.mockito</groupId>
75+
<artifactId>mockito-core</artifactId>
76+
<scope>test</scope>
77+
</dependency>
7378
</dependencies>
7479

7580
<build>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.auth.spi;
17+
18+
import java.nio.ByteBuffer;
19+
import org.reactivestreams.Publisher;
20+
import software.amazon.awssdk.annotations.Immutable;
21+
import software.amazon.awssdk.annotations.SdkPublicApi;
22+
import software.amazon.awssdk.annotations.ThreadSafe;
23+
import software.amazon.awssdk.http.auth.spi.internal.DefaultAsyncHttpSignRequest;
24+
import software.amazon.awssdk.identity.spi.Identity;
25+
import software.amazon.awssdk.utils.builder.SdkBuilder;
26+
27+
/**
28+
* Input parameters to sign a request with async payload, using {@link HttpSigner}.
29+
*
30+
* @param <IdentityT> The type of the identity.
31+
*/
32+
@SdkPublicApi
33+
@Immutable
34+
@ThreadSafe
35+
public interface AsyncHttpSignRequest<IdentityT extends Identity> extends HttpSignRequest<Publisher<ByteBuffer>, IdentityT> {
36+
/**
37+
* Get a new builder for creating a {@link AsyncHttpSignRequest}.
38+
*/
39+
static <IdentityT extends Identity> Builder<IdentityT> builder(IdentityT identity) {
40+
return new DefaultAsyncHttpSignRequest.BuilderImpl<>(identity);
41+
}
42+
43+
/**
44+
* A builder for a {@link AsyncHttpSignRequest}.
45+
*/
46+
interface Builder<IdentityT extends Identity>
47+
extends HttpSignRequest.Builder<Builder<IdentityT>, Publisher<ByteBuffer>, IdentityT>,
48+
SdkBuilder<Builder<IdentityT>, AsyncHttpSignRequest<IdentityT>> {
49+
}
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.auth.spi;
17+
18+
import java.nio.ByteBuffer;
19+
import org.reactivestreams.Publisher;
20+
import software.amazon.awssdk.annotations.Immutable;
21+
import software.amazon.awssdk.annotations.SdkPublicApi;
22+
import software.amazon.awssdk.annotations.ThreadSafe;
23+
import software.amazon.awssdk.http.auth.spi.internal.DefaultAsyncSignedHttpRequest;
24+
import software.amazon.awssdk.utils.builder.SdkBuilder;
25+
26+
/**
27+
* Represents a request with async payload that has been signed by {@link HttpSigner}.
28+
*/
29+
@SdkPublicApi
30+
@Immutable
31+
@ThreadSafe
32+
public interface AsyncSignedHttpRequest extends SignedHttpRequest<Publisher<ByteBuffer>> {
33+
34+
/**
35+
* Get a new builder for creating a {@link AsyncSignedHttpRequest}.
36+
*/
37+
static Builder builder() {
38+
return new DefaultAsyncSignedHttpRequest.BuilderImpl();
39+
}
40+
41+
/**
42+
* A builder for a {@link AsyncSignedHttpRequest}.
43+
*/
44+
interface Builder extends SignedHttpRequest.Builder<Builder, Publisher<ByteBuffer>>,
45+
SdkBuilder<Builder, AsyncSignedHttpRequest> {
46+
}
47+
}

core/http-auth-spi/src/main/java/software/amazon/awssdk/http/auth/spi/HttpAuthOption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ interface SignerPropertyConsumer {
8181
}
8282

8383
interface Builder extends SdkBuilder<Builder, HttpAuthOption> {
84-
<T> Builder schemeId(String schemeId);
84+
Builder schemeId(String schemeId);
8585

8686
<T> Builder putIdentityProperty(IdentityProperty<T> key, T value);
8787

core/http-auth-spi/src/main/java/software/amazon/awssdk/http/auth/spi/HttpAuthScheme.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ public interface HttpAuthScheme<T extends Identity> {
5858
* Retrieve the signer associated with this authentication scheme. This signer is guaranteed to support the identity
5959
* generated by the identity provider in this authentication scheme.
6060
*/
61-
HttpSigner signer();
61+
HttpSigner<T> signer();
6262
}

core/http-auth-spi/src/main/java/software/amazon/awssdk/http/auth/spi/HttpSignRequest.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,18 @@
2020
import software.amazon.awssdk.annotations.SdkPublicApi;
2121
import software.amazon.awssdk.annotations.ThreadSafe;
2222
import software.amazon.awssdk.http.SdkHttpRequest;
23-
import software.amazon.awssdk.http.auth.spi.internal.DefaultHttpSignRequest;
24-
import software.amazon.awssdk.utils.builder.SdkBuilder;
23+
import software.amazon.awssdk.identity.spi.Identity;
2524

2625
/**
27-
* Represents a request to be signed by {@link HttpSigner}.
26+
* Input parameters to sign a request using {@link HttpSigner}.
2827
*
29-
* @param <PayloadT> The type of payload of this request.
28+
* @param <PayloadT> The type of payload of the request.
29+
* @param <IdentityT> The type of the identity.
3030
*/
3131
@SdkPublicApi
3232
@Immutable
3333
@ThreadSafe
34-
public interface HttpSignRequest<PayloadT> {
35-
36-
/**
37-
* Get a new builder for creating a {@link HttpSignRequest}.
38-
*/
39-
static <T> Builder<T> builder(Class<T> payloadType) {
40-
return new DefaultHttpSignRequest.BuilderImpl(payloadType);
41-
}
42-
43-
/**
44-
* Returns the type of the payload.
45-
*/
46-
Class<PayloadT> payloadType();
34+
public interface HttpSignRequest<PayloadT, IdentityT extends Identity> {
4735

4836
/**
4937
* Returns the HTTP request object, without the request body payload.
@@ -56,28 +44,38 @@ static <T> Builder<T> builder(Class<T> payloadType) {
5644
Optional<PayloadT> payload();
5745

5846
/**
59-
* Returns the property that the {@link HttpSigner} can use during signing.
47+
* Returns the identity.
48+
*/
49+
IdentityT identity();
50+
51+
/**
52+
* Returns the value of a property that the {@link HttpSigner} can use during signing.
6053
*/
6154
<T> T property(SignerProperty<T> property);
6255

6356
/**
6457
* A builder for a {@link HttpSignRequest}.
6558
*/
66-
interface Builder<PayloadT> extends SdkBuilder<Builder<PayloadT>, HttpSignRequest> {
59+
interface Builder<B extends Builder<B, PayloadT, IdentityT>, PayloadT, IdentityT extends Identity> {
6760

6861
/**
6962
* Set the HTTP request object, without the request body payload.
7063
*/
71-
Builder request(SdkHttpRequest request);
64+
B request(SdkHttpRequest request);
7265

7366
/**
7467
* Set the body payload of the request. A payload is optional. By default, the payload will be empty.
7568
*/
76-
Builder payload(PayloadT payload);
69+
B payload(PayloadT payload);
70+
71+
/**
72+
* Set the identity of the request.
73+
*/
74+
B identity(IdentityT identity);
7775

7876
/**
7977
* Set a property that the {@link HttpSigner} can use during signing.
8078
*/
81-
<T> Builder putProperty(SignerProperty<T> key, T value);
79+
<T> B putProperty(SignerProperty<T> key, T value);
8280
}
8381
}

core/http-auth-spi/src/main/java/software/amazon/awssdk/http/auth/spi/HttpSigner.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,62 @@
1515

1616
package software.amazon.awssdk.http.auth.spi;
1717

18-
import java.nio.ByteBuffer;
19-
import org.reactivestreams.Publisher;
18+
import java.util.function.Consumer;
2019
import software.amazon.awssdk.annotations.SdkPublicApi;
21-
import software.amazon.awssdk.http.ContentStreamProvider;
20+
import software.amazon.awssdk.http.auth.spi.internal.DefaultAsyncHttpSignRequest;
21+
import software.amazon.awssdk.http.auth.spi.internal.DefaultSyncHttpSignRequest;
22+
import software.amazon.awssdk.identity.spi.Identity;
2223

2324
/**
2425
* Interface for the process of modifying a request destined for a service so that the service can authenticate the SDK
25-
* customer’s identity
26+
* customer’s identity.
27+
*
28+
* @param <IdentityT> The type of the identity.
2629
*/
2730
@SdkPublicApi
28-
public interface HttpSigner {
31+
public interface HttpSigner<IdentityT extends Identity> {
32+
33+
/**
34+
* Method that takes in inputs to sign a request with sync payload and returns a signed version of the request.
35+
*
36+
* @param request The inputs to sign a request.
37+
* @return A signed version of the request.
38+
*/
39+
SyncSignedHttpRequest sign(SyncHttpSignRequest<? extends IdentityT> request);
40+
41+
/**
42+
* Method that takes in inputs to sign a request with sync payload and returns a signed version of the request.
43+
* <p>
44+
* Similar to {@link #sign(SyncHttpSignRequest)}, but takes a lambda to configure a new {@link SyncHttpSignRequest.Builder}.
45+
* This removes the need to call {@link SyncHttpSignRequest#builder(IdentityT)}} and
46+
* {@link SyncHttpSignRequest.Builder#build()}.
47+
*
48+
* @param consumer A {@link Consumer} to which an empty {@link SyncHttpSignRequest.Builder} will be given.
49+
* @return A signed version of the request.
50+
*/
51+
default SyncSignedHttpRequest sign(Consumer<SyncHttpSignRequest.Builder<IdentityT>> consumer) {
52+
return sign(new DefaultSyncHttpSignRequest.BuilderImpl<IdentityT>().applyMutation(consumer).build());
53+
}
2954

3055
/**
31-
* Method that takes in a request and returns a signed version of the request.
56+
* Method that takes in inputs to sign a request with async payload and returns a signed version of the request.
3257
*
33-
* @param request The request to sign, with sync payload
34-
* @return A signed version of the input request
58+
* @param request The inputs to sign a request.
59+
* @return A signed version of the request.
3560
*/
36-
SignedHttpRequest<ContentStreamProvider> sign(HttpSignRequest<? extends ContentStreamProvider> request);
61+
AsyncSignedHttpRequest signAsync(AsyncHttpSignRequest<? extends IdentityT> request);
3762

3863
/**
39-
* Method that takes in a request and returns a signed version of the request.
64+
* Method that takes in inputs to sign a request with async payload and returns a signed version of the request.
65+
* <p>
66+
* Similar to {@link #signAsync(AsyncHttpSignRequest)}, but takes a lambda to configure a new
67+
* {@link AsyncHttpSignRequest.Builder}. This removes the need to call {@link AsyncHttpSignRequest#builder(IdentityT)}} and
68+
* {@link AsyncHttpSignRequest.Builder#build()}.
4069
*
41-
* @param request The request to sign, with async payload
42-
* @return A signed version of the input request
70+
* @param consumer A {@link Consumer} to which an empty {@link HttpSignRequest.Builder} will be given.
71+
* @return A signed version of the request.
4372
*/
44-
SignedHttpRequest<Publisher<ByteBuffer>> signAsync(HttpSignRequest<? extends Publisher<? extends ByteBuffer>> request);
73+
default AsyncSignedHttpRequest signAsync(Consumer<AsyncHttpSignRequest.Builder<IdentityT>> consumer) {
74+
return signAsync(new DefaultAsyncHttpSignRequest.BuilderImpl<IdentityT>().applyMutation(consumer).build());
75+
}
4576
}

core/http-auth-spi/src/main/java/software/amazon/awssdk/http/auth/spi/SignedHttpRequest.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,17 @@
2020
import software.amazon.awssdk.annotations.SdkPublicApi;
2121
import software.amazon.awssdk.annotations.ThreadSafe;
2222
import software.amazon.awssdk.http.SdkHttpRequest;
23-
import software.amazon.awssdk.http.auth.spi.internal.DefaultSignedHttpRequest;
24-
import software.amazon.awssdk.utils.builder.SdkBuilder;
2523

2624
/**
2725
* Represents a request that has been signed by {@link HttpSigner}.
2826
*
29-
* @param <PayloadT> The type of payload of this request.
27+
* @param <PayloadT> The type of payload of the request.
3028
*/
3129
@SdkPublicApi
3230
@Immutable
3331
@ThreadSafe
3432
public interface SignedHttpRequest<PayloadT> {
3533

36-
/**
37-
* Get a new builder for creating a {@link SignedHttpRequest}.
38-
*/
39-
static <T> Builder<T> builder(Class<T> payloadType) {
40-
return new DefaultSignedHttpRequest.BuilderImpl(payloadType);
41-
}
42-
43-
/**
44-
* Returns the type of the payload.
45-
*/
46-
Class<PayloadT> payloadType();
47-
4834
/**
4935
* Returns the HTTP request object, without the request body payload.
5036
*/
@@ -58,16 +44,16 @@ static <T> Builder<T> builder(Class<T> payloadType) {
5844
/**
5945
* A builder for a {@link SignedHttpRequest}.
6046
*/
61-
interface Builder<PayloadT> extends SdkBuilder<Builder<PayloadT>, SignedHttpRequest> {
47+
interface Builder<B extends Builder<B, PayloadT>, PayloadT> {
6248

6349
/**
6450
* Set the HTTP request object, without the request body payload.
6551
*/
66-
Builder request(SdkHttpRequest request);
52+
B request(SdkHttpRequest request);
6753

6854
/**
6955
* Set the body payload of the request. A payload is optional. By default, the payload will be empty.
7056
*/
71-
Builder payload(PayloadT payload);
57+
B payload(PayloadT payload);
7258
}
7359
}

core/http-auth-spi/src/main/java/software/amazon/awssdk/http/auth/spi/SignerProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/**
2626
* A strongly-typed property for input to an {@link HttpSigner}.
27-
* @param <T> The type of the attribute.
27+
* @param <T> The type of the property.
2828
*/
2929
@SdkPublicApi
3030
@Immutable
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.auth.spi;
17+
18+
import software.amazon.awssdk.annotations.Immutable;
19+
import software.amazon.awssdk.annotations.SdkPublicApi;
20+
import software.amazon.awssdk.annotations.ThreadSafe;
21+
import software.amazon.awssdk.http.ContentStreamProvider;
22+
import software.amazon.awssdk.http.auth.spi.internal.DefaultSyncHttpSignRequest;
23+
import software.amazon.awssdk.identity.spi.Identity;
24+
import software.amazon.awssdk.utils.builder.SdkBuilder;
25+
26+
/**
27+
* Input parameters to sign a request with sync payload, using {@link HttpSigner}.
28+
*
29+
* @param <IdentityT> The type of the identity.
30+
*/
31+
@SdkPublicApi
32+
@Immutable
33+
@ThreadSafe
34+
public interface SyncHttpSignRequest<IdentityT extends Identity> extends HttpSignRequest<ContentStreamProvider, IdentityT> {
35+
/**
36+
* Get a new builder for creating a {@link SyncHttpSignRequest}.
37+
*/
38+
static <IdentityT extends Identity> Builder<IdentityT> builder(IdentityT identity) {
39+
return new DefaultSyncHttpSignRequest.BuilderImpl<>(identity);
40+
}
41+
42+
/**
43+
* A builder for a {@link SyncHttpSignRequest}.
44+
*/
45+
interface Builder<IdentityT extends Identity>
46+
extends HttpSignRequest.Builder<Builder<IdentityT>, ContentStreamProvider, IdentityT>,
47+
SdkBuilder<Builder<IdentityT>, SyncHttpSignRequest<IdentityT>> {
48+
}
49+
}

0 commit comments

Comments
 (0)