converting to post-processor prototype

This commit is contained in:
mzack 2021-11-13 03:17:05 +01:00
parent e517797cfa
commit eb61c519c3
4 changed files with 36 additions and 27 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 {

View File

@ -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:"-"`