feature: finish git webhooks

This commit is contained in:
Usman Nasir
2020-03-19 22:13:47 +05:00
parent a80f6ebb50
commit 4179524bf5
4 changed files with 46 additions and 5 deletions

View File

@@ -67,7 +67,7 @@ class secMiddleware:
final_json = json.dumps(final_dic) final_json = json.dumps(final_dic)
return HttpResponse(final_json) return HttpResponse(final_json)
if request.build_absolute_uri().find('saveSpamAssassinConfigurations') > -1 or request.build_absolute_uri().find('docker') > -1 or request.build_absolute_uri().find('cloudAPI') > -1 or request.build_absolute_uri().find('filemanager') > -1 or request.build_absolute_uri().find('verifyLogin') > -1 or request.build_absolute_uri().find('submitUserCreation') > -1: if request.build_absolute_uri().find('webhook') > -1 or request.build_absolute_uri().find('saveSpamAssassinConfigurations') > -1 or request.build_absolute_uri().find('docker') > -1 or request.build_absolute_uri().find('cloudAPI') > -1 or request.build_absolute_uri().find('filemanager') > -1 or request.build_absolute_uri().find('verifyLogin') > -1 or request.build_absolute_uri().find('submitUserCreation') > -1:
continue continue
if key == 'recordContentAAAA' or key == 'backupDestinations' or key == 'ports' or key == 'imageByPass' or key == 'passwordByPass' or key == 'cronCommand' or key == 'emailMessage' or key == 'configData' or key == 'rewriteRules' or key == 'modSecRules' or key == 'recordContentTXT' or key == 'SecAuditLogRelevantStatus' or key == 'fileContent': if key == 'recordContentAAAA' or key == 'backupDestinations' or key == 'ports' or key == 'imageByPass' or key == 'passwordByPass' or key == 'cronCommand' or key == 'emailMessage' or key == 'configData' or key == 'rewriteRules' or key == 'modSecRules' or key == 'recordContentTXT' or key == 'SecAuditLogRelevantStatus' or key == 'fileContent':
continue continue

View File

@@ -120,6 +120,7 @@ urlpatterns = [
### Manage GIT ### Manage GIT
url(r'^(?P<domain>(.*))/manageGIT$', views.manageGIT, name='manageGIT'), url(r'^(?P<domain>(.*))/manageGIT$', views.manageGIT, name='manageGIT'),
url(r'^(?P<domain>(.*))/webhook$', views.webhook, name='webhook'),
url(r'^fetchFolderDetails$', views.fetchFolderDetails, name='fetchFolderDetails'), url(r'^fetchFolderDetails$', views.fetchFolderDetails, name='fetchFolderDetails'),
url(r'^initRepo$', views.initRepo, name='initRepo'), url(r'^initRepo$', views.initRepo, name='initRepo'),
url(r'^setupRemote$', views.setupRemote, name='setupRemote'), url(r'^setupRemote$', views.setupRemote, name='setupRemote'),

View File

@@ -859,5 +859,13 @@ def fetchGitLogs(request):
userID = request.session['userID'] userID = request.session['userID']
wm = WebsiteManager() wm = WebsiteManager()
return wm.fetchGitLogs(userID, json.loads(request.body)) return wm.fetchGitLogs(userID, json.loads(request.body))
except KeyError:
return redirect(loadLoginPage)
@csrf_exempt
def webhook(request, domain):
try:
wm = WebsiteManager()
return wm.webhook(domain, json.loads(request.body))
except KeyError: except KeyError:
return redirect(loadLoginPage) return redirect(loadLoginPage)

View File

@@ -10,7 +10,7 @@ django.setup()
import json import json
from plogical.acl import ACLManager from plogical.acl import ACLManager
import plogical.CyberCPLogFileWriter as logging import plogical.CyberCPLogFileWriter as logging
from websiteFunctions.models import Websites, ChildDomains from websiteFunctions.models import Websites, ChildDomains, GitLogs
from plogical.virtualHostUtilities import virtualHostUtilities from plogical.virtualHostUtilities import virtualHostUtilities
import subprocess import subprocess
import shlex import shlex
@@ -2714,7 +2714,6 @@ StrictHostKeyChecking no
except BaseException as msg: except BaseException as msg:
return HttpResponse(str(msg)) return HttpResponse(str(msg))
def startCloning(self, userID=None, data=None): def startCloning(self, userID=None, data=None):
try: try:
@@ -3026,7 +3025,7 @@ StrictHostKeyChecking no
## ##
webHookURL = 'https://%s/websites/%s/webhook' % (ACLManager.fetchIP(), self.domain) webHookURL = 'https://%s:8090/websites/%s/webhook' % (ACLManager.fetchIP(), self.domain)
data_ret = {'status': 1, 'repo': 1, 'finalBranches': branches, 'deploymentKey': deploymentKey, data_ret = {'status': 1, 'repo': 1, 'finalBranches': branches, 'deploymentKey': deploymentKey,
'remote': remote, 'remoteResult': remoteResult, 'totalCommits': totalCommits, 'home': home, 'remote': remote, 'remoteResult': remoteResult, 'totalCommits': totalCommits, 'home': home,
@@ -3846,7 +3845,6 @@ StrictHostKeyChecking no
json_data = json.dumps(data_ret) json_data = json.dumps(data_ret)
return HttpResponse(json_data) return HttpResponse(json_data)
def getLogsInJson(self, logs): def getLogsInJson(self, logs):
json_data = "[" json_data = "["
checker = 0 checker = 0
@@ -3902,6 +3900,40 @@ StrictHostKeyChecking no
data_ret = {'status': 0, 'error_message': 'Not a text file.'} data_ret = {'status': 0, 'error_message': 'Not a text file.'}
json_data = json.dumps(data_ret) json_data = json.dumps(data_ret)
return HttpResponse(json_data) return HttpResponse(json_data)
except BaseException as msg:
data_ret = {'status': 0, 'error_message': str(msg)}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
def webhook(self,domain, data=None):
try:
self.domain = domain
self.folder = '/home/%s/public_html' % (domain)
### set default ssh key
web = Websites.objects.get(domain=self.domain)
externalApp = web.externalApp
## Check if remote exists
command = 'git -C %s pull' % (self.folder)
commandStatus = ProcessUtilities.outputExecutioner(command , externalApp)
if commandStatus.find('Already up to date') == -1:
message = '[Webhook Fired] Status: %s.' % (commandStatus)
GitLogs(owner=web, type='INFO', message=message).save()
data_ret = {'status': 1, 'commandStatus': commandStatus}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
else:
message = '[Webhook Fired] Status: %s.' % (commandStatus)
GitLogs(owner=web, type='ERROR', message=message).save()
data_ret = {'status': 0, 'error_message': 'Pull not required.', 'commandStatus': commandStatus}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
except BaseException as msg: except BaseException as msg:
data_ret = {'status': 0, 'error_message': str(msg)} data_ret = {'status': 0, 'error_message': str(msg)}
json_data = json.dumps(data_ret) json_data = json.dumps(data_ret)