Ejemplo n.º 1
0
    def test_clear_buffer_on_error(self):

        s = Sultan()
        try:
            s.ls("/root").run()
        except:
            self.assertEqual(len(s.commands), 0)
Ejemplo n.º 2
0
    def test_run_basic(self, m_subprocess):

        m_subprocess.check_output.return_value = "sample_response"
        sultan = Sultan()
        response = sultan.ls("-lah /tmp").run()
        self.assertTrue(m_subprocess.check_output.called)
        self.assertEqual(response, ["sample_response"])
Ejemplo n.º 3
0
    def test_run_basic(self, m_subprocess):

        m_subprocess.Popen = mock.Mock()
        m_subprocess.Popen().communicate.return_value = ("sample_response", "")
        sultan = Sultan()
        response = sultan.ls("-lah /tmp").run()
        self.assertTrue(m_subprocess.Popen().communicate.called)
        self.assertEqual(response, ["sample_response"])
Ejemplo n.º 4
0
    def test_run_advanced(self):

        sultan = Sultan()
        try:
            sultan.mkdir("-p /tmp/mytestdir")\
                .mkdir("-p /tmp/mytestdir/foobar")\
                .touch("/tmp/mytestdir/a")\
                .touch("/tmp/mytestdir/b")\
                .run()

            response = sultan.ls("-1 /tmp/mytestdir/").run()
            self.assertEqual(response, ['a', 'b', 'foobar'])
        finally:
            if os.path.exists('/tmp/mytestdir'):
                shutil.rmtree('/tmp/mytestdir')
Ejemplo n.º 5
0
    def process(self, **kwargs):
        user = cherrypy.session['user']['name']
        #logger.info('setting up controller')
        #logger.info('try to import libraries')
        import sys
        sys.path.append(os.path.join(os.environ['SPLUNK_HOME'],'etc','apps','minishell','appserver','controllers')) #build local path and add it to the python path so we can load modules, hack!
        #logger.info('path='+str(sys.path))
        
        command = kwargs.get('command')
        PWD = kwargs.get('pwd')
        #logger.info('from js stack PWD='+PWD)
        splitCommand = shlex.split(command) if os.name == 'posix' else command.split(' ')
        #logger.info('splitCommand='+str(splitCommand))
        isRestartCommand = False
        if not command:
            error = "No command"
            return self.render_json(dict(success=False, payload=error, pwd=PWD))
        
        #logger.info('user='******' command='+str(command))
        #logger.info('import sultan')
        from sultan.api import Sultan
        s = Sultan()
        
        #merge all comand line parameters
        parameters = ""
        for param in splitCommand[1:]:
            parameters += ' ' + param
            
        #logger.info('user session info: '+json.dumps(cherrypy.session['user']))
            
        try:
            if splitCommand[0] == "help":
                    logger.info('user asked for help')
                    payload = """you can run splunk with command line parameters regardles of current working dir,
run git on the current working dir and list files. Does not support chaining!
Commands:
 ls     lists files, see http://linuxcommand.org/man_pages/ls1.html for more
 pwd    to see what the current working dir is
 cd     specify the fully qualified working dir like cd /var/log etc
 git    see https://git-scm.com/documentation for more
 tail   -f won't work
 head   yeah
 cat    again yeah.
 clear  clear the screen
 wget   see https://www.gnu.org/software/wget/manual/wget.html
 grep   see https://www.gnu.org/software/grep/manual/grep.html for more
 find   see https://www.gnu.org/software/findutils/manual/html_mono/find.html for more
 
 notice: if the command takes too long you will run into timeouts"""
            elif splitCommand[0] == "git":            
                with Sultan.load(cwd=PWD, sudo=False) as s:
                    returnvalue = s.git(parameters).run()
                    payload = '\n'.join(returnvalue)
            elif splitCommand[0] == "cat":            
                with Sultan.load(cwd=PWD, sudo=False) as s:
                    returnvalue = s.cat(parameters).run()
                    payload = '\n'.join(returnvalue)
            elif splitCommand[0] == "head":
                with Sultan.load(cwd=PWD, sudo=False) as s:
                    returnvalue = s.head(parameters).run()
                    payload = '\n'.join(returnvalue)
            elif splitCommand[0] == "wget":
                with Sultan.load(cwd=PWD, sudo=False) as s:
                    returnvalue = s.wget(parameters).run()
                    payload = '\n'.join(returnvalue)
            elif splitCommand[0] == "grep":
                with Sultan.load(cwd=PWD, sudo=False) as s:
                    returnvalue = s.grep(parameters).run()
                    payload = '\n'.join(returnvalue)
            elif splitCommand[0] == "tail":            
                if " -f " in command:
                    payload = "tail -f won't work"
                    return self.render_json(dict(success=False, payload=payload, pwd=PWD))
                with Sultan.load(cwd=PWD, sudo=False) as s:
                    returnvalue = s.tail(parameters).run()
                    payload = '\n'.join(returnvalue)
            elif splitCommand[0] == "find":            
                with Sultan.load(cwd=PWD, sudo=False) as s:
                    returnvalue = s.find(parameters).run()
                    payload = '\n'.join(returnvalue)
            elif splitCommand[0] == "ls":            
                with Sultan.load(cwd=PWD, sudo=False) as s:
                    returnvalue = s.ls(parameters).run()
                    payload = '\n'.join(returnvalue)
            elif splitCommand[0] == "splunk":            
                with Sultan.load( sudo=False) as s:
                    os.environ['SPLUNK_TOK'] = str(cherrypy.session['sessionKey'])
                    splunkCommand = command[7:]
                    logger.info('load command splunk with parameters=' + splunkCommand)
                    returnvalue = s.splunk(splunkCommand).run()
                    del os.environ['SPLUNK_TOK']
                    payload = '\n'.join(returnvalue)
                    #logger.info('user='******' in workingdir=' + PWD + ' command=' +splitCommand[0] + ' arguments=' + parameters)
            elif splitCommand[0] == "pwd":            
                try:
                    payload = PWD
                    #payload = 'pwd: ' + cherrypy.session['user']['pwd'] #pwd
                except KeyError, e:
                    cherrypy.session['user']['pwd'] = os.environ['SPLUNK_HOME']
                    logger.info('no pwd was set, default to splunk_home')
                    payload = 'pwd: ' + cherrypy.session['user']['pwd'] #pwd
                    return self.render_json(dict(success=False, payload=payload, pwd=PWD))
                logger.info('we showed the pwd')
                #logger.info('user session info: '+json.dumps(cherrypy.session['user']))
            elif splitCommand[0] == "cd":
                #logger.info('trying to set path')
                tmpPWD = os.path.abspath(os.path.join(PWD, splitCommand[1]))
                #logger.info('path we check if it exists... ' + tmpPWD)
                if os.path.isdir(tmpPWD):
                    #logger.info('path exists')
                    PWD = tmpPWD
                    payload = "set current working dir to " + PWD
                else:
                    logger.info('path does not exist, fail')
                    payload = 'does not seem to be a directory that exists...'
                    return self.render_json(dict(success=False, payload=payload, pwd=PWD))
Ejemplo n.º 6
0
    def test_execution(self):

        sultan = Sultan()
        sultan.touch("/tmp/foo").run()
        response = sultan.ls("-1 /tmp/foo").run()
        self.assertEqual(response, ["/tmp/foo"])