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

[Core] Avoid race condition for key generation #3292

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions sky/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
import filelock
import yaml

from sky import clouds
Expand Down Expand Up @@ -62,6 +63,7 @@
# TODO(zhwu): Support user specified key pair.
PRIVATE_SSH_KEY_PATH = '~/.ssh/sky-key'
PUBLIC_SSH_KEY_PATH = '~/.ssh/sky-key.pub'
_SSH_KEY_GENERATION_LOCK = '~/.ssh/.sky-key.lock'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_SSH_KEY_GENERATION_LOCK = '~/.ssh/.sky-key.lock'
# Lock protecting public/private sky-key generation.
_SSH_KEY_GENERATION_LOCK = '~/.ssh/.sky-key.lock'

Copy link
Member

Choose a reason for hiding this comment

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

How about putting all our generated files in ~/.sky/generated or a lock dir under ~/.sky?

Copy link
Collaborator Author

@Michaelvll Michaelvll Mar 9, 2024

Choose a reason for hiding this comment

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

Good point! Moved it to ~/.sky/generated/ssh and test it in a new environment by running 5 concurrent launch.

for i in `seq 1 5`; do sky launch -c test-gcp-$i -d -y --cloud gcp --cpus 2 echo hi & done



def _generate_rsa_key_pair() -> Tuple[str, str]:
Expand All @@ -84,9 +86,6 @@ def _generate_rsa_key_pair() -> Tuple[str, str]:

def _save_key_pair(private_key_path: str, public_key_path: str,
private_key: str, public_key: str) -> None:
private_key_dir = os.path.dirname(private_key_path)
os.makedirs(private_key_dir, exist_ok=True)

with open(
private_key_path,
'w',
Expand All @@ -106,17 +105,17 @@ def get_or_generate_keys() -> Tuple[str, str]:
"""Returns the aboslute private and public key paths."""
private_key_path = os.path.expanduser(PRIVATE_SSH_KEY_PATH)
public_key_path = os.path.expanduser(PUBLIC_SSH_KEY_PATH)
if not os.path.exists(private_key_path):
public_key, private_key = _generate_rsa_key_pair()
_save_key_pair(private_key_path, public_key_path, private_key,
public_key)
else:
# FIXME(skypilot): ran into failing this assert once, but forgot the
# reproduction (has private key; but has not generated public key).
# AssertionError: /home/ubuntu/.ssh/sky-key.pub
assert os.path.exists(public_key_path), (
'Private key found, but associated public key '
f'{public_key_path} does not exist.')

key_dir = os.path.dirname(private_key_path)
os.makedirs(key_dir, exist_ok=True)
with filelock.FileLock(_SSH_KEY_GENERATION_LOCK, timeout=10):
if not os.path.exists(private_key_path):
public_key, private_key = _generate_rsa_key_pair()
_save_key_pair(private_key_path, public_key_path, private_key,
public_key)
assert os.path.exists(public_key_path), (
'Private key found, but associated public key '
f'{public_key_path} does not exist.')
return private_key_path, public_key_path


Expand Down
Loading