2013-04-12 17:01:38 -05:00
|
|
|
# Copyright 2013 OpenStack Foundation
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
"""Keypair action implementations"""
|
|
|
|
|
2016-02-21 08:30:54 +08:00
|
|
|
import io
|
2016-06-27 11:04:05 +08:00
|
|
|
import logging
|
2013-04-12 17:01:38 -05:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2016-05-13 17:27:12 -05:00
|
|
|
from osc_lib.command import command
|
2016-06-08 14:17:14 -05:00
|
|
|
from osc_lib import exceptions
|
2016-05-13 16:14:09 -05:00
|
|
|
from osc_lib import utils
|
2016-06-08 14:17:14 -05:00
|
|
|
import six
|
|
|
|
|
2016-05-14 15:12:57 +08:00
|
|
|
from openstackclient.i18n import _
|
2013-04-12 17:01:38 -05:00
|
|
|
|
|
|
|
|
2016-06-27 11:04:05 +08:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class CreateKeypair(command.ShowOne):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Create new public or private key for server ssh access")
|
2013-04-12 17:01:38 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(CreateKeypair, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'name',
|
|
|
|
metavar='<name>',
|
2016-10-24 10:55:10 +02:00
|
|
|
help=_("New public or private key name")
|
2013-04-12 17:01:38 -05:00
|
|
|
)
|
2017-02-27 14:35:05 +08:00
|
|
|
key_group = parser.add_mutually_exclusive_group()
|
|
|
|
key_group.add_argument(
|
2013-04-12 17:01:38 -05:00
|
|
|
'--public-key',
|
|
|
|
metavar='<file>',
|
2016-10-24 10:55:10 +02:00
|
|
|
help=_("Filename for public key to add. If not used, "
|
|
|
|
"creates a private key.")
|
2013-04-12 17:01:38 -05:00
|
|
|
)
|
2017-02-27 14:35:05 +08:00
|
|
|
key_group.add_argument(
|
|
|
|
'--private-key',
|
|
|
|
metavar='<file>',
|
|
|
|
help=_("Filename for private key to save. If not used, "
|
|
|
|
"print private key in console.")
|
|
|
|
)
|
2013-04-12 17:01:38 -05:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
compute_client = self.app.client_manager.compute
|
|
|
|
|
|
|
|
public_key = parsed_args.public_key
|
|
|
|
if public_key:
|
|
|
|
try:
|
2016-03-18 11:36:33 -05:00
|
|
|
with io.open(os.path.expanduser(parsed_args.public_key)) as p:
|
2013-04-12 17:01:38 -05:00
|
|
|
public_key = p.read()
|
|
|
|
except IOError as e:
|
2016-05-14 15:12:57 +08:00
|
|
|
msg = _("Key file %(public_key)s not found: %(exception)s")
|
|
|
|
raise exceptions.CommandError(
|
|
|
|
msg % {"public_key": parsed_args.public_key,
|
|
|
|
"exception": e}
|
|
|
|
)
|
2013-04-12 17:01:38 -05:00
|
|
|
|
|
|
|
keypair = compute_client.keypairs.create(
|
|
|
|
parsed_args.name,
|
|
|
|
public_key=public_key,
|
|
|
|
)
|
|
|
|
|
2017-02-27 14:35:05 +08:00
|
|
|
private_key = parsed_args.private_key
|
|
|
|
# Save private key into specified file
|
|
|
|
if private_key:
|
|
|
|
try:
|
|
|
|
with io.open(
|
|
|
|
os.path.expanduser(parsed_args.private_key), 'w+'
|
|
|
|
) as p:
|
|
|
|
p.write(keypair.private_key)
|
|
|
|
except IOError as e:
|
|
|
|
msg = _("Key file %(private_key)s can not be saved: "
|
|
|
|
"%(exception)s")
|
|
|
|
raise exceptions.CommandError(
|
|
|
|
msg % {"private_key": parsed_args.private_key,
|
|
|
|
"exception": e}
|
|
|
|
)
|
2013-04-12 17:01:38 -05:00
|
|
|
# NOTE(dtroyer): how do we want to handle the display of the private
|
|
|
|
# key when it needs to be communicated back to the user
|
|
|
|
# For now, duplicate nova keypair-add command output
|
|
|
|
info = {}
|
2017-02-27 14:35:05 +08:00
|
|
|
if public_key or private_key:
|
2013-04-12 17:01:38 -05:00
|
|
|
info.update(keypair._info)
|
2017-02-27 14:35:05 +08:00
|
|
|
if 'public_key' in info:
|
|
|
|
del info['public_key']
|
|
|
|
if 'private_key' in info:
|
|
|
|
del info['private_key']
|
2013-07-03 18:12:58 -05:00
|
|
|
return zip(*sorted(six.iteritems(info)))
|
2013-04-12 17:01:38 -05:00
|
|
|
else:
|
|
|
|
sys.stdout.write(keypair.private_key)
|
|
|
|
return ({}, {})
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteKeypair(command.Command):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Delete public or private key(s)")
|
2013-04-12 17:01:38 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(DeleteKeypair, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'name',
|
2014-11-17 17:52:37 -06:00
|
|
|
metavar='<key>',
|
2016-06-27 11:04:05 +08:00
|
|
|
nargs='+',
|
2016-10-24 10:55:10 +02:00
|
|
|
help=_("Name of key(s) to delete (name only)")
|
2013-04-12 17:01:38 -05:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
compute_client = self.app.client_manager.compute
|
2016-06-27 11:04:05 +08:00
|
|
|
result = 0
|
|
|
|
for n in parsed_args.name:
|
|
|
|
try:
|
|
|
|
data = utils.find_resource(
|
|
|
|
compute_client.keypairs, n)
|
|
|
|
compute_client.keypairs.delete(data.name)
|
|
|
|
except Exception as e:
|
|
|
|
result += 1
|
2016-10-24 10:55:10 +02:00
|
|
|
LOG.error(_("Failed to delete key with name "
|
2017-02-13 18:41:24 +01:00
|
|
|
"'%(name)s': %(e)s"), {'name': n, 'e': e})
|
2016-06-27 11:04:05 +08:00
|
|
|
|
|
|
|
if result > 0:
|
|
|
|
total = len(parsed_args.name)
|
2016-10-24 10:55:10 +02:00
|
|
|
msg = (_("%(result)s of %(total)s keys failed "
|
2016-06-27 11:04:05 +08:00
|
|
|
"to delete.") % {'result': result, 'total': total})
|
|
|
|
raise exceptions.CommandError(msg)
|
2013-04-12 17:01:38 -05:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ListKeypair(command.Lister):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("List key fingerprints")
|
2013-04-12 17:01:38 -05:00
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
compute_client = self.app.client_manager.compute
|
|
|
|
columns = (
|
|
|
|
"Name",
|
|
|
|
"Fingerprint"
|
|
|
|
)
|
|
|
|
data = compute_client.keypairs.list()
|
|
|
|
|
|
|
|
return (columns,
|
|
|
|
(utils.get_item_properties(
|
|
|
|
s, columns,
|
|
|
|
) for s in data))
|
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ShowKeypair(command.ShowOne):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Display key details")
|
2013-04-12 17:01:38 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ShowKeypair, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'name',
|
2014-11-17 17:52:37 -06:00
|
|
|
metavar='<key>',
|
2016-10-24 10:55:10 +02:00
|
|
|
help=_("Public or private key to display (name only)")
|
2013-04-12 17:01:38 -05:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--public-key',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2016-10-24 10:55:10 +02:00
|
|
|
help=_("Show only bare public key paired with the generated key")
|
2013-04-12 17:01:38 -05:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
compute_client = self.app.client_manager.compute
|
|
|
|
keypair = utils.find_resource(compute_client.keypairs,
|
|
|
|
parsed_args.name)
|
|
|
|
|
|
|
|
info = {}
|
2014-03-07 15:34:28 -06:00
|
|
|
info.update(keypair._info)
|
2013-04-12 17:01:38 -05:00
|
|
|
if not parsed_args.public_key:
|
|
|
|
del info['public_key']
|
2013-07-03 18:12:58 -05:00
|
|
|
return zip(*sorted(six.iteritems(info)))
|
2013-04-12 17:01:38 -05:00
|
|
|
else:
|
|
|
|
# NOTE(dtroyer): a way to get the public key in a similar form
|
|
|
|
# as the private key in the create command
|
|
|
|
sys.stdout.write(keypair.public_key)
|
|
|
|
return ({}, {})
|