From 966b87e66c5129dcdcf727edd4ba99dfde956912 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 2 Sep 2020 15:28:22 +0800 Subject: [PATCH] add aws v4 signature support to go-experimental (#7326) --- .../resources/go-experimental/client.mustache | 23 +++++++++++++++++++ .../go-experimental/configuration.mustache | 14 +++++++++++ .../resources/go-experimental/go.mod.mustache | 3 +++ .../src/main/resources/go/go.mod.mustache | 4 +++- .../go-experimental/go-petstore/client.go | 1 + .../petstore/go/go-petstore-withXml/go.mod | 1 - samples/client/petstore/go/go-petstore/go.mod | 1 - .../x-auth-id-alias/go-experimental/client.go | 1 + .../go-experimental/go-petstore/client.go | 1 + .../client/petstore/go/go-petstore/go.mod | 1 - 10 files changed, 46 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-experimental/client.mustache b/modules/openapi-generator/src/main/resources/go-experimental/client.mustache index 5bd19cdbe59..76a3cb5df1b 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/client.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/client.mustache @@ -24,6 +24,10 @@ import ( "unicode/utf8" "golang.org/x/oauth2" + {{#withAWSV4Signature}} + awsv4 "github.com/aws/aws-sdk-go/aws/signer/v4" + awscredentials "github.com/aws/aws-sdk-go/aws/credentials" + {{/withAWSV4Signature}} ) var ( @@ -351,6 +355,25 @@ func (c *APIClient) prepareRequest( if auth, ok := ctx.Value(ContextAccessToken).(string); ok { localVarRequest.Header.Add("Authorization", "Bearer "+auth) } + + {{#withAWSV4Signature}} + // AWS Signature v4 Authentication + if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok { + creds := awscredentials.NewStaticCredentials(auth.AccessKey, auth.SecretKey, "") + signer := awsv4.NewSigner(creds) + var reader *strings.Reader + if body == nil { + reader = strings.NewReader("") + } else { + reader = strings.NewReader(body.String()) + } + timestamp := time.Now() + _, err := signer.Sign(localVarRequest, reader, "oapi", "eu-west-2", timestamp) + if err != nil { + return nil, err + } + } + {{/withAWSV4Signature}} } for header, value := range c.cfg.DefaultHeader { diff --git a/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache b/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache index 3cda1954db8..1f5436d84b7 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache @@ -31,6 +31,11 @@ var ( // ContextAPIKeys takes a string apikey as authentication for the request ContextAPIKeys = contextKey("apiKeys") + {{#withAWSV4Signature}} + // ContextAWSv4 takes an Access Key and a Secret Key for signing AWS Signature v4 + ContextAWSv4 = contextKey("awsv4") + + {{/withAWSV4Signature}} // ContextHttpSignatureAuth takes HttpSignatureAuth as authentication for the request. ContextHttpSignatureAuth = contextKey("httpsignature") @@ -59,6 +64,15 @@ type APIKey struct { Prefix string } +{{#withAWSV4Signature}} +// AWSv4 provides AWS Signature to a request passed via context using ContextAWSv4 +// https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html +type AWSv4 struct { + AccessKey string + SecretKey string +} + +{{/withAWSV4Signature}} // ServerVariable stores the information about a server variable type ServerVariable struct { Description string diff --git a/modules/openapi-generator/src/main/resources/go-experimental/go.mod.mustache b/modules/openapi-generator/src/main/resources/go-experimental/go.mod.mustache index 3b7d202ac4f..21fcfdeb96e 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/go.mod.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/go.mod.mustache @@ -4,4 +4,7 @@ go 1.13 require ( golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 + {{#withAWSV4Signature}} + github.com/aws/aws-sdk-go v1.34.14 + {{/withAWSV4Signature}} ) diff --git a/modules/openapi-generator/src/main/resources/go/go.mod.mustache b/modules/openapi-generator/src/main/resources/go/go.mod.mustache index 6c31cf3e9f1..c97cfff92ea 100644 --- a/modules/openapi-generator/src/main/resources/go/go.mod.mustache +++ b/modules/openapi-generator/src/main/resources/go/go.mod.mustache @@ -3,5 +3,7 @@ module {{gitHost}}/{{gitUserId}}/{{gitRepoId}}{{#isGoSubmodule}}/{{packageName}} require ( github.com/antihax/optional v1.0.0 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 - {{#withAWSV4Signature}}github.com/aws/aws-sdk-go v1.26.3{{/withAWSV4Signature}} + {{#withAWSV4Signature}} + github.com/aws/aws-sdk-go v1.34.14 + {{/withAWSV4Signature}} ) diff --git a/samples/client/petstore/go-experimental/go-petstore/client.go b/samples/client/petstore/go-experimental/go-petstore/client.go index 09eb6b64596..3c87567745c 100644 --- a/samples/client/petstore/go-experimental/go-petstore/client.go +++ b/samples/client/petstore/go-experimental/go-petstore/client.go @@ -357,6 +357,7 @@ func (c *APIClient) prepareRequest( if auth, ok := ctx.Value(ContextAccessToken).(string); ok { localVarRequest.Header.Add("Authorization", "Bearer "+auth) } + } for header, value := range c.cfg.DefaultHeader { diff --git a/samples/client/petstore/go/go-petstore-withXml/go.mod b/samples/client/petstore/go/go-petstore-withXml/go.mod index f55c1461f84..1af1846f985 100644 --- a/samples/client/petstore/go/go-petstore-withXml/go.mod +++ b/samples/client/petstore/go/go-petstore-withXml/go.mod @@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID require ( github.com/antihax/optional v1.0.0 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 - ) diff --git a/samples/client/petstore/go/go-petstore/go.mod b/samples/client/petstore/go/go-petstore/go.mod index f55c1461f84..1af1846f985 100644 --- a/samples/client/petstore/go/go-petstore/go.mod +++ b/samples/client/petstore/go/go-petstore/go.mod @@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID require ( github.com/antihax/optional v1.0.0 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 - ) diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go b/samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go index 30335de6c29..9c300260c9c 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go +++ b/samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go @@ -342,6 +342,7 @@ func (c *APIClient) prepareRequest( if auth, ok := ctx.Value(ContextAccessToken).(string); ok { localVarRequest.Header.Add("Authorization", "Bearer "+auth) } + } for header, value := range c.cfg.DefaultHeader { diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/client.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/client.go index a4194b6510e..a0833f77d79 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/client.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/client.go @@ -360,6 +360,7 @@ func (c *APIClient) prepareRequest( if auth, ok := ctx.Value(ContextAccessToken).(string); ok { localVarRequest.Header.Add("Authorization", "Bearer "+auth) } + } for header, value := range c.cfg.DefaultHeader { diff --git a/samples/openapi3/client/petstore/go/go-petstore/go.mod b/samples/openapi3/client/petstore/go/go-petstore/go.mod index f55c1461f84..1af1846f985 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/go.mod +++ b/samples/openapi3/client/petstore/go/go-petstore/go.mod @@ -3,5 +3,4 @@ module github.com/GIT_USER_ID/GIT_REPO_ID require ( github.com/antihax/optional v1.0.0 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 - )