Ejemplo n.º 1
0
def validate_chain_delete(chain_id):
    if not chain_id:
        raise abort(400, 'No chain id specified.')
    chain = chain_wrapper.get_chain(chain_id)
    if not chain:
        raise abort(400, 'The chain doesn\'t exists')
    return chain
Ejemplo n.º 2
0
def validate_chain_delete(chain_id):
    if not chain_id:
        raise abort(400, 'No chain id specified.')
    chain = chain_wrapper.get_chain(chain_id)
    if not chain:
        raise abort(400, 'The chain doesn\'t exists')
    return chain
Ejemplo n.º 3
0
def create_rule(rule_json):
    rule_json['family'], rule_json['tableName'], rule_json['chainName'] = rule_json['chain'].split(':')
    cmd_string = 'add rule {family} {tableName} {chainName} '.format(**rule_json)
    cmd_string += '{expression} {key} '.format(**rule_json)
    cmd_string += nft_utils.statements_to_str(rule_json['statements'])
    cmd = nft_utils.nft_command(cmd_string)
    cmd_result = cmd.wait()
    if cmd_result == 0:
        nft_utils.close_nft_command(cmd)
        from wrappers import chain_wrapper
        rule = rule_json
        # Get handle from last rule added
        rule['handle'] = chain_wrapper.get_chain(rule['chain'])['rules'][-1].split(':')[3]
        rule['id'] = rule['chain'] + ':' + rule['handle']
        return rule
    else:
        raise NFTError(Error(cmd.stdout.readlines()))
Ejemplo n.º 4
0
def chain(chain_id):
    '''
    GET:
      Get a chain by it's id
    DELETE:
      Delete the chain with the specified id
    '''
    if request.method == 'DELETE':
        try:
            chain = chain_validator.validate_chain_delete(chain_id)
            chain = chain_wrapper.delete_chain(chain_id)
            return jsonify(chain=chain)
        except NFTValidationError as e:
            return abort(400, e)
        except NFTError as e:
            return abort(500, e)
    else:
        return jsonify(chain=chain_wrapper.get_chain(chain_id))
Ejemplo n.º 5
0
def create_rule(rule_json):
    rule_json['family'], rule_json['tableName'], rule_json[
        'chainName'] = rule_json['chain'].split(':')
    cmd_string = 'add rule {family} {tableName} {chainName} '.format(
        **rule_json)
    cmd_string += '{expression} {key} '.format(**rule_json)
    cmd_string += nft_utils.statements_to_str(rule_json['statements'])
    cmd = nft_utils.nft_command(cmd_string)
    cmd_result = cmd.wait()
    if cmd_result == 0:
        nft_utils.close_nft_command(cmd)
        from wrappers import chain_wrapper
        rule = rule_json
        # Get handle from last rule added
        rule['handle'] = chain_wrapper.get_chain(
            rule['chain'])['rules'][-1].split(':')[3]
        rule['id'] = rule['chain'] + ':' + rule['handle']
        return rule
    else:
        raise NFTError(Error(cmd.stdout.readlines()))