예제 #1
0
    def get_authenticated_client(self):
        """Gets an authenticated Vault client.  Raises an exception if the client is not authenticated."""
        vault_uri = self._get_vault_uri()
        if not vault_uri:
            raise ImproperlyConfigured(
                'VAULT_ADDR is a required setting for a Vault authenticated informix connection'
            )

        hvac_client = hvac.Client(url=vault_uri)

        if self.settings_dict.get('VAULT_K8S_ROLE', None):
            self._auth_via_k8s(hvac_client)
        else:
            self._auth_via_token(hvac_client)

        try:
            if not hvac_client.is_authenticated():
                raise OperationalError(
                    'Vault client failed to authenticate, provide JWT via K8s '
                    'or basic token via VAULT_TOKEN in settings.  Ensure the credientials are valid and authorised.'
                )
        except hvac.exceptions.VaultError as err:
            msg = err.args[0]
            raise OperationalError(msg)

        return hvac_client
예제 #2
0
    def save(self, *args, **kwargs):
        # Check if chosen_dept == batch.dept is same or not.
        if self.admission_student.choosen_department != self.batch.department:
            raise OperationalError(
                f'Cannot assign {self.admission_student.choosen_department} '
                f'departments student to {self.batch.department} department.')
        elif self.admission_student.choosen_department == self.batch.department:
            # Set AdmissionStudent assigned_as_student=True
            self.admission_student.assigned_as_student = True
            self.admission_student.save()

        # Create temporary id for student id if temporary_id is not set yet.
        if not self.temp_serial or not self.temporary_id:
            last_temp_id = self._find_last_admitted_student_serial()
            current_temp_id = str(last_temp_id + 1)
            self.temp_serial = current_temp_id
            self.temporary_id = self.get_temp_id()
            super().save(*args, **kwargs)
            try:
                with transaction.atomic():
                    temp_serial_id = TempSerialID.objects.create(
                        student=self,
                        department=self.admission_student.choosen_department,
                        year=self.ac_session,
                        serial=current_temp_id)
                    temp_serial_id.save()
            except IntegrityError:
                pass
        super().save(*args, **kwargs)
예제 #3
0
파일: models.py 프로젝트: piehei/prplatform
    def save_and_destroy_lock(self, *args, **kwargs):

        if self.pk is not None:

            raise OperationalError(
                'This cannot be used to update the instance!')

        else:

            locks = self.exercise.reviewlocks_for(self.submitter_user)
            if locks.count() != 1:
                raise OperationalError(
                    f'There should be exactly 1 reviewlock! Found: {locks.count()}'
                )
            locks.first().delete()
            super().save(*args, **kwargs)
예제 #4
0
def set_diagram_category(request):
    try:
        diagram = get_model_by_uid(Diagram, uid=request.POST['pk'])

        if diagram.checked_out_by != request.user.username:
            raise OperationalError(f'The {Model} is not checked out by you.')

        category_name = get_posted_text(request).strip()

        if category_name == '':
            raise Exception(f'Category name cannot be empty.')

        category = diagram.category.single()

        if category_name != category.name:
            new_category = get_unique(Category, name=category_name)
            diagram.category.reconnect(category, new_category)
            diagram.save()

        return JsonResponse({'success': True})

    except Exception as e:
        return JsonResponse({
            'success': False,
            'error_msg': f'{full_qualname(e)}: {e}'
        })
예제 #5
0
 def unlock_from_match(self):
     if self.current_match is not None:
         if self.current_match.end_time is not None:
             if self.current_match.is_disputed is False:
                 self.current_match = None
                 return
         else:
             raise OperationalError(
                 _('Match has not ended or has been disputed'),
                 code='party_mutex_violation_unlock',
             )
     else:
         raise OperationalError(
             _('Party is not currently in a Match.'),
             code='party_mutex_unneeded',
         )
예제 #6
0
def set_model_string(request, Model: str, field: str):
    try:
        old_id = request.POST['pk']

        ModelClass = get_model_class(Model)
        model = get_model_by_uid(ModelClass, uid=old_id)

        if model.checked_out_by != request.user.username:
            raise OperationalError(f'The {Model} is not checked out by you.')

        if not hasattr(model, field):
            raise ValueError(f'A {Model} has no field "{field}" implemented.')

        string = get_posted_text(request)
        string = string.strip()

        if string == '':
            raise Exception(f'Name cannot be empty.')

        current_val = getattr(model, field)

        if current_val != string:
            setattr(model, field, string)
            model.save()

        return JsonResponse({'success': True})

    except Exception as e:
        return JsonResponse({
            'success': False,
            'error_msg': f'{full_qualname(e)}: {e}'
        })
def create_tenant_task(self, user_id) -> int:
    tenant = TenantData.objects.try_create_default()
    if not tenant:
        logger.warn("Failed to create tenant because of not unique name was generated")
        raise self.retry(exc=OperationalError('Failed to create tenant because of raise'))

    UserModel = get_user_model()
    try:
        profile = UserModel.objects.get(id=user_id).profile
    except (UserModel.MultipleObjectsReturned, UserModel.DoesNotExist) as ex:
        tenant.delete(force_drop=True)
        raise Reject(ex, requeue=False) from ex

    if profile.tenant:
        tenant.delete(force_drop=True)
        raise Reject('Tenant already created for user', requeue=False)

    try:
        profile.tenant = tenant
        profile.save()
    except BaseException:
        tenant.delete(force_drop=True)
        raise

    return tenant.id
예제 #8
0
    def _get_hvac_client(self):
        if self._hvac is None:
            # Build the hvac client and authenticate
            vault_url, vault_token = self._get_vault_login_credentials()

            if vault_url is None:
                raise ImproperlyConfigured(
                    "settings.DATABASES is improperly configured. "
                    "Please supply a valid Vault URL in VAULT_ADDR.")

            self._hvac = hvac.Client(url=vault_url)

            if vault_token is not None:
                self._hvac.token = vault_token
                try:
                    if not self._hvac.is_authenticated():
                        raise ImproperlyConfigured(
                            "settings.DATABASES is improperly configured. "
                            "Please supply a valid Vault token in VAULT_TOKEN."
                        )
                except hvac.exceptions.VaultError as e:
                    msg = e.args[0]
                    raise OperationalError(msg)

        return self._hvac
예제 #9
0
 def setUp(self) -> None:
     _log.debug("[FullErrorTests] patching for setup")
     self.s_connect = BaseDatabaseWrapper.connect
     BaseDatabaseWrapper.connect = Mock(
         side_effect=OperationalError('fail testing'))
     BaseDatabaseWrapper.connection = property(
         lambda x: None, lambda x, y: None)  # type: ignore
예제 #10
0
                def populate_variable_map(Model, model_vars, results):
                    if Model == Object:
                        var_name = 'n'
                    elif Model == Morphism:
                        var_name = 'r'

                    for i in range(len(results)):
                        query_node = model_vars[var_name + str(i)]

                        matched_node = Model.inflate(results[i])
                        matched_template, matched_vars = Variable.parse_into_template(
                            matched_node.name)

                        query_template = template_regexes[query_node.name][0]

                        var_subst_regex, var_count = Variable.variable_match_regex(
                            query_template)
                        match = var_subst_regex.match(
                            query_node.name
                        )  # BUGFIX: query_node here not matched_node

                        for i in range(var_count):
                            Vi = match.group('V' + str(i))
                            # Variables need to be matched consistently and we have to check this in both
                            # the apply_rule and rule_search since the user can pass in an ultimately inconsistent
                            # yet skeletally matched query diagram.
                            matched_var = matched_vars[i]

                            if matched_var in variable_map:
                                if variable_map[matched_var] != Vi:
                                    raise OperationalError(
                                        "The diagram variables are not matched consitently."
                                    )
                            else:
                                variable_map[matched_var] = Vi
예제 #11
0
    def delete_user(self, id):
        user = User.objects.get(id=id)
        user.delete()

        if user.id is None:
            return True
        else:
            raise OperationalError("delete user failed")
예제 #12
0
 def test_ops_heartbeat_db_failure(self):
     """/__heartbeat__ should respond with 500 when there is a database problem"""
     url = reverse('ops-heartbeat')
     to_mock = 'django.db.backends.base.base.BaseDatabaseWrapper.ensure_connection'
     with patch(to_mock) as mock_ensure_connection:
         mock_ensure_connection.side_effect = OperationalError(
             'FATAL:  the database system is shutting down')
         resp = self.client.get(url)
         self.assertEqual(500, resp.status_code)
예제 #13
0
    def test_healthcheck_db_fail(self):
        conn = connections['default']
        with patch.object(conn.__class__, 'cursor') as mock_method:
            mock_method.side_effect = OperationalError('Mocked database error')

            result = self.client.get(self.url)
            self.assertEqual(result.status_code,
                             status.HTTP_503_SERVICE_UNAVAILABLE)
            self.assertIn('ERROR', result.json().get('database'))
예제 #14
0
    def get_locked_instance(self):
        """
        Lock object and reload it from database.
        :return: reloaded locked object from database
        """
        if not self.pk:
            raise OperationalError('Unsaved object cannot be locked')

        return self.__class__.objects.filter(pk=self.pk).select_for_update().get()
예제 #15
0
 def kick_player(self, player):
     if self.players.filter(username__exact=player.username).exists():
         self.players.remove(player)
     else:
         raise OperationalError(
             _('Player %(username) is not in this party!'),
             code='party_non_member',
             params={'username': player.username},
         )
예제 #16
0
 def test_db_is_not_connected(self, mock_connection):
     """Test that db is not connected, log at ERROR level and counter increments."""
     mock_connection.cursor.side_effect = OperationalError("test exception")
     logging.disable(logging.NOTSET)
     before = REGISTRY.get_sample_value("db_connection_errors_total")
     with self.assertLogs(logger="koku.metrics", level=logging.ERROR):
         DatabaseStatus().connection_check()
     after = REGISTRY.get_sample_value("db_connection_errors_total")
     self.assertEqual(1, after - before)
예제 #17
0
def test_process_exception_returns_none_if_not_deadlock_error():
    request = HttpRequest()
    request.disable_deadlock_retry = False
    request.deadlock_retry_attempt = 1

    exception = OperationalError("not a deadlock")

    result = middleware.process_exception(request, exception)

    assert result is None
예제 #18
0
    def lock_to_match(self, match):
        if self.current_match is None:
            if match.end_time is None and match.is_disputed is False:
                self.current_match = match
                return

        raise OperationalError(
            _('Party is still locked to a Match or Match is un-joinable'),
            code='party_mutex_violation_lock',
        )
예제 #19
0
def test_db_is_ready_error(db, monkeypatch):
    monkeypatch.setattr("sys.exit", lambda v: v)
    with mock.patch(
            'etools_datamart.apps.init.management.commands.db-isready.Command._get_cursor',
            side_effect=OperationalError()):
        call_command("db-isready",
                     wait=True,
                     timeout=1,
                     stdout=StringIO(),
                     stderr=StringIO())
예제 #20
0
def test_process_exception_increments_deadlock_retry_count_on_deadlock():
    request = HttpRequest()
    request.disable_deadlock_retry = False
    request.deadlock_retry_attempt = 1

    exception = OperationalError("deadlock detected")

    result = middleware.process_exception(request, exception)

    assert result.deadlock_retry_attempt > 1
예제 #21
0
 def _random():
     val = None
     try:
         for _ in range(max_collision_check):
             val = secrets.randbelow(val_gap) - min_val
             if not model.objects.filter(**{field_name: val}).exists():
                 return val
     except ProgrammingError:
         return val
     raise OperationalError(f'Cannot find unique value of {model.__name__}.{field_name}')
예제 #22
0
 def test_celery_task(self, mock_collect):
     """Test celery task to increment prometheus counter."""
     before = REGISTRY.get_sample_value('db_connection_errors_total')
     with mock.patch('django.db.backends.utils.CursorWrapper') as mock_cursor:
         mock_cursor.side_effect = OperationalError('test exception')
         mock_collect.return_value = []
         task = collect_metrics.s().apply()
         self.assertTrue(task.successful())
     after = REGISTRY.get_sample_value('db_connection_errors_total')
     self.assertEqual(1, after - before)
예제 #23
0
    def test_handle__operational_error(self, mock_al):
        """ .. check that KeyError is caught."""
        self.c.messages = []  # weird how this is not emptied for each test run
        mock_al.side_effect = OperationalError('Operational Error')

        self.prepare_sample_data()
        self.assertRaises(SystemExit, self.c.handle, self.test_dir)
        self.assertTrue("Error: Operational Error." in self.c.messages)
        self.assertFalse(
            'Warning: reloading "yesterday" data' in self.c.messages)
예제 #24
0
 def test_supports_json_field_operational_error(self):
     if hasattr(connection.features, "supports_json_field"):
         del connection.features.supports_json_field
     msg = "unable to open database file"
     with mock.patch(
         "django.db.backends.base.base.BaseDatabaseWrapper.cursor",
         side_effect=OperationalError(msg),
     ):
         with self.assertRaisesMessage(OperationalError, msg):
             connection.features.supports_json_field
예제 #25
0
def test_process_exception_returns_none_if_retry_disabled():
    request = HttpRequest()
    request.disable_deadlock_retry = True
    request.deadlock_retry_attempt = 1

    exception = OperationalError("deadlock detected")

    result = middleware.process_exception(request, exception)

    assert result is None
예제 #26
0
 def test_query_exception(self):
     """Test _query() when an exception is thrown."""
     logging.disable(0)
     with mock.patch('django.db.backends.utils.CursorWrapper') as mock_cursor:
         mock_cursor = mock_cursor.return_value.__enter__.return_value
         mock_cursor.execute.side_effect = OperationalError('test exception')
         test_query = 'SELECT count(*) from now()'
         dbs = DatabaseStatus()
         with self.assertLogs(level=logging.WARNING):
             dbs.query(test_query)
예제 #27
0
 def test_query_exception(self, patched_sleep):  # pylint: disable=W0613
     """Test _query() when an exception is thrown."""
     logging.disable(logging.NOTSET)
     with mock.patch("django.db.backends.utils.CursorWrapper") as mock_cursor:
         mock_cursor = mock_cursor.return_value.__enter__.return_value
         mock_cursor.execute.side_effect = OperationalError("test exception")
         test_query = "SELECT count(*) from now()"
         dbs = DatabaseStatus()
         with self.assertLogs(logger="koku.metrics", level=logging.WARNING):
             result = dbs.query(test_query, "test_query")
         self.assertFalse(result)
예제 #28
0
 def test_supports_json_field_operational_error(self):
     if hasattr(connection.features, "supports_json_field"):
         del connection.features.supports_json_field
     msg = "unable to open database file"
     with mock.patch.object(
             connection,
             "cursor",
             side_effect=OperationalError(msg),
     ):
         with self.assertRaisesMessage(OperationalError, msg):
             connection.features.supports_json_field
예제 #29
0
def test_process_exception_returns_none_if_attempts_exceeded():
    request = HttpRequest()
    request.disable_deadlock_retry = False
    request.deadlock_retry_attempt = 11

    settings.DEADLOCK_RETRY_ATTEMPTS = 10

    exception = OperationalError("deadlock detected")

    result = middleware.process_exception(request, exception)

    assert result is None
예제 #30
0
def save_diagram_to_database(request, diagram_id):
    try:
        if request.method != 'POST':  #or not request.headers.get("contentType", "application/json; charset=utf-8"):
            raise OperationalError(
                'You can only use the POST method to save to the database.')
        user = request.user.username

        diagram = get_model_by_uid(Diagram, uid=diagram_id)

        if diagram is None:
            raise ObjectDoesNotExist(
                f'There exists no diagram with uid "{diagram_id}".')

        if diagram.checked_out_by != user:
            raise OperationalError(
                f'The diagram with id "{diagram_id}" is already checked out by {diagram.checked_out_by}'
            )

        body = request.body.decode('utf-8')

        if body:
            try:
                data = json.loads(body)
            except json.decoder.JSONDecodeError:
                # For some reason, empty diagrams are resulting in the body as a URL str (not JSON)
                data = [0, 0]
        else:
            data = [0, 0]

        diagram.delete_objects()
        diagram.load_from_editor(data)

        return JsonResponse('Wrote the following data to the database:\n' +
                            str(data),
                            safe=False)

    except Exception as e:
        #if DEBUG:
        #raise e
        return JsonResponse({'error_msg': f'{full_qualname(e)}: {str(e)}'})