-
Notifications
You must be signed in to change notification settings - Fork 31
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
SSH tunnel support for S3Store #882
Merged
Merged
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
58fd7fb
Move ssh tunnel to a new file
mjwen c6c98ab
Add new `local_port` arg, to allow S3 9000 port
mjwen ea77d0f
Add sshtunnel support for S3Store
mjwen e8fcac6
Run pre-commit
mjwen e51d006
Enable force_reset for connect
mjwen 6db3d02
Fix some doc string
mjwen 92f807b
Fix to pass original S3 tests
mjwen b730130
Add test force_reset
mjwen 814ad40
Add test for local_port for ssh tunnel
mjwen 0b0d2da
Add test S3 store wit h ssh tunnel
mjwen bf56d2a
Add brief doc on using SSHTunnel
mjwen afa1050
Remove unnecessary assignment
mjwen aa38775
explicit catch error
mjwen 07d3df9
Add sshtunnel to mkdocs.yml
mjwen cc048d1
Add a fake ssh tunnel to test S3Store
mjwen d31a054
Merge branch 'main' into feat_s3_sshtunnel
rkingsbury 8308ad7
Merge branch 'main' into feat_s3_sshtunnel
rkingsbury 31c6ce7
Fix typo
mjwen 6cae273
Merge remote-tracking branch 'origin/feat_s3_sshtunnel' into feat_s3_…
mjwen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from socket import socket | ||
from typing import Dict, Optional, Tuple | ||
|
||
from monty.json import MSONable | ||
from sshtunnel import SSHTunnelForwarder | ||
|
||
|
||
class SSHTunnel(MSONable): | ||
__TUNNELS: Dict[str, SSHTunnelForwarder] = {} | ||
|
||
def __init__( | ||
self, | ||
tunnel_server_address: str, | ||
remote_server_address: str, | ||
local_port: Optional[int] = None, | ||
username: Optional[str] = None, | ||
password: Optional[str] = None, | ||
private_key: Optional[str] = None, | ||
**kwargs, | ||
): | ||
""" | ||
Args: | ||
tunnel_server_address: string address with port for the SSH tunnel server | ||
remote_server_address: string address with port for the server to connect to | ||
local_port: optional port to use for the local address (127.0.0.1); | ||
if `None`, a random open port will be automatically selected | ||
username: optional username for the ssh tunnel server | ||
password: optional password for the ssh tunnel server; If a private_key is | ||
supplied this password is assumed to be the private key password | ||
private_key: ssh private key to authenticate to the tunnel server | ||
kwargs: any extra args passed to the SSHTunnelForwarder | ||
""" | ||
|
||
self.tunnel_server_address = tunnel_server_address | ||
self.remote_server_address = remote_server_address | ||
self.local_port = local_port | ||
self.username = username | ||
self.password = password | ||
self.private_key = private_key | ||
self.kwargs = kwargs | ||
|
||
if remote_server_address in SSHTunnel.__TUNNELS: | ||
self.tunnel = SSHTunnel.__TUNNELS[remote_server_address] | ||
else: | ||
if local_port is None: | ||
local_port = _find_free_port("127.0.0.1") | ||
local_bind_address = ("127.0.0.1", local_port) | ||
|
||
ssh_address, ssh_port = tunnel_server_address.split(":") | ||
ssh_port = int(ssh_port) # type: ignore | ||
|
||
remote_bind_address, remote_bind_port = remote_server_address.split(":") | ||
remote_bind_port = int(remote_bind_port) # type: ignore | ||
|
||
if private_key is not None: | ||
ssh_password = None | ||
ssh_private_key_password = password | ||
else: | ||
ssh_password = password | ||
ssh_private_key_password = None | ||
|
||
self.tunnel = SSHTunnelForwarder( | ||
ssh_address_or_host=(ssh_address, ssh_port), | ||
local_bind_address=local_bind_address, | ||
remote_bind_address=(remote_bind_address, remote_bind_port), | ||
ssh_username=username, | ||
ssh_password=ssh_password, | ||
ssh_private_key_password=ssh_private_key_password, | ||
ssh_pkey=private_key, | ||
**kwargs, | ||
) | ||
|
||
def start(self): | ||
if not self.tunnel.is_active: | ||
self.tunnel.start() | ||
|
||
def stop(self): | ||
if self.tunnel.tunnel_is_up: | ||
self.tunnel.stop() | ||
|
||
@property | ||
def local_address(self) -> Tuple[str, int]: | ||
return self.tunnel.local_bind_address | ||
|
||
|
||
def _find_free_port(address="0.0.0.0"): | ||
s = socket() | ||
s.bind((address, 0)) # Bind to a free port provided by the host. | ||
return s.getsockname()[1] # Return the port number assigned. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Binding a socket to all network interfaces Medium