Example #1
0
def update_from_yaml(path='secrets/secrets.d',
                     keep_original=False,
                     verbose=False):
    """Helper function to convert old YAML style directories."""
    yaml_files = [
        fn for fn in get_files_from_path(path) if fn.endswith('.yml')
    ]
    for yaml_file in yaml_files:
        # json_file = f"{os.path.splitext(yaml_file)[0]}.json"
        json_file = yaml_file.replace('.yml', '.json')
        if verbose:
            logger.info("[+] converting '%s' to JSON", yaml_file)
        yaml_to_json(yaml_file=yaml_file, json_file=json_file)
        if not keep_original:
            if verbose:
                logger.info("[+] removing '%s'", yaml_file)
            safe_delete_file(yaml_file)
Example #2
0
 def take_action(self, parsed_args):
     if '-' in parsed_args.arg and parsed_args.convert:
         raise RuntimeError('[-] stdin cannot be used with ``--convert``')
     for arg in parsed_args.arg:
         path = os.path.abspath(arg) if arg != '-' else arg
         if parsed_args.convert:
             update_from_yaml(path=path,
                              keep_original=parsed_args.keep_original,
                              verbose=(self.app_args.verbose_level >= 1))
         elif path == '-':
             yaml_to_json(yaml_file=path)
         else:
             yaml_files = [
                 fn for fn in get_files_from_path(path)
                 if fn.endswith('.yml')
             ]
             for yaml_file in yaml_files:
                 self.logger.info("[+] converting '%s'", yaml_file)
                 yaml_to_json(yaml_file=yaml_file)
Example #3
0
    def clone_from(self, src=None):
        """
        Clone from existing definition file(s)

        The source can be (a) a directory full of one or more
        group descriptions, (b) a single group descriptions file,
        or (c) an existing environment's descriptions file(s).
        """
        src = src.strip('/')
        if src in ['', None]:
            raise RuntimeError('[-] no source provided')
        if os.path.isdir(src):
            if not src.endswith('.d'):
                raise RuntimeError(
                    "[-] refusing to process a directory without "
                    f"a '.d' extension ('{src}')")
        elif os.path.isfile(src) and not src.endswith('.json'):
            raise RuntimeError("[-] refusing to process a file without "
                               f"a '.json' extension ('{src}')")
        if not (os.path.exists(src) or self.environment_exists(env=src)):
            raise RuntimeError(
                f"[-] directory or environment '{src}' does not exist")
        dest = self.get_descriptions_path(create=True)
        if src.endswith('.d'):
            # Copy anything when cloning from directory.
            src_files = get_files_from_path(src)
            for src_file in src_files:
                copy(src_file, dest)
        elif src.endswith('.json'):
            # Copy just the one file when cloning from a file.
            copy(src, dest)
        else:
            # Only copy descriptions when cloning from environment.
            src_env = SecretsEnvironment(environment=src)
            copydescriptions(src_env.get_descriptions_path(), dest)
        remove_other_perms(dest)
        self.read_secrets_descriptions()
        self.find_new_secrets()