Skip to content

Commit 6aa1193

Browse files
author
Simca
committed
KeyPair creation failed during first configuration
1 parent 0917c0a commit 6aa1193

File tree

5 files changed

+43
-30
lines changed

5 files changed

+43
-30
lines changed

src/openstack_cli/commands/conf/keys/create.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from openstack_cli.core.output import StatusOutput
1717
from openstack_cli.modules.apputils.terminal import Console
1818
from openstack_cli.modules.openstack import OpenStack
19-
from openstack_cli.modules.openstack.api_objects import VMKeyPairItemBuilder
19+
from openstack_cli.modules.openstack.api_objects import VMKeyPairItemBuilder, VMNewKeyPairItemBuilder
2020
from openstack_cli.core.config import Configuration
2121
from openstack_cli.modules.apputils.discovery import CommandMetaInfo
2222

@@ -42,31 +42,8 @@ def _create_key(conf:Configuration, ostack:OpenStack, keyBuilder: VMKeyPairItemB
4242

4343

4444
def __init__(conf: Configuration, name: str):
45-
from cryptography.hazmat.primitives import serialization as crypto_serialization
46-
from cryptography.hazmat.primitives.asymmetric import rsa
47-
from cryptography.hazmat.backends import default_backend as crypto_default_backend
48-
4945
ostack = OpenStack(conf)
50-
51-
key = rsa.generate_private_key(
52-
backend=crypto_default_backend(),
53-
public_exponent=65537,
54-
key_size=2048
55-
)
56-
private_key = key.private_bytes(
57-
crypto_serialization.Encoding.PEM,
58-
crypto_serialization.PrivateFormat.TraditionalOpenSSL, # PKCS8 is not supported best by paramiko
59-
crypto_serialization.NoEncryption())
60-
public_key = key.public_key().public_bytes(
61-
crypto_serialization.Encoding.OpenSSH,
62-
crypto_serialization.PublicFormat.OpenSSH
63-
)
64-
65-
keyBuilder = VMKeyPairItemBuilder() \
66-
.set_name(name) \
67-
.set_private_key(private_key) \
68-
.set_public_key(public_key)
69-
46+
keyBuilder = VMNewKeyPairItemBuilder().set_name(name)
7047
_create_key(conf, ostack, keyBuilder)
7148

7249

src/openstack_cli/commands/scripts/list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
__module__ = CommandMetaInfo("list", item_help="List available scripts")
2222
__args__ = __module__.arg_builder\
23-
.add_default_argument("name", str, "dadad")\
23+
.add_default_argument("name", str, "", default="")\
2424
.add_argument("test", str, "", default="dda")
2525

2626

src/openstack_cli/commands/snap/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
from openstack_cli.core.config import Configuration
1717
from openstack_cli.modules.apputils.discovery import CommandMetaInfo, NotImplementedCommandException
1818

19-
__module__ = CommandMetaInfo("snap", item_help="Manage OpenStack Snapshoots",
20-
default_sub_command="list", exec_with_child=True)
19+
__module__ = CommandMetaInfo(
20+
"snap",
21+
item_help="Manage OpenStack Snapshoots",
22+
default_sub_command="list",
23+
exec_with_child=True
24+
)
2125

2226

23-
def __init__(conf: Configuration):
27+
def __init__(conf: Configuration, **kwargs):
2428
print("====Wait for version 1.3=====")
2529
raise NotImplementedCommandException()

src/openstack_cli/core/updates/upgrade_catalog_11.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from openstack_cli.core.output import Console, StatusOutput
2121
from openstack_cli.modules.apputils.config.upgrades import UpgradeCatalog, upgrade
2222
from openstack_cli.modules.openstack import OpenStack, AuthRequestType
23+
from openstack_cli.modules.openstack.api_objects import VMNewKeyPairItemBuilder
2324
from openstack_cli.modules.openstack.objects import VMProject
2425

2526

@@ -107,7 +108,7 @@ def __call__(self, *args, **kwargs):
107108

108109
if not _is_cfg_key and not _is_srv_key:
109110
print(f"Creating new '{_default_keypair_name}' keypair..")
110-
_create_key(conf, osvm, _default_keypair_name)
111+
_create_key(conf, osvm, VMNewKeyPairItemBuilder().set_name(_default_keypair_name))
111112
print(f"Key '{_default_keypair_name}' could be exported using command 'conf keys export {_default_keypair_name}'")
112113

113114
if not _is_cfg_key and _is_srv_key:

src/openstack_cli/modules/openstack/api_objects.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,37 @@ def build(self):
575575
return self.__key
576576

577577

578+
class VMNewKeyPairItemBuilder(VMKeyPairItemBuilder):
579+
def __init__(self):
580+
super(VMNewKeyPairItemBuilder, self).__init__()
581+
582+
from cryptography.hazmat.primitives import serialization as crypto_serialization
583+
from cryptography.hazmat.primitives.asymmetric import rsa
584+
from cryptography.hazmat.backends import default_backend as crypto_default_backend
585+
586+
key = rsa.generate_private_key(
587+
backend=crypto_default_backend(),
588+
public_exponent=65537,
589+
key_size=2048
590+
)
591+
private_key = key.private_bytes(
592+
crypto_serialization.Encoding.PEM,
593+
crypto_serialization.PrivateFormat.TraditionalOpenSSL, # PKCS8 is not supported best by paramiko
594+
crypto_serialization.NoEncryption())
595+
public_key = key.public_key().public_bytes(
596+
crypto_serialization.Encoding.OpenSSH,
597+
crypto_serialization.PublicFormat.OpenSSH
598+
)
599+
super(VMNewKeyPairItemBuilder, self).set_private_key(private_key)
600+
super(VMNewKeyPairItemBuilder, self).set_public_key(public_key)
601+
602+
def set_public_key(self, key: str or bytes):
603+
raise NotImplementedError("Use VMKeyPairItemBuilder instead")
604+
605+
def set_private_key(self, key: str or bytes):
606+
raise NotImplementedError("Use VMKeyPairItemBuilder instead")
607+
608+
578609
class VMKeypairItem(SerializableObject):
579610
keypair: VMKeypairItemValue = None
580611

0 commit comments

Comments
 (0)