From eb61c519c3bfa38a324e4088a79b57de6d628c21 Mon Sep 17 00:00:00 2001 From: mzack Date: Sat, 13 Nov 2021 03:17:05 +0100 Subject: [PATCH] converting to post-processor prototype --- v2/pkg/protocols/http/http.go | 4 +-- v2/pkg/protocols/http/request.go | 51 +++++++++++++++++++------------- v2/pkg/templates/compile.go | 4 +-- v2/pkg/templates/templates.go | 4 +-- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/v2/pkg/protocols/http/http.go b/v2/pkg/protocols/http/http.go index 7a6228b1b..765b217e6 100644 --- a/v2/pkg/protocols/http/http.go +++ b/v2/pkg/protocols/http/http.go @@ -138,8 +138,8 @@ type Request struct { // description: | // SelfContained specifies if the request is self contained. - SelfContained bool `yaml:"-" json:"-"` - AwsSign bool `yaml:"-" json:"-"` + SelfContained bool `yaml:"-" json:"-"` + PostProcessors []string `yaml:"-" json:"-"` // description: | // CookieReuse is an optional setting that enables cookie reuse for diff --git a/v2/pkg/protocols/http/request.go b/v2/pkg/protocols/http/request.go index eb8a716a3..6059f1679 100644 --- a/v2/pkg/protocols/http/request.go +++ b/v2/pkg/protocols/http/request.go @@ -197,6 +197,10 @@ func (request *Request) executeTurboHTTP(reqURL string, dynamicValues, previous return requestErr } +func (request *Request) hasPreprocessors() bool { + return len(request.PostProcessors) > 0 +} + // ExecuteWithResults executes the final request on a URL func (request *Request) ExecuteWithResults(reqURL string, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error { // verify if pipeline was requested @@ -296,6 +300,7 @@ func (request *Request) executeRequest(reqURL string, generatedRequest *generate // For race conditions we can't dump the request body at this point as it's already waiting the open-gate event, already handled with a similar code within the race function if !generatedRequest.original.Race { var dumpError error + // TODO: dump is currently not working with post-processors - somehow it alters the signature dumpedRequest, dumpError = dump(generatedRequest, reqURL) if dumpError != nil { return dumpError @@ -349,33 +354,37 @@ func (request *Request) executeRequest(reqURL string, generatedRequest *generate } } if resp == nil { - // aws sign the request if necessary - if request.AwsSign { - var awsSigner *AwsSigner - payloads := request.options.Options.Vars.AsMap() - if request.options.Options.EnvironmentVariables { - var err error - awsSigner, err = NewAwsSignerFromEnv() - if err != nil { - return err - } - } else { // get from variables { + for _, postProcessor := range request.PostProcessors { + switch postProcessor { + case "aws-sign": + var awsSigner *AwsSigner + payloads := request.options.Options.Vars.AsMap() awsAccessKeyId := types.ToString(payloads["aws-id"]) awsSecretAccessKey := types.ToString(payloads["aws-secret"]) - awsSigner, err = NewAwsSigner(awsAccessKeyId, awsSecretAccessKey) + if awsAccessKeyId != "" && awsSecretAccessKey != "" { + awsSigner, err = NewAwsSigner(awsAccessKeyId, awsSecretAccessKey) + } else { + awsSigner, err = NewAwsSignerFromEnv() + } if err != nil { return err } - } - args := SignArguments{ - Service: types.ToString(payloads["service"]), - Region: types.ToString(payloads["region"]), - Time: time.Now(), - } - err = awsSigner.SignHTTP(generatedRequest.request.Request, args) - if err != nil { - return err + service := types.ToString(payloads["service"]) + region := types.ToString(payloads["region"]) + if service == "" || region == "" { + return errors.New("service and region are mandatory") + } + + args := SignArguments{ + Service: types.ToString(payloads["service"]), + Region: types.ToString(payloads["region"]), + Time: time.Now(), + } + err = awsSigner.SignHTTP(generatedRequest.request.Request, args) + if err != nil { + return err + } } } resp, err = request.httpClient.Do(generatedRequest.request) diff --git a/v2/pkg/templates/compile.go b/v2/pkg/templates/compile.go index 654abcb64..97f1afa70 100644 --- a/v2/pkg/templates/compile.go +++ b/v2/pkg/templates/compile.go @@ -107,9 +107,9 @@ func Parse(filePath string, preprocessor Preprocessor, options protocols.Execute // parseSelfContainedRequests parses the self contained template requests. func (template *Template) parseSelfContainedRequests() { - if template.AwsSign { + if len(template.PostProcessors) > 0 { for _, request := range template.RequestsHTTP { - request.AwsSign = true + request.PostProcessors = template.PostProcessors } } if !template.SelfContained { diff --git a/v2/pkg/templates/templates.go b/v2/pkg/templates/templates.go index b4648d990..7d5ae7702 100644 --- a/v2/pkg/templates/templates.go +++ b/v2/pkg/templates/templates.go @@ -73,8 +73,8 @@ type Template struct { // description: | // Self Contained marks Requests for the template as self-contained - SelfContained bool `yaml:"self-contained,omitempty" jsonschema:"title=mark requests as self-contained,description=Mark Requests for the template as self-contained"` - AwsSign bool `yaml:"aws-sign,omitempty"` + SelfContained bool `yaml:"self-contained,omitempty" jsonschema:"title=mark requests as self-contained,description=Mark Requests for the template as self-contained"` + PostProcessors []string `yaml:"post-processors,omitempty"` // TotalRequests is the total number of requests for the template. TotalRequests int `yaml:"-" json:"-"`