|
| 1 | +/* |
| 2 | + * Copyright 2020 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.aws.go.codegen; |
| 17 | + |
| 18 | +import software.amazon.smithy.aws.go.codegen.customization.AwsCustomGoDependency; |
| 19 | +import software.amazon.smithy.go.codegen.GoDependency; |
| 20 | +import software.amazon.smithy.go.codegen.GoWriter; |
| 21 | +import software.amazon.smithy.go.codegen.SmithyGoDependency; |
| 22 | +import software.amazon.smithy.go.codegen.SymbolUtils; |
| 23 | +import software.amazon.smithy.model.Model; |
| 24 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 25 | + |
| 26 | +/** |
| 27 | + * Generates Client Configuration, Middleware, and Config Resolvers for AWS Signature Version 4a support. |
| 28 | + */ |
| 29 | +public final class AwsSignatureVersion4aUtils { |
| 30 | + public static final String RESOLVE_CREDENTIAL_PROVIDER = "resolveCredentialProvider"; |
| 31 | + public static final String REGISTER_MIDDLEWARE_FUNCTION = "swapWithCustomHTTPSignerMiddleware"; |
| 32 | + public static final String V4A_SIGNER_INTERFACE_NAME = "httpSignerV4a"; |
| 33 | + public static final String SIGNER_OPTION_FIELD_NAME = V4A_SIGNER_INTERFACE_NAME; |
| 34 | + public static final String NEW_SIGNER_FUNC_NAME = "newDefaultV4aSigner"; |
| 35 | + public static final String SIGNER_RESOLVER = "resolveHTTPSignerV4a"; |
| 36 | + |
| 37 | + public static void writeCredentialProviderResolver(GoWriter writer) { |
| 38 | + writer.pushState(); |
| 39 | + writer.putContext("resolverName", RESOLVE_CREDENTIAL_PROVIDER); |
| 40 | + writer.putContext("fieldName", AddAwsConfigFields.CREDENTIALS_CONFIG_NAME); |
| 41 | + writer.putContext("credType", SymbolUtils.createPointableSymbolBuilder("CredentialsProvider", |
| 42 | + AwsCustomGoDependency.INTERNAL_SIGV4A).build()); |
| 43 | + writer.putContext("anonType", SymbolUtils.createPointableSymbolBuilder("AnonymousCredentials", |
| 44 | + AwsCustomGoDependency.AWS_CORE).build()); |
| 45 | + writer.putContext("adapType", SymbolUtils.createPointableSymbolBuilder("SymmetricCredentialAdaptor", |
| 46 | + AwsCustomGoDependency.INTERNAL_SIGV4A).build()); |
| 47 | + writer.write(""" |
| 48 | + func $resolverName:L(o *Options) { |
| 49 | + if o.$fieldName:L == nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if _, ok := o.$fieldName:L.($credType:T); ok { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + switch o.$fieldName:L.(type) { |
| 58 | + case $anonType:T, $anonType:P: |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + o.$fieldName:L = &$adapType:T{SymmetricProvider: o.$fieldName:L} |
| 63 | + } |
| 64 | + """); |
| 65 | + writer.popState(); |
| 66 | + } |
| 67 | + |
| 68 | + public static void writerSignerInterface(GoWriter writer) { |
| 69 | + writer.pushState(); |
| 70 | + writer.putContext("ifaceName", V4A_SIGNER_INTERFACE_NAME); |
| 71 | + writer.putContext("contextType", SymbolUtils.createValueSymbolBuilder("Context", |
| 72 | + SmithyGoDependency.CONTEXT).build()); |
| 73 | + writer.putContext("credType", SymbolUtils.createValueSymbolBuilder("Credentials", |
| 74 | + AwsGoDependency.INTERNAL_SIGV4A).build()); |
| 75 | + writer.putContext("reqType", SymbolUtils.createPointableSymbolBuilder("Request", |
| 76 | + SmithyGoDependency.NET_HTTP).build()); |
| 77 | + writer.putContext("timeType", SymbolUtils.createPointableSymbolBuilder("Time", |
| 78 | + SmithyGoDependency.TIME).build()); |
| 79 | + writer.putContext("optionsType", SymbolUtils.createPointableSymbolBuilder("SignerOptions", |
| 80 | + AwsGoDependency.INTERNAL_SIGV4A).build()); |
| 81 | + writer.write(""" |
| 82 | + type $ifaceName:L interface { |
| 83 | + SignHTTP(ctx $contextType:T, credentials $credType:T, r $reqType:P, payloadHash, |
| 84 | + service string, regionSet []string, signingTime $timeType:T, |
| 85 | + optFns ...func($optionsType:P)) error |
| 86 | + } |
| 87 | + """); |
| 88 | + writer.popState(); |
| 89 | + } |
| 90 | + |
| 91 | + public static void writerConfigFieldResolver(GoWriter writer, ServiceShape serviceShape) { |
| 92 | + writer.pushState(); |
| 93 | + writer.putContext("resolverName", SIGNER_RESOLVER); |
| 94 | + writer.putContext("optionName", SIGNER_OPTION_FIELD_NAME); |
| 95 | + writer.putContext("newSigner", NEW_SIGNER_FUNC_NAME); |
| 96 | + writer.write(""" |
| 97 | + func $resolverName:L(o *Options) { |
| 98 | + if o.$optionName:L != nil { |
| 99 | + return |
| 100 | + } |
| 101 | + o.$optionName:L = $newSigner:L(*o) |
| 102 | + } |
| 103 | + """); |
| 104 | + writer.popState(); |
| 105 | + } |
| 106 | + |
| 107 | + public static void writeNewV4ASignerFunc(GoWriter writer, ServiceShape serviceShape) { |
| 108 | + writeNewV4ASignerFunc(writer, serviceShape, false); |
| 109 | + } |
| 110 | + |
| 111 | + public static void writeNewV4ASignerFunc( |
| 112 | + GoWriter writer, |
| 113 | + ServiceShape serviceShape, |
| 114 | + boolean disableURIPathEscaping |
| 115 | + ) { |
| 116 | + writer.pushState(); |
| 117 | + writer.putContext("funcName", NEW_SIGNER_FUNC_NAME); |
| 118 | + writer.putContext("signerType", SymbolUtils.createPointableSymbolBuilder("Signer", |
| 119 | + AwsCustomGoDependency.INTERNAL_SIGV4A).build()); |
| 120 | + writer.putContext("newSigner", SymbolUtils.createValueSymbolBuilder("NewSigner", |
| 121 | + AwsCustomGoDependency.INTERNAL_SIGV4A).build()); |
| 122 | + writer.putContext("signerOptions", SymbolUtils.createPointableSymbolBuilder("SignerOptions", |
| 123 | + AwsCustomGoDependency.INTERNAL_SIGV4A).build()); |
| 124 | + writer.putContext("loggerField", AddAwsConfigFields.LOGGER_CONFIG_NAME); |
| 125 | + writer.putContext("modeField", AddAwsConfigFields.LOG_MODE_CONFIG_NAME); |
| 126 | + writer.putContext("disableEscape", disableURIPathEscaping); |
| 127 | + writer.write(""" |
| 128 | + func $funcName:L(o Options) $signerType:P { |
| 129 | + return $newSigner:T(func(so $signerOptions:P){ |
| 130 | + so.Logger = o.$loggerField:L |
| 131 | + so.LogSigning = o.$modeField:L.IsSigning() |
| 132 | + so.DisableURIPathEscaping = $disableEscape:L |
| 133 | + }) |
| 134 | + } |
| 135 | + """); |
| 136 | + writer.popState(); |
| 137 | + } |
| 138 | + |
| 139 | + public static void writeMiddlewareRegister( |
| 140 | + Model model, |
| 141 | + GoWriter writer, |
| 142 | + ServiceShape serviceShape, |
| 143 | + GoDependency signerMiddleware |
| 144 | + ) { |
| 145 | + writer.pushState(); |
| 146 | + writer.putContext("funcName", REGISTER_MIDDLEWARE_FUNCTION); |
| 147 | + writer.putContext("stackType", SymbolUtils.createPointableSymbolBuilder("Stack", |
| 148 | + SmithyGoDependency.SMITHY_MIDDLEWARE).build()); |
| 149 | + writer.putContext("newMiddleware", SymbolUtils.createValueSymbolBuilder( |
| 150 | + "NewSignHTTPRequestMiddleware", signerMiddleware).build()); |
| 151 | + writer.putContext("middleOptions", SymbolUtils.createValueSymbolBuilder( |
| 152 | + "SignHTTPRequestMiddlewareOptions", signerMiddleware).build()); |
| 153 | + writer.putContext("registerMiddleware", SymbolUtils.createValueSymbolBuilder( |
| 154 | + "RegisterSigningMiddleware", signerMiddleware).build()); |
| 155 | + writer.putContext("credFileName", AddAwsConfigFields.CREDENTIALS_CONFIG_NAME); |
| 156 | + writer.putContext("v4Signer", AwsSignatureVersion4.SIGNER_CONFIG_FIELD_NAME); |
| 157 | + writer.putContext("v4aSigner", SIGNER_OPTION_FIELD_NAME); |
| 158 | + writer.putContext("logMode", AddAwsConfigFields.LOG_MODE_CONFIG_NAME); |
| 159 | + writer.write(""" |
| 160 | + func $funcName:L(stack $stackType:P, o Options) error { |
| 161 | + mw := $newMiddleware:T($middleOptions:T{ |
| 162 | + CredentialsProvider: o.$credFileName:L, |
| 163 | + V4Signer: o.$v4Signer:L, |
| 164 | + V4aSigner: o.$v4aSigner:L, |
| 165 | + LogSigning: o.$logMode:L.IsSigning(), |
| 166 | + }) |
| 167 | + |
| 168 | + return $registerMiddleware:T(stack, mw) |
| 169 | + } |
| 170 | + """); |
| 171 | + writer.popState(); |
| 172 | + } |
| 173 | +} |
0 commit comments