From b9ba692a42fa3e0002a965d52b96f29e991613d7 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 2 Mar 2017 18:46:03 +0000 Subject: [PATCH] Added support for disabling ssl verification on gitlab backend --- orchestra/contrib/orchestration/helpers.py | 2 +- orchestra/contrib/saas/backends/gitlab.py | 27 +++++++++++----------- orchestra/contrib/saas/settings.py | 3 +++ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/orchestra/contrib/orchestration/helpers.py b/orchestra/contrib/orchestration/helpers.py index a3aa6d64..7f0deae8 100644 --- a/orchestra/contrib/orchestration/helpers.py +++ b/orchestra/contrib/orchestration/helpers.py @@ -53,7 +53,7 @@ def get_instance_url(operation): try: url = change_url(operation.instance) except NoReverseMatch: - alt_repr = '%s-%i' % (operation.content_type, operation.object_id) + alt_repr = '%s-%s' % (operation.content_type, operation.object_id) return _("Deleted {0}").format(operation.instance_repr or alt_repr) return orchestra_settings.ORCHESTRA_SITE_URL + url diff --git a/orchestra/contrib/saas/backends/gitlab.py b/orchestra/contrib/saas/backends/gitlab.py index 8efbcffa..e015624b 100644 --- a/orchestra/contrib/saas/backends/gitlab.py +++ b/orchestra/contrib/saas/backends/gitlab.py @@ -15,9 +15,10 @@ class GitLabSaaSController(ServiceController): serialize = True actions = ('save', 'delete', 'validate_creation') doc_settings = (settings, - ('SAAS_GITLAB_DOMAIN', 'SAAS_GITLAB_ROOT_PASSWORD'), + ('SAAS_GITLAB_DOMAIN', 'SAAS_GITLAB_ROOT_PASSWORD', 'SAAS_GITLAB_VERIFY_SSL'), ) - + verify = settings.SAAS_GITLAB_VERIFY_SSL + def get_base_url(self): return 'https://%s/api/v3' % settings.SAAS_GITLAB_DOMAIN @@ -28,7 +29,7 @@ class GitLabSaaSController(ServiceController): def validate_response(self, response, *status_codes): if response.status_code not in status_codes: raise RuntimeError("[%i] %s" % (response.status_code, response.content)) - return json.loads(response.content.decode('utf8')) + return response.json() def authenticate(self): login_url = self.get_base_url() + '/session' @@ -36,7 +37,7 @@ class GitLabSaaSController(ServiceController): 'login': 'root', 'password': settings.SAAS_GITLAB_ROOT_PASSWORD, } - response = requests.post(login_url, data=data) + response = requests.post(login_url, data=data, verify=self.verify) session = self.validate_response(response, 201) token = session['private_token'] self.headers = { @@ -52,7 +53,7 @@ class GitLabSaaSController(ServiceController): 'username': saas.name, 'name': saas.account.get_full_name(), } - response = requests.post(user_url, data=data, headers=self.headers) + response = requests.post(user_url, data=data, headers=self.headers, verify=self.verify) user = self.validate_response(response, 201) saas.data['user_id'] = user['id'] # Using queryset update to avoid triggering backends with the post_save signal @@ -62,11 +63,11 @@ class GitLabSaaSController(ServiceController): def change_password(self, saas, server): self.authenticate() user_url = self.get_user_url(saas) - response = requests.get(user_url, headers=self.headers) + response = requests.get(user_url, headers=self.headers, verify=self.verify) user = self.validate_response(response, 200) - user = json.loads(response.content.decode('utf8')) + user = response.json() user['password'] = saas.password - response = requests.put(user_url, data=user, headers=self.headers) + response = requests.put(user_url, data=user, headers=self.headers, verify=self.verify) user = self.validate_response(response, 200) print(json.dumps(user, indent=4)) @@ -75,17 +76,17 @@ class GitLabSaaSController(ServiceController): return self.authenticate() user_url = self.get_user_url(saas) - response = requests.get(user_url, headers=self.headers) + response = requests.get(user_url, headers=self.headers, verify=self.verify) user = self.validate_response(response, 200) user['state'] = 'active' if saas.active else 'blocked', - response = requests.patch(user_url, data=user, headers=self.headers) + response = requests.patch(user_url, data=user, headers=self.headers, verify=self.verify) user = self.validate_response(response, 200) print(json.dumps(user, indent=4)) def delete_user(self, saas, server): self.authenticate() user_url = self.get_user_url(saas) - response = requests.delete(user_url, headers=self.headers) + response = requests.delete(user_url, headers=self.headers, verify=self.verify) user = self.validate_response(response, 200, 404) print(json.dumps(user, indent=4)) @@ -95,8 +96,8 @@ class GitLabSaaSController(ServiceController): username = saas.name email = saas.data['email'] users_url = self.get_base_url() + '/users/' - response = requests.get(users_url, headers=self.headers) - users = json.loads(response.content.decode('utf8')) + response = requests.get(users_url, headers=self.headers, verify=self.verify) + users = response.json() for user in users: if user['username'] == username: print('ValidationError: user-exists') diff --git a/orchestra/contrib/saas/settings.py b/orchestra/contrib/saas/settings.py index 9535ace9..a15a6bd9 100644 --- a/orchestra/contrib/saas/settings.py +++ b/orchestra/contrib/saas/settings.py @@ -262,6 +262,9 @@ SAAS_GITLAB_DOMAIN = Setting('SAAS_GITLAB_DOMAIN', help_text="Uses ORCHESTRA_BASE_DOMAIN by default.", ) +SAAS_GITLAB_VERIFY_SSL = Setting('SAAS_GITLAB_VERIFY_SSL', + True, +) # Moodle