Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:support of pass throught hosts #1397

Merged
merged 20 commits into from
Jan 30, 2024
Merged

feat:support of pass throught hosts #1397

merged 20 commits into from
Jan 30, 2024

Conversation

shivamsouravjha
Copy link
Contributor

@shivamsouravjha shivamsouravjha commented Jan 18, 2024

Related Issue

  • Info about Issue or bug

Closes: #1382

Describe the changes you've made

  • filter url (same method same url, same url no method, different method same url)
  • Ignore host for test filter, with host - path: ".*/get$"
  • stubs for reqres path with w/o regex checked

Genrated Yaml

record:
    path: ""
    # mandatory
    command: ""
    proxyport: 0
    containerName: ""
    networkName: ""
    delay: 5
    buildDelay: 30s
    tests:
        filters:
            - path: ""
              urlMethods: []
              headers: {}
              host: ""
    stubs:
        filters:
            - path: ""
              host: "reqres.in"
              port: 0
test:
    path: ""
    # mandatory
    command: ""
    proxyport: 0
    containerName: ""
    networkName: ""
    # example: "test-set-1": ["test-1", "test-2", "test-3"]
    selectedTests:
    # to use globalNoise, please follow the guide at the end of this file.
    globalNoise:
        global:
            body: {}
            header: {}
    delay: 5
    buildDelay: 30s
    apiTimeout: 5
    stubs:
        filters:
            - path: ""
              host: "reqres.in"
              port: 0
    withCoverage: false
    coverageReportPath: ""
    # Example on using tests
    # tests: 
    #   filters:
    #    - path: "/user/app"
    #      urlMethods: ["GET"]
    #      headers: {
    #        "^asdf*": "^test"
    #      }
    #      host: "dc.services.visualstudio.com"
    # Example on using stubs
    # stubs: 
    #   filters:
    #    - path: "/user/app"
    #      port: 8080
    #    - port: 8081
    #    - host: "dc.services.visualstudio.com"
    #    - port: 8081
    #      host: "dc.services.visualstudio.com"
    #      path: "/user/app"
    #
    # Example on using globalNoise
    # globalNoise: 
    #    global:
    #      body: {
    #         # to ignore some values for a field, 
    #         # pass regex patterns to the corresponding array value
    #         "url": ["https?://\S+", "http://\S+"],
    #      }
    #      header: {
    #         # to ignore the entire field, pass an empty array
    #         "Date": [],
    #       }
    #     # to ignore fields or the corresponding values for a specific test-set,
    #     # pass the test-set-name as a key to the "test-sets" object and
    #     # populate the corresponding "body" and "header" objects 
    #     test-sets:
    #       test-set-1:
    #         body: {
    #           # ignore all the values for the "url" field
    #           "url": []
    #         }
    #         header: { 
    #           # we can also pass the exact value to ignore for a field
    #           "User-Agent": ["PostmanRuntime/7.34.0"]
    #         }

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, local variables)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

pkg/models/const.go Fixed Show fixed Hide fixed
Copy link
Member

@charankamarapu charankamarapu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the comments

withCoverage: false
coverageReportPath: ""
`
# Example on using tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be added in config guide. You can see how is the config guide attached to final output.

pkg/hooks/loader.go Outdated Show resolved Hide resolved
cmd/record.go Outdated
if err != nil {
if err == errFileNotFound {
r.logger.Info("continuing without configuration file because file not found")
r.logger.Info("Keploy config not found, using default config")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This log is wrong if keploy config is present we will not use default config. We consider only flags and continue with flags.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide an update on this

cmd/record.go Outdated Show resolved Hide resolved
cmd/test.go Outdated Show resolved Hide resolved
pkg/platform/yaml/yaml.go Outdated Show resolved Hide resolved
pkg/platform/yaml/yaml.go Outdated Show resolved Hide resolved
pkg/proxy/integrations/httpparser/httpparser.go Outdated Show resolved Hide resolved
pkg/proxy/integrations/httpparser/httpparser.go Outdated Show resolved Hide resolved
cmd/record.go Show resolved Hide resolved
Copy link
Member

@charankamarapu charankamarapu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the comments

@@ -141,6 +142,16 @@ func (h *Hook) GetProxyPort() uint32 {
return h.proxyPort
}

func (h *Hook) GetProxyHost() models.Stubs {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename the function. Current name provide wrong info

return h.passThroughHosts
}

func (h *Hook) SetProxyHosts(passThroughHosts []models.Filters) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename the function

urlMatched = true
}

if len(urlMethods) != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this logic has flaw lets say user has given path - x and port -y . even if there is a request with path - z and port - y it will be matched and considered as passthrough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like this selected code block deals with some other logic.

@@ -31,17 +32,17 @@ type HttpParser struct {
}

// ProcessOutgoing implements proxy.DepInterface.
func (http *HttpParser) ProcessOutgoing(request []byte, clientConn, destConn net.Conn, ctx context.Context) {
func (http *HttpParser) ProcessOutgoing(request []byte, clientConn, destConn net.Conn, ctx context.Context, sourcePort int) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you extract port from clientConn or destConn..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will increase computation time, better to use a precalculated value

utils/utils.go Show resolved Hide resolved
@shivamsouravjha shivamsouravjha force-pushed the shivam/passthrough branch 2 times, most recently from 70ef6a4 to 77cb6cd Compare January 29, 2024 10:37
Copy link
Member

@charankamarapu charankamarapu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please address the comments, rest looks fine.

cmd/record.go Outdated
if err != nil {
if err == errFileNotFound {
r.logger.Info("continuing without configuration file because file not found")
r.logger.Info("Keploy config not found, ontinuing without configuration")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please correct the spelling error.

cmd/test.go Outdated
if err != nil {
if err == errFileNotFound {
t.logger.Info("continuing without configuration file because file not found")
t.logger.Info("Keploy config not found, using default config")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide an update on this.

return err, false
}
headerValueMatch := regex.MatchString(headerNameValue)
if headerNameMatch || headerValueMatch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a and condition..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

earlier I thought of using either header name or value to be independent, will update it so that the combination is unique(i.e, the and condition)

pkg/proxy/integrations/httpparser/httpparser.go Outdated Show resolved Hide resolved

}
for _, filter := range portPassThrough {
regex, err := regexp.Compile(filter.Path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible in config to provide path without port. Why is path and port are combined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We separate only port and combination of port +host in service layer. then in this layer we separate port+ host and only host. thus there are two loops too.

}
matches := regex.MatchString(req.URL.String())
if matches && host != "" || req.Host == host {
passthroughHost = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have a break statement here..?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done so

if req.Host == host {
passThrough := models.PassThroughHosts
portPassThrough := []models.Filters{}
for _, filters := range h.GetPassThroughHosts().Filters {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly here too.

Copy link
Member

@charankamarapu charankamarapu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it!

@charankamarapu charankamarapu merged commit 7e44c3b into main Jan 30, 2024
13 checks passed
@charankamarapu charankamarapu deleted the shivam/passthrough branch January 30, 2024 06:01
@github-actions github-actions bot locked and limited conversation to collaborators Jan 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature]: Pass Through for specific API end points in keploy test mode
2 participants