Skip to content

HttpSigner interface changes #4018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

gosar
Copy link
Contributor

@gosar gosar commented May 16, 2023

Motivation and Context

Modifications

  • Make Identity explicit in HttpSignRequest
  • Add consumer builder pattern to HttpSigner methods
  • Make explicit sub types for sync and async to make it easier to deal with generics, especially with Publisher. Removes need to use Class directly in methods.

Other fixes:

  • Remove for HttpAuthOption.Builder.schemeId
  • Fix generics related warning in Signer interfaces

Usage is shown in HttpSignerTest included in this PR.

Testing

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@gosar gosar requested a review from a team as a code owner May 16, 2023 18:23
@gosar gosar mentioned this pull request May 16, 2023
12 tasks
gosar added 3 commits May 16, 2023 12:28
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.
@gosar gosar requested a review from millems May 18, 2023 18:15
static <T> Builder<T> builder(Class<T> payloadType) {
return new DefaultHttpSignRequest.BuilderImpl(payloadType);
}
public interface HttpSignRequest<PayloadT, IdentityT extends Identity> {
Copy link
Contributor

@millems millems May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo, this should implement ToCopyableBuilder and its builder should implement CopyableBuilder. That prevents some issues here, like needing to remember to override every parent method in the child class.

Unfortunately it makes the parameters on this type even crazier.

To make the room, we could change IdentityT identity() to Identity identity() and Optional<PayloadT> payload() to Optional<? extends Object> payload(), and then override them in the child types to return the exact types. Even better, we might just remove the getters from here entirely and rely on the subtype. I don't expect it to be a major problem to need to use the subtype to get access to the payload or identity.

We'd also need to remove the payload and identity setters from this base builder, but that's probably okay - we can just put them in the child types. It should be very rare that someone needs to set the identity or payload using the base builder type...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That prevents some issues here, like needing to remember to override every parent method in the child class.

Without using To/CopyableBuilder, I was able to remove the overriding of methods in the builder sub-interfaces to return the sub-type by adding a generic type parameter for the builder, in these commits de704e7 and 3e24cc4.

I'm not sure if we'll really need to mutate these objects, so I think we can defer making these To/CopyableBuilder until needed?

@@ -58,16 +58,16 @@ static <T> Builder<T> builder(Class<T> payloadType) {
/**
* A builder for a {@link SignedHttpRequest}.
*/
interface Builder<PayloadT> extends SdkBuilder<Builder<PayloadT>, SignedHttpRequest> {
interface Builder<PayloadT> extends SdkBuilder<Builder<PayloadT>, SignedHttpRequest<PayloadT>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here. CopyableBuilder?

gosar added 5 commits May 19, 2023 14:15
And removed `payloadType()` accessor.

Also, HttpSignRequest builders take Identity as parameter instead of Class<IdentityT>.
* Added missing Builder method override for identity
* Fixed javadocs
* Fixed toString
Comment on lines 47 to +52
Optional<PayloadT> payload();

/**
* Returns the property that the {@link HttpSigner} can use during signing.
* Returns the identity.
*/
IdentityT identity();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo, I'd rather not parameterize this types and just do Optional<?> payload() and Identity identity() here. Subtypes would override them with the more-specific return type.

This makes it so that code that deal with the base sign request can just deal with a SignRequest instead of a SignRequest<?, ?>, and the base builder can just deal with a Builder<?> instead of a Builder<?, ?, ?>.

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think code that deals with base sign request, might benefit from the type, atleast the IdentityT. So the BearerTokenSigner can extract the token from HttpSignRequest<?, TokenIdentity identity>. Though I can see removing the PayloadT (will reduce it by one), but means the subtypes have to (re)define the payload() method instead of specifying the generic in type definition. And as an interface, having a HttpSignRequest that has a payload() accessor but doesn't know it's type doesn't feel great to me, so I'm in favor of keeping them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. And we can reassess at the surface area review as well, once we can see the impact on the signers, etc.

Comment on lines 69 to +77
/**
* Set the body payload of the request. A payload is optional. By default, the payload will be empty.
*/
Builder payload(PayloadT payload);
B payload(PayloadT payload);

/**
* Set the identity of the request.
*/
B identity(IdentityT identity);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with the suggestion I have above, we'd remove these from the base builder and put it in the subtypes.

Copy link
Contributor

@millems millems left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor nit, otherwise lgtm.

*/
@SdkPublicApi
@SdkProtectedApi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still a public api.

Comment on lines 47 to +52
Optional<PayloadT> payload();

/**
* Returns the property that the {@link HttpSigner} can use during signing.
* Returns the identity.
*/
IdentityT identity();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. And we can reassess at the surface area review as well, once we can see the impact on the signers, etc.

@gosar gosar merged commit 28ddc60 into feature/master/sra-identity-auth May 24, 2023
@gosar gosar deleted the gosar/sra-ia-signer-interface-updates-2 branch May 24, 2023 17:40
@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug C 4 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 103 Code Smells

65.3% 65.3% Coverage
1.0% 1.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants