def get_file_with_creds(bucket, key, access_key, secret_key): """ Get a file from S3 using Specific Credentials :param bucket: name of bucket :param key: key id in bucket :param access_key: user's access_key :param secret_key: user's secret_key :return: Stream object with contents :raise: AWSError: couldn't fetch file contents from S3 """ if key is None or len(key) <= 0: raise ValidationError("Key name cannot be empty.") if bucket is None or len(bucket) <= 0: raise ValidationError("Bucket name cannot be empty.") s3 = boto3.client("s3", aws_access_key_id=access_key, aws_secret_access_key=secret_key) try: response = s3.get_object(Bucket=bucket, Key=key) except ClientError as error: raise AWSError("Error loading file from S3: {}".format(str(error))) return response
def validateProposalMetadata(pmd): typecheck(pmd.proposal, [Proposal]) if len(data.all_data)>1: typecheck(pmd.team, [MemberDict]) else: typecheck(pmd.team, [type(None)]) for s in pmd.supersede: typecheck(s, [Proposal]) for c in pmd.confirm: typecheck(c, [Election]) dupcheck(pmd.supersede, "proposal meta data supersede list") dupcheck(pmd.confirm, "proposal meta data confirm list") dupcheck(pmd.ballot_options, "proposal meta data ballot options") dupcheck(pmd.voting_methods, "proposal meta data validation methods") for vm in pmd.voting_methods: if vm not in vv_methods: raise ValidationError("Proposal meta data refers to unknown validation method %s." % vm) if not len(pmd.title): raise ValidationError("Proposal needs a title.") if pmd.team==None: # genesis membership proposal meta data? if len(data.all_data)>1: raise ValidationError("Genesis member list meta data must be second item.") if pmd.proposal_hash!=data.genesis_members_hash: raise ValidationError("Only genesis member vote allowed.")
def put_file_with_creds(bucket, key, content, access_key, secret_key): """ Get a file from S3 using Specific Credentials :param bucket: name of bucket :param key: key id in bucket :param content: file body content :param access_key: user's access_key :param secret_key: user's secret_key :raise: AWSError: couldn't fetch file contents from S3 """ if key is None or len(key) <= 0: raise ValidationError("Key name cannot be empty.") if bucket is None or len(bucket) <= 0: raise ValidationError("Bucket name cannot be empty.") s3 = boto3.client("s3", aws_access_key_id=access_key, aws_secret_access_key=secret_key) try: s3.put_object( Bucket=bucket, Key=key, Body=content) except ClientError as error: raise AWSError("Problem putting {} from {} bucket ({})" .format(key, bucket, str(error))) return
def send_file(token, chat_id, text, file_bucket, file_key, config=None): """ Returns a file to the user using the S3 link provided :param token: telegram api key :param chat_id: ID of the chat with the user :param text: text to be sent with the link :param file_bucket: bucket of the file in S3 :param file_key: key of file to send in S3 :param config: if we should use file_id :return: response from Telegram API call :raise: TelegramError: when Telegram API call fails """ if file_bucket is None or len(file_bucket) == "": raise ValidationError("S3 Bucket name is empty") if file_key is None or len(file_key) == "": raise ValidationError("S3 Key name is empty") if text is None or len(text) <= 0: raise ValidationError("Text cannot be empty") if config is not None: # Bypass file_id when we are proxying the file _send_document_from_s3(token, chat_id, file_bucket, file_key, config) return metadata = storage.get_object_metadata(file_bucket, file_key).metadata if "file_id" in metadata: # send file_id file_id = metadata["file_id"] _send_document_cached(token, chat_id, file_bucket, file_key, file_id) else: _send_document_from_s3(token, chat_id, file_bucket, file_key, config)
def _get_metadata_for_role(resource, policy_name, policy_metadata_filter): metadata = discovery_utils.get_cloud_canvas_metadata(resource, policy_name) if metadata is None: return None if isinstance(metadata, dict): metadata = [metadata] if not isinstance(metadata, list): raise ValidationError( '{} metadata not an object or list on resource {} in stack {}.'. format(policy_name, resource['LogicalResourceId'], stack_arn)) entry_found = None for entry in metadata: try: entry_accepted = policy_metadata_filter(entry) except ValidationError as e: raise ValidationError( 'Invalid {} metadata entry was found on resource {} in stack {}. {}' .format(policy_name, logical_resource_name, stack_arn, e.message)) if entry_accepted: if entry_found is not None: raise ValidationError( 'More than one applicable {} metadata entry was found on resource {} in stack {}.' .format(policy_name, logical_resource_name, stack_arn)) entry_found = entry return entry_found
def __init__(self, a, b, n): """ INPUT a int b int """ if (a < 1) or (b < 1): raise ValidationError( "Try different pairs of a and b", "a or b is no more than 1" ) if n < 1: raise ValidationError( "Try different number_of_iteration", "number_of_iteration is too small" ) self.__a = a self.__b = b self.__n = n self.__map = {} if a == 1 and b == 1: self.__type = 0 elif a == b: self.__type = 1 else: self.__type = 2 self.__A = np.array([ [1, a], [b, 1 + a * b] ], np.dtype('B'))
def __call__(self, name, value): if value is None: value = self.default if value is None: raise ValidationError( 'A value for property {} must be provided.'.format(name)) if isinstance(value, basestring): return [value] else: if not isinstance(value, list): raise ValidationError( 'The {} property value must be a string or a list of strings.' .format(name)) for entry in value: if not isinstance(entry, basestring): raise ValidationError( 'The {} property must be a string or a list of strings.' .format(name)) return value
def _validate_setpoint(self, name, value): try: value = int(value) except (TypeError, ValueError): raise ValidationError(name, 'value must be an integer') if value < 30 or value > 100: raise ValidationError(name, 'value must be in the range 30-100')
def handler(event, context): props = properties.load(event, { 'ConfigurationBucket': properties.String(), # Currently not used 'ConfigurationKey': properties.String(), # Depend on unique upload id in key to force Cloud Formation to call handler 'RoleLogicalId': properties.String(), 'MetadataKey': properties.String(), 'PhysicalResourceId': properties.String(), 'UsePropagationDelay': properties.String(), 'RequireRoleExists': properties.String(default='true'), 'ResourceGroupStack': properties.String(default=''), 'DeploymentStack': properties.String(default='')}) if props.ResourceGroupStack is '' and props.DeploymentStack is '': raise ValidationError('A value for the ResourceGroupStack property or the DeploymentStack property must be provided.') if props.ResourceGroupStack is not '' and props.DeploymentStack is not '': raise ValidationError('A value for only the ResourceGroupStack property or the DeploymentStack property can be provided.') use_propagation_delay = props.UsePropagationDelay.lower() == 'true' data = {} stack_infos = [] if props.ResourceGroupStack is not '': resource_group_info = stack_info.ResourceGroupInfo(props.ResourceGroupStack) # create a list of stack-infos, starting at the resource group level and working our way upward stack_infos = _build_stack_infos_list(resource_group_info) else: # DeploymentStack deployment_info = stack_info.DeploymentInfo(props.DeploymentStack) # create a list of stack-infos, starting at the deployment level and working our way upward stack_infos = _build_stack_infos_list(deployment_info) # go through each of the stack infos, trying to find the specified role for stack in stack_infos: role = stack.resources.get_by_logical_id(props.RoleLogicalId, expected_type='AWS::IAM::Role', optional=True) if role is not None: break role_physical_id = None if role is not None: role_physical_id = role.physical_id if role_physical_id is None: if props.RequireRoleExists.lower() == 'true': raise ValidationError('Could not find role \'{}\'.'.format(props.RoleLogicalId)) else: if type(stack_infos[0]) is stack_info.ResourceGroupInfo: _process_resource_group_stack(event['RequestType'], stack_infos[0], role_physical_id, props.MetadataKey, use_propagation_delay) else: for resource_group_info in stack_infos[0].resource_groups: _process_resource_group_stack(event['RequestType'], resource_group_info, role_physical_id, props.MetadataKey, use_propagation_delay) custom_resource_response.succeed(event, context, data, props.PhysicalResourceId)
def __call__(self, value): if value is None: return if self.size is not None and len(value) != self.size: raise ValidationError("length must be exactly {}".format(self.size)) if self.minimum is not None and len(value) < self.minimum: raise ValidationError("minimum length is {}".format(self.minimum)) if self.maximum is not None and len(value) > self.maximum: raise ValidationError("maximum length is {}".format(self.maximum))
def _make_resource_statement(resource_group_info, logical_resource_name, metadata_key): try: response = discovery_utils.try_with_backoff( lambda: resource_group_info.get_client().describe_stack_resource( StackName=resource_group_info.stack_arn, LogicalResourceId=logical_resource_name)) print 'describe_stack_resource(LogicalResourceId="{}", StackName="{}") response: {}'.format( logical_resource_name, resource_group_info.stack_arn, response) except Exception as e: print 'describe_stack_resource(LogicalResourceId="{}", StackName="{}") error: {}'.format( logical_resource_name, resource_group_info.stack_arn, getattr(e, 'response', e)) raise e resource = response['StackResourceDetail'] metadata = discovery_utils.get_cloud_canvas_metadata( resource, metadata_key) if metadata is None: return None metadata_actions = metadata.get('Action', None) if metadata_actions is None: raise ValidationError( 'No Action was specified for CloudCanvas Access metdata on the {} resource in stack {}.' .format(logical_resource_name, resource_group_info.stack_arn)) if not isinstance(metadata_actions, list): metadata_actions = [metadata_actions] for action in metadata_actions: if not isinstance(action, basestring): raise ValidationError( 'Non-string Action specified for CloudCanvas Access metadata on the {} resource in stack {}.' .format(logical_resource_name, resource_group_info.stack_arn)) if 'PhysicalResourceId' not in resource: return None if 'ResourceType' not in resource: return None resource = discovery_utils.get_resource_arn(resource_group_info.stack_arn, resource['ResourceType'], resource['PhysicalResourceId']) resource_suffix = metadata.get('ResourceSuffix', None) if resource_suffix is not None: resource += resource_suffix return { 'Sid': logical_resource_name + 'Access', 'Effect': 'Allow', 'Action': metadata_actions, 'Resource': resource }
def post(self, document, context=None, root_doc=None): if context is None: context = {} if root_doc is None: root_doc = document schema = self.schema c = schema.get('__create_document', lambda *args: True) if not c(root_doc): raise Exception('can not create document') ret = {} set_document = set(document.keys()) set_schema = set(schema.keys()) - set(self.kw) intersection = set_document & set_schema missing = set_schema - set_document if len(set_document - set_schema) > 0: raise Exception('keywords not in schema') for key in missing | intersection: if schema[key]['type'].__class__ == Schema: if document.get(key): ret[key] = schema[key]['type'].post( document[key], context, root_doc) elif type(schema[key]['type']) is list: #schema = schema[key]['type'][0] if document.get(key): ret[key] = [ schema[key]['type'][0].post(k, context, root_doc) for k in document[key] ] elif 'computed' not in schema[key]: validation = schema[key].get('validation', public) required = schema[key].get('required', False) mtype = schema[key]['type'] initial = schema[key].get('initial') initial = initial and initial(root_doc) #initial(context) v = document.get(key, initial) if required and v is None: raise ValidationError('required') ##if v is not None and (not type(v) is mtype or not validation(v)): if v is not None and (not isinstance(v, mtype) or not validation(v)): raise ValidationError('not valid prop or missing', key) if key in intersection or initial is not None: ret[key] = document.get(key, initial) else: create = schema[key].get('computed') val = create(document) ret[key] = val return ret
def verify_auth_token(token): """"Verify authentication token""" s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except SignatureExpired: raise ValidationError('Expired Token') except BadSignature: raise ValidationError('Invalid Token') return User.query.get(data['id'])
def _policy_metadata_filter(entry, function_name): metadata_function_name = entry.get('FunctionName', None) if not metadata_function_name: raise ValidationError('No FunctionName specified.') if not isinstance(metadata_function_name, basestring): raise ValidationError('Non-string FunctionName specified.') return metadata_function_name == function_name
def __call__(self, *args, **kwargs): # Variables used to format error messages func, param = self.context.get('func', '?'), self.context.get('param', '?') if self.sig is not None: # Callable signature avaliable # Bound arguments to the callable signature bounded_args = self.sig.bind(*args, **kwargs) bounded_args.apply_defaults() args = list(bounded_args.args) param_validators = self.validator.children[:-1] if len(args) != len(param_validators): # Incorrect number of args raise ValidationError( message='{} expects {} arguments but got {} instead'. format(param, len(args), len(param_validators)), func=func) # Validate each argument for k, arg, validator in zip(count(), args, param_validators): try: args[k] = validator.validate( arg, context={ 'func': func, 'param': '{} argument of {}'.format(ordinal(k + 1), param) }) except ValidationError: raise ValidationError( message='{} argument passed to {} must be {}'.format( ordinal(k + 1), param, validator.niddle), func=func) # Invoke the callable result = CallableWrapper.__call__(self, *args) else: # Callable signature not avaliable result = CallableWrapper.__call__(self, *args, **kwargs) # Validate the result context = {'func': func, 'param': 'return value of {}'.format(param)} validator = self.validator.children[-1] try: result = validator.validate(result, context=context) except ValidationError: raise ValidationError(expected=validator.niddle, **context) # Finally return the result of the callable return result
def __call__(self, name, value): if value is None: value = self.default if value is None: raise ValidationError('A value for property {} must be provided.'.format(name)) if not isinstance(value, basestring): raise ValidationError('The {} property value must be a string.'.format(name)) return value
def submit_ids(user: User): """ Route to create ids upload package. User must be in `INFO_VERIFIED` state. Will transition user to `ID_NOT_VERIFIED` One should call `/v1/ids/<id>/verify` in order to actually start verification process (see docs there) """ if user.state != INFO_VERIFIED: raise InvalidState(user.state) schema = Schema( { 'upload1': Coerce(ObjectId), 'upload2': Coerce(ObjectId), 'doc_type': In(DOC_TYPES), 'doc_country': All(Length(2, 2), str), Optional('doc_state', default=None): Any(None, All(Length(2, 20), str)), }, extra=REMOVE_EXTRA, required=True) data = schema(request.json) upload1 = Upload.objects(user=user, id=data['upload1']).get() upload2 = Upload.objects(user=user, id=data['upload2']).get() try: if not (upload1.stored_size <= 4 * 1024 * 1024): raise ValidationError('Invalid image size') if not (upload2.stored_size <= 4 * 1024 * 1024): raise ValidationError('Invalid image size') except KeyError as err: raise MissingFile(str(err)) id_upload = IDUpload.create( user=user, upload1=upload1, upload2=upload2, doc_country=data['doc_country'], doc_state=data['doc_state'], doc_type=data['doc_type'], ) user.update(doc_type=data['doc_type']) user.transition(ID_NOT_VERIFIED) return jsonify(id_upload.to_json()), 201
def __call__(self, name, value): if value is None: value = self.default if value is None: raise ValidationError('A value for property {} must be provided.'.format(name)) if not isinstance(value, dict): raise ValidationError('The {} property value must be a string.'.format(name)) return _Properties(value, self.schema, prefix=name + '.')
def __check_input_matrix(self, matrix): if matrix.shape[0] != matrix.shape[1]: raise ValidationError( "Try different matrix", "matrix is not a square matrix" ) if matrix.shape[0] < 2: raise ValidationError( "Try different maps_dimension", "matrix dimension is too small" )
def _make_resource_statement(cf, stack_arn, policy_name, logical_resource_name, policy_metadata_filter): print 'describe_stack_resource on resource {} in stack {}.'.format( logical_resource_name, stack_arn) try: res = discovery_utils.try_with_backoff( lambda: cf.describe_stack_resource( StackName=stack_arn, LogicalResourceId=logical_resource_name)) print 'describe_stack_resource {} result: {}'.format( logical_resource_name, res) except Exception as e: print 'describe_stack_resource {} error: {}'.format( logical_resource_name, getattr(e, 'response', e)) raise e resource = res['StackResourceDetail'] metadata = _get_metadata_for_role(resource, policy_name, policy_metadata_filter) if metadata is None: return metadata_actions = metadata.get('Action', None) if metadata_actions is None: raise ValidationError( 'No Action was specified for CloudCanvas {} metdata on the {} resource in stack {}.' .format(policy_name, resource['LogicalResourceId'], stack_arn)) if not isinstance(metadata_actions, list): metadata_actions = [metadata_actions] for action in metadata_actions: if not isinstance(action, basestring): raise ValidationError( 'Non-string Action specified for CloudCanvas {} metadata on the {} resource in stack {}.' .format(policy_name, resource['LogicalResourceId'], stack_arn)) resource = discovery_utils.get_resource_arn(stack_arn, resource['ResourceType'], resource['PhysicalResourceId']) resource_suffix = metadata.get('ResourceSuffix', None) if resource_suffix is not None: resource += resource_suffix return { 'Sid': logical_resource_name + 'Access', 'Effect': 'Allow', 'Action': metadata_actions, 'Resource': resource }
def edit_message_reply_markup(token, message_id, reply_markup, chat_id=None): """ Edit reply markup of the messages sent :param token: Telegram bot token :param message_id: ID of the inline_message to be edited :param reply_markup: the replacement reply markup :param chat_id: Telegram Chat ID :return: Telegram response object :raise: TelegramError: Telegram API call failed """ if not message_id: raise ValidationError("inline_message_id cannot be empty") if not reply_markup: raise ValidationError("reply_markup cannot be empty") post_data = {"reply_markup": reply_markup} if chat_id: post_data["message_id"] = message_id post_data["chat_id"] = chat_id else: post_data["inline_message_id"] = message_id headers = { "Content-Type": "application/json", "Content-Length": str(len(json.dumps(post_data))) } url = TELEGRAM_HOSTNAME + "/bot" + token + "/editMessageReplyMarkup" try: response = requests.post(url, headers=headers, data=json.dumps(post_data)) except ConnectionError as error: raise TelegramError("Error connecting to Telegram API: {}".format( str(error))) except HTTPError as error: raise TelegramError("Error in POST request to Telegram API: {}".format( str(error))) except Timeout as error: raise TelegramError("Timeout connecting to Telegram API: {}".format( str(error))) if response.status_code >= 400: raise TelegramError("Error response from Telegram API: {} {}".format( str(response), response.text)) return response
def validateElection(election): typecheck(election.proposal, [Proposal]) typecheck(election.proposal_meta, [ProposalMetadata]) for vote in election.vote: typecheck(vote, [Vote]) dupcheck(election.vote, "election votes") if not len(election.vote): raise ValidationError("Empty elections are nonsensical.") for vote in election.vote: if (vote.proposal != election.proposal or vote.proposal_meta != election.proposal_meta): raise ValidationError("Vote in election not matching proposal or meta data.")
def _make_resource_statement(feature_info, logical_resource_name): try: response = feature_info.get_client().describe_stack_resource( StackName=feature_info.stack_arn, LogicalResourceId=logical_resource_name) print 'describe_stack_resource(LogicalResourceId="{}", StackName="{}") response: {}'.format( logical_resource_name, feature_info.stack_arn, response) except Exception as e: print 'describe_stack_resource(LogicalResourceId="{}", StackName="{}") error: {}'.format( logical_resource_name, feature_info.stack_arn, e) raise e resource = response['StackResourceDetail'] metadata = discovery_utils.get_cloud_canvas_metadata( resource, 'PlayerAccess') if metadata is None: return None metadata_actions = metadata.get('Action', None) if metadata_actions is None: raise ValidationError( 'No Action was specified for CloudCanvas PlayerAccess metdata on the {} resource in stack {}.' .format(logical_resource_name, feature_info.stack_arn)) if not isinstance(metadata_actions, list): metadata_actions = [metadata_actions] for action in metadata_actions: if not isinstance(action, basestring): raise ValidationError( 'Non-string Action specified for CloudCanvas PlayerAccess metadata on the {} resource in stack {}.' .format(logical_resource_name, feature_info.stack_arn)) if 'PhysicalResourceId' not in resource: return None return { 'Sid': logical_resource_name + 'Access', 'Effect': 'Allow', 'Action': metadata_actions, 'Resource': discovery_utils.get_resource_arn(feature_info.stack_arn, resource['ResourceType'], resource['PhysicalResourceId']) }
def __getitem__(self, key): value = self.get(key) if not value: raise ValidationError( 'The {} stack has no {} parameter. This parameter is required.' .format(self.__stack_arn, key)) return value
def _create_role_policy(stack_arn, policy_name, default_statements, policy_metadata_filter): if not isinstance(default_statements, list): raise ValidationError('The default_statements value is not a list.') cf = stack_info.get_cloud_formation_client(stack_arn) res = cf.list_stack_resources(StackName=stack_arn) policy = { 'Version': '2012-10-17', 'Statement': copy.deepcopy(default_statements) } for resource in res['StackResourceSummaries']: statement = _make_resource_statement(cf, stack_arn, policy_name, resource['LogicalResourceId'], policy_metadata_filter) if statement is not None: policy['Statement'].append(statement) print 'generated policy: {}'.format(policy) if len(policy['Statement']) == 0: return None else: return json.dumps(policy, indent=4)
def get_resource(self, logical_name, expected_type=None, optional=False): resources = self.get_resources() resource = _find_resource_by_logical_name(resources, logical_name) if expected_type is not None: if resource is None: if optional: return None else: raise ValidationError('There is no {} resource in stack {}.'.format(logical_name, self.stack_arn)) if resource['ResourceType'] != expected_type: raise ValidationError('The {} resource in stack {} has type {} but type {} was expected.'.format( logical_name, self.stack_arn, resource['ResourceType'], expected_type)) return resource
def validate(self): validation_ok = True validation_message = "" if not self.storageClass: validation_message += "Storage class cannot be empty\n" validation_ok = False if self.storageClass and self.storageClass not in consts.SUPPORTED_S3_STORAGE_CLASSES: validation_message += "Invalid storage class:[{}]\n".format( self.storageClass) validation_ok = False if self.region not in consts.SUPPORTED_REGIONS: validation_message += "Invalid region:[{}]\n".format(self.region) validation_ok = False if self.requestNumber and not self.requestType: validation_message += "requestType cannot be empty if you specity requestNumber\n" validation_ok = False if self.requestType and self.requestType not in consts.SUPPORTED_REQUEST_TYPES: validation_message += "Invalid request type:[{}]\n".format( self.requestType) validation_ok = False if not validation_ok: raise ValidationError(validation_message) return validation_ok
def remind(book): current_year = datetime.now().year in_a_days_to_remind = datetime.now() + timedelta(days=DAYS_TO_REMIND) persons_to_remind_of = [] for person in book.get_list(): try: birthday = datetime.strptime(person["birthday"], '%d.%m.%Y') current_year_birthday = birthday.replace(year=current_year) if current_year_birthday > datetime.now() and current_year_birthday < in_a_days_to_remind: persons_to_remind_of.append(person) except ValueError: raise ValidationError("The {birthday} date in your book " "is not valid. Please check {name}" .format(**person)) if len(persons_to_remind_of) > 0: print "The following persons are celebrating birthday " \ "in the next {days} days".format(days=DAYS_TO_REMIND) for person in persons_to_remind_of: print_person(person) else: print "There is nobody celebrating birthday in {days} days. " \ "Call parents!".format(days=DAYS_TO_REMIND)
def __call__(self, value): if value is None: return if value not in self.possibilities: choices = ', '.join(self.possibilities) msg = "must be one of the following choices: {}".format(choices) raise ValidationError(msg)
def add(book): print "Please add the info below" name = raw_input("Name: ") phone = raw_input("Phone: ") while True: birthday_input = raw_input("Date of birth (dd.mm.yyyy): ") if is_valid_date(birthday_input): break else: raise ValidationError( "The date {birthday} is invalid." "Format should be dd.mm.yyyy!".format(birthday=birthday_input)) person = {"name": name, "phone": phone, "birthday": birthday_input} searched_person = book.search_exact(name) if searched_person is None: book.add_person(person) print "Person '{name}' added successfully!\n".format(name=name) else: print_person(searched_person) raise AlreadyExistError("You have the '{name}' in your book already. " "He's info above.".format(**person))