Skip to content
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

Add functionality for non-SSL endpoints for backwards compatibility #2176

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 2023 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.go.codegen;

import java.util.ArrayList;
import java.util.List;

import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoDelegator;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.GoStackStepMiddlewareGenerator;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.MiddlewareIdentifier;
import software.amazon.smithy.go.codegen.SmithyGoDependency;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.endpoints.EndpointMiddlewareGenerator;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar;
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ToShapeId;
import software.amazon.smithy.utils.ListUtils;
isaiahvita marked this conversation as resolved.
Show resolved Hide resolved


/*
* Adds support for non-SSL endpoints during endpoint resolution.
* The new Rules Engine endpoint resolution doesnt support non-SSL endpoints.
* So this middleware exists for backwards compatibility with legacy
* endpoint resolution. It is operation specific because it is being inserted
* directly after the operation-specific endpoint resolution middleware.
*/
public class EndpointDisableHttps implements GoIntegration {

private final List<RuntimeClientPlugin> runtimeClientPlugins = new ArrayList<>();

public static String getAddMiddlewareFuncName(String operationName) {
return String.format("add%sEndpointDisableHTTPSMiddleware", operationName);
}


public static String getMiddlewareObjectName(String operationName) {
return String.format("op%sEndpointDisableHTTPSMiddleware", operationName);
}

/**
* Gets the sort order of the customization from -128 to 127, with lowest
* executed first. Needs to execute after Rules Engine endpoint
* resolution middleware insertion.
*
* @return Returns the sort order, defaults to 127.
*/
@Override
public byte getOrder() {
return 127;
}

@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return runtimeClientPlugins;
}

@Override
public void processFinalizedModel(GoSettings settings, Model model) {

TopDownIndex topDownIndex = TopDownIndex.of(model);
var serviceShape = settings.getService(model);

for (ToShapeId operation : topDownIndex.getContainedOperations(serviceShape)) {
OperationShape operationShape = model.expectShape(operation.toShapeId(), OperationShape.class);
String operationName = operationShape.getId().getName();

runtimeClientPlugins.add(RuntimeClientPlugin.builder()
.servicePredicate((m, s) -> s.equals(serviceShape))
.operationPredicate((m, s, o) -> o.equals(operationShape))
.registerMiddleware(MiddlewareRegistrar.builder()
.resolvedFunction(SymbolUtils.createValueSymbolBuilder(getAddMiddlewareFuncName(operationName))
.build())
.useClientOptions()
.build())
.build());

}
}

@Override
public void writeAdditionalFiles(
GoSettings settings,
Model model,
SymbolProvider symbolProvider,
GoDelegator goDelegator
) {

TopDownIndex topDownIndex = TopDownIndex.of(model);
var serviceShape = settings.getService(model);



for (ToShapeId operation : topDownIndex.getContainedOperations(serviceShape)) {
OperationShape operationShape = model.expectShape(operation.toShapeId(), OperationShape.class);

goDelegator.useShapeWriter(operationShape, writer -> {
Symbol operationSymbol = symbolProvider.toSymbol(operationShape);
String operationName = operationSymbol.getName();


GoStackStepMiddlewareGenerator middleware = GoStackStepMiddlewareGenerator.createSerializeStepMiddleware(
getMiddlewareObjectName(operationName), MiddlewareIdentifier.string(getMiddlewareObjectName(operationName)));
middleware.writeMiddleware(writer, this::generateMiddlewareResolverBody,
this::generateMiddlewareStructureMembers);


writer.write(
"""
func $L(stack $P, o Options) error {
return stack.Serialize.Insert(&$L{
EndpointDisableHTTPS: o.EndpointOptions.DisableHTTPS,
}, \"$L\", middleware.After)
}
""",
getAddMiddlewareFuncName(operationName),
SymbolUtils.createPointableSymbolBuilder("Stack", SmithyGoDependency.SMITHY_MIDDLEWARE).build(),
getMiddlewareObjectName(operationName),
EndpointMiddlewareGenerator.getMiddlewareObjectName(operationName)
);
writer.write("");


});
}
}


private void generateMiddlewareResolverBody(GoStackStepMiddlewareGenerator g, GoWriter writer) {
writer.write(
"""
req, ok := in.Request.($P)
if !ok {
return out, metadata, $T(\"unknown transport type %T\", in.Request)
}

if m.EndpointDisableHTTPS {
req.URL.Scheme = \"http\"
}

return next.HandleSerialize(ctx, in)
""",
SymbolUtils.createPointableSymbolBuilder("Request", SmithyGoDependency.SMITHY_HTTP_TRANSPORT).build(),
SymbolUtils.createValueSymbolBuilder("Errorf", SmithyGoDependency.FMT).build()
);
}

private void generateMiddlewareStructureMembers(GoStackStepMiddlewareGenerator g, GoWriter writer) {
writer.write("EndpointDisableHTTPS $L", "bool");
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ software.amazon.smithy.aws.go.codegen.customization.S3ControlHostPrefixAccountId
software.amazon.smithy.aws.go.codegen.customization.S3HttpPathBucketFilterIntegration
software.amazon.smithy.aws.go.codegen.customization.S3HostPrefixRequestRouteFilterIntegration
software.amazon.smithy.aws.go.codegen.customization.S3HttpLabelBucketFilterIntegration
software.amazon.smithy.aws.go.codegen.AwsEndpointAuthSchemeGenerator
software.amazon.smithy.aws.go.codegen.AwsEndpointAuthSchemeGenerator
software.amazon.smithy.aws.go.codegen.EndpointDisableHttps
32 changes: 32 additions & 0 deletions service/s3/api_op_AbortMultipartUpload.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions service/s3/api_op_CompleteMultipartUpload.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions service/s3/api_op_CopyObject.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions service/s3/api_op_CreateBucket.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading