def test_default_config(self):
        FormClass = make_edit_config_form(ServiceWithDefaultConfig)
        self.assertNotEqual(None, FormClass)

        FormClass2 = make_run_config_form(ServiceWithDefaultConfig)
        self.assertNotEqual(None, FormClass)

        fields = _get_config_fields(ServiceWithDefaultConfig, True)
        self.assertEqual(1, len(fields))

        private_fields = _get_config_fields(ServiceWithDefaultConfig, False)
        self.assertEqual(2, len(private_fields))
Esempio n. 2
0
def edit_config(request, name):
    """
    Edit a service's configuration.
    """

    analyst = request.user.username
    service = CRITsService.objects(name=name,
                                   status__ne="unavailable").first()
    if not service:
        error = 'Service "%s" is unavailable. Please review error logs.' % name
        return render_to_response('error.html', {'error': error},
                           RequestContext(request))
    # TODO: fix code so we don't have to do this
    service = service.to_dict()

    # Get the class that implements this service.
    service_class = crits.service_env.manager.get_service_class(name)

    # format_config returns a list of tuples
    old_config = service_class.format_config(service['config'],
                                             printable=False)

    logger.debug("old_config")
    logger.debug(str(old_config))

    ServiceEditConfigForm = make_edit_config_form(service_class)

    if request.method == "POST":
        #Populate the form with values from the POST request
        form = ServiceEditConfigForm(request.POST)
        if form.is_valid():
            logger.debug("Ingoing data:")
            logger.debug(str(form.cleaned_data))
            new_config = service_class.parse_config(form.cleaned_data)

            logger.info("Service %s configuration modified." % name)
            logger.debug(str(new_config))

            result = crits.service_env.manager.update_config(name, new_config,
                                                             analyst)

            if not result['success']:
                return render_to_response('error.html',
                                          {'error': result['message']},
                                          RequestContext(request))

            return redirect(reverse('crits.services.views.detail',
                                    args=[name]))
        else:
            # Return the form to the user to fix any errors.
            pass

    else:
        # Populate the form with the current values in the database.
        form = ServiceEditConfigForm(dict(old_config))

    error = _get_config_error(service)

    return render_to_response('services_edit_config.html',
                              {
                                'form': form,
                                'service': service,
                                'config_error': error,
                              },
                              RequestContext(request))
    def test_no_default_config(self):
        FormClass = make_edit_config_form(ServiceWithNoDefaultConfig)
        self.assertEqual(None, FormClass)

        FormClass2 = make_run_config_form(ServiceWithNoDefaultConfig)
        self.assertNotEqual(None, FormClass2)
Esempio n. 4
0
def edit_config(request, name):
    """
    Edit a service's configuration.
    """

    analyst = request.user.username
    service = CRITsService.objects(name=name, status__ne="unavailable").first()
    if not service:
        error = 'Service "%s" is unavailable. Please review error logs.' % name
        return render_to_response('error.html', {'error': error},
                                  RequestContext(request))
    # TODO: fix code so we don't have to do this
    service = service.to_dict()

    # Get the class that implements this service.
    service_class = crits.service_env.manager.get_service_class(name)

    # format_config returns a list of tuples
    old_config = service_class.format_config(service['config'],
                                             printable=False)

    logger.debug("old_config")
    logger.debug(str(old_config))

    ServiceEditConfigForm = make_edit_config_form(service_class)

    if request.method == "POST":
        #Populate the form with values from the POST request
        form = ServiceEditConfigForm(request.POST)
        if form.is_valid():
            logger.debug("Ingoing data:")
            logger.debug(str(form.cleaned_data))
            new_config = service_class.parse_config(form.cleaned_data)

            logger.info("Service %s configuration modified." % name)
            logger.debug(str(new_config))

            result = crits.service_env.manager.update_config(
                name, new_config, analyst)

            if not result['success']:
                return render_to_response('error.html',
                                          {'error': result['message']},
                                          RequestContext(request))

            return redirect(reverse('crits.services.views.detail',
                                    args=[name]))
        else:
            # Return the form to the user to fix any errors.
            pass

    else:
        # Populate the form with the current values in the database.
        form = ServiceEditConfigForm(dict(old_config))

    error = _get_config_error(service)

    return render_to_response('services_edit_config.html', {
        'form': form,
        'service': service,
        'config_error': error,
    }, RequestContext(request))