Exemplo n.º 1
0
 def load(identity_db):
     args = UvnIdentityDatabase.get_load_args(identity_db=identity_db)
     db_file = args["basedir"] / UvnDefaults["nameserver"]["persist_file"]
     return yml_obj(UvnNameserver,
                    db_file,
                    from_file=True,
                    identity_db=identity_db,
                    **args)
Exemplo n.º 2
0
 def load(identity_db):
     args = UvnIdentityDatabase.get_load_args(identity_db=identity_db)
     registry_file = args["basedir"] / UvnDefaults["registry"][
         "persist_file"]
     nameserver = UvnNameserver.load(identity_db=identity_db)
     return yml_obj(UvnRegistry,
                    registry_file,
                    from_file=True,
                    identity_db=identity_db,
                    nameserver=nameserver,
                    **args)
Exemplo n.º 3
0
    def _load_deployment(self, deployment_id, store=True, deployment_dir=None):
        if (deployment_dir is None):
            deployment_dir = self.paths.dir_deployment(deployment_id)
        deployment_manifest_file = deployment_dir / UvnDefaults["registry"][
            "deployment_file"]
        db_args = UvnIdentityDatabase.get_load_args(
            identity_db=self.identity_db)
        deployment = yml_obj(UvnDeployment,
                             deployment_manifest_file,
                             from_file=True,
                             registry=self,
                             **db_args)
        if (deployment.id != deployment_id):
            raise UvnException(
                f"Invalid deployment loaded: {deployment.id}, expected {deployment_id}"
            )
        if (store):
            self.deployments.append(deployment)

        return deployment
Exemplo n.º 4
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
Exemplo n.º 5
0
    def bootstrap(package, install_prefix, keep=False):
        package = pathlib.Path(package)
        install_prefix = pathlib.Path(install_prefix).resolve()

        logger.activity("installing cell package: {}", package.name)
        # Create a temporary directory to extract the installer and bootstrap
        # the gpg database
        tmp_dir = tempfile.mkdtemp(prefix="{}-".format(package.stem))
        tmp_dir = pathlib.Path(tmp_dir)

        try:
            logger.debug("extracting {} to {}", package, tmp_dir)

            shutil.unpack_archive(str(package), extract_dir=str(tmp_dir),
                format=UvnDefaults["cell"]["pkg"]["clear_format"])

            # Load installer manifest
            manifest = UvnCellInstaller._manifest_file(tmp_dir)
            installer = yml_obj(UvnCellInstaller, manifest, from_file=True)
            
            logger.debug("loaded installer for cell {} of UVN {} [{}]",
                installer.cell_name, installer.uvn_address, installer.uvn_deployment)

            installer_files = UvnCellInstaller._installer_files(
                tmp_dir, bootstrap=installer._bootstrap)

            # Check that all files are there as expected
            missing_files = [str(f) for f in installer_files.values()
                                        if not f.exists()]
            if missing_files:
                raise UvnException("missing uvn installer files: [{}]".format(
                    ",".join(missing_files)))

            installer_dir = tmp_dir / UvnDefaults["cell"]["pkg"]["export_name"]

            if installer._bootstrap:
                logger.activity("bootstrap: {} -> {}", package.stem, install_prefix)
                bootstrap_dir = installer_dir
                registry = None
            else:
                # extract deployment package into target cell's dir
                logger.activity("deployment: {} -> {}", package.stem, install_prefix)
                bootstrap_dir = install_prefix
                identity_db = UvnIdentityDatabase.load(basedir=bootstrap_dir)
                from libuno.reg import UvnRegistry
                registry = UvnRegistry.load(identity_db)

            
            # Decrypt cell package and extract it
            UvnIdentityDatabase.bootstrap_cell(
                bootstrap_dir=bootstrap_dir,
                registry=registry,
                uvn_address=installer.uvn_address,
                uvn_admin=installer.uvn_admin,
                cell_name=installer.cell_name,
                cell_admin=installer.cell_admin,
                cell_pkg=installer_files["cell_pkg"],
                cell_sig=installer_files["cell_sig"],
                uvn_public_key=installer_files.get("uvn_public_key"),
                cell_public_key=installer_files.get("cell_public_key"),
                cell_private_key=installer_files.get("cell_private_key"),
                cell_secret=installer_files.get("cell_secret"),
                keep=keep)
            
            if installer._bootstrap:
                shutil.copytree(str(installer_dir), str(install_prefix))

        finally:
            if not keep:
                # Delete temporary directory
                shutil.rmtree(str(tmp_dir))
            else:
                logger.warning("[tmp] not deleted: {}", tmp_dir)

        logger.activity("installed package: {} -> {}", package.name, install_prefix)