Example #1
0
 def _add_user_options(sub_parser: ArgparseWrapper):
     user_options = sub_parser.add_argument_group(
         title=title_formatter("Options"))
     user_options.add_argument('-h',
                               '--help',
                               action='help',
                               help='Display help screen and exit')
Example #2
0
 def _add_logout_options(logout_subparser: ArgparseWrapper):
     logout_options = logout_subparser.add_argument_group(
         title=title_formatter("Options"))
     logout_options.add_argument('-h',
                                 '--help',
                                 action='help',
                                 help='Display help screen and exit')
Example #3
0
 def _add_init_options(sub_parser: ArgparseWrapper):
     init_options = sub_parser.add_argument_group(
         title=title_formatter("Options"))
     init_options.add_argument('-u',
                               '--url',
                               metavar='VALUE',
                               action='store',
                               dest='url',
                               help='Provide URL of Conjur server')
     init_options.add_argument('-a',
                               '--account',
                               metavar='VALUE',
                               action='store',
                               dest='name',
                               help='Provide Conjur account name. '
                               'Optional for Conjur Enterprise - overrides '
                               'the value on the Conjur Enterprise server')
     init_options.add_argument(
         '-c',
         '--ca-cert',
         metavar='VALUE',
         action='store',
         dest='certificate',
         help='Optional- use this option to provide Conjur server RootCA '
         'to the cli in case it is not already trusted by this machine')
     init_options.add_argument(
         '-s',
         '--self-signed',
         action='store_true',
         dest='is_self_signed',
         help=
         'Optional- state if you want to work with self-signed certificate')
     init_options.add_argument(
         '--force',
         action='store_true',
         dest='force',
         help='Optional- force overwrite of existing files')
     init_options.add_argument('-h',
                               '--help',
                               action='help',
                               help='Display help screen and exit')
 def _add_login_options(login_subparser: ArgparseWrapper):
     login_options = login_subparser.add_argument_group(
         title=title_formatter("Options"))
     login_options.add_argument(
         '-i',
         '--id',
         metavar='VALUE',
         action='store',
         dest='identifier',
         help='Provide a login name to log into Conjur server')
     login_options.add_argument('-p',
                                '--password',
                                metavar='VALUE',
                                action='store',
                                dest='password',
                                help='Provide a password or API key for '
                                'the specified login name')
     login_options.add_argument('-h',
                                '--help',
                                action='help',
                                help='Display help screen and exit')
Example #5
0
 def _add_list_options(list_subparser: ArgparseWrapper):
     list_options = list_subparser.add_argument_group(
         title=title_formatter("Options"))
     list_options.add_argument(
         '-i',
         '--inspect',
         action='store_true',
         dest='inspect',
         help='Optional- list the metadata for resources')
     list_options.add_argument(
         '-k',
         '--kind',
         action='store',
         metavar='VALUE',
         dest='kind',
         help='Optional- filter resources by specified kind '
         '(user | host | layer | group '
         '| policy | variable | webservice)')
     list_options.add_argument(
         '-l',
         '--limit',
         action='store',
         metavar='VALUE',
         dest='limit',
         help='Optional- limit list of resources to specified number')
     list_options.add_argument(
         '-o',
         '--offset',
         action='store',
         metavar='VALUE',
         dest='offset',
         help='Optional- skip specified number of resources')
     list_options.add_argument(
         '-r',
         '--role',
         action='store',
         metavar='VALUE',
         dest='role',
         help='Optional- retrieve list of resources that specified role '
         'is entitled to see (VALUE must include resource\'s full '
         'identifier. See example below)')
     list_options.add_argument(
         '-s',
         '--search',
         action='store',
         metavar='VALUE',
         dest='search',
         help='Optional- search for resources based on specified query')
     list_options.add_argument(
         '-m',
         '--members-of',
         action='store',
         metavar='VALUE',
         dest='members_of',
         help='Optional - retrieve list of direct members of a specified '
         'group/layer. Note: If more than one resource in Conjur uses'
         ' the same ID, VALUE must specify full resource identifier')
     list_options.add_argument(
         '-pr',
         '--permitted-roles',
         action='store',
         metavar='VALUE',
         dest='permitted_roles_identifier',
         help='Optional - retrieve roles that have the specified '
         'privilege on the resource. Use \'--privilege\' option to '
         'specify privilege. Note: If more than one resource in '
         'Conjur uses the same ID, specify full resource identifier')
     list_options.add_argument(
         '-p',
         '--privilege',
         action='store',
         metavar='VALUE',
         dest='privilege',
         help='Use together with \'--permitted-roles\' option - specify '
         'the privilege you are querying')
     list_options.add_argument('-h',
                               '--help',
                               action='help',
                               help='Display help screen and exit')
class ArgparserWrapperTest(unittest.TestCase):
    capture_stream = io.StringIO()

    arg_parse = ArgparseWrapper(description='Conjur CLI', add_help=False)

    subparser = arg_parse.add_subparsers(dest='resource', title="Commands")
    variable_parser = subparser.add_parser('testCommand',
                                           help='Manage testCommand')
    variable_subparsers = variable_parser.add_subparsers(dest='action')
    variable_subparsers.add_parser('subCommand')
    '''
    Returns correct error message
    '''
    def test_error_prints_help_and_exits(self):
        with self.assertRaises(SystemExit):
            with redirect_stderr(self.capture_stream):
                self.arg_parse.error("--unknown")

        self.assertRegex(self.capture_stream.getvalue(),
                         "Error unrecognized arguments:")

    '''
    Returns updated namespace with provided args
    Command: conjur testCommand
    '''

    def test_no_flag_returns_namespace(self):
        with redirect_stderr(self.capture_stream):
            output = self.arg_parse.parse_args(['testCommand'])

        self.assertEquals("Namespace(resource='testCommand', action=None)",
                          str(output))

    '''
    Returns namespace when not given any flags and does not error
    Command: conjur
    '''

    def test_no_flag_returns_prints_command_help(self):
        with redirect_stderr(self.capture_stream):
            output = self.arg_parse.parse_args([])

        self.assertIn("Namespace(resource=None)", str(output))

    '''
    Unknown flag without an action should print error and exit 1
    Command: conjur --unknown
    '''

    def test_no_action_returns_prints_error(self):
        with self.assertRaises(SystemExit):
            with redirect_stderr(self.capture_stream):
                self.arg_parse.parse_args(["--unknown"])

        self.assertRegex(self.capture_stream.getvalue(),
                         "Error unrecognized arguments: --unknown")

    '''
    Unknown flag with an action should print error and exit 1
    Command: Conjur testCommand --unknown
    '''

    def test_action_with_unknown_flag_returns_prints_error(self):
        with self.assertRaises(SystemExit):
            with redirect_stderr(self.capture_stream):
                with patch.object(self.arg_parse,
                                  '_get_resource_namespace',
                                  return_value=None):
                    self.arg_parse.parse_args(["testCommand", "--unknown"])

        self.assertRegex(self.capture_stream.getvalue(),
                         "Error unrecognized arguments: --unknown")

    '''
    Unknown subCommand should print error and exit 1
    Command: conjur testCommand unknown
    '''

    def test_action_with_unknown_subcommand_returns_prints_error(self):
        with self.assertRaises(SystemExit):
            with redirect_stderr(self.capture_stream):
                self.arg_parse.parse_args(["testCommand", "unknown"])

        self.assertRegex(self.capture_stream.getvalue(),
                         "(choose from 'subCommand')")

    '''
    Valid input should pass and return correct namespace object
    Command: conjur testCommand subCommand
    '''

    def test_correct_arg_should_pass(self):
        output = self.arg_parse.parse_args(["testCommand", "subCommand"])

        self.assertEquals(
            str(output),
            "Namespace(resource='testCommand', action='subCommand')")

    def test_no_resource_namespace(self):
        with patch.object(self.arg_parse,
                          '_get_resource_namespace',
                          return_value=None):
            self.assertEquals(
                self.arg_parse._get_resource_namespace('will return None'),
                None)

    '''
    Subcommand error will write and exit
    '''

    def test_subcommand_prints_and_exits(self):
        with self.assertRaises(SystemExit):
            with redirect_stdout(self.capture_stream):
                self.arg_parse._subcommand_error("oops!", "Usage:")
            self.assertRegex(self.capture_stream.getvalue(), "Error oops!")
 def _add_hostfactory_options(parser: ArgparseWrapper):
     options = parser.add_argument_group(title=title_formatter("Options"))
     options.add_argument('-h', '--help', action='help',
                          help='Display help screen and exit')
    def _add_whoami_options(whoami_subparser: ArgparseWrapper):
        whoami_options = whoami_subparser.add_argument_group(title=title_formatter("Options"))

        whoami_options.add_argument('-h', '--help', action='help',
                                    help='Display help screen and exit')
 def _add_policy_options(policy_subparser: ArgparseWrapper):
     policy_options = policy_subparser.add_argument_group(title=title_formatter("Options"))
     policy_options.add_argument('-h', '--help', action='help',
                                 help='Display help screen and exit')