Exemple #1
0
        def repr_py(self, yml_repr, **kwargs):
            deployment = kwargs.get("deployment")
            cell = kwargs["registry"].cells[yml_repr["cell"]]

            peers = [
                repr_py(CellDeployment.Peer, p, **kwargs)
                for p in yml_repr["peers"]
            ]

            backbone = [
                repr_py(CellDeployment.BackboneConnection, c, **kwargs)
                for c in yml_repr["backbone"]
            ]

            if (deployment is not None):
                psks = deployment.psks
            elif ("psks" in yml_repr):
                psks = PresharedKeys()

            py_repr = CellDeployment(cell=cell,
                                     peers=peers,
                                     backbone=backbone,
                                     psks=psks,
                                     deploy_id=yml_repr["deploy_id"])

            return py_repr
Exemple #2
0
        def repr_py(self, yml_repr, **kwargs):
            registry = kwargs["registry"]

            strategy = repr_py(DeploymentStrategy, yml_repr["strategy"],
                               **kwargs)
            deploy_time = repr_py(Timestamp, yml_repr["deploy_time"], **kwargs)
            cells = [registry.cell(c) for c in yml_repr["cells"]]
            psks = repr_py(PresharedKeys, yml_repr["psks"], **kwargs)
            address_range = tuple(
                ipaddress.ip_address(a) for a in yml_repr["address_range"])

            py_repr = UvnDeployment(id=yml_repr["id"],
                                    strategy=strategy,
                                    deploy_time=deploy_time,
                                    address_range=address_range,
                                    cells=cells,
                                    deployed_cells=[],
                                    psks=psks,
                                    registry=registry,
                                    loaded=bool(kwargs.get("from_file")))

            py_repr.deployed_cells = [
                repr_py(CellDeployment, c, deployment=py_repr, **kwargs)
                for c in yml_repr.get("deployed_cells", [])
            ]

            return py_repr
Exemple #3
0
 def repr_py(self, yml_repr, **kwargs):
     py_repr = Particle(name=yml_repr["name"],
                        n=yml_repr["n"],
                        contact=yml_repr["contact"],
                        psks=repr_py(PresharedKeys, yml_repr["psks"],
                                     **kwargs),
                        keymat=repr_py(WireGuardKeyPair,
                                       yml_repr["keymat"], **kwargs))
     return py_repr
Exemple #4
0
 def repr_py(self, yml_repr, **kwargs):
     last_activity = None
     if ("last_activity" in yml_repr):
         last_activity = repr_py(Timestamp, 
                             yml_repr["last_activity"], **kwargs)
     activity_timeout = repr_py(Duration, 
                             yml_repr["activity_timeout"], **kwargs)
     py_repr = ActivityMonitor(
                 activity_timeout = activity_timeout,
                 last_activity = last_activity)
     return py_repr
Exemple #5
0
        def repr_py(self, yml_repr, **kwargs):

            return RegistryRouterPorts(
                min=yml_repr["min"],
                max=yml_repr["max"],
                reserved=yml_repr["reserved"],
                allocated={
                    k: repr_py(CellRouterPort, p, **kwargs)
                    for k, p in yml_repr["allocated"].items()
                },
                psks=repr_py(PresharedKeys, yml_repr["psks"], **kwargs),
                keymat=repr_py(WireGuardKeyPair, yml_repr["keymat"], **kwargs))
Exemple #6
0
 def repr_py(self, yml_repr, **kwargs):
     return UvnNameserver(identity_db=kwargs["identity_db"],
                          db={
                              k: repr_py(UvnNameserver.Record, v,
                                         **kwargs)
                              for k, v in yml_repr["db"].items()
                          })
Exemple #7
0
 def repr_py(self, yml_repr, **kwargs):
     keymat = repr_py(CellKeyMaterial, yml_repr["keymat"], **kwargs)
     py_repr = CellIdentity(name=yml_repr["name"],
                            n=yml_repr["n"],
                            address=yml_repr["address"],
                            keymat=keymat,
                            location=yml_repr["location"],
                            admin=yml_repr["admin"],
                            admin_name=yml_repr["admin_name"])
     return py_repr
Exemple #8
0
 def repr_py(self, yml_repr, **kwargs):
     return CellRouterPort(
         cell_n=yml_repr["cell_n"],
         n=yml_repr["n"],
         psk=yml_repr["psk"],
         keymat=repr_py(WireGuardKeyPair, yml_repr["keymat"]),
         registry_pubkey=yml_repr["registry_pubkey"],
         addr_local=ipaddress.ip_address(yml_repr["addr_local"]),
         addr_remote=ipaddress.ip_address(yml_repr["addr_remote"]),
         interface=yml_repr["interface"],
         area=yml_repr["area"])
Exemple #9
0
 def repr_py(self, yml_repr, **kwargs):
     return UvnRegistry.RegistryVpn(
         interface=yml_repr["interface"],
         registry_endpoint=yml_repr["registry_endpoint"],
         registry_address=ipaddress.ip_address(
             yml_repr["registry_address"]),
         registry_port=yml_repr["registry_port"],
         registry_pubkey=yml_repr["registry_pubkey"],
         registry_privkey=yml_repr["registry_privkey"],
         peers=[
             repr_py(UvnRegistry.RegistryVpnPeer, p, **kwargs)
             for p in yml_repr["peers"]
         ])
Exemple #10
0
        def repr_py(self, yml_repr, **kwargs):
            cell_id = repr_py(CellIdentity, yml_repr["id"], **kwargs)
            if isinstance(yml_repr["registry_vpn"], dict):
                registry_vpn = repr_py(Cell.RegistryConnection,
                                       yml_repr["registry_vpn"], **kwargs)
            else:
                registry_vpn = None

            if isinstance(yml_repr["router_port"], dict):
                router_port = repr_py(CellRouterPort, yml_repr["router_port"],
                                      **kwargs)
            else:
                router_port = None

            py_repr = Cell(cell_id=cell_id,
                           peer_ports=yml_repr["peer_ports"],
                           psk=yml_repr["registry_psk"],
                           registry_vpn=registry_vpn,
                           router_port=router_port,
                           loaded=True)

            return py_repr
Exemple #11
0
 def repr_py(self, yml_repr, **kwargs):
     py_repr = CellDeployment.BackboneConnection(
         cell_name=yml_repr["cell_name"],
         cell_pubkey=yml_repr["cell_pubkey"],
         cell_privkey=yml_repr["cell_privkey"],
         interface=yml_repr["interface"],
         net_cell_n=yml_repr["net_cell_n"],
         port_i=yml_repr["port_i"],
         port_local=yml_repr["port_local"],
         addr_local=ipaddress.ip_address(yml_repr["addr_local"]),
         network_local=yml_repr["network_local"],
         network=ipaddress.ip_network(yml_repr["network"]),
         network_mask=int(yml_repr["network_mask"]),
         peers=[
             repr_py(CellDeployment.BackboneConnection.Peer, p,
                     **kwargs) for p in yml_repr["peers"]
         ])
     return py_repr
Exemple #12
0
        def repr_py(self, yml_repr, **kwargs):
            identity_db = kwargs["identity_db"]
            nameserver = kwargs["nameserver"]
            deployments = kwargs.get("deployments")
            cells = {
                c["id"]["name"]: repr_py(Cell, c, **kwargs)
                for c in yml_repr["cells"]
            }
            particles = {
                p["name"]: repr_py(Particle, p, **kwargs)
                for p in yml_repr["particles"]
            }
            keymat = repr_py(WireGuardKeyPair, yml_repr["keymat"], **kwargs)

            if "vpn_config" in yml_repr:
                vpn_config = repr_py(UvnRegistry.RegistryVpn,
                                     yml_repr["vpn_config"], **kwargs)
            else:
                vpn_config = None

            if "router_ports" in yml_repr:
                router_ports = repr_py(RegistryRouterPorts,
                                       yml_repr["router_ports"], **kwargs)
            else:
                router_ports = None

            py_repr = UvnRegistry(identity_db=identity_db,
                                  cells=cells,
                                  particles=particles,
                                  keymat=keymat,
                                  ports=yml_repr["ports"],
                                  loaded=True,
                                  pkg_cell=yml_repr.get("pkg_cell", None),
                                  deployment_id=yml_repr.get(
                                      "deployment_id", None),
                                  vpn_config=vpn_config,
                                  nameserver=nameserver,
                                  router_ports=router_ports)

            # Register cells with identity_db
            for c in py_repr.cells.values():
                with_secret = (py_repr.packaged
                               and c.id.name == py_repr.pkg_cell)
                py_repr.identity_db.register_cell(name=c.id.name,
                                                  address=c.id.address,
                                                  admin=c.id.admin,
                                                  admin_name=c.id.admin_name,
                                                  with_secret=with_secret)

            deployment_loaded = False

            if ("deployments" in yml_repr):
                for d in yml_repr["deployments"]:
                    if (deployments is not None and d in deployments):
                        py_repr.deployments.append(deployments[d])
                        continue

                    try:
                        py_repr._load_deployment(deployment_id=d)
                        if d == py_repr.deployment_id:
                            deployment_loaded = True
                    except Exception as e:
                        # traceback.print_exc()
                        logger.exception(e)
                        logger.warning("failed to load deployment {}: {}", d,
                                       e)

            if not py_repr.packaged:
                # Generate particle configurations
                py_repr._generate_particles()
            else:
                if (py_repr.deployment_id !=
                        UvnDefaults["registry"]["deployment_bootstrap"]
                        and not deployment_loaded):
                    raise UvnException(
                        f"required deployment not loaded: {py_repr.deployment_id}"
                    )
                try:
                    file_path = py_repr.paths.basedir / UvnDefaults[
                        "registry"]["cell_file"]
                    db_args = UvnIdentityDatabase.get_load_args(
                        identity_db=identity_db)
                    cell = yml_obj(Cell,
                                   file_path,
                                   from_file=True,
                                   identity_db=identity_db,
                                   **db_args)
                    if cell.id.name != py_repr.pkg_cell:
                        raise UvnException(
                            f"invalid UVN package: expected={py_repr.pkg_cell}, found={cell.id.name}"
                        )
                    py_repr.cells[cell.id.name] = cell
                    # Generate particle server
                    py_repr._register_particles(py_repr.deployed_cell)
                    # logger.activity("[loaded] UVN package for {} [{}]",
                    #     cell.id.name, py_repr.deployment_id)
                except Exception as e:
                    raise UvnException(
                        f"failed to load UVN package for {py_repr.pkg_cell}: {e}"
                    )

            logger.activity(
                "[{}]{} loaded UVN: {}",
                py_repr.pkg_cell if py_repr.packaged else "root",
                f"[{py_repr.deployment_id}]" if py_repr.packaged else "",
                py_repr.identity_db.registry_id.address)

            return py_repr
Exemple #13
0
 def keymat_deserialize(k):
     return repr_py(WireGuardKeyPair, k, **kwargs)