Ejemplo n.º 1
0
class Test_SecretsEnvironment_no_env_vars(unittest.TestCase):
    def setUp(self):
        self.cwd = os.getcwd()
        self.host = HOST
        self.keys_subdir = KEYS_SUBDIR
        self.secrets_env = None
        with patch.dict('os.environ'):
            for v in ['D2_ENVIRONMENT', 'D2_SECRETS_BASEDIR']:
                try:
                    del os.environ[v]
                except KeyError as e:
                    pass
            self.secrets_env = SecretsEnvironment()

    def tearDown(self):
        pass

    def test_no_D2_ENVIRONMENT(self):
        """Asserting D2_ENVIRONMENT not set in environment"""
        self.assertIsNone(os.environ.get('D2_ENVIRONMENT'))

    def test_environment_path(self):
        self.assertEqual(self.secrets_env.environment_path(), secrets_dir())

    def test_environment_path_subdir(self):
        self.assertEqual(
            self.secrets_env.environment_path(subdir=self.keys_subdir),
            keys_dir())

    def test_environment_path_subdir_host(self):
        self.assertEqual(
            self.secrets_env.environment_path(subdir=KEYS_SUBDIR,
                                              host=self.host),
            keys_with_host_dir(host=self.host))
Ejemplo n.º 2
0
 def take_action(self, parsed_args):
     self.LOG.debug('creating environment(s)')
     # basedir = self.app.get_secrets_basedir()
     if len(parsed_args.args) == 0:
         parsed_args.args = list(self.app.environment)
     for e in parsed_args.args:
         se = SecretsEnvironment(environment=e)
         se.environment_create(source=parsed_args.clone_from)
         self.app.LOG.info('environment "{}" '.format(e) +
                           '({}) created'.format(se.environment_path()))
Ejemplo n.º 3
0
 def setUp(self):
     self.cwd = os.getcwd()
     self.host = HOST
     self.keys_subdir = KEYS_SUBDIR
     self.secrets_env = None
     with patch.dict('os.environ'):
         for v in ['D2_ENVIRONMENT', 'D2_SECRETS_BASEDIR']:
             try:
                 del os.environ[v]
             except KeyError as e:
                 pass
         self.secrets_env = SecretsEnvironment()
Ejemplo n.º 4
0
 def prepare_to_run_command(self, cmd):
     self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__)
     self.LOG.debug('using environment "{}"'.format(
         self.options.environment))
     self.environment = self.options.environment
     self.secrets_basedir = self.options.secrets_basedir
     self.secrets_file = self.options.secrets_file
     self.secrets = SecretsEnvironment(
         environment=self.environment,
         secrets_basedir=self.secrets_basedir,
         secrets_file=self.secrets_file,
         export_env_vars=self.options.export_env_vars,
         env_var_prefix=self.options.env_var_prefix,
     )
Ejemplo n.º 5
0
 def take_action(self, parsed_args):
     self.LOG.debug('managing localized environment default')
     cwd = os.getcwd()
     env_file = os.path.join(cwd, '.python_secrets_environment')
     if parsed_args.unset_default:
         try:
             os.remove(env_file)
         except Exception as e:  # noqa
             self.LOG.info('no default environment was set')
         else:
             self.LOG.info('default environment unset')
     elif parsed_args.environment is None:
         # No environment specified, show current setting
         if os.path.exists(env_file):
             with open(env_file, 'r') as f:
                 env_string = f.read().replace('\n', '')
             print(env_string)
         else:
             self.LOG.info('default environment is "{}"'.format(
                 SecretsEnvironment().environment()))
     else:
         # Set default to specified environment
         with open(env_file, 'w') as f:
             f.write(parsed_args.environment)
         self.LOG.info('default environment set to "{}"'.format(
             parsed_args.environment))
Ejemplo n.º 6
0
 def get_parser(self, prog_name):
     parser = super(GroupsPath, self).get_parser(prog_name)
     default_environment = SecretsEnvironment().environment()
     parser.add_argument('environment',
                         nargs='?',
                         default=default_environment)
     return parser
Ejemplo n.º 7
0
 def take_action(self, parsed_args):
     self.LOG.debug('listing environment(s)')
     default_environment = SecretsEnvironment().environment()
     columns = (['Environment', 'Default'])
     basedir = self.app.secrets.secrets_basedir()
     data = ([(e, _is_default(e, default_environment))
              for e in os.listdir(basedir)
              if os.path.isdir(os.path.join(basedir, e))])
     return columns, data
Ejemplo n.º 8
0
 def take_action(self, parsed_args):
     self.LOG.debug('renaming environment')
     basedir = self.app.secrets.secrets_basedir()
     source = parsed_args.source[0]
     dest = parsed_args.dest[0]
     if source is None:
         raise RuntimeError('No source name provided')
     if dest is None:
         raise RuntimeError('No destination name provided')
     if not SecretsEnvironment(environment=source).environment_exists():
         raise RuntimeError('Source environment "{}"'.format(source) +
                            ' does not exist')
     if SecretsEnvironment(environment=dest).environment_exists():
         raise RuntimeError('Desitnation environment "{}"'.format(dest) +
                            ' already exist')
     os.rename(os.path.join(basedir, source), os.path.join(basedir, dest))
     self.LOG.info('environment "{}" '.format(source) +
                   'renamed to "{}"'.format(dest))
Ejemplo n.º 9
0
 def setUp(self):
     self.host = HOST
     with patch.dict('os.environ'):
         for v in ['D2_ENVIRONMENT', 'D2_SECRETS_BASEDIR']:
             try:
                 del os.environ[v]
             except KeyError as e:
                 pass
         self.secrets_env = SecretsEnvironment()
Ejemplo n.º 10
0
 def setUp(self):
     self.cwd = os.getcwd()
     self.host = HOST
     self.keys_subdir = KEYS_SUBDIR
     self.envname = TESTENV
     self.basedir = os.path.join(
         HOME, SECRETS_SUBDIR if sys.platform.startswith('win') else '.' +
         SECRETS_SUBDIR)
     self.secrets_env = None
     with patch.dict('os.environ'):
         os.environ['D2_ENVIRONMENT'] = self.envname
         os.environ['D2_SECRETS_BASEDIR'] = self.basedir
         self.secrets_dir = secrets_dir(env=self.envname,
                                        basedir=self.basedir)
         self.keys_dir = keys_dir(secrets_dir=self.secrets_dir)
         self.keys_with_host_dir = keys_with_host_dir(
             keys_dir=self.keys_dir, host=self.host)
         self.secrets_env = SecretsEnvironment()
Ejemplo n.º 11
0
class Test_SecretsEnvironment_args(unittest.TestCase):
    def setUp(self):
        self.cwd = os.getcwd()
        self.host = HOST
        self.keys_subdir = KEYS_SUBDIR
        self.envname = TESTENV
        self.basedir = os.path.join(
            HOME, SECRETS_SUBDIR if sys.platform.startswith('win') else '.' +
            SECRETS_SUBDIR)
        self.secrets_env = None
        with patch.dict('os.environ'):
            for v in ['D2_ENVIRONMENT', 'D2_SECRETS_BASEDIR']:
                try:
                    del os.environ[v]
                except KeyError as e:
                    pass
            self.secrets_dir = secrets_dir(env=self.envname,
                                           basedir=self.basedir)
            self.keys_dir = keys_dir(secrets_dir=self.secrets_dir)
            self.keys_with_host_dir = keys_with_host_dir(
                keys_dir=self.keys_dir, host=self.host)
            self.secrets_env = SecretsEnvironment(environment=self.envname,
                                                  secrets_basedir=self.basedir)

    def tearDown(self):
        pass

    def test_no_D2_ENVIRONMENT(self):
        """Asserting D2_ENVIRONMENT not set in environment"""
        self.assertIsNone(os.environ.get('D2_ENVIRONMENT'))

    def test_environment_path(self):
        self.assertEqual(self.secrets_env.environment_path(), self.secrets_dir)

    def test_environment_path_subdir(self):
        self.assertEqual(
            self.secrets_env.environment_path(subdir=self.keys_subdir),
            self.keys_dir)

    def test_environment_path_subdir_host(self):
        self.assertEqual(
            self.secrets_env.environment_path(subdir=KEYS_SUBDIR,
                                              host=self.host),
            self.keys_with_host_dir)
Ejemplo n.º 12
0
 def take_action(self, parsed_args):
     self.LOG.debug('returning environment path')
     e = SecretsEnvironment(environment=self.app.options.environment)
     if parsed_args.tmpdir:
         tmpdir = e.tmpdir_path()
         tmpdir_mode = 0o700
         try:
             os.makedirs(tmpdir, tmpdir_mode)
             self.LOG.info('created tmpdir {}'.format(tmpdir))
         except FileExistsError:
             mode = os.stat(tmpdir).st_mode
             current_mode = S_IMODE(mode)
             if current_mode != tmpdir_mode:
                 os.chmod(tmpdir, tmpdir_mode)
                 self.LOG.info('changed mode on {} from {} to {}'.format(
                     tmpdir, oct(current_mode), oct(tmpdir_mode)))
         finally:
             self._print(tmpdir, parsed_args.json)
     else:
         self._print(e.environment_path(), parsed_args.json)
Ejemplo n.º 13
0
 def get_parser(self, prog_name):
     parser = super(EnvironmentsCreate, self).get_parser(prog_name)
     parser.add_argument(
         '-C',
         '--clone-from',
         action='store',
         dest='clone_from',
         default=None,
         help="Environment directory to clone from (default: None)")
     parser.add_argument('args',
                         nargs='*',
                         default=[SecretsEnvironment().environment()])
     return parser
Ejemplo n.º 14
0
 def get_parser(self, prog_name):
     parser = super().get_parser(prog_name)
     default_environment = SecretsEnvironment().environment()
     parser.add_argument('--no-files',
                         action='store_true',
                         dest='no_files',
                         default=False,
                         help='Do not include files in listing ' +
                         '(default: False)')
     parser.add_argument('environment',
                         nargs='?',
                         default=default_environment)
     return parser
Ejemplo n.º 15
0
 def build_option_parser(self, description, version):
     parser = super(PythonSecretsApp,
                    self).build_option_parser(description, version)
     # Global options
     _env = SecretsEnvironment()
     parser.add_argument('-d',
                         '--secrets-basedir',
                         metavar='<secrets-basedir>',
                         dest='secrets_basedir',
                         default=_env.secrets_basedir(),
                         help="Root directory for holding secrets " +
                         "(Env: D2_SECRETS_BASEDIR; default: {})".format(
                             _env.secrets_basedir()))
     parser.add_argument(
         '-e',
         '--environment',
         metavar='<environment>',
         dest='environment',
         default=_env.environment(),
         help="Deployment environment selector " +
         "(Env: D2_ENVIRONMENT; default: {})".format(_env.environment()))
     parser.add_argument('-s',
                         '--secrets-file',
                         metavar='<secrets-file>',
                         dest='secrets_file',
                         default=_env.secrets_basename(),
                         help="Secrets file (default: {})".format(
                             _env.secrets_basename()))
     parser.add_argument('-P',
                         '--env-var-prefix',
                         metavar='<prefix>',
                         dest='env_var_prefix',
                         default=None,
                         help="Prefix string for environment variables " +
                         "(default: None)")
     parser.add_argument('-E',
                         '--export-env-vars',
                         action='store_true',
                         dest='export_env_vars',
                         default=False,
                         help="Export secrets as environment variables " +
                         "(default: False)")
     parser.add_argument('--init',
                         action='store_true',
                         dest='init',
                         default=False,
                         help="Initialize directory for holding secrets.")
     return parser
Ejemplo n.º 16
0
class Test_SecretsEnvironment_with_env_vars(unittest.TestCase):
    def setUp(self):
        self.cwd = os.getcwd()
        self.host = HOST
        self.keys_subdir = KEYS_SUBDIR
        self.envname = TESTENV
        self.basedir = os.path.join(
            HOME, SECRETS_SUBDIR if sys.platform.startswith('win') else '.' +
            SECRETS_SUBDIR)
        self.secrets_env = None
        with patch.dict('os.environ'):
            os.environ['D2_ENVIRONMENT'] = self.envname
            os.environ['D2_SECRETS_BASEDIR'] = self.basedir
            self.secrets_dir = secrets_dir(env=self.envname,
                                           basedir=self.basedir)
            self.keys_dir = keys_dir(secrets_dir=self.secrets_dir)
            self.keys_with_host_dir = keys_with_host_dir(
                keys_dir=self.keys_dir, host=self.host)
            self.secrets_env = SecretsEnvironment()

    def tearDown(self):
        pass

    def test_environment(self):
        self.assertEqual(self.secrets_env.environment(), self.envname)

    def test_environment_path(self):
        self.assertEqual(self.secrets_env.environment_path(), self.secrets_dir)

    def test_environment_path_subdir(self):
        self.assertEqual(self.secrets_env.environment_path(subdir=KEYS_SUBDIR),
                         self.keys_dir)

    def test_environment_path_subdir_host(self):
        self.assertEqual(
            self.secrets_env.environment_path(subdir=KEYS_SUBDIR, host=HOST),
            self.keys_with_host_dir)
Ejemplo n.º 17
0
 def get_parser(self, prog_name):
     parser = super().get_parser(prog_name)
     default_environment = SecretsEnvironment().environment()
     parser.add_argument('environment',
                         nargs='?',
                         default=default_environment)
     parser.add_argument(
         '--json',
         action='store_true',
         dest='json',
         default=False,
         help="Output in JSON (e.g., for Terraform external data source; " +
         "default: False)")
     parser.add_argument(
         '--tmpdir',
         action='store_true',
         dest='tmpdir',
         default=False,
         help='Create and/or return tmpdir for this environment ' +
         '(default: False)')
     return parser
Ejemplo n.º 18
0
 def take_action(self, parsed_args):
     self.LOG.debug('returning groups path')
     e = SecretsEnvironment(environment=parsed_args.environment)
     print(e.descriptions_path())
Ejemplo n.º 19
0
 def take_action(self, parsed_args):
     self.LOG.debug('outputting environment tree')
     e = SecretsEnvironment(environment=parsed_args.environment)
     e.requires_environment()
     print_files = bool(parsed_args.no_files is False)
     tree(e.environment_path(), print_files=print_files)
Ejemplo n.º 20
0
class PythonSecretsApp(App):
    """Python secrets application class"""
    def __init__(self):
        super(PythonSecretsApp, self).__init__(
            description=__doc__.strip(),
            version=__version__,
            command_manager=CommandManager(namespace='python_secrets'),
            deferred_help=True,
        )
        self.secrets = None
        self.environment = None
        self.secrets_basedir = None
        self.secrets_file = None

    def build_option_parser(self, description, version):
        parser = super(PythonSecretsApp,
                       self).build_option_parser(description, version)
        # Global options
        _env = SecretsEnvironment()
        parser.add_argument('-d',
                            '--secrets-basedir',
                            metavar='<secrets-basedir>',
                            dest='secrets_basedir',
                            default=_env.secrets_basedir(),
                            help="Root directory for holding secrets " +
                            "(Env: D2_SECRETS_BASEDIR; default: {})".format(
                                _env.secrets_basedir()))
        parser.add_argument(
            '-e',
            '--environment',
            metavar='<environment>',
            dest='environment',
            default=_env.environment(),
            help="Deployment environment selector " +
            "(Env: D2_ENVIRONMENT; default: {})".format(_env.environment()))
        parser.add_argument('-s',
                            '--secrets-file',
                            metavar='<secrets-file>',
                            dest='secrets_file',
                            default=_env.secrets_basename(),
                            help="Secrets file (default: {})".format(
                                _env.secrets_basename()))
        parser.add_argument('-P',
                            '--env-var-prefix',
                            metavar='<prefix>',
                            dest='env_var_prefix',
                            default=None,
                            help="Prefix string for environment variables " +
                            "(default: None)")
        parser.add_argument('-E',
                            '--export-env-vars',
                            action='store_true',
                            dest='export_env_vars',
                            default=False,
                            help="Export secrets as environment variables " +
                            "(default: False)")
        parser.add_argument('--init',
                            action='store_true',
                            dest='init',
                            default=False,
                            help="Initialize directory for holding secrets.")
        return parser

    def initialize_app(self, argv):
        self.LOG.debug('initialize_app')
        if sys.version_info <= (3, 6):
            raise RuntimeError('This program uses the Python "secrets" ' +
                               'module, which requires Python 3.6 or higher')

    def prepare_to_run_command(self, cmd):
        self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__)
        self.LOG.debug('using environment "{}"'.format(
            self.options.environment))
        self.environment = self.options.environment
        self.secrets_basedir = self.options.secrets_basedir
        self.secrets_file = self.options.secrets_file
        self.secrets = SecretsEnvironment(
            environment=self.environment,
            secrets_basedir=self.secrets_basedir,
            secrets_file=self.secrets_file,
            export_env_vars=self.options.export_env_vars,
            env_var_prefix=self.options.env_var_prefix,
        )

    def clean_up(self, cmd, result, err):
        self.LOG.debug('clean_up %s', cmd.__class__.__name__)
        if err:
            self.LOG.debug('got an error: %s', err)
            if self.secrets.changed():
                self.LOG.info('not writing secrets out due to error')
        else:
            if self.secrets.changed():
                self.secrets.write_secrets()