Example #1
0
def run():
    try:
        configuration = read_configuration()
        if 'id' not in configuration:
            print('Name: ' + configuration['name'])
            print('Status: NOT PUBLISHED')

        else:
            token = read_jwt_token()
            response = requests.get(
                url=config.get('url_api') + '/api/v1/script/' +
                configuration['id'],
                headers={'Authorization': 'Bearer ' + token})
            if response.status_code != 200:
                if response.status_code == 401:
                    print(colored('Do you need login', 'red'))
                else:
                    print(colored('Error obtaining info of script.', 'red'))
                return False
            else:
                data = response.json()
                print('Id: ' + data['data']['id'])
                print('Slug: ' + data['data']['name'])
                print('Name: ' + data['data']['slug'])
                print('Status: ' + data['data']['status'])
                print('CreatedAt: ' + data['data']['created_at'])
                print('Run script: ' + config.get('url_api') +
                      '/api/v1/script/' + data['data']['name'] + '/run?params')

    except Exception as error:
        logging.error(error)
        return False

    return True
Example #2
0
def publish(public=False, overwrite=False):
    """Publish script in API"""
    tarfile = None
    try:
        configuration = read_configuration()
        if 'name' not in configuration:
            print('Name required in configuration file')
            return False
        tarfile = make_tarfile(configuration['name'])
        logging.debug('Doing request with file %s' % (tarfile))
        token = read_jwt_token()

        response = None
        if 'id' in configuration:
            if overwrite:
                sure = True
            else:
                sure = sure_overwrite()

            if not sure:
                return False

            response = requests.patch(
                url=config.get('url_api') + '/api/v1/script/' +
                configuration['id'],
                files={'file': open(tarfile, 'rb')},
                headers={'Authorization': 'Bearer ' + token})
        else:
            response = requests.post(
                url=config.get('url_api') + '/api/v1/script',
                files={'file': open(tarfile, 'rb')},
                headers={'Authorization': 'Bearer ' + token})

        if response.status_code != 200:
            logging.error(response.json())
            if response.status_code == 401:
                print(colored('Do you need to login?', 'red'))
            else:
                print(colored('Error publishing script.', 'red'))
            return False

        data = response.json()
        configuration['id'] = data['data']['id']
        write_configuration(configuration)
        if public:
            response = requests.post(
                url=config.get('url_api') + '/api/v1/script/' +
                configuration['id'] + '/publish',
                headers={'Authorization': 'Bearer ' + token})
            if response.status_code != 200:
                logging.error(response.json())
                print(colored('Error making the script public.', 'red'))
                return False
        return True
    except (OSError, IOError) as e:
        print(colored('Execute this command in a GEF project', 'red'))
        return False
    finally:
        if tarfile:
            os.remove(path=tarfile)
Example #3
0
def run():
    """Run command"""
    try:
        configuration = read_configuration()
        if 'id' not in configuration:
            print(colored('Script NOT PUBLISHED', 'red'))
            return True

        else:
            token = read_jwt_token()
            response = requests.get(
                url=config.get('url_api') + '/api/v1/script/' +
                configuration['id'],
                headers={'Authorization': 'Bearer ' + token})
            if response.status_code != 200:
                if response.status_code == 401:
                    print(colored('Do you need login', 'red'))
                else:
                    print(colored('Error obtaining info of script.', 'red'))
                return False
            script = response.json()

            show_logs(script['data'])

    except (KeyboardInterrupt, SystemExit):
        raise
    except Exception as error:
        logging.error(error)
        return False

    return True
Example #4
0
def run(script_id=None):
    """Download command"""
    if not script_id:
        logging.error('invalid script_id')
        return False
    try:
        token = read_jwt_token()
        response = requests.get(url=config.get('url_api') + '/api/v1/script/' +
                                script_id + '/download',
                                headers={'Authorization': 'Bearer ' + token},
                                stream=True)
        if response.status_code != 200:
            if response.status_code == 401:
                print(colored('Do you need login', 'red'))
            else:
                print(colored('Error obtaining info of script.', 'red'))
            return False
            # path = os.getcwd() + '/' + configuration.get('id')
        tar = tarfile.open(mode="r:gz", fileobj=response.raw)
        tar.extractall(path="./" + script_id)

    except (KeyboardInterrupt, SystemExit):
        raise
    except Exception as error:
        logging.error(error)
        return False

    return True
Example #5
0
def run_docker(tempdir, dockerid, param):
    """Run docker"""
    try:
        service_account = read_gee_service_account()
        rollbar_token = config.get('ROLLBAR_SCRIPT_TOKEN')
        subprocess.run("docker run -e ENV=dev -e EE_SERVICE_ACCOUNT_JSON={2} -e ROLLBAR_SCRIPT_TOKEN={3} {0} {1}".format(dockerid, param, service_account, rollbar_token), shell=True, check=True, cwd=tempdir)
        return True
    except subprocess.CalledProcessError as error:
        logging.error(error)
        return False
Example #6
0
def run():
    """Login command"""

    email = config.get('email')
    while email is '' or not is_valid_email(email):
        email = input("Please enter your email: ")

    password = config.get('password')
    while password is None or not is_valid_password(password):
        password = getpass(prompt='Please enter your password:'******'url_api')+'/auth', json={'email': email, 'password': password})

    if response.status_code != 200:
        print('Error login.')
        return False

    body = response.json()

    config.set('JWT', body['access_token'])

    return True
Example #7
0
def get_logs(script, last_date):
    """Get logs from server"""
    logging.debug('Obtaining logs')
    token = read_jwt_token()
    start_query = ''
    if last_date:
        start_query = '?start=' + last_date.isoformat()

    response = requests.get(url=config.get('url_api') + '/api/v1/script/' +
                            script['id'] + '/log' + start_query,
                            headers={'Authorization': 'Bearer ' + token})
    if response.status_code != 200:
        if response.status_code == 401:
            print(colored('Do you need login', 'red'))
        else:
            print(colored('Error obtaining logs of script.', 'red'))
        return False, None
    return True, response.json()['data']
Example #8
0
def read_gee_service_account():
    """Obtain jwt token of config user"""
    return config.get('EE_SERVICE_ACCOUNT_JSON')
Example #9
0
def read_jwt_token():
    """Obtain jwt token of config user"""
    return config.get('JWT')
Example #10
0
def read_gee_token():
    """Obtain jwt token of config user"""
    return config.get('EE_PRIVATE_KEY')