Esempio n. 1
0
    def test_add_scan_valid_scan(self):
        project_id = '123'
        scan_manager.add_scan(project_id, self.mock_session)

        assert self.mock_session.add.called
        kall = self.mock_session.add.call_args
        args, _ = kall
        scan = args[0]
        assert scan.project_id == project_id
Esempio n. 2
0
    def test_add_scan_valid_scan(self):
        project_id = '123'
        scan_manager.add_scan(project_id, self.mock_session)

        assert self.mock_session.add.called
        kall = self.mock_session.add.call_args
        args, _ = kall
        scan = args[0]
        assert scan.project_id == project_id
Esempio n. 3
0
    def test_add_scan_with_lang(self):
        project_id = '123'
        lang = 'nodejs'
        scan_manager.add_scan(project_id, self.mock_session, lang=lang)

        assert self.mock_session.add.called
        kall = self.mock_session.add.call_args
        args, _ = kall
        scan = args[0]
        assert scan.project_id == project_id
        assert scan.lang == lang
Esempio n. 4
0
    def test_add_scan_with_lang(self):
        project_id = '123'
        lang = 'nodejs'
        scan_manager.add_scan(project_id, self.mock_session, lang=lang)

        assert self.mock_session.add.called
        kall = self.mock_session.add.call_args
        args, _ = kall
        scan = args[0]
        assert scan.project_id == project_id
        assert scan.lang == lang
Esempio n. 5
0
def add_scan_for_project_with_repo(repo_url: str, branch: str=None):
    """
    If a project with repo_url exists in the database, adds a scan to it

    :param repo_url: (str) repo url for the project to launch the scan
    :param branch: (str, Optional) branch for the project to launch the scan
    :return:
    """
    assert type(repo_url) is str

    with db.session_scope() as session:

        project = get_project_by_repo(repo_url, session)

        allowed_scan = True
        if ALLOWED_SCANS_PER_PERIOD > 0:
            previous_scans = get_num_scans_in_last_minutes(project.id, ALLOWED_SCANS_CHECK_PERIOD, session)
            allowed_scan = previous_scans < ALLOWED_SCANS_PER_PERIOD

        if allowed_scan:
            scan = add_scan(project.id, session, branch=branch)
            session.commit()

            celery = Celery('deeptracy', broker=BROKER_URI)
            celery.send_task('prepare_scan', [scan.id])
        else:
            raise APIError('cant create more scans', status_code=503)
def post_scan():
    """Add a scan on the database

    Add a scan language on existing project

    Example:
        Body
        {
          "project_id": "00001",
          "lang": "javascript",
          "branch": "develop" //Optional
        }

    :return codes:  201 on success
                    400 on errors
    """
    with db.session_scope() as session:
        data = request.get_json()
        if not data:
            return api_error_response('invalid payload'), 400

        project_id = get_required_field(data, 'project_id')
        lang = data.get('lang', None)

        branch = data.get('branch', None)
        if branch is None or branch == '':
            branch = 'master'
        else:
            project = get_project(project_id, session)
            command = 'git ls-remote --ref {}'.format(project.repo)

        # if defined, limit the number of scans that can be created by a given period for the same project
        logger.debug(' allowed scans per period {}/{}'.format(
            ALLOWED_SCANS_PER_PERIOD, ALLOWED_SCANS_CHECK_PERIOD))
        allowed_scan = True
        if ALLOWED_SCANS_PER_PERIOD > 0:
            previous_scans = get_num_scans_in_last_minutes(
                project_id, ALLOWED_SCANS_CHECK_PERIOD, session)
            allowed_scan = previous_scans < ALLOWED_SCANS_PER_PERIOD

        if allowed_scan:
            scan = add_scan(project_id, session, lang=lang, branch=branch)
            session.commit()

            # when the scan is added to the database, a celery task is inserted for that scan to start the process
            celery = Celery('deeptracy', broker=BROKER_URI)
            celery.send_task('prepare_scan', [scan.id])

            return jsonify(scan.to_dict()), 201
        else:
            return api_error_response('cant create more scans'), 403
Esempio n. 7
0
def post_scan():
    """Add a scan on the database

    Add a scan language on existing project

    Example:
        Body
        {
          "project_id": "00001",
          "lang": "javascript",
          "branch": "develop" //Optional
        }

    :return codes:  201 on success
                    400 on errors
    """
    with db.session_scope() as session:
        data = request.get_json()
        if not data:
            return api_error_response('invalid payload'), 400

        project_id = get_required_field(data, 'project_id')
        lang = data.get('lang', None)

        branch = data.get('branch', None)
        if branch is None or branch == '':
            branch = 'master'
        else:
            project = get_project(project_id, session)
            command = 'git ls-remote --ref {}'.format(project.repo)

        # if defined, limit the number of scans that can be created by a given period for the same project
        logger.debug(' allowed scans per period {}/{}'.format(ALLOWED_SCANS_PER_PERIOD, ALLOWED_SCANS_CHECK_PERIOD))
        allowed_scan = True
        if ALLOWED_SCANS_PER_PERIOD > 0:
            previous_scans = get_num_scans_in_last_minutes(project_id, ALLOWED_SCANS_CHECK_PERIOD, session)
            allowed_scan = previous_scans < ALLOWED_SCANS_PER_PERIOD

        if allowed_scan:
            scan = add_scan(project_id, session, lang=lang, branch=branch)
            session.commit()

            # when the scan is added to the database, a celery task is inserted for that scan to start the process
            celery = Celery('deeptracy', broker=BROKER_URI)
            celery.send_task('prepare_scan', [scan.id])

            return jsonify(scan.to_dict()), 201
        else:
            return api_error_response('cant create more scans'), 403
Esempio n. 8
0
 def test_add_scan_invalid_project_id(self):
     with self.assertRaises(AssertionError):
         scan_manager.add_scan(None, self.mock_session)
Esempio n. 9
0
 def test_add_scan_invalid_project_id(self):
     with self.assertRaises(AssertionError):
         scan_manager.add_scan(None, self.mock_session)