Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 18 | import logging |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 19 | import os |
| 20 | import re |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 21 | import urlparse |
| 22 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 23 | import boto.exception |
| 24 | import keystoneclient.exceptions |
| 25 | |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 26 | import tempest.clients |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 27 | from tempest.common.utils.file_utils import have_effective_read_access |
| 28 | import tempest.config |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 29 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 30 | A_I_IMAGES_READY = False # ari,ami,aki |
| 31 | S3_CAN_CONNECT_ERROR = "Unknown Error" |
| 32 | EC2_CAN_CONNECT_ERROR = "Unknown Error" |
| 33 | |
| 34 | |
| 35 | def setup_package(): |
| 36 | global A_I_IMAGES_READY |
| 37 | global S3_CAN_CONNECT_ERROR |
| 38 | global EC2_CAN_CONNECT_ERROR |
| 39 | secret_matcher = re.compile("[A-Za-z0-9+/]{32,}") # 40 in other system |
| 40 | id_matcher = re.compile("[A-Za-z0-9]{20,}") |
| 41 | |
| 42 | def all_read(*args): |
| 43 | return all(map(have_effective_read_access, args)) |
| 44 | |
| 45 | config = tempest.config.TempestConfig() |
| 46 | materials_path = config.boto.s3_materials_path |
| 47 | ami_path = materials_path + os.sep + config.boto.ami_manifest |
| 48 | aki_path = materials_path + os.sep + config.boto.aki_manifest |
| 49 | ari_path = materials_path + os.sep + config.boto.ari_manifest |
| 50 | |
| 51 | A_I_IMAGES_READY = all_read(ami_path, aki_path, ari_path) |
| 52 | boto_logger = logging.getLogger('boto') |
| 53 | level = boto_logger.level |
| 54 | boto_logger.setLevel(logging.CRITICAL) # suppress logging for these |
| 55 | |
| 56 | def _cred_sub_check(connection_data): |
| 57 | if not id_matcher.match(connection_data["aws_access_key_id"]): |
| 58 | raise Exception("Invalid AWS access Key") |
| 59 | if not secret_matcher.match(connection_data["aws_secret_access_key"]): |
| 60 | raise Exception("Invalid AWS secret Key") |
| 61 | raise Exception("Unknown (Authentication?) Error") |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 62 | openstack = tempest.clients.Manager() |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 63 | try: |
| 64 | if urlparse.urlparse(config.boto.ec2_url).hostname is None: |
| 65 | raise Exception("Failed to get hostname from the ec2_url") |
| 66 | ec2client = openstack.ec2api_client |
| 67 | try: |
| 68 | ec2client.get_all_regions() |
| 69 | except boto.exception.BotoServerError as exc: |
| 70 | if exc.error_code is None: |
| 71 | raise Exception("EC2 target does not looks EC2 service") |
| 72 | _cred_sub_check(ec2client.connection_data) |
| 73 | |
| 74 | except keystoneclient.exceptions.Unauthorized: |
| 75 | EC2_CAN_CONNECT_ERROR = "AWS credentials not set," +\ |
| 76 | " faild to get them even by keystoneclient" |
| 77 | except Exception as exc: |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 78 | EC2_CAN_CONNECT_ERROR = str(exc) |
| 79 | else: |
| 80 | EC2_CAN_CONNECT_ERROR = None |
| 81 | |
| 82 | try: |
| 83 | if urlparse.urlparse(config.boto.s3_url).hostname is None: |
| 84 | raise Exception("Failed to get hostname from the s3_url") |
| 85 | s3client = openstack.s3_client |
| 86 | try: |
| 87 | s3client.get_bucket("^INVALID*#()@INVALID.") |
| 88 | except boto.exception.BotoServerError as exc: |
| 89 | if exc.status == 403: |
| 90 | _cred_sub_check(s3client.connection_data) |
| 91 | except Exception as exc: |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 92 | S3_CAN_CONNECT_ERROR = str(exc) |
| 93 | except keystoneclient.exceptions.Unauthorized: |
| 94 | S3_CAN_CONNECT_ERROR = "AWS credentials not set," +\ |
| 95 | " faild to get them even by keystoneclient" |
| 96 | else: |
| 97 | S3_CAN_CONNECT_ERROR = None |
| 98 | boto_logger.setLevel(level) |