""" Convert {parents,children,pubclients}.csv into new XML formats. $Id$ Copyright (C) 2010 Internet Systems Consortium ("ISC") Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ import subprocess, re, os, getopt, sys, base64, urlparse import rpki.sundial, rpki.myrpki, rpki.config from lxml.etree import Element, SubElement, ElementTree section_regexp = re.compile("\s*\[\s*(.+?)\s*\]\s*$") variable_regexp = re.compile("\s*([-a-zA-Z0-9_]+)(\s*=\s*)(.+?)\s*$") cfg_file = "rpki.conf" template_file = os.path.join(os.path.dirname(sys.argv[0]), "examples", "rpki.conf") new_cfg_file = None preserve_valid_until = False opts, argv = getopt.getopt(sys.argv[1:], "c:hn:pt:?", ["config=", "new_config=", "preserve_valid_until", "template_config=", "help"]) for o, a in opts: if o in ("-h", "--help", "-?"): print __doc__ sys.exit(0) elif o in ("-c", "--config"): cfg_file = a elif o in ("-n", "--new_config"): new_cfg_file = a elif o in ("-p", "--preserve_valid_until"): preserve_valid_until = True elif o in ("-t", "--template_config"): template_file = a if argv: raise RuntimeError, "Unexpected arguments %r" % (argv,) if os.path.samefile(cfg_file, template_file): raise RuntimeError, "Old config and template for new config can't be the same file" if new_cfg_file is None: new_cfg_file = cfg_file + ".new" if os.path.exists(new_cfg_file): raise RuntimeError, "%s already exists, NOT overwriting" % new_cfg_file cfg = rpki.config.parser(cfg_file) # These have no counterparts in new config file, just read them from old repository_bpki_certificate = cfg.get(option = "repository_bpki_certificate", section = "myrpki") repository_handle = cfg.get(option = "repository_handle", section = "myrpki") parents_csv = cfg.get(option = "parents_csv", section = "myrpki", default = "parents.csv") children_csv = cfg.get(option = "children_csv", section = "myrpki", default = "children.csv") pubclients_csv = cfg.get(option = "pubclients_csv", section = "myrpki", default = "pubclients.csv") pubd_base = cfg.get(option = "pubd_base", section = "myirbe") # Here we need to construct values for the new config file from the # old one. Basic model here is to look at whatever variables need to # be set in the template (mostly just the [myrpki], I hope), pull # necessary data from old config file any way we can. Stuff that # didn't make the jump from old config file to new we can just ignore, # stuff that is automated via macro expansions in the new config file # should be ok without modification. r = {} if cfg.has_section("myrpki"): for i in ("handle", "roa_csv", "prefix_csv", "asn_csv", "xml_filename"): r["myrpki", i] = cfg.get(section = "myrpki", option = i) r["myrpki", "bpki_resources_directory"] = cfg.get(option = "bpki_directory", section = "myrpki") if cfg.has_section("myirbe"): r["myrpki", "bpki_servers_directory"] = cfg.get(option = "bpki_directory", section = "myirbe") r["myrpki", "run_rpkid"] = True r["myrpki", "run_pubd"] = cfg.getboolean(option = "want_pubd", section = "myirbe", default = False) r["myrpki", "run_rootd"] = cfg.getboolean(option = "want_rootd", section = "myirbe", default = False) else: for i in ("run_rpkid", "run_pubd", "run_rootd"): r["myrpki", i] = False if cfg.has_section("rpkid"): r["myrpki", "rpkid_server_host"] = cfg.get(option = "server-host", section = "rpkid") r["myrpki", "rpkid_server_port"] = cfg.get(option = "server-port", section = "rpkid") if cfg.has_section("irdbd"): u = urlparse.urlparse(cfg.get(option = "https-url", section = "irdbd")) r["myrpki", "irdbd_server_host"] = u.hostname or "localhost" r["myrpki", "irdbd_server_port"] = u.port or 443 if cfg.has_section("pubd"): r["myrpki", "pubd_server_host"] = cfg.get(option = "server-host", section = "pubd") r["myrpki", "pubd_server_port"] = cfg.get(option = "server-port", section = "pubd") r["myrpki", "publication_base_directory"] = cfg.get(option = "publication-base", section = "pubd") if cfg.has_section("rootd"): r["myrpki", "rootd_server_port"] = cfg.get(option = "server-port", section = "rootd") u = urlparse.urlparse(cfg.get(option = "rpki-base-uri", section = "rootd")) r["myrpki", "publication_rsync_server"] = u.netloc for i in ("rpkid", "irdbd", "pubd"): if cfg.has_section(i): for j in ("sql-database", "sql-username", "sql-password"): r[i, j] = cfg.get(section = i, option = j) f = open(new_cfg_file, "w") f.write("# Automatically converted from %s using %s as a template.\n\n" % (cfg_file, template_file)) section = None for line in open(template_file): m = section_regexp.match(line) if m: section = m.group(1) m = variable_regexp.match(line) if m: option, whitespace = m.group(1, 2) else: option = None if (section, option) in r: line = "%s%s%s\n" % (option, whitespace, r[section, option]) f.write(line) f.close() print "Wrote", new_cfg_file # Get all of these from the new config file; in theory we just set all # of them, but we want to use values matching new config in any case. newcfg = rpki.config.parser(new_cfg_file, "myrpki") handle = newcfg.get("handle") bpki_resources_directory = newcfg.get("bpki_resources_directory") bpki_servers_directory = newcfg.get("bpki_servers_directory") pubd_server_host = newcfg.get("pubd_server_host") pubd_server_port = newcfg.get("pubd_server_port") rpkid_server_host = newcfg.get("rpkid_server_host") rpkid_server_port = newcfg.get("rpkid_server_port") entitydb_dir = newcfg.get("entitydb_dir", "entitydb") bpki_resources_pemfile = bpki_resources_directory + "/ca.cer" bpki_servers_pemfile = bpki_servers_directory + "/ca.cer" def entitydb(*args): return os.path.join(entitydb_dir, *args) # Now convert the .csv files. It'd be nice to have XML validation # enabled for this, so try to turn it on ourselves if the magic # environment variable hasn't already been set. rng_file = os.path.join(os.path.dirname(sys.argv[0]), "myrpki.rng") if not os.getenv("MYRPKI_RNG") and os.path.exists(rng_file): os.putenv("MYRPKI_RNG", rng_file) for d in map(entitydb, ("children", "parents", "repositories", "pubclients")): if not os.path.exists(d): os.makedirs(d) one_year_from_now = str(rpki.sundial.now() + rpki.sundial.timedelta(days = 365)) if os.path.exists(children_csv): for child_handle, valid_until, child_resource_pemfile in rpki.myrpki.csv_reader(children_csv, columns = 3): try: e = Element("parent", valid_until = valid_until if preserve_valid_until else one_year_from_now, service_uri = "https://%s:%s/up-down/%s/%s" % (rpkid_server_host, rpkid_server_port, handle, child_handle), child_handle = child_handle, parent_handle = handle) rpki.myrpki.PEMElement(e, "bpki_resource_ta", bpki_resources_pemfile) rpki.myrpki.PEMElement(e, "bpki_server_ta", bpki_servers_pemfile) rpki.myrpki.PEMElement(e, "bpki_child_ta", child_resource_pemfile) rpki.myrpki.etree_write(e, entitydb("children", "%s.xml" % child_handle)) except IOError: pass if os.path.exists(parents_csv): for parent_handle, parent_service_uri, parent_cms_pemfile, parent_https_pemfile, parent_myhandle, parent_sia_base in rpki.myrpki.csv_reader(parents_csv, columns = 6): try: e = Element("parent", valid_until = one_year_from_now, service_uri = parent_service_uri, child_handle = parent_myhandle, parent_handle = parent_handle) rpki.myrpki.PEMElement(e, "bpki_resource_ta", parent_cms_pemfile) rpki.myrpki.PEMElement(e, "bpki_server_ta", parent_https_pemfile) rpki.myrpki.PEMElement(e, "bpki_child_ta", bpki_resources_pemfile) rpki.myrpki.etree_write(e, entitydb("parents", "%s.xml" % parent_handle)) client_handle = "/".join(parent_sia_base.rstrip("/").split("/")[3:]) assert client_handle.startswith(repository_handle) e = Element("repository", parent_handle = parent_handle, client_handle = client_handle, service_uri = "%s/client/%s" % (pubd_base.rstrip("/"), client_handle), sia_base = parent_sia_base, type = "confirmed") rpki.myrpki.PEMElement(e, "bpki_server_ta", repository_bpki_certificate) rpki.myrpki.PEMElement(e, "bpki_client_ta", bpki_resources_pemfile) SubElement(e, "contact_info").text = "Automatically generated by convert-csv.py" rpki.myrpki.etree_write(e, entitydb("repositories", "%s.xml" % parent_handle)) except IOError: pass if os.path.exists(pubclients_csv): for client_handle, client_resource_pemfile, client_sia_base in rpki.myrpki.csv_reader(pubclients_csv, columns = 3): try: parent_handle = client_handle.split("/")[-2] if "/" in client_handle else handle e = Element("repository", parent_handle = parent_handle, client_handle = client_handle, service_uri = "https://%s:%s/client/%s" % (pubd_server_host, pubd_server_port, client_handle), sia_base = client_sia_base, type = "confirmed") rpki.myrpki.PEMElement(e, "bpki_server_ta", bpki_servers_pemfile) rpki.myrpki.PEMElement(e, "bpki_client_ta", client_resource_pemfile) SubElement(e, "contact_info").text = "Automatically generated by convert-csv.py" rpki.myrpki.etree_write(e, entitydb("pubclients", "%s.xml" % client_handle.replace("/", "."))) except IOError: pass Rob Austein <sra@hactrn.net> 2008-10-25 05:03:51 +0000 Remove "//" check' href='/sra/rpki.net/commit/rcynic/rcynic.c?id=766799a690f5c909d19c9f2cb26ff39d526f76c7'>766799a6
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423