Skip to content

Commit

Permalink
Add palantir-style eclipse config
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenbaker committed Dec 17, 2024
1 parent cd2c625 commit 1ccf1f6
Show file tree
Hide file tree
Showing 1,663 changed files with 17,491 additions and 26,300 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
/*
* Copyright 2019 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.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.apigateway.openapi;

import java.util.List;
Expand All @@ -35,10 +24,13 @@ public List<ApiGatewayConfig.ApiType> getApiTypes() {

@Override
public OpenApi after(Context<? extends Trait> context, OpenApi openApi) {
return context.getService().getTrait(ApiKeySourceTrait.class)
return context.getService()
.getTrait(ApiKeySourceTrait.class)
.map(trait -> {
LOGGER.fine(() -> String.format(
"Adding %s trait to %s", EXTENSION_NAME, context.getService().getId()));
"Adding %s trait to %s",
EXTENSION_NAME,
context.getService().getId()));
return openApi.toBuilder().putExtension(EXTENSION_NAME, trait.getValue()).build();
})
.orElse(openApi);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
/*
* Copyright 2019 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.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.apigateway.openapi;

import java.util.List;
Expand Down Expand Up @@ -75,8 +64,7 @@ public Map<String, List<String>> updateSecurity(
Context<? extends Trait> context,
Shape shape,
SecuritySchemeConverter<? extends Trait> converter,
Map<String, List<String>> requirement
) {
Map<String, List<String>> requirement) {
// Only modify requirements that exactly match the updated scheme.
if (requirement.size() != 1
|| !requirement.keySet().iterator().next().equals(converter.getOpenApiAuthSchemeName())) {
Expand All @@ -99,8 +87,7 @@ public OperationObject updateOperation(
OperationShape shape,
OperationObject operation,
String httpMethodName,
String path
) {
String path) {
ServiceShape service = context.getService();
AuthorizerIndex authorizerIndex = AuthorizerIndex.of(context.getModel());

Expand Down Expand Up @@ -146,16 +133,16 @@ private boolean usesApiGatewayApiKeys(ServiceShape service, String operationAuth

@Override
public OpenApi after(Context<? extends Trait> context, OpenApi openapi) {
return context.getService().getTrait(AuthorizersTrait.class)
return context.getService()
.getTrait(AuthorizersTrait.class)
.map(authorizers -> addComputedAuthorizers(context, openapi, authorizers))
.orElse(openapi);
}

private OpenApi addComputedAuthorizers(
Context<? extends Trait> context,
OpenApi openApi,
AuthorizersTrait trait
) {
AuthorizersTrait trait) {
OpenApi.Builder builder = openApi.toBuilder();
ComponentsObject.Builder components = openApi.getComponents().toBuilder();

Expand All @@ -180,18 +167,16 @@ private OpenApi addComputedAuthorizers(
private boolean isAuthConverterMatched(
Context<? extends Trait> context,
SecuritySchemeConverter<? extends Trait> converter,
ShapeId scheme
) {
ShapeId scheme) {
return converter.getAuthSchemeId().equals(scheme)
&& context.getService().hasTrait(converter.getAuthSchemeType());
&& context.getService().hasTrait(converter.getAuthSchemeType());
}

private <T extends Trait> SecurityScheme convertAuthScheme(
Context<? extends Trait> context,
SecuritySchemeConverter<T> converter,
AuthorizerDefinition authorizer,
String authorizerName
) {
String authorizerName) {
T authTrait = context.getService().expectTrait(converter.getAuthSchemeType());
SecurityScheme createdScheme = converter.createSecurityScheme(context, authTrait);
SecurityScheme.Builder schemeBuilder = createdScheme.toBuilder();
Expand All @@ -213,14 +198,14 @@ private <T extends Trait> SecurityScheme convertAuthScheme(
.withOptionalMember("authorizerUri", authorizer.getUri().map(Node::from))
.withOptionalMember("authorizerCredentials", authorizer.getCredentials().map(Node::from))
.withOptionalMember("identityValidationExpression",
authorizer.getIdentityValidationExpression().map(Node::from))
authorizer.getIdentityValidationExpression().map(Node::from))
.withOptionalMember("identitySource", authorizer.getIdentitySource().map(Node::from))
.withOptionalMember("authorizerResultTtlInSeconds",
authorizer.getResultTtlInSeconds().map(Node::from))
authorizer.getResultTtlInSeconds().map(Node::from))
.withOptionalMember("authorizerPayloadFormatVersion",
authorizer.getAuthorizerPayloadFormatVersion().map(Node::from))
authorizer.getAuthorizerPayloadFormatVersion().map(Node::from))
.withOptionalMember("enableSimpleResponses",
authorizer.getEnableSimpleResponses().map(Node::from))
authorizer.getEnableSimpleResponses().map(Node::from))
.build();
if (authorizerNode.size() != 0) {
schemeBuilder.putExtension(EXTENSION_NAME, authorizerNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
/*
* Copyright 2019 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.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.apigateway.openapi;

import java.util.List;
Expand Down Expand Up @@ -59,10 +48,11 @@ public OpenApi after(Context<? extends Trait> context, OpenApi openApi) {
if (!binaryTypes.isEmpty()) {
LOGGER.fine(() -> "Adding recognized binary types to model: " + binaryTypes);
return openApi.toBuilder()
.putExtension(EXTENSION_NAME, Stream.concat(binaryTypes.stream(), Stream.of(DEFAULT_BINARY_TYPE))
.distinct()
.map(Node::from)
.collect(ArrayNode.collect()))
.putExtension(EXTENSION_NAME,
Stream.concat(binaryTypes.stream(), Stream.of(DEFAULT_BINARY_TYPE))
.distinct()
.map(Node::from)
.collect(ArrayNode.collect()))
.build();
}

Expand All @@ -75,7 +65,8 @@ private Stream<String> supportedMediaTypes(Context<? extends Trait> context) {
TopDownIndex topDownIndex = TopDownIndex.of(context.getModel());

// Find the media types defined on all request and response bindings.
return topDownIndex.getContainedOperations(context.getService()).stream()
return topDownIndex.getContainedOperations(context.getService())
.stream()
.flatMap(operation -> Stream.concat(
OptionalUtils.stream(
binaryMediaType(model, httpBindingIndex.getRequestBindings(operation))),
Expand All @@ -84,7 +75,8 @@ private Stream<String> supportedMediaTypes(Context<? extends Trait> context) {
}

private Optional<String> binaryMediaType(Model model, Map<String, HttpBinding> httpBindings) {
return httpBindings.values().stream()
return httpBindings.values()
.stream()
.filter(binding -> binding.getLocation().equals(HttpBinding.Location.PAYLOAD))
.map(HttpBinding::getMember)
.flatMap(member -> OptionalUtils.stream(member.getMemberTrait(model, MediaTypeTrait.class)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
/*
* Copyright 2019 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.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.apigateway.openapi;

import java.util.ArrayList;
Expand Down Expand Up @@ -82,13 +71,17 @@ public List<ApiGatewayConfig.ApiType> getApiTypes() {

@Override
public PathItem updatePathItem(Context<? extends Trait> context, String path, PathItem pathItem) {
return context.getService().getTrait(CorsTrait.class)
return context.getService()
.getTrait(CorsTrait.class)
.map(corsTrait -> addPreflightIntegration(context, path, pathItem, corsTrait))
.orElse(pathItem);
}

private static PathItem addPreflightIntegration(
Context<? extends Trait> context, String path, PathItem pathItem, CorsTrait corsTrait) {
Context<? extends Trait> context,
String path,
PathItem pathItem,
CorsTrait corsTrait) {
// Filter out any path for which an OPTIONS handler has already been defined
if (pathItem.getOptions().isPresent()) {
LOGGER.fine(() -> path + " already defines an OPTIONS request, so no need to generate CORS-preflight");
Expand All @@ -103,7 +96,10 @@ private static PathItem addPreflightIntegration(
}

private static <T extends Trait> Map<CorsHeader, String> deduceCorsHeaders(
Context<T> context, String path, PathItem pathItem, CorsTrait corsTrait) {
Context<T> context,
String path,
PathItem pathItem,
CorsTrait corsTrait) {
Map<CorsHeader, String> corsHeaders = new HashMap<>();
corsHeaders.put(CorsHeader.MAX_AGE, String.valueOf(corsTrait.getMaxAge()));
corsHeaders.put(CorsHeader.ALLOW_ORIGIN, corsTrait.getOrigin());
Expand All @@ -121,7 +117,8 @@ private static <T extends Trait> Map<CorsHeader, String> deduceCorsHeaders(
headerNames.addAll(corsTrait.getAdditionalAllowedHeaders());

// Sets additional allowed headers from the API Gateway config.
Set<String> additionalAllowedHeaders = context.getConfig().getExtensions(ApiGatewayConfig.class)
Set<String> additionalAllowedHeaders = context.getConfig()
.getExtensions(ApiGatewayConfig.class)
.getAdditionalAllowedCorsHeadersSet();
headerNames.addAll(additionalAllowedHeaders);
headerNames.addAll(findAllHeaders(path, pathItem));
Expand All @@ -134,7 +131,8 @@ private static <T extends Trait> Map<CorsHeader, String> deduceCorsHeaders(
// Add protocol headers.
Model model = context.getModel();
TopDownIndex topDownIndex = TopDownIndex.of(model);
Map<String, OperationShape> operations = topDownIndex.getContainedOperations(context.getService()).stream()
Map<String, OperationShape> operations = topDownIndex.getContainedOperations(context.getService())
.stream()
.collect(Collectors.toMap(o -> o.getId().getName(), o -> o));
for (OperationObject operationObject : pathItem.getOperations().values()) {
if (operationObject.getOperationId().isPresent()) {
Expand All @@ -144,27 +142,32 @@ private static <T extends Trait> Map<CorsHeader, String> deduceCorsHeaders(
}

LOGGER.fine(() -> String.format(
"Adding the following %s headers to `%s`: %s", CorsHeader.ALLOW_HEADERS, path, headerNames));
"Adding the following %s headers to `%s`: %s",
CorsHeader.ALLOW_HEADERS,
path,
headerNames));
corsHeaders.put(CorsHeader.ALLOW_HEADERS, String.join(",", headerNames));

return corsHeaders;
}

private static <T extends Trait> Set<String> getSecuritySchemeRequestHeaders(
Context<? extends Trait> context,
SecuritySchemeConverter<T> converter
) {
SecuritySchemeConverter<T> converter) {
T t = context.getService().expectTrait(converter.getAuthSchemeType());
return converter.getAuthRequestHeaders(context, t);
}

private static Collection<String> findAllHeaders(String path, PathItem pathItem) {
// Get all "in" = "header" parameters and gather up their "name" properties.
return pathItem.getOperations().values().stream()
return pathItem.getOperations()
.values()
.stream()
.flatMap(operationObject -> operationObject.getParameters().stream())
.filter(parameter -> parameter.getIn().filter(in -> in.equals("header")).isPresent())
.map(parameter -> parameter.getName().orElseThrow(() -> new OpenApiException(
"OpenAPI header parameter is missing a name in " + path)))
.map(parameter -> parameter.getName()
.orElseThrow(() -> new OpenApiException(
"OpenAPI header parameter is missing a name in " + path)))
.collect(Collectors.toList());
}

Expand All @@ -173,7 +176,10 @@ private static String getAllowMethods(PathItem item) {
}

private static OperationObject createPreflightOperation(
Context<? extends Trait> context, String path, PathItem pathItem, Map<CorsHeader, String> headers) {
Context<? extends Trait> context,
String path,
PathItem pathItem,
Map<CorsHeader, String> headers) {
return OperationObject.builder()
.tags(ListUtils.of("CORS"))
.security(Collections.emptyList())
Expand Down Expand Up @@ -218,7 +224,9 @@ private static ResponseObject createPreflightResponse(Map<CorsHeader, String> he
}

private static ObjectNode createPreflightIntegration(
Context<? extends Trait> context, Map<CorsHeader, String> headers, PathItem pathItem) {
Context<? extends Trait> context,
Map<CorsHeader, String> headers,
PathItem pathItem) {
IntegrationResponse.Builder responseBuilder = IntegrationResponse.builder().statusCode("200");

// Add each CORS header to the mock integration response.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
/*
* Copyright 2020 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.
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.apigateway.openapi;

import java.util.ArrayList;
Expand Down Expand Up @@ -55,9 +44,9 @@ public OperationObject postProcessOperation(
OperationShape shape,
OperationObject operation,
String method,
String path
) {
return context.getService().getTrait(CorsTrait.class)
String path) {
return context.getService()
.getTrait(CorsTrait.class)
.map(trait -> addCorsHeadersToResponses(context, shape, operation, method, trait))
.orElse(operation);
}
Expand All @@ -67,13 +56,17 @@ private OperationObject addCorsHeadersToResponses(
OperationShape shape,
OperationObject operationObject,
String method,
CorsTrait trait
) {
CorsTrait trait) {
OperationObject.Builder builder = operationObject.toBuilder();

for (Map.Entry<String, ResponseObject> entry : operationObject.getResponses().entrySet()) {
ResponseObject updated = createUpdatedResponseWithCorsHeaders(
context, shape, operationObject, method, trait, entry.getValue());
context,
shape,
operationObject,
method,
trait,
entry.getValue());
builder.putResponse(entry.getKey(), updated);
}

Expand All @@ -86,8 +79,7 @@ private ResponseObject createUpdatedResponseWithCorsHeaders(
OperationObject operationObject,
String method,
CorsTrait trait,
ResponseObject response
) {
ResponseObject response) {
// Determine which headers have been added to the response.
List<String> headers = new ArrayList<>();

Expand Down
Loading

0 comments on commit 1ccf1f6

Please sign in to comment.