def get_user_name_or_id(self): try: user = self.client.get_user_info() return user.get('UserName', user['User']['UserId']) except ClientError as ce: # If the user doesn't have access rights to IAMClient # we can find the user name in the error response return utils.find_expression('(?<=user\/)(\S+)', str(ce))
def parse_error_invocation_response(response, function_name): if "Task timed out" in response['Payload']: # Find the timeout time message = utils.find_expression(str(response['Payload']), '(Task timed out .* seconds)') # Modify the error message to ease the error readability error_msg = message.replace("Task", "Function '%s'" % function_name) error_log = "Error in function response: %s" % error_msg else: error_msg = "Error in function response." error_log = "Error in function response: %s" % response['Payload'] logger.error(error_msg, error_log)
def get_user_info(self): ''' Retrieves information about the specified IAM user, including the user's creation date, path, unique ID, and ARN. https://boto3.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.get_user ''' try: return self.client.get_user() except ClientError as ce: if ce.response['Error']['Code'] == 'AccessDenied': # If the user doesn't have access rights to IAMClient # we can find the user name in the error response user_name = utils.find_expression(str(ce), '(?<=user\/)(\S+)') return { 'UserName': user_name, 'User': { 'UserName': user_name, 'UserId': '' } }
def add_account_id(self): self.properties['account_id'] = utils.find_expression( self.properties['iam']['role'], '\d{12}')
def get_api_lambda_uri(self): self.aws_acc_id = utils.find_expression('\d{12}', self.lambda_role) api_gateway_uri = 'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/' lambda_uri = 'arn:aws:lambda:us-east-1:{0}:function:{1}/invocations'.format( self.aws_acc_id, self.function_name) return api_gateway_uri + lambda_uri
def validate_function_name(function_name): if not utils.find_expression(function_name, valid_lambda_name_regex): error_msg = 'Find name restrictions in: https://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html#SSS-CreateFunction-request-FunctionName' raise ValidatorError(parameter='function_name', parameter_value=function_name, error_msg=error_msg)
def validate_function_name(function_name, name_regex): if not utils.find_expression(function_name, name_regex): raise Exception( "'{0}' is an invalid lambda function name.".format(function_name))