Skip to content

Commit 671b0bb

Browse files
committed
predefined ssh key name - removing prompt to user
1 parent c240624 commit 671b0bb

File tree

2 files changed

+42
-56
lines changed

2 files changed

+42
-56
lines changed

src/ibm_ray_config/modules/gen2/ray/ssh_key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def __init__(self, base_config: Dict[str, Any]) -> None:
1010
for available_node_type in self.base_config['available_node_types']:
1111
self.defaults['key_id'] = self.base_config['available_node_types'][available_node_type]['node_config'].get('key_id')
1212
break
13-
13+
# collects default value for the question regrading ssh_private_key location
1414
self.defaults['ssh_key_filename'] = self.base_config['auth']['ssh_private_key']
1515

16-
def update_config(self, ssh_key_id, ssh_key_path, ssh_user):
16+
def update_config(self, ssh_key_id, ssh_key_path, ssh_user):
1717
self.base_config['auth']['ssh_private_key'] = ssh_key_path
1818
self.base_config['auth']['ssh_user'] = ssh_user
1919

src/ibm_ray_config/modules/gen2/ssh_key.py

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@
22
import subprocess
33
from typing import Any, Dict
44
from pathlib import Path
5-
5+
from uuid import uuid4
66
import inquirer
77
from inquirer import errors
88
from ibm_ray_config.modules.config_builder import ConfigBuilder, update_decorator, spinner
99
from ibm_ray_config.modules.utils import (Color, color_msg, find_default, find_name_id,
10-
validate_exists, validate_not_empty)
10+
validate_exists, validate_not_empty, free_dialog)
1111

1212
from ibm_cloud_sdk_core import ApiException
13-
DEFAULT_KEY_NAME = 'default-ssh-key'
13+
DEFAULT_KEY_NAME = f'ray-{os.environ.get("USERNAME")}-{str(uuid4())[:5]}'
1414

15-
def generate_keypair(keyname):
15+
def generate_keypair():
1616
"""Returns newly generated public ssh-key's contents and private key's path"""
17-
home = str(Path.home())
18-
filename = f"{home}{os.sep}.ssh{os.sep}id.rsa.{keyname}"
19-
try:
20-
os.remove(filename)
21-
except Exception:
22-
pass
17+
filename = f"{os.sep}tmp{os.sep}{DEFAULT_KEY_NAME}"
2318

2419
os.system(f'ssh-keygen -b 2048 -t rsa -f {filename} -q -N ""')
2520
print(f"\n\n\033[92mSSH key pair been generated\n")
26-
print(f"private key: {os.path.abspath(filename)}")
27-
print(f"public key {os.path.abspath(filename)}.pub\033[0m")
21+
print(f"private key intermediate location: {os.path.abspath(filename)}")
22+
print(f"public key intermediate location: {os.path.abspath(filename)}.pub\033[0m")
2823
with open(f"{filename}.pub", 'r') as file:
2924
ssh_key_data = file.read()
3025
ssh_key_path = os.path.abspath(filename)
@@ -37,28 +32,20 @@ def get_ssh_key(ibm_vpc_client, name):
3732
return key
3833

3934
def register_ssh_key(ibm_vpc_client, config, auto=False):
40-
"""Returns the key's name on the VPC platform, it's public key's contents and the local path to it.
35+
"""Returns:
36+
1. key's name on the VPC platform.
37+
2. it's public key's contents.
38+
3. local path to private key ONLY if user generated a key, ELSE None.
4139
Registers an either existing or newly generated ssh-key to a specific VPC. """
40+
4241
if config.get('ibm_vpc'):
4342
resource_group_id = config['ibm_vpc']['resource_group_id']
4443
else:
4544
for available_node_type in config['available_node_types']:
4645
resource_group_id = config['available_node_types'][available_node_type]['node_config']['resource_group_id']
4746
break
4847

49-
50-
questions = [
51-
inquirer.Text(
52-
'keyname', message='Please specify a name for the new key', validate=validate_not_empty)
53-
]
54-
55-
answers = {}
56-
if not auto:
57-
answers = inquirer.prompt(questions, raise_keyboard_interrupt=True)
58-
else:
59-
answers['keyname'] = DEFAULT_KEY_NAME
60-
61-
keyname = answers['keyname']
48+
keyname = DEFAULT_KEY_NAME
6249

6350
EXISTING_CONTENTS = 'Paste existing public key contents'
6451
EXISTING_PATH = 'Provide path to existing public key'
@@ -76,11 +63,11 @@ def register_ssh_key(ibm_vpc_client, config, auto=False):
7663
else:
7764
answers["answer"] = GENERATE_NEW
7865

79-
ssh_key_data = ""
80-
ssh_key_path = None
66+
public_ssh_key_data = ""
67+
private_ssh_key_path = None
8168
if answers["answer"] == EXISTING_CONTENTS:
8269
print("Registering from file contents")
83-
ssh_key_data = input(
70+
public_ssh_key_data = input(
8471
"[\033[33m?\033[0m] Please paste the contents of your public ssh key. It should start with ssh-rsa: ")
8572
elif answers["answer"] == EXISTING_PATH:
8673
print("Register in vpc existing key from path")
@@ -90,31 +77,26 @@ def register_ssh_key(ibm_vpc_client, config, auto=False):
9077
]
9178
answers = inquirer.prompt(questions, raise_keyboard_interrupt=True)
9279
with open(os.path.abspath(os.path.expanduser(answers["public_key_path"])), 'r') as file:
93-
ssh_key_data = file.read()
94-
else:
95-
ssh_key_data, ssh_key_path = generate_keypair(keyname)
80+
public_ssh_key_data = file.read()
81+
else: # choice GENERATE_NEW
82+
public_ssh_key_data, private_ssh_key_path = generate_keypair()
9683

97-
response = None
98-
try: # regardless of the above, try registering an ssh-key
99-
response = ibm_vpc_client.create_key(public_key=ssh_key_data, name=keyname, resource_group={
84+
try: # regardless of the above choices, try registering an ssh-key
85+
response = ibm_vpc_client.create_key(public_key=public_ssh_key_data, name=keyname, resource_group={
10086
"id": resource_group_id}, type='rsa')
10187
except ApiException as e:
102-
print(e)
103-
104-
if "Key with name already exists" in e.message and keyname == DEFAULT_KEY_NAME:
105-
key = get_ssh_key(ibm_vpc_client, DEFAULT_KEY_NAME)
106-
ibm_vpc_client.delete_key(id=key['id'])
107-
response = ibm_vpc_client.create_key(public_key=ssh_key_data, name=keyname, resource_group={
108-
"id": resource_group_id}, type='rsa')
88+
if "Key with name already exists" in e.message:
89+
print(color_msg(f"Key by the name '{keyname}' already exists",Color.RED))
90+
keyname = free_dialog("Please specify a *unique* name for the new key")["answer"]
10991
else:
11092
if "Key with fingerprint already exists" in e.message:
11193
print(color_msg("Can't register an SSH key with the same fingerprint",Color.RED))
112-
raise # can't continue the configuration process without a valid ssh key
94+
exit(1) # can't continue the configuration process without a valid ssh key
11395

114-
print(f"\033[92mnew SSH key {keyname} been registered in vpc\033[0m")
96+
print(f"\033[92mnew SSH key: '{keyname}' been registered in vpc\033[0m")
11597

11698
result = response.get_result()
117-
return result['name'], result['id'], ssh_key_path
99+
return result['name'], result['id'], private_ssh_key_path
118100

119101

120102
DEPENDENCIES = {'ibm_vpc': {'resource_group_id': None}}
@@ -151,28 +133,32 @@ def get_ssh_key_objects():
151133
CREATE_NEW_SSH_KEY = f"""Register new SSH key in IBM VPC {color_msg("[Best Practice]",Color.LIGHTGREEN)}"""
152134

153135
default = find_default(self.defaults, ssh_key_objects, id='key_id')
136+
default = default if default else CREATE_NEW_SSH_KEY
154137
ssh_key_name, ssh_key_id = find_name_id(
155-
ssh_key_objects, 'Choose ssh key', do_nothing=CREATE_NEW_SSH_KEY, default=CREATE_NEW_SSH_KEY)
156-
157-
ssh_key_path = None
158-
if not ssh_key_name:
159-
ssh_key_name, ssh_key_id, ssh_key_path = register_ssh_key(
138+
ssh_key_objects, 'Choose ssh key', do_nothing=CREATE_NEW_SSH_KEY, default=default)
139+
140+
private_ssh_key_path = None
141+
if not ssh_key_name: # user chose to create a new key / no keys registered
142+
ssh_key_name, ssh_key_id, private_ssh_key_path = register_ssh_key(
160143
self.ibm_vpc_client, self.base_config)
161144

162145
self.ssh_key_id = ssh_key_id
163146
self.ssh_key_name = ssh_key_name
164147

165-
if not ssh_key_path:
148+
# private_ssh_key_path is known only if user generated a key.
149+
if not private_ssh_key_path:
166150
questions = [
167151
inquirer.Text(
168-
"private_key_path", message=f'Please paste path to \033[92mprivate\033[0m ssh key associated with selected public key {ssh_key_name}', validate=self._validate_keypair, default=self.defaults.get('ssh_key_filename') or "~/.ssh/id_rsa")
152+
"private_key_path", message=f'Please paste path to \033[92mprivate\033[0m ssh key associated with selected public key {ssh_key_name}',
153+
validate=self._validate_keypair,
154+
default=self.defaults.get('ssh_key_filename') or "~/.ssh/id_rsa")
169155
]
170156
answers = inquirer.prompt(questions, raise_keyboard_interrupt=True)
171-
ssh_key_path = os.path.abspath(
157+
private_ssh_key_path = os.path.abspath(
172158
os.path.expanduser(answers["private_key_path"]))
173159

174160
# currently the user is hardcoded to root
175-
return ssh_key_id, ssh_key_path, 'root'
161+
return ssh_key_id, private_ssh_key_path, 'root'
176162

177163
@update_decorator
178164
def verify(self, base_config):

0 commit comments

Comments
 (0)