コード例 #1
0
ファイル: auth.py プロジェクト: yuemanxilou/st2
    def run(self, args, **kwargs):

        cli = BaseCLIApp()

        # Determine path to config file
        try:
            config_file = cli._get_config_file_path(args)
        except ValueError:
            # config file not found in args or in env, defaulting
            config_file = config_parser.ST2_CONFIG_PATH

        # Update existing configuration with new credentials
        config = ConfigParser()
        config.read(config_file)

        return config.get('credentials', 'username')
コード例 #2
0
    def test_cli_config_file_path(self):
        app = BaseCLIApp()
        args = mock.Mock()

        # 1. Absolute path
        args.config_file = '/tmp/full/abs/path/config.ini'
        result = app._get_config_file_path(args=args)
        self.assertEqual(result, args.config_file)

        args.config_file = '/home/user/st2/config.ini'
        result = app._get_config_file_path(args=args)
        self.assertEqual(result, args.config_file)

        # 2. Path relative to user home directory, should get expanded
        args.config_file = '~/.st2/config.ini'
        result = app._get_config_file_path(args=args)
        expected = os.path.join(os.path.expanduser('~' + USER), '.st2/config.ini')
        self.assertEqual(result, expected)

        # 3. Relative path (should get converted to absolute one)
        args.config_file = 'config.ini'
        result = app._get_config_file_path(args=args)
        expected = os.path.join(os.getcwd(), 'config.ini')
        self.assertEqual(result, expected)

        args.config_file = '.st2/config.ini'
        result = app._get_config_file_path(args=args)
        expected = os.path.join(os.getcwd(), '.st2/config.ini')
        self.assertEqual(result, expected)
コード例 #3
0
ファイル: auth.py プロジェクト: zwunix/st2
    def run(self, args, **kwargs):

        if not args.password:
            args.password = getpass.getpass()
        instance = self.resource(ttl=args.ttl) if args.ttl else self.resource()

        cli = BaseCLIApp()

        # Determine path to config file
        try:
            config_file = cli._get_config_file_path(args)
        except ValueError:
            # config file not found in args or in env, defaulting
            config_file = config_parser.ST2_CONFIG_PATH

        # Retrieve token
        manager = self.manager.create(instance,
                                      auth=(args.username, args.password),
                                      **kwargs)
        cli._cache_auth_token(token_obj=manager)

        # Update existing configuration with new credentials
        config = ConfigParser()
        config.read(config_file)

        # Modify config (and optionally populate with password)
        if not config.has_section('credentials'):
            config.add_section('credentials')

        config.set('credentials', 'username', args.username)
        if args.write_password:
            config.set('credentials', 'password', args.password)
        else:
            # Remove any existing password from config
            config.remove_option('credentials', 'password')

        config_existed = os.path.exists(config_file)
        with open(config_file, 'w') as cfg_file_out:
            config.write(cfg_file_out)
        # If we created the config file, correct the permissions
        if not config_existed:
            os.chmod(config_file, 0o660)

        return manager
コード例 #4
0
ファイル: auth.py プロジェクト: lyandut/st2
    def run(self, args, **kwargs):

        if not args.password:
            args.password = getpass.getpass()
        instance = self.resource(ttl=args.ttl) if args.ttl else self.resource()

        cli = BaseCLIApp()

        # Determine path to config file
        try:
            config_file = cli._get_config_file_path(args)
        except ValueError:
            # config file not found in args or in env, defaulting
            config_file = config_parser.ST2_CONFIG_PATH

        # Retrieve token
        manager = self.manager.create(instance, auth=(args.username, args.password), **kwargs)
        cli._cache_auth_token(token_obj=manager)

        # Update existing configuration with new credentials
        config = ConfigParser()
        config.read(config_file)

        # Modify config (and optionally populate with password)
        if not config.has_section('credentials'):
            config.add_section('credentials')

        config.set('credentials', 'username', args.username)
        if args.write_password:
            config.set('credentials', 'password', args.password)
        else:
            # Remove any existing password from config
            config.remove_option('credentials', 'password')

        with open(config_file, 'w') as cfg_file_out:
            config.write(cfg_file_out)

        return manager