Skip to content

Commit 7bf6e00

Browse files
authored
Add validator for services with no auth trait (#1929)
Adds a validator that warns when there's a service shape that has multiple authDefinition traits applied, but no auth trait. This encourages the use of the auth trait so auth scheme priority is being modeled explicitly. It also allows for tooling to look for and use this validation event. Documentation was also updated to explain that without the auth trait, auth scheme ordering is alphabetical. A test for the auth trait (auth-trait-must-target-service-schemes) was updated to avoid getting the warning message from the new validator.
1 parent 6855a8f commit 7bf6e00

File tree

8 files changed

+99
-1
lines changed

8 files changed

+99
-1
lines changed

docs/source-2.0/spec/authentication-traits.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ the ``auth`` trait, then the operation is expected to support each of the
276276
service. Each entry in the ``auth`` trait is a shape ID that MUST refer to an
277277
authentication scheme trait applied to the service in which it is bound.
278278

279+
.. note::
280+
When a service has multiple authentication scheme traits applied and no
281+
``auth`` trait, the ordering of authentication schemes is alphabetical
282+
based on the absolute shape ID of each authentication scheme trait.
283+
279284
The following example defines all combinations in which ``auth`` can be applied
280285
to services and operations:
281286

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2023 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.smithy.model.validation.validators;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.stream.Collectors;
21+
import software.amazon.smithy.model.Model;
22+
import software.amazon.smithy.model.knowledge.ServiceIndex;
23+
import software.amazon.smithy.model.shapes.ServiceShape;
24+
import software.amazon.smithy.model.traits.AuthTrait;
25+
import software.amazon.smithy.model.validation.AbstractValidator;
26+
import software.amazon.smithy.model.validation.ValidationEvent;
27+
28+
/**
29+
* Validates the @authDefinition traits applied to service shapes.
30+
*/
31+
public class ServiceAuthDefinitionsValidator extends AbstractValidator {
32+
33+
@Override
34+
public List<ValidationEvent> validate(Model model) {
35+
List<ValidationEvent> events = new ArrayList<>();
36+
ServiceIndex index = ServiceIndex.of(model);
37+
38+
List<ServiceShape> services = model.getServiceShapes().stream()
39+
.filter(serviceShape -> !serviceShape.hasTrait(AuthTrait.ID))
40+
.filter(serviceShape -> index.getAuthSchemes(serviceShape).size() > 1)
41+
.collect(Collectors.toList());
42+
43+
for (ServiceShape service : services) {
44+
events.add(warning(service, "This service uses multiple authentication schemes but does not have "
45+
+ "the `@auth` trait applied. The `@auth` trait defines a priority ordering "
46+
+ "of auth schemes for a client to use. Without it, the ordering of auth "
47+
+ "schemes is alphabetical based on the absolute shape ID of the auth "
48+
+ "schemes."));
49+
50+
}
51+
return events;
52+
}
53+
}

smithy-model/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ software.amazon.smithy.model.validation.validators.ResourceIdentifierBindingVali
3636
software.amazon.smithy.model.validation.validators.ResourceIdentifierValidator
3737
software.amazon.smithy.model.validation.validators.ResourceLifecycleValidator
3838
software.amazon.smithy.model.validation.validators.ResourceOperationInputOutputValidator
39+
software.amazon.smithy.model.validation.validators.ServiceAuthDefinitionsValidator
3940
software.amazon.smithy.model.validation.validators.ServiceValidator
4041
software.amazon.smithy.model.validation.validators.SetValidator
4142
software.amazon.smithy.model.validation.validators.ShapeIdConflictValidator

smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/auth/auth-trait-must-target-service-schemes.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
],
1111
"traits": {
1212
"smithy.api#httpBasicAuth": {},
13-
"smithy.api#httpDigestAuth": {}
13+
"smithy.api#httpDigestAuth": {},
14+
"smithy.api#auth": [
15+
"smithy.api#httpDigestAuth",
16+
"smithy.api#httpBasicAuth"
17+
]
1418
}
1519
},
1620
"ns.foo#ValidOperation": {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[WARNING] smithy.example#FooService: This service uses multiple authentication schemes but does not have the `@auth` trait applied. The `@auth` trait defines a priority ordering of auth schemes for a client to use. Without it, the ordering of auth schemes is alphabetical based on the absolute shape ID of the auth schemes. | ServiceAuthDefinitions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
$version: "2"
2+
3+
namespace smithy.example
4+
5+
// Service shape with multiple auth schemes and no auth trait should cause warning
6+
@httpBasicAuth
7+
@httpDigestAuth
8+
@httpBearerAuth
9+
service FooService {
10+
version: "2023-08-15"
11+
operations: [GetFoo]
12+
}
13+
14+
operation GetFoo {
15+
output: GetFooOutput
16+
}
17+
18+
structure GetFooOutput {}

smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/service-auth-definitions/one-auth-scheme.errors

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
$version: "2"
2+
3+
namespace smithy.example
4+
5+
// Service shape with one auth schemes and no auth trait should NOT cause warning
6+
@httpBasicAuth
7+
service FooService {
8+
version: "2023-08-15"
9+
operations: [GetFoo]
10+
}
11+
12+
operation GetFoo {
13+
output: GetFooOutput
14+
}
15+
16+
structure GetFooOutput {}

0 commit comments

Comments
 (0)