예제 #1
0
 def get_file(folder):
     from app import message_format
     try:
         with open(folder + '/result.json', "rb") as file_open:
             log = json.load(file_open)
     except IOError:
         log = message_format(-99, 'Command not execute yet.')
     return log
예제 #2
0
    def get(self, command, option=None):
        from auth import Auth
        from app import message_format

        Auth().verify_access('history', command, option)
        Auth().verify_deny('history', command, option)

        if command == 'list':
            list_async = []
            active_folder = self.api_directory + '/active/' + datetime.today().strftime('%Y%m')
            status_code = 200
            message = ''
            try:
                if request.args.get('past'):
                    tar = tarfile.open(self.api_directory + '/archive/' + request.args.get('past') + '.tar.gz')
                    tar.extractall(self.api_directory + '/garbage/')
                    tar.close()
                    active_folder = self.api_directory + '/garbage/' + request.args.get('past')
                for folder in os.listdir(active_folder):
                    obj = {}
                    exp = folder.split('_')
                    obj['date'] = exp[0]
                    obj['command'] = exp[1]
                    obj['ticket'] = Auth().generate_token(dict({'folder': folder}), exp=False)
                    obj['result'] = self.get_file(
                        active_folder + '/' + folder)

                    list_async.append(obj)
            except IOError:
                status_code = 500
                message = 'IOError Past Log Not Found'
                list_async = []
            except OSError:
                status_code = 600
                message = 'OSError Past Log Not Found'
                list_async = []
            return message_format(status_code, message, list_async)
        elif command == 'result':
            decode = Auth().verify_token(request.args.get('ticket'))
            return self.get_file(decode['folder'])
        else:
            raise HistoryException('Api not found(history/' + command + ')', 405, HistoryException.NOT_FOUND)
예제 #3
0
    def get(self, command=None, option=None):
        from auth import Auth
        from app import message_format
        Auth().verify_access('config', command, option)
        Auth().verify_deny('config', command, option)

        if command == 'barman' and option == 'help':
            return message_format(200, '',
                                  BarmanCommandParser().load_barman_config())
        elif command == 'barman' and option == 'reload':
            return message_format(200, '', BarmanCommandParser().parse_man_5())
        elif command == 'barman' and option == 'change':
            config_set_str = None
            for param in request.args:
                if param != 'token':
                    spl = param.split('.')
                    config_set_str = "parser.set('" + spl[0] + "','" + spl[
                        1] + "','" + request.args.get(param) + "')\\n"
            with open(
                    flask.current_app.config.config_directory +
                    '/template/config_change_template', 'rb') as configfile:
                template = configfile.read()
                template = template.replace(
                    '{config_file}',
                    self.barman_config.get('config_file')).replace(
                        '{config_set}', config_set_str)
            f = tempfile.NamedTemporaryFile(
                delete=False,
                dir=flask.current_app.config.config_main.read_section(
                    'application').get('store_directory') + '/garbage/')

            temp_file = f.name
            with open(temp_file, 'wb') as configfile:
                configfile.write(template)
            os.chmod(temp_file, 775)
            command_text = ''
            if self.barman_config.get('remote').lower() == 'true':
                command_text = self.barman_config.get('remote_ssh') + ' < '
            command_text += '' + temp_file
            result = Command().execute_command(command_text)
            return message_format(200, '', self.get_barman_config())

        elif command == 'barman':
            return message_format(200, '', self.get_barman_config())

        elif command == 'barmanapi' and option == 'change':
            config = self.current_app.config.config_main
            for param in request.args:
                if param != 'token':
                    spl = param.split('.')
                    if request.args.get(param) != config.get(spl[0], spl[1]):
                        config.set(spl[0], spl[1], request.args.get(param))
                        config.write_config()
            return message_format(200, '', self.get_config())

        elif command == 'barmanapi':
            return message_format(200, '', self.get_config('dead'))

        else:
            raise ConfigurationException(
                'Api not found(config/' + command + ')', 405,
                ConfigurationException.NOT_FOUND)
예제 #4
0
    def get(self, command, option=None):
        barman_config = self.current_app.config.config_main.read_section('barman')
        from auth import Auth

        Auth().verify_access('barman', command, option)
        Auth().verify_deny('barman', command, option)

        if not request.args.get('token'):
            from auth import AuthException

            raise AuthException('Please send a token parameter', 401, AuthException.NOT_FOUND)

        from parser import BarmanCommandParser

        self.compress_history_data()
        from app import message_format
        if command == 'reload':
            BarmanCommandParser().parse_man_5()

            return message_format(200, '', BarmanCommandParser().parse_man())
        barman_commands = BarmanCommandParser().load_barman_commands()

        if command == 'help':
            return message_format(200, '', barman_commands)

        if barman_commands.get(command):
            if option == 'help':
                return message_format(200, '', barman_commands[command])
            elif not option:
                required = barman_commands[command].get('required')
                optional = barman_commands[command].get('optional')
                command_option = ''
                option_counter = 1
                if optional:
                    for opt in optional:
                        val = optional.get(opt)
                        if request.args.get(opt):
                            option_counter += 1
                            if val.get('values') == 'TRUE/FALSE' and request.args.get(opt) == 'TRUE':
                                command_option += " --" + opt
                            else:
                                command_option += " --" + opt + ' ' + request.args.get(opt)
                if required:
                    for opt in required:
                        if request.args.get(opt):
                            if opt != 'token':
                                option_counter += 1
                                command_option += ' ' + request.args.get(opt)
                        else:
                            raise BarmanException('Please send a required parameter.(' + opt + ')', 401,
                                                  BarmanException.NOT_FOUND)

                if len(request.args) > option_counter:
                    raise BarmanException('Some parameters are invalid', 403, BarmanException.INVALID_PARAM)

                exec_command = Command().prepare_command(command, command_option)

                if eval(barman_config.get('async_command')).count(command):
                    self.create_operation_directory(command, request.args.get('token'))
                    with open(self.command_directory + '/command', "wb") as file_open:
                        file_open.write(exec_command)
                    Command().execute_command(self.command_directory, sync=False)

                    ticket = Auth().generate_token(dict({'folder': self.command_directory}), exp=False)
                    return message_format(200, '', {'ticket': ticket, 'message': 'For result /history/result?ticket=' + ticket + '&token=XX'})
                else:
                    return Command().execute_command(exec_command, sync=True)

        else:
            raise BarmanException(BarmanException.NOT_FOUND, '405')

        return
예제 #5
0
    def get(self, command, option=None):
        self.verify_access('auth', command, option)
        self.verify_deny('auth', command, option)
        user_config = self.get_user_config()

        if command == 'token':
            return {'token': self.generate_token(dict({'user': g.user}))}

        elif command == 'user' and option == 'list':
            user_list = {}
            for username in user_config.sections():
                user_list[username] = self.get_user_property(username)
            from app import message_format
            # return {'status_code': 200, 'message': '', 'list': user_list}
            return message_format(200, '', user_list)

        elif command == 'user' and option == 'change':
            if not request.args.get('username'):
                raise AuthException("Parameter Not Found(username)", 404,
                                    AuthException.NOT_FOUND)
            try:
                user = user_config.read_section(request.args.get('username'))
                if request.args.get('password') and user.get(
                        'password') != User().hash_password(
                            request.args.get('password')):
                    user_config.set(
                        request.args.get('username'), 'password',
                        User().hash_password(request.args.get('password')))

                if request.args.get('access') and user.get(
                        'access') != request.args.get('access'):
                    user_config.set(request.args.get('username'), 'access',
                                    request.args.get('access'))

                if request.args.get('deny') and user.get(
                        'deny') != request.args.get('deny'):
                    user_config.set(request.args.get('username'), 'deny',
                                    request.args.get('deny'))

                user_config.write_config()

                from app import message_format
                return message_format(
                    200, '',
                    self.get_user_property(request.args.get('username')))
            except ConfigParserException:
                raise AuthException(
                    "Username not found(" + request.args.get('username') + ")",
                    403, AuthException.NOT_FOUND)

        elif command == 'user' and option == 'add':
            if not request.args.get('username'):
                raise AuthException("Parameter Not Found(username)", 404,
                                    AuthException.NOT_FOUND)

            try:
                user = user_config.read_section(request.args.get('username'))
                if user:
                    raise AuthException(
                        "Username registered before(" +
                        request.args.get('username') + ")", 403,
                        AuthException.USER_FOUND)
            except ConfigParserException:
                if request.args.get('username'):
                    user_config.add_section(request.args.get('username'))

                if request.args.get('password'):
                    user_config.set(
                        request.args.get('username'), 'password',
                        User().hash_password(request.args.get('password')))
                else:
                    raise AuthException("Parameter Not Found(password)", 404,
                                        AuthException.NOT_FOUND)

                if request.args.get('access'):
                    user_config.set(request.args.get('username'), 'access',
                                    request.args.get('access'))
                else:
                    user_config.set(request.args.get('username'), 'access', [])

                if request.args.get('deny'):
                    user_config.set(request.args.get('username'), 'deny',
                                    request.args.get('deny'))
                else:
                    user_config.set(request.args.get('username'), 'deny', [])

                user_config.write_config()
                from app import message_format
                return message_format(
                    200, '',
                    self.get_user_property(request.args.get('username')))
        elif command == 'user' and option is None:
            from app import message_format
            return message_format(
                200, '', {
                    'access': eval(g.user.get('access')),
                    'deny': eval(g.user.get('deny'))
                })
        else:
            api = 'auth'
            if command:
                api += '/' + command
            if option:
                api += '/' + option
            raise AuthException('Api not found(' + api, '405',
                                AuthException.NOT_FOUND)
예제 #6
0
파일: auth.py 프로젝트: emin100/barmanapi
    def get(self, command, option=None):
        self.verify_access('auth', command, option)
        self.verify_deny('auth', command, option)
        user_config = self.get_user_config()

        if command == 'token':
            return {'token': self.generate_token(dict({'user': g.user}))}

        elif command == 'user' and option == 'list':
            user_list = {}
            for username in user_config.sections():
                user_list[username] = self.get_user_property(username)
            from app import message_format
            # return {'status_code': 200, 'message': '', 'list': user_list}
            return message_format(200, '', user_list)

        elif command == 'user' and option == 'change':
            if not request.args.get('username'):
                raise AuthException("Parameter Not Found(username)", 404,
                                    AuthException.NOT_FOUND)
            try:
                user = user_config.read_section(request.args.get('username'))
                if request.args.get('password') and user.get('password') != User().hash_password(
                        request.args.get('password')):
                    user_config.set(request.args.get('username'), 'password',
                                    User().hash_password(request.args.get('password')))

                if request.args.get('access') and user.get('access') != request.args.get('access'):
                    user_config.set(request.args.get('username'), 'access', request.args.get('access'))

                if request.args.get('deny') and user.get('deny') != request.args.get('deny'):
                    user_config.set(request.args.get('username'), 'deny', request.args.get('deny'))

                user_config.write_config()

                from app import message_format
                return message_format(200, '', self.get_user_property(request.args.get('username')))
            except ConfigParserException:
                raise AuthException("Username not found(" + request.args.get('username') + ")", 403,
                                    AuthException.NOT_FOUND)

        elif command == 'user' and option == 'add':
            if not request.args.get('username'):
                raise AuthException("Parameter Not Found(username)", 404,
                                    AuthException.NOT_FOUND)

            try:
                user = user_config.read_section(request.args.get('username'))
                if user:
                    raise AuthException("Username registered before(" + request.args.get('username') + ")", 403,
                                        AuthException.USER_FOUND)
            except ConfigParserException:
                if request.args.get('username'):
                    user_config.add_section(request.args.get('username'))

                if request.args.get('password'):
                    user_config.set(request.args.get('username'), 'password',
                                    User().hash_password(request.args.get('password')))
                else:
                    raise AuthException("Parameter Not Found(password)", 404,
                                        AuthException.NOT_FOUND)

                if request.args.get('access'):
                    user_config.set(request.args.get('username'), 'access', request.args.get('access'))
                else:
                    user_config.set(request.args.get('username'), 'access', [])

                if request.args.get('deny'):
                    user_config.set(request.args.get('username'), 'deny', request.args.get('deny'))
                else:
                    user_config.set(request.args.get('username'), 'deny', [])

                user_config.write_config()
                from app import message_format
                return message_format(200, '', self.get_user_property(request.args.get('username')))
        elif command == 'user' and option is None:
            from app import message_format
            return message_format(200, '', {'access': eval(g.user.get('access')), 'deny': eval(g.user.get('deny'))})
        else:
            api = 'auth'
            if command:
                api += '/' + command
            if option:
                api += '/' + option
            raise AuthException('Api not found(' + api, '405', AuthException.NOT_FOUND)