예제 #1
0
파일: rules.py 프로젝트: 0dadj1an/exafs
def ipv4_rule():
    net_ranges = get_user_nets(session['user_id'])
    form = IPv4Form(request.form)

    # add values to form instance
    form.action.choices = get_user_actions(session['user_role_ids'])

    form.net_ranges = net_ranges

    if request.method == 'POST' and form.validate():

        model = get_ipv4_model_if_exists(form.data, 1)

        if model:
            model.expires = round_to_ten_minutes(webpicker_to_datetime(form.expires.data))
            flash_message = u'Existing IPv4 Rule found. Expiration time was updated to new value.'
        else:
            model = Flowspec4(
                source=form.source.data,
                source_mask=form.source_mask.data,
                source_port=form.source_port.data,
                destination=form.dest.data,
                destination_mask=form.dest_mask.data,
                destination_port=form.dest_port.data,
                protocol=form.protocol.data,
                flags=";".join(form.flags.data),
                packet_len=form.packet_len.data,
                expires=round_to_ten_minutes(webpicker_to_datetime(form.expires.data)),
                comment=quote_to_ent(form.comment.data),
                action_id=form.action.data,
                user_id=session['user_id'],
                rstate_id=get_state_by_time(webpicker_to_datetime(form.expires.data))
            )
            flash_message = u'IPv4 Rule saved'
            db.session.add(model)

        db.session.commit()
        flash(flash_message, 'alert-success')

        # announce route if model is in active state
        if model.rstate_id == 1:
            route = messages.create_ipv4(model, constants.ANNOUNCE)
            announce_route(route)

        # log changes
        log_route(session['user_id'], model, RULE_TYPES['IPv4'])

        return redirect(url_for('index'))
    else:
        for field, errors in form.errors.items():
            for error in errors:
                print(u"Error in the %s field - %s" % (
                    getattr(form, field).label.text,
                    error
                ))

    default_expires = datetime.now() + timedelta(days=7)
    form.expires.data = datetime_to_webpicker(default_expires)

    return render_template('forms/ipv4_rule.j2', form=form, action_url=url_for('rules.ipv4_rule'))
예제 #2
0
파일: apiv1.py 프로젝트: 0dadj1an/exafs
def create_ipv4(current_user):
    """
    Api method for new IPv4 rule
    :param data: parsed json request
    :param current_user: data from jwt token
    :return: json response
    """
    net_ranges = get_user_nets(current_user['id'])
    json_request_data = request.get_json()
    form = IPv4Form(data=json_request_data, meta={'csrf': False})
    # add values to form instance
    form.action.choices = get_user_actions(current_user['role_ids'])
    form.net_ranges = net_ranges

    # if the form is not valid, we should return 404 with errors
    if not form.validate():
        form_errors = get_form_errors(form)
        if form_errors:
            return jsonify(form_errors), 400

    model = get_ipv4_model_if_exists(form.data, 1)

    if model:
        model.expires, pref_format = parse_api_time(form.expires.data)
        flash_message = u'Existing IPv4 Rule found. Expiration time was updated to new value.'
    else:
        expires, pref_format = parse_api_time(form.expires.data)
        model = Flowspec4(source=form.source.data,
                          source_mask=form.source_mask.data,
                          source_port=form.source_port.data,
                          destination=form.dest.data,
                          destination_mask=form.dest_mask.data,
                          destination_port=form.dest_port.data,
                          protocol=form.protocol.data,
                          flags=";".join(form.flags.data),
                          packet_len=form.packet_len.data,
                          expires=expires,
                          comment=quote_to_ent(form.comment.data),
                          action_id=form.action.data,
                          user_id=current_user['id'],
                          rstate_id=get_state_by_time(expires))
        flash_message = u'IPv4 Rule saved'
        db.session.add(model)

    db.session.commit()

    # announce route if model is in active state
    if model.rstate_id == 1:
        route = messages.create_ipv4(model, ANNOUNCE)
        announce_route(route)

    # log changes
    log_route(current_user['id'], model, RULE_TYPES['IPv4'])

    pref_format = output_date_format(json_request_data, pref_format)
    return jsonify({
        'message': flash_message,
        'rule': model.to_dict(pref_format)
    }), 201
예제 #3
0
파일: test_models.py 프로젝트: CESNET/exafs
def test_get_ipv4_model_if_exists(db):
    """
    test if the function find existing model correctly
    :param db: conftest fixture
    :return:
    """
    model = models.Flowspec4(source="192.168.1.1",
                             source_mask="32",
                             source_port="80",
                             destination="",
                             destination_mask="",
                             destination_port="",
                             protocol="tcp",
                             flags="",
                             packet_len="",
                             action_id=1,
                             expires=datetime.now(),
                             user_id=4,
                             rstate_id=1)
    db.session.add(model)
    db.session.commit()

    form_data = {
        'source': '192.168.1.1',
        'source_mask': '32',
        'source_port': '80',
        'dest': '',
        'dest_mask': '',
        'dest_port': '',
        'protocol': 'tcp',
        'flags': '',
        'packet_len': '',
        'action': 1
    }

    result = models.get_ipv4_model_if_exists(form_data, 1)
    assert result
    assert result == model