-
Notifications
You must be signed in to change notification settings - Fork 218
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
adds http-checksum trait support #741
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d0fa3c
adds httpChecksum trait support
skotambkar 49a0182
lowercase request,response member within smithy http-checksum trait
skotambkar 808398a
Change location to be an enum.
skotambkar f89e719
address feedback around documentation and java lang usage
skotambkar bdbb518
add support for list of locations in httpChecksum trait. Add validator
skotambkar c7e8c7e
feedback on trait validation for aws protocol. Update to require non …
skotambkar b7083e3
feedback around documentation
skotambkar 29ebe23
minor documentation update
skotambkar 2c94124
java code refactor and bug fix - default list of location [\"header\"…
skotambkar 3b98cba
add validation for httpChecksum trait usage along with httpPrefixHead…
skotambkar d933380
minor code clean-up for trait validators
skotambkar 5f8f90a
add documentation for checksum member behavior when present on input …
skotambkar 8b0dd99
rephrase wording for checksum header conflict section
skotambkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...ws-traits/src/main/java/software/amazon/smithy/aws/traits/HttpChecksumTraitValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.aws.traits; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import software.amazon.smithy.aws.traits.auth.SigV4Trait; | ||
import software.amazon.smithy.aws.traits.protocols.AwsProtocolTrait; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.knowledge.ServiceIndex; | ||
import software.amazon.smithy.model.knowledge.TopDownIndex; | ||
import software.amazon.smithy.model.shapes.OperationShape; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.traits.HttpChecksumProperties.Location; | ||
import software.amazon.smithy.model.traits.HttpChecksumTrait; | ||
import software.amazon.smithy.model.traits.OptionalAuthTrait; | ||
import software.amazon.smithy.model.traits.Trait; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Validates checksum location modeling specific to AWS usage. For response property within httpChecksum trait, | ||
* only "header" is a valid checksum location. If service, operation uses sigv4 authentication scheme, the | ||
* request property within httpChecksum trait must include "header" as supported checksum location. | ||
*/ | ||
@SmithyInternalApi | ||
public class HttpChecksumTraitValidator extends AbstractValidator { | ||
|
||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
ServiceIndex serviceIndex = ServiceIndex.of(model); | ||
TopDownIndex topDownIndex = TopDownIndex.of(model); | ||
|
||
List<ServiceShape> services = model.shapes(ServiceShape.class).collect(Collectors.toList()); | ||
for (ServiceShape service : services) { | ||
if (!isTargetProtocol(service)) { | ||
continue; | ||
} | ||
|
||
for (OperationShape operation : topDownIndex.getContainedOperations(service)) { | ||
if (operation.hasTrait(HttpChecksumTrait.class)) { | ||
events.addAll(validateSupportedLocations(serviceIndex, service, operation)); | ||
} | ||
} | ||
} | ||
|
||
return events; | ||
} | ||
|
||
/** | ||
* Validates supported locations within httpChecksum trait. For response property, only "header" | ||
* is a valid checksum location. For service, operation using sigv4, the request property must include | ||
* "header" as supported checksum location. | ||
* | ||
* @param serviceIndex index resolving auth schemes | ||
* @param service service shape for the API | ||
* @param operation operation shape | ||
* @return List of validation events that occurred when validating the model. | ||
*/ | ||
protected List<ValidationEvent> validateSupportedLocations( | ||
ServiceIndex serviceIndex, | ||
ServiceShape service, | ||
OperationShape operation | ||
) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
HttpChecksumTrait trait = operation.expectTrait(HttpChecksumTrait.class); | ||
|
||
// validate response property only supports "header" as location | ||
trait.getResponseProperty().ifPresent(property -> { | ||
Set<Location> locations = property.getLocations(); | ||
if (locations.size() > 1 || !locations.contains(Location.HEADER)) { | ||
events.add(error(operation, trait, | ||
String.format("For aws protocols, the `response` property of the `httpChecksum` trait " | ||
+ "only supports `header` as `location`, found \"%s\".", locations))); | ||
} | ||
}); | ||
|
||
// if SigV4 auth scheme is used, validate request property locations contain "header" as supported location. | ||
if (hasSigV4AuthScheme(serviceIndex, service, operation)) { | ||
trait.getRequestProperty().ifPresent(property -> { | ||
if (!property.getLocations().contains(Location.HEADER)) { | ||
events.add(error(operation, trait, | ||
"For operation using sigv4 auth scheme, the `request` property of the " | ||
+ "`httpChecksum` trait must support `header` checksum location.")); | ||
} | ||
}); | ||
} | ||
|
||
return events; | ||
} | ||
|
||
/** | ||
* isTargetProtocol returns true if service uses a target protocol. By default, | ||
* target protocol resolves to aws protocol. | ||
* | ||
* @param service is the service shape for which target protocol usage is checked. | ||
* @return boolean indicating target protocol is used by the service. | ||
*/ | ||
protected boolean isTargetProtocol(ServiceShape service) { | ||
// By default, target protocol is AWS protocol. | ||
return service.hasTrait(AwsProtocolTrait.class); | ||
} | ||
|
||
/** | ||
* Returns true if the SigV4Trait is a auth scheme for the service and operation. | ||
* | ||
* @param serviceIndex index resolving auth schemes | ||
* @param service service shape for the API | ||
* @param operation operation shape | ||
* @return if SigV4 is an auth scheme for the operation and service. | ||
*/ | ||
private boolean hasSigV4AuthScheme(ServiceIndex serviceIndex, ServiceShape service, OperationShape operation) { | ||
Map<ShapeId, Trait> auth = serviceIndex.getEffectiveAuthSchemes(service.getId(), operation.getId()); | ||
return auth.containsKey(SigV4Trait.ID) && !operation.hasTrait(OptionalAuthTrait.class); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ts/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...rc/test/resources/software/amazon/smithy/aws/traits/errorfiles/http-checksum-trait.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[WARNING] ns.foo#ValidOperation: This shape applies a trait that is unstable: smithy.api#httpChecksum | UnstableTrait | ||
[WARNING] ns.foo#InvalidOperation: This shape applies a trait that is unstable: smithy.api#httpChecksum | UnstableTrait | ||
[WARNING] ns.foo#InvalidOperation2: This shape applies a trait that is unstable: smithy.api#httpChecksum | UnstableTrait | ||
[ERROR] ns.foo#InvalidOperation: For aws protocols, the `response` property of the `httpChecksum` trait only supports `header` as `location`, found "[trailer, header]". | HttpChecksumTrait | ||
[ERROR] ns.foo#InvalidOperation: For operation using sigv4 auth scheme, the `request` property of the `httpChecksum` trait must support `header` checksum location. | HttpChecksumTrait | ||
[ERROR] ns.foo#InvalidOperation2: For aws protocols, the `response` property of the `httpChecksum` trait only supports `header` as `location`, found "[trailer]". | HttpChecksumTrait |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we consider making these a little more explicit, customizable, and future proof by using a map similar to:
Here,
request
andresponse
are maps where each key is the algorithm, and each value is a union of possible locations (i.e., it can be set toheader
ortrailer
, and we could expand toquery
in the future for requests if needed). The value of the union is the binding location name (i.e., header name or trailing header name).This provides more flexibility than the current proposal because each algorithm can use whatever header or query string parameter is necessary. The header/query binding is explicit and does not need prefixes. The names can now support anything, including
Content-MD5
if we ever wanted to support it (we probably don't but just an example).Implementations don't need to perform any prefix logic to determine the header name either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea, have few concerns. Let's sync offline on this.