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

Add SourceAddr support #82

Merged
merged 4 commits into from
Oct 31, 2023
Merged

Conversation

zetaab
Copy link
Contributor

@zetaab zetaab commented Oct 26, 2023

supports both ipv4 and ipv6.

The format is [::1]:51608 or 127.0.0.1:51701, so it will map golang net/http RemoteAddr field.

@jcchavezs
Copy link
Collaborator

jcchavezs commented Oct 26, 2023

Hi @zetaab thanks for opening this. I have a few things to mention here:

  1. The http-wasm ABI does not support this method and usually adding a new method requires discussion in the ABI before jumping into the implementation.
  2. I think we don't need such method. Instead we should add back the header x-remote-addr, having remoteAddr as a first class field for the request is a Golang specific concern and hence it isn't portable (see for example what happens with the field Host in https://github.com/http-wasm/http-wasm-host-go/blob/main/handler/nethttp/host.go#L121

I will dig into the assignment of the req.RemoteAddr.

cc @vikaschoudhary16

@anuraaga
Copy link
Collaborator

Instead we should add back the header

Remote addr is based on TCP connection information, not headers, so it wouldn't make sense to translate it and it should probably be a native abi. X-forwarded headers might be what you are thinking of, some frameworks may merge them into their exposed remote addr accessor, but I don't believe Go does, not sure though. Even in such frameworks I believe they would then always have a "originalIP" type of field.

To maintain being "raw", we would ideally have a ABI to get the true remote addr and ensure any x-forwarded headers that came in the input are preserved in the headers passed to wasm.

@zetaab
Copy link
Contributor Author

zetaab commented Oct 26, 2023

the difference between Host and RemoteAddr is that Host is really header in packet level. RemoteAddr is not header.

@jcchavezs
Copy link
Collaborator

jcchavezs commented Oct 26, 2023

Thanks for the feedback @zetaab and @anuraaga. I was looking into this in golang source code and stumbled upon a couple of things:

  1. In CGI it comes from REMOTE_ADDR variable.
  2. In TCP it comes from the IP source address and TCP source port so no headers involvement.

That said I think it makes sense to add it to the ABI as @anuraaga suggested. Would you be up to open first a PR into https://github.com/http-wasm/http-wasm/blob/main/content/http-handler-abi.md adding the functions you already added to the host/guest?

@zetaab zetaab changed the title Add RemoteAddr support Add SourceAddr support Oct 27, 2023
@jcchavezs
Copy link
Collaborator

@zetaab do you mind adding a test for getSourceAddress in https://github.com/http-wasm/http-wasm-host-go/blob/main/tck/run.go#L44 ? (is that all regarding TCK @anuraaga ?)

@anuraaga
Copy link
Collaborator

A tck test requires both the host and guest side, which is here

https://github.com/http-wasm/http-wasm-host-go/blob/main/tck/guest/main.go

So we have to merge to both before writing the test. We would update the guest sdk version in that module to the unreleased code when doing so.

So we shouldn't block any merges on tck tests. But it reminded me I think we need a test in handlertest, which is this repo's unit tests.

https://github.com/http-wasm/http-wasm-host-go/blob/main/testing/handlertest/testhandler.go

@zetaab
Copy link
Contributor Author

zetaab commented Oct 31, 2023

@anuraaga @jcchavezs tests added. One minor issue is that the whole test pipeline does not listen for ipv6, so I could not add tests for ipv6 case.

@@ -2,4 +2,4 @@ module github.com/anuraaga/http-wasm-tck/guest

go 1.19

require github.com/http-wasm/http-wasm-guest-tinygo v0.1.1
require github.com/http-wasm/http-wasm-guest-tinygo v0.3.1-0.20231031134125-487a6e2eec5e
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not binding to release, it uses current latest commit

@@ -214,6 +216,24 @@ func (h *handler) testReadBody(req api.Request, resp api.Response, expectedBody
return true, 0
}

func (h *handler) testGetSourceAddr(req api.Request, resp api.Response, expectedAddr string) (next bool, reqCtx uint32) {
addr := req.GetSourceAddr()
raw := strings.Split(addr, ":")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

port number is always something random in this case. So its difficult to verify

Copy link
Collaborator

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

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

Thanks!

@jcchavezs jcchavezs merged commit 6e96a18 into http-wasm:main Oct 31, 2023
5 checks passed
@zetaab zetaab deleted the feature/remoteaddr branch October 31, 2023 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants