Skip to content

Commit

Permalink
fix plumbing for event streams and endpoint discovery (#2362)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws authored Nov 10, 2023
1 parent d0b37b2 commit 096c09e
Show file tree
Hide file tree
Showing 92 changed files with 477 additions and 334 deletions.
2 changes: 1 addition & 1 deletion aws/signer/v4/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ type streamingEventsPayload struct{}

// AddStreamingEventsPayload adds the streamingEventsPayload middleware to the stack.
func AddStreamingEventsPayload(stack *middleware.Stack) error {
return stack.Finalize.Add(&streamingEventsPayload{}, middleware.After)
return stack.Finalize.Add(&streamingEventsPayload{}, middleware.Before)
}

func (s *streamingEventsPayload) ID() string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import software.amazon.smithy.go.codegen.GoEventStreamIndex;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.GoStackStepMiddlewareGenerator;
import software.amazon.smithy.go.codegen.GoStdlibTypes;
import software.amazon.smithy.go.codegen.GoValueAccessUtils;
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.SmithyGoTypes;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator.GenerationContext;
import software.amazon.smithy.go.codegen.knowledge.GoPointableIndex;
Expand All @@ -42,6 +44,7 @@
import software.amazon.smithy.model.traits.EventHeaderTrait;
import software.amazon.smithy.model.traits.EventPayloadTrait;
import software.amazon.smithy.model.traits.HttpTrait;
import software.amazon.smithy.utils.MapUtils;
import software.amazon.smithy.utils.StringUtils;

public final class AwsEventStreamUtils {
Expand Down Expand Up @@ -306,26 +309,46 @@ defer func() {
""", getEventStreamApiSymbol("ApplyHTTPTransportFixes"))
.write("");

w.write("""
requestSignature, err := $T(request.Request)
if err != nil {
return out, metadata, $T("failed to get event stream seed signature: %v", err)
w.writeGoTemplate("""
requestSignature, err := $getSignature:T(request.Request)
if err != nil {
return out, metadata, $errorf:T("failed to get event stream seed signature: %v", err)
}
identity := getIdentity(ctx)
if identity == nil {
return out, metadata, $errorf:T("no identity")
}
creds, ok := identity.($credentialsAdapter:P)
if !ok {
return out, metadata, $errorf:T("identity is not sigv4 credentials")
}
rscheme := getResolvedAuthScheme(ctx)
if rscheme == nil {
return out, metadata, $errorf:T("no resolved auth scheme")
}
name, ok := $getSigningName:T(&rscheme.SignerProperties)
if !ok {
return out, metadata, $errorf:T("no sigv4 signing name")
}
""", getSignedRequestSignature, errorf).write("")
.openBlock("signer := $T(", ")", getSymbol("NewStreamSigner",
AwsGoDependency.AWS_SIGNER_V4, false), () -> w
.write("""
$T(ctx),
$T(ctx),
$T(ctx),
requestSignature,
""", getSymbol("GetSigningCredentials",
AwsGoDependency.AWS_MIDDLEWARE, false),
getSymbol("GetSigningName",
AwsGoDependency.AWS_MIDDLEWARE, false),
getSymbol("GetSigningRegion",
AwsGoDependency.AWS_MIDDLEWARE, false)
)).write("");
region, ok := $getSigningRegion:T(&rscheme.SignerProperties)
if !ok {
return out, metadata, $errorf:T("no sigv4 signing region")
}
signer := v4.NewStreamSigner(creds.Credentials, name, region, requestSignature)
""",
MapUtils.of(
"getSignature", getSignedRequestSignature,
"errorf", GoStdlibTypes.Fmt.Errorf,
"credentialsAdapter", SdkGoTypes.Internal.Auth.Smithy.CredentialsAdapter,
"getSigningName", SmithyGoTypes.Transport.Http.GetSigV4SigningName,
"getSigningRegion", SmithyGoTypes.Transport.Http.GetSigV4SigningRegion
));

var events = inputInfo.get().getEventStreamTarget().asUnionShape()
.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import software.amazon.smithy.go.codegen.GoWriter;
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.ClientMember;
import software.amazon.smithy.go.codegen.integration.ClientMemberResolver;
import software.amazon.smithy.go.codegen.integration.ConfigField;
Expand Down Expand Up @@ -169,9 +170,9 @@ private static void generateAddDiscoverEndpointMiddleware(
writer.addUseImports(SmithyGoDependency.SMITHY_MIDDLEWARE);

String closeBlock = String.format("}, \"%s\", middleware.After)",
EndpointGenerator.MIDDLEWARE_NAME);
EndpointMiddlewareGenerator.MIDDLEWARE_ID);

writer.openBlock("return stack.Serialize.Insert(&$T{", closeBlock, DISCOVERY_MIDDLEWARE, () -> {
writer.openBlock("return stack.Finalize.Insert(&$T{", closeBlock, DISCOVERY_MIDDLEWARE, () -> {
writer.openBlock("Options: []func($P){", "},", DISCOVERY_ENDPOINT_OPTIONS, () -> {
writer.openBlock("func (opt $P) {", "},", DISCOVERY_ENDPOINT_OPTIONS, () -> {
writer.write("opt.$L = o.EndpointOptions.$L", DISABLE_HTTPS, DISABLE_HTTPS);
Expand Down Expand Up @@ -328,9 +329,10 @@ private static void generateFetchDiscoveredEndpointFunction(

writer.addUseImports(SmithyGoDependency.CONTEXT);
writer.openBlock(
"func (c *Client) $L(ctx context.Context, input interface{}, optFns ...func($P)) ($T, error) {", "}",
"func (c *Client) $L(ctx context.Context, optFns ...func($P)) ($T, error) {", "}",
fetchDiscoveredEndpointFuncName, DISCOVERY_ENDPOINT_OPTIONS, DISCOVERY_ENDPOINT_WEIGHTED_ADDRESS,
() -> {
writer.write("input := getOperationInput(ctx)");
writer.write("in, ok := input.($P)", symbolProvider.toSymbol(inputShape));
writer.openBlock("if !ok {", "}", () -> {
writer.addUseImports(SmithyGoDependency.FMT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.smithy.aws.go.codegen.customization.auth;

import software.amazon.smithy.aws.go.codegen.SdkGoTypes;
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoDelegator;
import software.amazon.smithy.go.codegen.GoSettings;
Expand All @@ -24,6 +25,7 @@
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.utils.ListUtils;

import java.util.List;
Expand All @@ -42,10 +44,15 @@ public class IgnoreAnonymousCredentials implements GoIntegration {
.resolver(buildPackageSymbol("ignoreAnonymousAuth"))
.build();

private static boolean hasCredentials(Model model, ServiceShape service) {
return service.hasTrait(SigV4Trait.class) || service.hasTrait(SigV4Trait.class);
}

@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return ListUtils.of(
RuntimeClientPlugin.builder()
.servicePredicate(IgnoreAnonymousCredentials::hasCredentials)
.addConfigFieldResolver(IGNORE_ANONYMOUS_AUTH)
.build()
);
Expand All @@ -55,7 +62,9 @@ public List<RuntimeClientPlugin> getClientPlugins() {
public void writeAdditionalFiles(
GoSettings settings, Model model, SymbolProvider symbolProvider, GoDelegator goDelegator
) {
goDelegator.useFileWriter("options.go", settings.getModuleName(), generateResolver());
if (hasCredentials(model, settings.getService(model))) {
goDelegator.useFileWriter("options.go", settings.getModuleName(), generateResolver());
}
}

private GoWriter.Writable generateResolver() {
Expand Down
2 changes: 0 additions & 2 deletions internal/protocoltest/awsrestjson/api_client.go

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

6 changes: 0 additions & 6 deletions internal/protocoltest/awsrestjson/options.go

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

2 changes: 0 additions & 2 deletions internal/protocoltest/ec2query/api_client.go

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

6 changes: 0 additions & 6 deletions internal/protocoltest/ec2query/options.go

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

2 changes: 0 additions & 2 deletions internal/protocoltest/jsonrpc10/api_client.go

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

6 changes: 0 additions & 6 deletions internal/protocoltest/jsonrpc10/options.go

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

2 changes: 0 additions & 2 deletions internal/protocoltest/query/api_client.go

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

6 changes: 0 additions & 6 deletions internal/protocoltest/query/options.go

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

2 changes: 0 additions & 2 deletions internal/protocoltest/restxml/api_client.go

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

6 changes: 0 additions & 6 deletions internal/protocoltest/restxml/options.go

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

2 changes: 0 additions & 2 deletions internal/protocoltest/restxmlwithnamespace/api_client.go

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

6 changes: 0 additions & 6 deletions internal/protocoltest/restxmlwithnamespace/options.go

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

7 changes: 7 additions & 0 deletions service/apigateway/internal/customizations/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Test_EmptyResponse(t *testing.T) {
Retryer: func() aws.Retryer {
return aws.NopRetryer{}
},
Credentials: &fakeCredentials{},
}

client := apigateway.NewFromConfig(cfg)
Expand All @@ -79,3 +80,9 @@ func Test_EmptyResponse(t *testing.T) {
})
}
}

type fakeCredentials struct{}

func (*fakeCredentials) Retrieve(_ context.Context) (aws.Credentials, error) {
return aws.Credentials{}, nil
}
2 changes: 0 additions & 2 deletions service/codecatalyst/api_client.go

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

6 changes: 0 additions & 6 deletions service/codecatalyst/options.go

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

7 changes: 4 additions & 3 deletions service/dynamodb/api_op_BatchGetItem.go

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

7 changes: 4 additions & 3 deletions service/dynamodb/api_op_BatchWriteItem.go

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

7 changes: 4 additions & 3 deletions service/dynamodb/api_op_CreateBackup.go

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

Loading

0 comments on commit 096c09e

Please sign in to comment.