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

Support socket address family VSOCK #409

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

ananthb
Copy link

@ananthb ananthb commented Apr 5, 2023

Add support for VSOCK stream sockets.

Fixes: #408

@mmerickel
Copy link
Member

Can you please add your name to CONTRIBUTORS.txt? Thanks!

I'm not familiar with VSOCK sockets, gonna need to wait for @bertjwregeer to review this.

Also note the failing ci/cd jobs that need to be fixed.

@ananthb
Copy link
Author

ananthb commented Apr 5, 2023

Can you please add your name to CONTRIBUTORS.txt? Thanks!

Sure thing.

I'm not familiar with VSOCK sockets,...

From waitress' point of view, they should be identical to UNIX sockets. The vsock address family is available to Linux VMs and the host machine when using the KVM virtualisation module. It allows the host machine and any VMs running on it to communicate without using the network. So its only available locally on Linux.

Also note the failing ci/cd jobs that need to be fixed.

Fixed.

@ananthb
Copy link
Author

ananthb commented Apr 6, 2023

Fixed the new failure by limiting this feature to only CPython on Linux as it is unavailable elsewhere.

@sudhirj
Copy link

sudhirj commented Apr 8, 2023

Adding a bit of context here... there's an upcoming paradigm called secure enclaves, where cloud providers set up specially isolated and secured VMs with no writable disks, no network access, and signed code execution.

These special VMs are suitable for processing very sensitive data like medical records, credit cards or anything else that too sensitive to want to process on a normal machine.

When running these VMs have no TCP sockets in them. The only way to communicate with them from the host machine is over a VSOCK socket. Works pretty much the same as any other socket, but is a different address family (integer, not IP address) and port scheme.

We want to run a Python server inside this secure VM, and asking waitress to listen on a VSOCK is by far the cleanest solution we've seen so far. We're dogfooding this PR internally.

https://aws.amazon.com/ec2/nitro/nitro-enclaves/

raise ValueError("Only Internet or UNIX stream sockets may be used.")
if sock.type != socket.SOCK_STREAM or sock.family not in supported_families:
raise ValueError(
"Only Internet, UNIX, or VSOCK stream sockets may be used."
Copy link
Author

@ananthb ananthb Apr 10, 2023

Choose a reason for hiding this comment

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

Why does this restriction exist btw? Is there an issue listening to multiple types of sockets simultaneously?

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 it's a fair question - I don't know what the history is here but it doesn't feel like anything would fundamentally break by using multiple different socket types. Things that come to mind are any WSGI environ settings that should be dynamic that aren't right now based on the source connection.

Copy link
Author

Choose a reason for hiding this comment

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

Each new socket type added later on will increase the number of combined checks needed. I could try removing this restriction and seeing if anything breaks.

Removing this restriction would also mean better systemd socket activation support. Systemd can pass multiple different types of sockets to apps quite easily and waitress should support that too.

Copy link
Member

Choose a reason for hiding this comment

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

The combinatorial explosion can be prevented by just a better loop and more complicated error message generation but again that’s assuming there’s a technical reason for keeping the checks.

Copy link
Author

Choose a reason for hiding this comment

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

Yes. Technically there is no basis for this check. The only question is if any waitress code assumes so or depends on this.

Copy link
Member

Choose a reason for hiding this comment

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

Part of it is that there is a different expectation with being able to receive from a UNIX socket (local, defined by file system permissions) and a TCP/IP socket.

There is no remote peer in the case of a UNIX socket that has a port number, certain values can't be automatically provided instead they are guessed at (such as the Host header if none is provided by the remote client over a UNIX socket).

Keeping the socket types separate may not be strictly required, it's not something that has been tested or validated and adding the additional testing may not be worth it. If you want to listen on both a UNIX socket and TCP/IP then that can be two different instances of waitress (or a single front-end proxy that does support that).

Copy link
Author

@ananthb ananthb left a comment

Choose a reason for hiding this comment

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

Why can't we mix different types of sockets?

except Exception:
raise ValueError("Invalid host/port specified.")
except Exception as exc:
raise ValueError("Invalid host/port specified.") from exc
Copy link
Member

Choose a reason for hiding this comment

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

fwiw I do not believe that the from exc is necessary in these cases you have modified - it is an explicit form of what already happens when raising a new exception from within an except block.

Copy link
Author

Choose a reason for hiding this comment

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

I setup a new linter (not sure which one) and it suggested I try this.

@digitalresistor
Copy link
Member

I've merged main into this branch because I wanted to run CI/CD on this. However there are multiple failing tests. If you could fix it so that the tests successfully run on Github CI/CD then I would be happy to move forward with this.

@ananthb
Copy link
Author

ananthb commented Jun 28, 2024

@digitalresistor I can fix those tests if you're still interested.

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.

Support VSOCK sockets
4 participants