key_pair.py (ec2-api-14.0.1) | : | key_pair.py (ec2-api-15.0.0) | ||
---|---|---|---|---|
skipping to change at line 17 | skipping to change at line 17 | |||
# http://www.apache.org/licenses/LICENSE-2.0 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
# | # | |||
# Unless required by applicable law or agreed to in writing, software | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | # distributed under the License is distributed on an "AS IS" BASIS, | |||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
# See the License for the specific language governing permissions and | # See the License for the specific language governing permissions and | |||
# limitations under the License. | # limitations under the License. | |||
import base64 | import base64 | |||
from cryptography.hazmat import backends | ||||
from cryptography.hazmat.primitives.asymmetric import rsa | ||||
from cryptography.hazmat.primitives import serialization as crypt_serialization | ||||
from novaclient import exceptions as nova_exception | from novaclient import exceptions as nova_exception | |||
from oslo_config import cfg | from oslo_config import cfg | |||
from oslo_log import log as logging | from oslo_log import log as logging | |||
from ec2api.api import common | from ec2api.api import common | |||
from ec2api import clients | from ec2api import clients | |||
from ec2api import exception | from ec2api import exception | |||
from ec2api.i18n import _ | from ec2api.i18n import _ | |||
CONF = cfg.CONF | CONF = cfg.CONF | |||
skipping to change at line 77 | skipping to change at line 80 | |||
filter=filter) | filter=filter) | |||
return {'keySet': formatted_key_pairs} | return {'keySet': formatted_key_pairs} | |||
def _validate_name(name): | def _validate_name(name): | |||
if len(name) > 255: | if len(name) > 255: | |||
raise exception.InvalidParameterValue( | raise exception.InvalidParameterValue( | |||
value=name, | value=name, | |||
parameter='KeyName', | parameter='KeyName', | |||
reason='lenght is exceeds maximum of 255') | reason='lenght is exceeds maximum of 255') | |||
# We may wish to make the algorithm configurable. This would require API | ||||
# changes. | ||||
def _generate_key_pair(): | ||||
key = rsa.generate_private_key( | ||||
backend=backends.default_backend(), | ||||
public_exponent=65537, | ||||
key_size=2048 | ||||
) | ||||
private_key = key.private_bytes( | ||||
crypt_serialization.Encoding.PEM, | ||||
crypt_serialization.PrivateFormat.TraditionalOpenSSL, | ||||
crypt_serialization.NoEncryption(), | ||||
).decode() | ||||
public_key = key.public_key().public_bytes( | ||||
crypt_serialization.Encoding.OpenSSH, | ||||
crypt_serialization.PublicFormat.OpenSSH, | ||||
).decode() | ||||
return private_key, public_key | ||||
def create_key_pair(context, key_name): | def create_key_pair(context, key_name): | |||
_validate_name(key_name) | _validate_name(key_name) | |||
nova = clients.nova(context) | nova = clients.nova(context) | |||
private_key, public_key = _generate_key_pair() | ||||
try: | try: | |||
key_pair = nova.keypairs.create(key_name) | key_pair = nova.keypairs.create(key_name, public_key) | |||
except nova_exception.OverLimit: | except nova_exception.OverLimit: | |||
raise exception.ResourceLimitExceeded(resource='keypairs') | raise exception.ResourceLimitExceeded(resource='keypairs') | |||
except nova_exception.Conflict: | except nova_exception.Conflict: | |||
raise exception.InvalidKeyPairDuplicate(key_name=key_name) | raise exception.InvalidKeyPairDuplicate(key_name=key_name) | |||
formatted_key_pair = _format_key_pair(key_pair) | formatted_key_pair = _format_key_pair(key_pair) | |||
formatted_key_pair['keyMaterial'] = key_pair.private_key | formatted_key_pair['keyMaterial'] = private_key | |||
return formatted_key_pair | return formatted_key_pair | |||
def import_key_pair(context, key_name, public_key_material): | def import_key_pair(context, key_name, public_key_material): | |||
_validate_name(key_name) | _validate_name(key_name) | |||
if not public_key_material: | if not public_key_material: | |||
raise exception.MissingParameter( | raise exception.MissingParameter( | |||
_('The request must contain the parameter PublicKeyMaterial')) | _('The request must contain the parameter PublicKeyMaterial')) | |||
nova = clients.nova(context) | nova = clients.nova(context) | |||
public_key = base64.b64decode(public_key_material).decode("utf-8") | public_key = base64.b64decode(public_key_material).decode("utf-8") | |||
try: | try: | |||
End of changes. 5 change blocks. | ||||
2 lines changed or deleted | 25 lines changed or added |