Example #1
0
    def get_owasp(self, owasp_id: str) -> OWASP:
        """
        Fetch OWASP by ID

        :param owasp_id: OWASP ID
        """
        cache_data = Cache.get('owasp', owasp_id)
        if cache_data:
            return mapper_json_api(OWASP, {'data': cache_data})
        owasp = self.json_api.owasps(owasp_id).get()
        Cache.add(owasp.get('data', {}))
        return mapper_json_api(OWASP, owasp)
Example #2
0
    def get_analyses(self, file_id: int) -> List[Analysis]:
        """
        List analyses for file

        :param file_id: File ID
        """
        out = list()
        file_ = file_ = self.json_api.files(file_id).get()
        for d in file_.get('included', []):
            Cache.add(d)

        analyses = file_['data']['relationships']['analyses']['data']
        for analysis_id in analyses:
            analysis = Cache.get('analyses', analysis_id['id'])
            analysis = {'data': analysis}
            if not analysis:
                analysis = self.json_api.analyses(analysis_id['id']).get()
                Cache.add(analysis['data'])
                for d in analysis.get('included', []):
                    Cache.add(d)

            vuln_id = analysis['data']['relationships']['vulnerability'][
                'data']['id']
            analysis['data']['attributes']['vulnerability-id'] = vuln_id

            out.append(mapper_json_api(Analysis, analysis))
        return out
Example #3
0
    def get_vulnerability(self, vulnerability_id: int) -> Vulnerability:
        """
        Fetch vulnerability by vulnerability ID

        :param vulnerability_id: vulnerability ID
        """
        vulnerability = self.json_api.vulnerabilities(vulnerability_id).get()
        return mapper_json_api(Vulnerability, vulnerability)
Example #4
0
    def get_user(self, user_id: int) -> User:
        """
        Fetch user by user ID
        :param user_id: User ID
        """
        user = self.json_api.users(user_id).get()

        return mapper_json_api(User, user)
Example #5
0
    def get_file(self, file_id: int) -> File:
        """
        Fetch file by file ID

        :param file_id: File ID
        """
        file_ = self.json_api.files(file_id).get()
        return mapper_json_api(File, file_)
Example #6
0
    def get_project(self, project_id: int) -> Project:
        """
        Fetch project by project ID

        :param project_id: Project ID
        """
        project = self.json_api.projects(project_id).get()

        return mapper_json_api(Project, project)
Example #7
0
    def paginated_data(self, response, mapper_class):
        initial_data = [
            mapper_json_api(mapper_class, dict(data=value))
            for value in response['data']
        ]

        if not response.get('links'):
            return initial_data

        link = response['links']['next']

        while link is not None:
            resp = self.drf_api.direct_get(urljoin(self.host, link))
            link = resp['links']['next']
            initial_data += [
                mapper_json_api(mapper_class, dict(data=value))
                for value in resp['data']
            ]

        return initial_data
Example #8
0
 def generate_access_token(self):
     """
     Generates personal access token
     """
     access_token = requests.post(urljoin(self.host, 'api/personaltokens'),
                                  auth=(self.user_id, self.token),
                                  data={
                                      'name':
                                      'appknox-python for {} @{}'.format(
                                          self.username,
                                          str(int(time.time())))
                                  })
     return mapper_json_api(PersonalToken, access_token.json())
Example #9
0
    def get_last_file(self, project_id: int) -> File:
        """
        Fetch latest file for the project

        :param project_id: Project ID
        """
        filter_options = {
            'projectId': project_id,
            'limit': -1,
            'lastFileOnly': 'true'
        }
        last_file = self.json_api.files().get(**filter_options)
        return mapper_json_api(File, last_file)