Beispiel #1
0
    def create_storage(storage_type,
                       password,
                       *,
                       input_auth=None,
                       attributes=None,
                       compression_level='5',
                       indent=4,
                       sieve=None):
        if storage_type == 'history':
            events_to_show = _get_history_events(sieve)
        elif storage_type == 'violations':
            try:
                events_to_show = _get_violation_events(sieve, input_auth,
                                                       attributes, indent)
            except USBRipError as e:
                print_critical(str(e), initial_error=e.errors['initial_error'])
                return 1

        if events_to_show is None:
            return 1

        if events_to_show:
            min_date, max_date = _get_dates(events_to_show)
            json_file = f'{USBStorage._STORAGE_BASE}/{min_date}-{max_date}.json'
        else:
            json_file = f'{USBStorage._STORAGE_BASE}/{datetime.now().strftime("%Y%m%dT%H%M%S")}.json'

        try:
            _dump_events(events_to_show, storage_type, json_file, indent)
        except USBRipError as e:
            print_critical(str(e), initial_error=e.errors['initial_error'])
            return 1

        storage_full_path = f'{USBStorage._STORAGE_BASE}/{storage_type}.7z'
        if os.path.exists(storage_full_path):
            os.remove(storage_full_path)

        try:
            out = _7zip_pack(storage_full_path, json_file, password,
                             compression_level)
        except USBRipError as e:
            os.remove(json_file)
            print_critical(str(e),
                           errcode=e.errors['errcode'],
                           initial_error=e.errors['initial_error'])
            return 1

        if 'Everything is Ok' in out:
            print_info(f'New {storage_type} storage: "{storage_full_path}"')
            print_secret('Your password is', secret=password)
            os.remove(json_file)
        else:
            print_critical('Undefined behaviour while creating storage',
                           initial_error=out)
Beispiel #2
0
    def create_storage(storage_type,
                       *,
                       password=None,
                       input_auth=None,
                       attributes=None,
                       compression_level='5',
                       indent=4,
                       sieve=None):
        if storage_type == 'history':
            events_to_show = _get_history_events(sieve)
        elif storage_type == 'violations':
            try:
                events_to_show = _get_violation_events(sieve, input_auth,
                                                       attributes, indent)
            except USBRipError as e:
                print_critical(str(e), initial_error=e.errors['initial_error'])
                return 1

        if events_to_show is None:
            return 1

        if events_to_show:
            min_date, max_date = _get_dates(events_to_show)
            json_file = '{}/{}-{}.json'.format(USBStorage._STORAGE_BASE,
                                               min_date, max_date)
        else:
            json_file = '{}/{}.json'.format(USBStorage._STORAGE_BASE,
                                            datetime.now().strftime('%m%d'))

        try:
            _dump_events(events_to_show, storage_type, json_file, indent)
        except USBRipError as e:
            print_critical(str(e), initial_error=e.errors['initial_error'])
            return 1

        if password is None:
            print_warning('No password provided, generating random one')
            password = _gen_random_password(12)

        storage_full_path = '{}/{}.7z'.format(USBStorage._STORAGE_BASE,
                                              storage_type)
        if os.path.exists(storage_full_path):
            os.remove(storage_full_path)

        try:
            out = _7zip_pack(storage_full_path, json_file, password,
                             compression_level)
        except USBRipError as e:
            os.remove(json_file)
            print_critical(str(e),
                           errcode=e.errors['errcode'],
                           initial_error=e.errors['initial_error'])
            return 1

        if 'Everything is Ok' in out:
            print_info('New {} storage: \'{}\''.format(storage_type,
                                                       storage_full_path))
            print_secret('Your password is', secret=password)
            os.remove(json_file)
        else:
            print_critical('Undefined behaviour while creating storage',
                           initial_error=out)