"Fossies" - the Fresh Open Source Software Archive

Member "ec2-api-12.0.0/ec2api/api/key_pair.py" (14 Apr 2021, 3924 Bytes) of package /linux/misc/openstack/ec2-api-12.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "key_pair.py" see the Fossies "Dox" file reference documentation.

    1 # Copyright 2014
    2 # The Cloudscaling Group, Inc.
    3 #
    4 # Licensed under the Apache License, Version 2.0 (the "License");
    5 # you may not use this file except in compliance with the License.
    6 # You may obtain a copy of the License at
    7 # http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS,
   11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12 # See the License for the specific language governing permissions and
   13 # limitations under the License.
   14 
   15 import base64
   16 
   17 from novaclient import exceptions as nova_exception
   18 from oslo_config import cfg
   19 from oslo_log import log as logging
   20 
   21 from ec2api.api import common
   22 from ec2api import clients
   23 from ec2api import exception
   24 from ec2api.i18n import _
   25 
   26 
   27 CONF = cfg.CONF
   28 LOG = logging.getLogger(__name__)
   29 
   30 
   31 """Keypair-object related API implementation
   32 """
   33 
   34 
   35 Validator = common.Validator
   36 
   37 
   38 class KeyPairDescriber(common.UniversalDescriber):
   39 
   40     KIND = 'kp'
   41     FILTER_MAP = {'fingerprint': 'keyFingerprint',
   42                   'key-name': 'keyName'}
   43 
   44     def format(self, _item, key_pair):
   45         return _format_key_pair(key_pair)
   46 
   47     def get_db_items(self):
   48         return []
   49 
   50     def get_os_items(self):
   51         # Original EC2 in nova filters out vpn keys for admin user.
   52         # We're not filtering out the vpn keys for now.
   53         # In order to implement this we'd have to configure vpn_key_suffix
   54         # in our config which we consider an overkill.
   55         # suffix = CONF.vpn_key_suffix
   56         # if context.is_admin or not key_pair['name'].endswith(suffix):
   57         nova = clients.nova(self.context)
   58         return nova.keypairs.list()
   59 
   60     def auto_update_db(self, item, os_item):
   61         pass
   62 
   63     def get_id(self, os_item):
   64         return ''
   65 
   66     def get_name(self, key_pair):
   67         return key_pair.name
   68 
   69 
   70 def describe_key_pairs(context, key_name=None, filter=None):
   71     formatted_key_pairs = KeyPairDescriber().describe(context, names=key_name,
   72                                                       filter=filter)
   73     return {'keySet': formatted_key_pairs}
   74 
   75 
   76 def _validate_name(name):
   77     if len(name) > 255:
   78         raise exception.InvalidParameterValue(
   79             value=name,
   80             parameter='KeyName',
   81             reason='lenght is exceeds maximum of 255')
   82 
   83 
   84 def create_key_pair(context, key_name):
   85     _validate_name(key_name)
   86     nova = clients.nova(context)
   87     try:
   88         key_pair = nova.keypairs.create(key_name)
   89     except nova_exception.OverLimit:
   90         raise exception.ResourceLimitExceeded(resource='keypairs')
   91     except nova_exception.Conflict:
   92         raise exception.InvalidKeyPairDuplicate(key_name=key_name)
   93     formatted_key_pair = _format_key_pair(key_pair)
   94     formatted_key_pair['keyMaterial'] = key_pair.private_key
   95     return formatted_key_pair
   96 
   97 
   98 def import_key_pair(context, key_name, public_key_material):
   99     _validate_name(key_name)
  100     if not public_key_material:
  101         raise exception.MissingParameter(
  102             _('The request must contain the parameter PublicKeyMaterial'))
  103     nova = clients.nova(context)
  104     public_key = base64.b64decode(public_key_material).decode("utf-8")
  105     try:
  106         key_pair = nova.keypairs.create(key_name, public_key)
  107     except nova_exception.OverLimit:
  108         raise exception.ResourceLimitExceeded(resource='keypairs')
  109     except nova_exception.Conflict:
  110         raise exception.InvalidKeyPairDuplicate(key_name=key_name)
  111     return _format_key_pair(key_pair)
  112 
  113 
  114 def delete_key_pair(context, key_name):
  115     nova = clients.nova(context)
  116     try:
  117         nova.keypairs.delete(key_name)
  118     except nova_exception.NotFound:
  119         # aws returns true even if the key doesn't exist
  120         pass
  121     return True
  122 
  123 
  124 def _format_key_pair(key_pair):
  125     return {'keyName': key_pair.name,
  126             'keyFingerprint': key_pair.fingerprint
  127             }