def implementation_of_do_action(self, request, signer=None): if not isinstance(request, AcsRequest): raise ClientException(error_code.SDK_INVALID_REQUEST, error_msg.get_msg('SDK_INVALID_REQUEST')) # add core version core_version = "2.9.0" request.add_header('x-sdk-core-version', core_version) if isinstance(request, CommonRequest): request.trans_to_acs_request() endpoint = self._resolve_endpoint(request) http_response = self._make_http_response(endpoint, request, signer) if self._url_test_flag: raise ClientException("URLTestFlagIsSet", http_response.get_url()) # Do the actual network thing try: status, headers, body = http_response.get_response_object() return status, headers, body except IOError as e: raise ClientException( error_code.SDK_SERVER_UNREACHABLE, error_msg.get_msg('SDK_SERVER_UNREACHABLE') + ': ' + str(e))
def _parse_error_info_from_response_body(response_body): try: body_obj = json.loads(response_body.decode('utf-8')) if 'Code' in body_obj and 'Message' in body_obj: return body_obj['Code'], body_obj['Message'] else: return (error_code.SDK_UNKNOWN_SERVER_ERROR, response_body) except ValueError: # failed to parse body as json format return (error_code.SDK_UNKNOWN_SERVER_ERROR, error_msg.get_msg('SDK_UNKNOWN_SERVER_ERROR'))
def resolve_endpoint(region_id, request, location_service): param = dict() param['regionId'] = region_id param['product'] = request.get_product() param['locationProduct'] = request.get_location_service_code() param['locationService'] = location_service param['endpointType'] = request.get_location_endpoint_type() if hasattr(request, 'get_domain'): param['requestDomain'] = request.get_domain() for resolver in resolvers: supported, endpoint = resolver.resolve_endpoint(param) if supported: return endpoint raise ClientException( error_code.SDK_INVALID_REGION_ID, error_msg.get_msg('SDK_INVALID_REGION_ID'))
def _get_session_ak_and_sk(self): request = GetSessionAkRequest() request.set_method("GET") request.set_duration_seconds(self._session_period) try: response_str = self._sts_client.do_action_with_exception(request) response = json.loads(response_str.decode('utf-8')) session_ak = str(response.get("SessionAccessKey").get("SessionAccessKeyId")) session_sk = str(response.get("SessionAccessKey").get("SessionAccessKeySecret")) self._session_credential = session_ak, session_sk except exceptions.ServerException as srv_ex: if srv_ex.error_code == 'InvalidAccessKeyId.NotFound' or srv_ex.error_code == 'SignatureDoesNotMatch': raise exceptions.ClientException(error_code.SDK_INVALID_CREDENTIAL, error_msg.get_msg('SDK_INVALID_CREDENTIAL')) else: raise
def __init__(self, rsa_key_pair_credential, region_id, debug=False): if not debug and rsa_key_pair_credential.session_period < self._MIN_SESSION_PERIOD \ or rsa_key_pair_credential.session_period > self._MAX_SESSION_PERIOD: raise exceptions.ClientException( error_code.SDK_INVALID_SESSION_EXPIRATION, error_msg.get_msg('SDK_INVALID_SESSION_EXPIRATION').format(self._MIN_SESSION_PERIOD, self._MAX_SESSION_PERIOD)) rsa_key_pair_credential.region_id = region_id self._public_key_id = rsa_key_pair_credential.public_key_id self._private_key = rsa_key_pair_credential.private_key self._session_period = rsa_key_pair_credential.session_period self._schedule_interval = rsa_key_pair_credential.session_period if debug \ else max(rsa_key_pair_credential.session_period * 0.8, 5) from aliyunsdkcore.client import AcsClient self._sts_client = AcsClient(self._public_key_id, self._private_key, rsa_key_pair_credential.region_id) self._session_credential = None self._get_session_ak_and_sk() self._scheduler = sched.scheduler(time.time, time.sleep) self._daemon_thread = threading.Thread(target=self._refresh_session_ak_and_sk, args=[True, 0]) self._daemon_thread.setDaemon(True) self._daemon_thread.start()
def get_signer(cred, region_id, do_action_api, debug=False): if cred['ak'] is not None and cred['secret'] is not None: access_key_credential = credentials.AccessKeyCredential( cred['ak'], cred['secret']) return access_key_signer.AccessKeySigner(access_key_credential) elif os.environ.get( 'ALIYUN_ACCESS_KEY_ID') is not None and os.environ.get( 'ALIYUN_ACCESS_KEY_SECRET') is not None: access_key_credential = credentials.AccessKeyCredential( os.environ.get('ALIYUN_ACCESS_KEY_ID'), os.environ.get('ALIYUN_ACCESS_KEY_SECRET')) return access_key_signer.AccessKeySigner(access_key_credential) elif cred['credential'] is not None: credential = cred['credential'] if isinstance(credential, credentials.AccessKeyCredential): return access_key_signer.AccessKeySigner(credential) elif isinstance(credential, credentials.StsTokenCredential): return sts_token_signer.StsTokenSigner(credential) elif isinstance(credential, credentials.RamRoleArnCredential): return ram_role_arn_signer.RamRoleArnSigner( credential, do_action_api) elif isinstance(credential, credentials.EcsRamRoleCredential): return ecs_ram_role_singer.EcsRamRoleSigner(credential) elif isinstance(credential, credentials.RsaKeyPairCredential): return rsa_key_pair_signer.RsaKeyPairSigner( credential, region_id, debug) elif cred['public_key_id'] is not None and cred[ 'private_key'] is not None: logging.info( "'AcsClient(regionId, pub_key_id, pri_key)' is deprecated") rsa_key_pair_credential = credentials.RsaKeyPairCredential( cred['public_key_id'], cred['private_key'], cred['session_period'], region_id, debug) return rsa_key_pair_signer.RsaKeyPairSigner( rsa_key_pair_credential) else: raise exceptions.ClientException( error_code.SDK_INVALID_CREDENTIAL, error_msg.get_msg('SDK_INVALID_CREDENTIAL'))