Exemplo n.º 1
0
    def _get_navops_token(self) -> str:
        cmd = '{} token'.format(self.navops_cli)

        try:
            p = executeCommand(cmd)
        except Exception as ex:
            logger.info(str(ex))
            raise ConfigException(str(ex))

        if p.getExitStatus() != 0:
            raise ConfigException(p.getStdErr())

        return p.getStdOut().decode()
Exemplo n.º 2
0
    def get_auth_method(self) -> str:
        """
        Gets the authentication method that should be used.

        :return str: token or password

        :raises ConfigException: if no auth method is configured

        """
        #
        # For the CFM user, always use password authentication
        #
        if self.username == self._cm.getCfmUser() and self.password:
            return self.AUTH_METHOD_PASSWORD

        #
        # For all other cases, if the navops CLI is present, or there
        # is a token, use token-based authentication
        #
        if self.navops_cli or self.token:
            return self.AUTH_METHOD_TOKEN

        #
        # Otherwise, fall back to password authentication
        #
        if self.username and self.password:
            return self.AUTH_METHOD_PASSWORD

        raise ConfigException('Authentication required. Use "tortuga login".')
Exemplo n.º 3
0
    def _load_from_file(cls, filename) -> 'TortugaScriptConfig':
        if not os.path.exists(filename):
            raise ConfigFileNotFoundException(
                'Config file not found: {}'.format(filename))

        with open(filename) as fp:
            try:
                config_data = json.load(fp)
            except json.JSONDecodeError:
                raise ConfigException(
                    'Invalid config file: {}'.format(filename))

        try:
            unmarshalled = TortugaScriptConfigSchema().load(config_data)
        except ValidationError:
            raise ConfigException('Invalid config file: {}'.format(filename))

        return TortugaScriptConfig(**unmarshalled.data)
Exemplo n.º 4
0
    def get_token(self) -> str:
        """
        Gets the current authentication token.

        :return str: the token

        :raises ConfigException: if token is unavailable

        """
        if self.navops_cli:
            try:
                return self._get_navops_token()
            except ConfigException:
                pass

        if self.token:
            return self.token

        raise ConfigException('Authentication required. Use "tortuga login".')