From 9db7d600f4ffe38a51dea48598a2c830a4b83a49 Mon Sep 17 00:00:00 2001 From: "aaron.weaver2@gmail.com" Date: Mon, 12 Mar 2018 21:15:09 -0400 Subject: [PATCH] Adding language and app info --- defectdojo_api/__init__.py | 2 +- defectdojo_api/defectdojo.py | 159 ++++++++++++++++++++++++++++++++--- 2 files changed, 146 insertions(+), 15 deletions(-) diff --git a/defectdojo_api/__init__.py b/defectdojo_api/__init__.py index b3ddbc4..7b344ec 100644 --- a/defectdojo_api/__init__.py +++ b/defectdojo_api/__init__.py @@ -1 +1 @@ -__version__ = '1.1.1' +__version__ = '1.1.2' diff --git a/defectdojo_api/defectdojo.py b/defectdojo_api/defectdojo.py index 764ed89..b6424ae 100644 --- a/defectdojo_api/defectdojo.py +++ b/defectdojo_api/defectdojo.py @@ -79,6 +79,14 @@ def get_test_uri(self, test_id): """ return "/api/" + self.api_version + "/tests/" + str(test_id) + "/" + def get_language_uri(self, language_type_id): + """Returns the DefectDojo API URI for a test. + + :param test_id: Id of the test + + """ + return "/api/" + self.api_version + "/language_types/" + str(language_type_id) + "/" + def version_url(self): """Returns the DefectDojo API version. @@ -780,13 +788,13 @@ def get_credential_mapping(self, cred_mapping_id, limit=20): """ return self._request('GET', 'credential_mappings/' + str(cred_mapping_id) + '/') - ##### Container API ##### - - def list_containers(self, name=None, container_type=None, limit=20): - """Retrieves all the globally configured credentials. + ##### App Analysis API ##### + def list_app_analysis(self, id=None, product_id=None, name=None, limit=20): + """Retrieves source code languages. - :param name_contains: Search by credential name. - :param username: Search by username + :param id: Search by lanaguage id. + :param product: Search by product id + :param language_name: Search by language name :param limit: Number of records to return. """ @@ -795,20 +803,143 @@ def list_containers(self, name=None, container_type=None, limit=20): if limit: params['limit'] = limit + if id: + params['id'] = id + + if product_id: + params['product__id'] = product_id + if name: - params['name__contains'] = name + params['name__icontains'] = language_name + + return self._request('GET', 'app_analysis/', params) + + def create_app_analysis(self, product_id, user_id, name, confidence, version, icon, website): + """ + Create a application analysis to product mapping. + :param id: Language identification. + """ + + data = { + 'product': self.get_product_uri(product_id), + 'user': self.get_user_uri(user_id), + 'name': name, + 'confidence': confidence, + 'version': version, + 'icon': icon, + 'website': website + } + + return self._request('POST', 'app_analysis/', data=data) + + def delete_app_analysis(self, id): + """ + Deletes an app analysis using the given id. + :param id: Language identification. + """ + return self._request('DELETE', 'app_analysis/' + str(id) + '/') + + def delete_all_app_analysis_product(self, product_id): + """ + Delete all app analysis using the given id. + :product_id id: Product to remove + """ + app_analysis = self.list_app_analysis(product_id=product_id) + + if app_analysis.success: + for app in app_analysis.data["objects"]: + self.delete_app_analysis(self.get_id_from_url(app['resource_uri'])) + + ##### Language API ##### + + def list_languages(self, id=None, product_id=None, language_name=None, limit=20): + """Retrieves source code languages. + + :param id: Search by lanaguage id. + :param product: Search by product id + :param language_name: Search by language name + :param limit: Number of records to return. - if container_type: - params['container_type__contains'] = container_type + """ + + params = {} + if limit: + params['limit'] = limit + + if id: + params['id'] = id + + if product_id: + params['product__id'] = product_id + + if language_name: + params['language_type__language__icontains'] = language_name + + return self._request('GET', 'languages/', params) + + def create_language(self, product_id, user_id, files, code, blank, comment, language_type_id=None, language_name=None): + """ + Create a language to product mapping. + :param product_id: Product identification. + """ + #If language name specified then lookup + if language_name: + languages = self.list_language_types(language_name=language_name) + + if languages.success: + for language in languages.data["objects"]: + language_type = language['resource_uri'] + + data = { + 'product': self.get_product_uri(product_id), + 'language_type': language_type, + 'user': self.get_user_uri(user_id), + 'files': files, + 'code': code, + 'blank': blank, + 'comment': comment + } + + return self._request('POST', 'languages/', data=data) - return self._request('GET', 'container/', params) + def delete_language(self, id): + """ + Deletes a language using the given id. + :param id: Language identification. + """ + return self._request('DELETE', 'languages/' + str(id) + '/') - def get_container(self, container_id, limit=20): + def delete_all_languages_product(self, product_id): """ - Retrieves a finding using the given container id. - :param container_id: Container identification. + Delete all languages for a given product id. + :param id: Language identification. + """ + languages = self.list_languages(product_id=product_id) + + if languages.success: + for language in languages.data["objects"]: + self.delete_language(self.get_id_from_url(language['resource_uri'])) + + def list_language_types(self, id=None, language_name=None, limit=20): + """Retrieves source code languages. + + :param id: Search by lanaguage id. + :param language_name: Search by language name + :param limit: Number of records to return. + """ - return self._request('GET', 'container/' + str(container_id) + '/') + + params = {} + if limit: + params['limit'] = limit + + if id: + params['id'] = id + + if language_name: + params['language__icontains'] = language_name + + return self._request('GET', 'language_types/', params) ###### Tool API #######