Beispiel #1
0
    def test_import_bad(self):
        sample_file_path = os.path.join(os.path.dirname(__file__), 'files',
                                        'bad.yaml')

        with self.assertRaisesRegex(
                ValueError, r"Error parsing file \(.+\) as YAML or JSON:"):
            parse_yaml_or_json_file(sample_file_path)
    def __add_config_file(self, config_file):
        """Adds a JSON or YAML file as config to this Config.

        Parameters
        ----------
        config_file : str (file path)
            A string that is a path to an existing YAML or JSON file to
            parse and validate as a configuration to add to this Config.

        Raises
        ------
        ValueError
            If can not parse given file as YAML or JSON
        AssertionError
            If dictionary parsed from given YAML or JSON file is not a valid config.
        """
        # parse the configuration file
        try:
            parsed_config_file = parse_yaml_or_json_file(config_file)
        except ValueError as error:
            raise ValueError(
                f"Error parsing config file ({config_file}) as json or yaml"
            ) from error

        # add the config parsed from file
        try:
            self.__add_config_dict(parsed_config_file, config_file)
        except AssertionError as error:
            raise AssertionError(
                f"Failed to add parsed configuration file ({config_file}): {error}"
            ) from error
    def test_decrypt_parent_source_dict(self, sops_mock):
        encrypted_config_file_path = os.path.join(
            os.path.dirname(__file__), 'files',
            'step-runner-config-secret-stuff.yml')

        encrypted_config = parse_yaml_or_json_file(encrypted_config_file_path)
        encrypted_config_json = json.dumps(encrypted_config)

        config_value = ConfigValue(
            value=
            'ENC[AES256_GCM,data:UGKfnzsSrciR7GXZJhOCMmFrz3Y6V3pZsd3P,iv:yuReqA+n+rRXVHMc+2US5t7yPx54sooZSXWV4KLjDIs=,tag:jueP7/ZWLfYrEuhh+4eS8g==,type:str]',
            parent_source=encrypted_config,
            path_parts=[
                'step-runner-config', 'global-environment-defaults', 'DEV',
                'kube-api-token'
            ])

        sops_decryptor = SOPS()

        sops_decryptor.decrypt(config_value)
        sops_mock.assert_called_once_with(
            '--decrypt',
            '--extract=["step-runner-config"]["global-environment-defaults"]["DEV"]["kube-api-token"]',
            '--input-type=json',
            '/dev/stdin',
            _in=encrypted_config_json,
            _out=Any(StringIO),
            _err=Any(StringIO))
    def test_import_json(self):
        sample_file_path = os.path.join(
            os.path.dirname(__file__),
            'files',
            'sample.json'
        )
        sample_dict = parse_yaml_or_json_file(sample_file_path)

        self.assertTrue(isinstance(sample_dict, dict))
        self.assertIsNotNone(sample_dict['step-runner-config'])
        self.assertEqual(sample_dict['step-runner-config']['global-defaults']['service-name'], 'fruit')
        self.assertEqual(sample_dict['step-runner-config']['package'][0]['implementer'], 'Maven')
    def test_decrypt_parent_source_dict(self):
        encrypted_config_file_path = os.path.join(
            os.path.dirname(__file__), 'files',
            'step-runner-config-secret-stuff.yml')

        encrypted_config = parse_yaml_or_json_file(encrypted_config_file_path)

        config_value = ConfigValue(
            value=
            'ENC[AES256_GCM,data:UGKfnzsSrciR7GXZJhOCMmFrz3Y6V3pZsd3P,iv:yuReqA+n+rRXVHMc+2US5t7yPx54sooZSXWV4KLjDIs=,tag:jueP7/ZWLfYrEuhh+4eS8g==,type:str]',
            parent_source=encrypted_config,
            path_parts=[
                'step-runner-config', 'global-environment-defaults', 'DEV',
                'kube-api-token'
            ])

        sops_decryptor = SOPS()

        decrypted_value = sops_decryptor.decrypt(config_value)

        self.assertEqual(decrypted_value, 'super secret kube api token')