Esempio n. 1
0
def ns_default_command(record_ns_key_default):
    try:
        data_record = model.read_by_id("record", record_ns_key_default)
    except Exception as e:
        raise e
    else:
        zone = model.read_by_id("zone", str(data_record['zone']))
        ttl = model.read_by_id("ttl", str(data_record['ttl']))['value']
        types = model.read_by_id("type", str(data_record['type']))['value']
        data_content = model.content_by_record(data_record['key'])
        for i in data_content:
            json_command = {
                zone['value']: {
                    "id_zone": zone['key'],
                    "type": "general",
                    "command": "zone",
                    "general": {
                        "sendblock": {
                            "cmd": "zone-set",
                            "zone": zone['value'],
                            "owner": data_record['value'],
                            "rtype": types,
                            "ttl": ttl,
                            "data": i['value']
                        },
                        "receive": {
                            "type": "block"
                        }
                    }
                }
            }
            producer.send(json_command)
Esempio n. 2
0
def set_zone(record_id, command):
    """Send zone command with JSON structure to broker."""
    record, zone, type_, ttl, rdata = get_other_data(record_id)
    zone_name = zone["zone"]

    # escape space and double quote in txt rdata
    rdata = rdata["rdata"]
    if type_["type"] == "TXT":
        rdata = json.dumps(rdata)

    zone_begin = {"cmd": "zone-begin", "zone": zone_name}
    zone_set = generate_command(
        zone=zone_name,
        owner=record["owner"],
        rtype=type_["type"],
        ttl=ttl["ttl"],
        rdata=rdata,
        command=command,
    )
    zone_commit = {"cmd": "zone-commit", "zone": zone_name}

    queries = []
    for query in [zone_begin, zone_set, zone_commit]:
        queries.append(query)

    # agent_type: master
    # because zone only created in master, slave will get zone via axfr
    message = {"agent": {"agent_type": ["master"]}, "knot": queries}

    producer.send(message)
Esempio n. 3
0
def delegate(zone, zone_id, command, agent_type):
    """Send delegation config command with JSON structure to broker."""
    config = helpers.get_config()
    try:
        clusters = config["knot_servers"]
    except KeyError:
        raise ValueError("Can't Knot server list in config")

    cluster = clusters[agent_type]

    # default for master
    cluster_type = "notify"
    cluster_type_item = "notify"
    if agent_type == "slave":
        cluster_type = "master"
        cluster_type_item = "master"

    queries = [
        {
            "item": "file",
            "data": f"{zone}.zone",
            "identifier": zone
        },
        {
            "item": "serial-policy",
            "data": "dateserial"
        },
        {
            "item": "module",
            "data": "mod-stats/default"
        },
    ]
    queries.extend([{
        "item": cluster_type_item,
        "data": item
    } for item in cluster[cluster_type]])

    queries.extend([{"item": "acl", "data": acl} for acl in cluster["acl"]])

    queries_ = []
    for query in queries:
        query["cmd"] = command
        query["section"] = "zone"
        query["zone"] = zone
        queries_.append(query)

    message = {
        "agent": {
            "agent_type": [agent_type]
        },
        "knot": [{
            "cmd": "conf-begin"
        }, *queries_, {
            "cmd": "conf-commit"
        }],
    }

    producer.send(message)
Esempio n. 4
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('zone', type=str, required=True)
        parser.add_argument('project_id', type=str, required=True)
        args = parser.parse_args()
        zone = args['zone']
        project_id = args['project_id']
        user = model.get_user_by_project_id(project_id)['key']
        zone_key = utils.get_last_key("zone")

        # Validation Unique Zone
        if utils.check_unique("zone", "value", zone):
            return response(401, message="Duplicate zone Detected")
        # Validation Zone Name
        if validation.zone_validation(zone):
            return response(401, message="Named Error")
        # Check Relation Zone to User
        if model.check_relation("user", user):
            return response(401,
                            message="Relation to user error Check Your Key")

        zone_data = {
            "key": zone_key,
            "value": zone,
            "created_at": utils.get_datetime(),
            "user": user,
        }
        try:
            model.insert_data("zone", zone_key, zone_data)
        except Exception as e:
            return response(401, message=str(e))
        else:

            # Adding Zone Config
            config_command = command.config_zone(zone, zone_key)
            producer.send(config_command)
            # ADDING DEFAULT RECORD
            record_key_soa = utils.get_last_key("record")
            add_soa_record(zone_key, record_key_soa)
            command.soa_default_command(record_key_soa)

            record_key_ns = utils.get_last_key("record")
            add_ns_default(zone_key, record_key_ns)
            command.ns_default_command(record_key_ns)

            record_key_cname = utils.get_last_key("record")
            add_cname_default(zone_key, record_key_cname, zone)
            json_command = command.record_insert(record_key_cname)
            producer.send(json_command)
            # DEFAULT RECORD END

            return response(200, data=zone_data, message="Inserted")
Esempio n. 5
0
def soa_default_command(soa_record_key):
    try:
        data_record = model.read_by_id("record", soa_record_key)
    except Exception as e:
        raise e

    try:
        zone = model.read_by_id("zone", data_record['zone'])
    except Exception as e:
        raise e

    try:
        data_type = model.read_by_id("type", data_record['type'])['value']
    except Exception as e:
        raise e

    if data_type != "SOA":
        return False

    try:
        data_ttl = model.read_by_id("ttl", data_record['ttl'])['value']
    except Exception as e:
        raise e
    try:
        data_content = model.content_by_record(data_record['key'])[0]['value']
    except Exception as e:
        raise e

    json_command = {
        zone['value']: {
            "id_zone": zone['key'],
            "command": "zone",
            "type": "general",
            "general": {
                "sendblock": {
                    "cmd": "zone-set",
                    "zone": zone['value'],
                    "owner": data_record['value'],
                    "rtype": "SOA",
                    "ttl": data_ttl,
                    "data": data_content
                },
                "receive": {
                    "type": "block"
                }
            }
        }
    }
    producer.send(json_command)
Esempio n. 6
0
 def get(self, key):
     zone_data = model.read_by_id("zone", key)['value']
     command_data = command.config_zone(zone_data, key)
     try:
         test = producer.send(command_data)
     except Exception as e:
         return response(401, message=str(e))
     else:
         data = {"send_status": test, "command": command_data}
         return response(200, data=data, message=test)
Esempio n. 7
0
def set_default_zone(record_ids):
    """Send zone command with JSON structure to broker."""
    # We can use `send_zone` but it will be slow since each zone will be sent
    # separately

    zone_sets = []
    for record_id in record_ids:
        record, zone, type_, ttl, rdata = get_other_data(record_id)

        # escape space and double quote in txt rdata
        rdata = rdata["rdata"]
        if type_["type"] == "TXT":
            rdata = json.dumps(rdata)

        zone_name = zone["zone"]
        zone_set = generate_command(
            zone=zone_name,
            owner=record["owner"],
            rtype=type_["type"],
            ttl=ttl["ttl"],
            rdata=rdata,
            command="zone-set",
        )
        zone_sets.append(zone_set)

    zone_begin = {"cmd": "zone-begin", "zone": zone_name}
    zone_commit = {"cmd": "zone-commit", "zone": zone_name}

    queries = []
    for query in [zone_begin, *zone_sets, zone_commit]:
        queries.append(query)

    # agent_type: master
    # because zone only created in master, slave will get zone via axfr
    message = {"agent": {"agent_type": ["master"]}, "knot": queries}

    producer.send(message)
Esempio n. 8
0
def set_config(zone, zone_id, command):
    """Send config command with JSON structure to broker."""

    # there are two option to put conf-begin and conf-commit
    # either here (api) or in (agent)
    # I'd rather choose to put it here for finer tuning
    conf_begin = {"cmd": "conf-begin", "zone": zone}
    conf_set = {
        "cmd": command,
        "section": "zone",
        "item": "domain",
        "data": zone
    }
    conf_commit = {"cmd": "conf-commit", "zone": zone}

    queries = []
    for query in [conf_begin, conf_set, conf_commit]:
        queries.append(query)

    # agent_type: master, slave
    # because config created both in  master and slave
    message = {"agent": {"agent_type": ["master", "slave"]}, "knot": queries}

    producer.send(message)