def test_key_validation(self):
     priv = self.key.get_private_key()
     self.assertTrue(RSAKey.validate_private_key(priv))
     self.assertTrue(RSAKey.validate_private_key_file('TEST_PRIVATE_KEY'))
     self.assertTrue(RSAKey.validate_public_key(self.key.get_public_key()))
Beispiel #2
0
    def parse(self):
        """Parse and validate arguments from the command line and set
        global configurations.
        """
        self.args = self.parser.parse_args()
        cfg = GlobalConfigStore()
        cfg.prog = self.parser.prog
        cfg.debug = self.args.debug

        # Check of configuration file is available to us
        conf_avail = False
        if self.args.config:
            try:
                cfg.load(self.args.config)
                conf_avail = True
            except BastioConfigError as ex:
                self.parser.error(ex.message)

        # Check and validate agent's key if we are about to upload the key to
        # Bastio's servers or we are about to start the agent
        if self.args.command in ('upload-key', 'start'):
            # Get agent key file path from configuration file (if available)
            # or from the command line argument
            try:
                if conf_avail:
                    cfg.apikey = cfg.apikey if cfg.get_apikey else \
                            self.args.api_key
                    cfg.agentkey = cfg.agentkey if cfg.get_agentkey else \
                            self.args.agent_key
                else:
                    cfg.apikey = self.args.api_key
                    cfg.agentkey = self.args.agent_key
            except BastioConfigError as ex:
                _die(ex.message)
            # Check agent's key file readability and validate it
            res = _check_file_readability(cfg.agentkey)
            if not res[0]:
                self.parser.error('agent key file `{}` does not exist'.format(
                    cfg.agentkey))
            if not res[1]:
                self.parser.error(('permission to read the agent key file `{}` '
                    'is denied').format(cfg.agentkey))
            res = RSAKey.validate_private_key_file(cfg.agentkey)
            if not res:
                self.parser.error('agent key file `{}` is invalid'.format(
                    cfg.agentkey))

        # Parse and validate commands and their arguments
        if self.args.command == 'generate-key':
            try:
                if conf_avail:
                    cfg.agentkey = cfg.agentkey if cfg.get_agentkey else \
                            self.args.agent_key
                else:
                    cfg.agentkey = self.args.agent_key
                cfg.bits = self.args.bits
            except BastioConfigError as ex:
                _die(ex.message)
        elif self.args.command == 'upload-key':
            try:
                # Check new key file's readability and validate it if provided
                new_key = self.args.new_agent_key
                if new_key:
                    res = _check_file_readability(new_key)
                    if not res[0]:
                        self.parser.error(
                                'new agent key file `{}` does not exist'.format(
                                    new_key))
                    if not res[1]:
                        self.parser.error((
                                'permission to read the new agent key file `{}` '
                                'is denied').format(new_key))
                    res = RSAKey.validate_private_key_file(new_key)
                    if not res:
                        self.parser.error(
                                'new agent key file `{}` is invalid'.format(
                                    new_key))
                    cfg.new_agentkey = new_key
            except BastioConfigError as ex:
                _die(ex.message)
        elif self.args.command == 'start':
            try:
                if conf_avail:
                    cfg.host = cfg.host if cfg.get_host else self.args.host
                    cfg.port = cfg.port if cfg.getint_port else self.args.port
                    cfg.stacksize = cfg.stacksize if cfg.getint_stacksize else \
                            self.args.stack_size
                    cfg.minthreads = cfg.minthreads if cfg.getint_minthreads else \
                            self.args.min_threads
                else:
                    cfg.host = self.args.host
                    cfg.port = self.args.port
                    cfg.stacksize = self.args.stack_size
                    cfg.minthreads = self.args.min_threads
            except BastioConfigError as ex:
                _die(ex.message)
        else:
            # NOTE: This execution branch is blocked by argparse
            # so it is here only to account for extremely unlikely cases
            _die("unsupported command `{}`".format(self.args.command))
        return self.args.command