diff options
author | Rob Austein <sra@hactrn.net> | 2007-10-01 22:56:39 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2007-10-01 22:56:39 +0000 |
commit | a6748e3876136dc252061a09e16c828eb45c1842 (patch) | |
tree | 20b4646a2caa982e9c69894d8d5c5c178e06ddf2 /scripts | |
parent | 20f735ac7884111b47bd8ba974ed3e8fd1242e89 (diff) |
Make rpki.resource_set.resource_set.from_sql() into a class method.
Rework IRDB code.
svn path=/docs/rpki-db-schema.pdf; revision=1068
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/graphviz-sql.sh | 2 | ||||
-rwxr-xr-x | scripts/irdb.py | 57 | ||||
-rw-r--r-- | scripts/rpki/left_right.py | 10 | ||||
-rw-r--r-- | scripts/rpki/resource_set.py | 8 |
4 files changed, 27 insertions, 50 deletions
diff --git a/scripts/graphviz-sql.sh b/scripts/graphviz-sql.sh index e52fda79..3a9bfa0d 100644 --- a/scripts/graphviz-sql.sh +++ b/scripts/graphviz-sql.sh @@ -15,7 +15,7 @@ for i in "$@" do sqlt-graph --db MySQL --output-type canon --show-datatypes --show-constraints $i | perl -0777 -pe ' - s/\\\n/ /g; + s/\\\n//g; s/ +/ /g; s/\\\|/|/g; s/\\{([a-z0-9_]+)\|/${1}|{/gi; diff --git a/scripts/irdb.py b/scripts/irdb.py index 5423c506..f06c4097 100755 --- a/scripts/irdb.py +++ b/scripts/irdb.py @@ -1,6 +1,7 @@ # $Id$ -import rpki.https, tlslite.api, rpki.config, rpki.resource_set, MySQLdb, rpki.cms +import tlslite.api, MySQLdb +import rpki.https, rpki.config, rpki.resource_set, rpki.cms def handler(query, path): try: @@ -14,55 +15,27 @@ def handler(query, path): assert instanceof(q_msg, rpki.left_right.msg) r_msg = rpki.left_right.msg() for q_pdu in q_msg: - assert isinstance(q_pdu, rpki.left_right.list_resources_elt) and \ - q_pdu.type == "query" and \ - len(q_pdu.resources) == 0 - - org_id = q_pdu.child_id - if org_id is None: - org_id = q_pdu.self_id - cur.execute("""SELECT resource_class_id, subject_name - FROM registrant, resource_class - WHERE registrant.IRBE_mapped_id = '%s' - AND registrant.registrant_id = resource_class.registrant_id - """ % org_id) - resource_classes = cur.fetchall() + assert isinstance(q_pdu, rpki.left_right.list_resources_elt) and q_pdu.type == "query" r_pdu = rpki.left_right.list_resources_elt() r_pdu.type = "reply" r_pdu.self_id = q_pdu.self_id r_pdu.child_id = q_pdu.child_id - # Hmm, I screwed up when I described this table to Tim, - # valid_until should be on the top-level "registrant" table, not - # the "resource_class" table. It's an optional attribute in the - # XML so just punt it for now. + if q_pdu.child_id is not None: + field = "child_id" + else: + field = "self_id" - for resource_class_id, subject_name in resource_classes: - resource_class = rpki.left_right.resource_class_elt() - if subject_name: - resource_class.subject_name = subject_name - - resource_class.as = rpki.resource_set.resource_set_as() - resource_class.as.from_sql(cur, - """SELECT start_as, end_as FROM asn - WHERE resource_class_id = '%s' - """ % resource_class_id) - - resource_class.ipv4 = rpki.resource_set.resource_set_ipv4() - resource_class.ipv4.from_sql(cur, - """SELECT start_ip, end_ip FROM net - WHERE resource_class_id = '%s' AND version = 4 - """ % resource_class_id) - - resource_class.ipv6 = rpki.resource_set.resource_set_ipv6() - resource_class.ipv6.from_sql(cur, - """SELECT start_ip, end_ip FROM net - WHERE resource_class_id = '%s' AND version = 6 - """ % resource_class_id) - - r_pdu.resources.append(resource_class) + cur.execute("SELECT registrant_id, subject_name, valid_until FROM registrant WHERE registrant.%s = %s" % (field, getattr(q_pdu, field))) + assert cur.rowcount == 1, "This query should have produced a single exact match, something's messed up (self_id = %s, child_id = %s)" % (self_id, child_id) + registrant_id, subject_name, valid_until = cur.fetchone() + r_pdu.subject_name = subject_name + r_pdu.valid_until = valid_until.strftime("%Y-%m-%dT%H:%M:%SZ") + r_pdu.as = rpki.resource_set.resource_set_as.from_sql(cur, "SELECT start_as, end_as FROM asn WHERE registrant_id = %s" % registrant_id) + r_pdu.ipv4 = rpki.resource_set.resource_set_ipv4.from_sql(cur, "SELECT start_ip, end_ip FROM net WHERE registrant_id = %s AND version = 4" % registrant_id) + r_pdu.ipv6 = rpki.resource_set.resource_set_ipv6.from_sql(cur, "SELECT start_ip, end_ip FROM net WHERE registrant_id = %s AND version = 6" % registrant_id) r_msg.append(r_pdu) r_elt = r_msg.toXML() diff --git a/scripts/rpki/left_right.py b/scripts/rpki/left_right.py index 069835a3..fd6a17ce 100644 --- a/scripts/rpki/left_right.py +++ b/scripts/rpki/left_right.py @@ -417,10 +417,12 @@ class route_origin_elt(data_elt): roa = None def sql_fetch_hook(self, db, cur): - self.ipv4 = rpki.resource_set.resource_set_ipv4() - self.ipv4.from_sql(cur, "SELECT start_ip, end_ip FROM route_origin_range WHERE route_origin_id = %s AND start_ip NOT LIKE '%:%'", self.route_origin_id) - self.ipv6 = rpki.resource_set.resource_set_ipv6() - self.ipv4.from_sql(cur, "SELECT start_ip, end_ip FROM route_origin_range WHERE route_origin_id = %s AND start_ip LIKE '%:%'", self.route_origin_id) + self.ipv4 = rpki.resource_set.resource_set_ipv4.from_sql(cur, + "SELECT start_ip, end_ip FROM route_origin_range WHERE route_origin_id = %s AND start_ip NOT LIKE '%:%'", + self.route_origin_id) + self.ipv6 = rpki.resource_set.resource_set_ipv6.from_sql(cur, + "SELECT start_ip, end_ip FROM route_origin_range WHERE route_origin_id = %s AND start_ip LIKE '%:%'", + self.route_origin_id) cur.execute("SELECT roa, ca_detail_id FROM roa WHERE route_origin_id = %s", self.route_origin_id) roas = cur.fetchall() if len(roas) == 1: diff --git a/scripts/rpki/resource_set.py b/scripts/rpki/resource_set.py index 45fa096e..b0de948b 100644 --- a/scripts/rpki/resource_set.py +++ b/scripts/rpki/resource_set.py @@ -208,15 +208,17 @@ class resource_set(list): assert isinstance(item, (type(i), type(i.min))) return False - def from_sql(self, cursor, query): - """Populate resource set from an SQL query. + @classmethod + def from_sql(cls, cursor, query): + """Create resource set from an SQL query. cursor is a DB API 2.0 cursor object. query is an SQL query that returns a sequence of (min, max) pairs. """ + cursor.execute(query) - self[:] = [self.range_type(b, e) for (b,e) in cursor.fetchall()] + return cls(ini = [cls.range_type(b, e) for (b,e) in cursor.fetchall()]) class resource_set_as(resource_set): """ASN resource set.""" |