Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tabs for PCI-DSS & GDPR compliance #527

Merged
merged 10 commits into from
Feb 6, 2019
163 changes: 119 additions & 44 deletions SplunkAppForWazuh/appserver/controllers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Find more information about this on the LICENSE file.
"""


import jsonbak
import requestsbak
import csv
Expand All @@ -22,8 +21,7 @@
from db import database
from log import log
from splunk.clilib import cli_common as cli


from requirements import pci_requirements,gdpr_requirements

class api(controllers.BaseController):

Expand All @@ -43,6 +41,23 @@ def __init__(self):
except Exception as e:
self.logger.error("Error in API module constructor: %s" % (e))

def get_credentials(self,the_id):
try:
api = self.db.get(the_id)
if api:
opt_username = api[0]["userapi"]
opt_password = api[0]["passapi"]
opt_base_url = api[0]["url"]
opt_base_port = api[0]["portapi"]
url = opt_base_url + ":" + opt_base_port
auth = requestsbak.auth.HTTPBasicAuth(opt_username, opt_password)
verify = False
return url, auth, verify
else:
raise Exception('API not found')
except Exception as e:
raise e

def getSelfAdminStanza(self):
"""Get the configuration from a stanza.
"""
Expand Down Expand Up @@ -117,9 +132,6 @@ def format_output(self, arr):
except Exception as e:
raise e

# /custom/SplunkAppForWazuh/api/node
# This will perform an HTTP request to Wazuh API
# It will return the full API response with including its error codes
@expose_page(must_login=False, methods=['POST'])
def request(self, **kwargs):
"""Make requests to the Wazuh API as a proxy backend.
Expand All @@ -144,45 +156,37 @@ def request(self, **kwargs):
method = kwargs['method']
del kwargs['method']
the_id = kwargs['id']
api = self.db.get(the_id)
if api:
opt_username = api[0]["userapi"]
opt_password = api[0]["passapi"]
opt_base_url = api[0]["url"]
opt_base_port = api[0]["portapi"]
opt_endpoint = kwargs["endpoint"]
del kwargs['id']
del kwargs['endpoint']
url = opt_base_url + ":" + opt_base_port
auth = requestsbak.auth.HTTPBasicAuth(opt_username, opt_password)
verify = False
if method == 'GET':
request = self.session.get(
url + opt_endpoint, params=kwargs, auth=auth,
verify=verify).json()
if method == 'POST':
if 'origin' in kwargs:
if kwargs['origin'] == 'xmleditor':
headers = {'Content-Type': 'application/xml'}
elif kwargs['origin'] == 'json':
headers = {'Content-Type': 'application/json'}
elif kwargs['origin'] == 'raw':
headers = {'Content-Type': 'application/octet-stream'}
kwargs = str(kwargs['content'])
request = self.session.post(url + opt_endpoint, data=kwargs, auth=auth,verify=verify, headers=headers).json()
else:
request = self.session.post(
url + opt_endpoint, data=kwargs, auth=auth,
verify=verify).json()
if method == 'PUT':
request = self.session.put(
url + opt_endpoint, data=kwargs, auth=auth,
verify=verify).json()
if method == 'DELETE':
request = self.session.delete(
url,auth,verify = self.get_credentials(the_id)
opt_endpoint = kwargs["endpoint"]
del kwargs['id']
del kwargs['endpoint']
if method == 'GET':
request = self.session.get(
url + opt_endpoint, params=kwargs, auth=auth,
verify=verify).json()
if method == 'POST':
if 'origin' in kwargs:
if kwargs['origin'] == 'xmleditor':
headers = {'Content-Type': 'application/xml'}
elif kwargs['origin'] == 'json':
headers = {'Content-Type': 'application/json'}
elif kwargs['origin'] == 'raw':
headers = {'Content-Type': 'application/octet-stream'}
kwargs = str(kwargs['content'])
request = self.session.post(url + opt_endpoint, data=kwargs, auth=auth,verify=verify, headers=headers).json()
else:
request = self.session.post(
url + opt_endpoint, data=kwargs, auth=auth,
verify=verify).json()
result = jsonbak.dumps(request)
if method == 'PUT':
request = self.session.put(
url + opt_endpoint, data=kwargs, auth=auth,
verify=verify).json()
if method == 'DELETE':
request = self.session.delete(
url + opt_endpoint, data=kwargs, auth=auth,
verify=verify).json()
result = jsonbak.dumps(request)
except Exception as e:
self.logger.error("Error making API request: %s" % (e))
return jsonbak.dumps({'error': str(e)})
Expand All @@ -197,7 +201,6 @@ def autocomplete(self, **kwargs):
return jsonbak.dumps({'error': str(e)})
return parsed_json


# POST /api/csv : Generates a CSV file with the returned data from API
@expose_page(must_login=False, methods=['POST'])
def csv(self, **kwargs):
Expand Down Expand Up @@ -289,3 +292,75 @@ def csv(self, **kwargs):
self.logger.error("Error in CSV generation!: %s" % (str(e)))
return jsonbak.dumps({"error": str(e)})
return csv_result

@expose_page(must_login=False, methods=['GET'])
def pci(self, **kwargs):
try:
if not 'requirement' in kwargs:
raise Exception('Missing requirement.')
pci_description = ''
requirement = kwargs['requirement']
if requirement == 'all':
if not 'id' in kwargs:
return jsonbak.dumps(pci_requirements.pci)
the_id = kwargs['id']
url,auth,verify = self.get_credentials(the_id)
opt_endpoint = '/rules/pci'
request = self.session.get(
url + opt_endpoint, params=kwargs, auth=auth,
verify=verify).json()
if request['error'] != 0:
return jsonbak.dumps({'error':request['error']})
data = request['data']['items']
result = {}
for item in data:
result[item] = pci_requirements.pci[item]
return jsonbak.dumps(result)
else:
if not requirement in pci_requirements.pci:
return jsonbak.dumps({'error':'Requirement not found.'})
pci_description = pci_requirements.pci[requirement]
result = {}
result['pci'] = {}
result['pci']['requirement'] = requirement
result['pci']['description'] = pci_description
return jsonbak.dumps(result)
except Exception as e:
self.logger.error("Error getting PCI-DSS requirements: %s" % (str(e)))
return jsonbak.dumps({"error": str(e)})

@expose_page(must_login=False, methods=['GET'])
def gdpr(self, **kwargs):
try:
if not 'requirement' in kwargs:
raise Exception('Missing requirement.')
pci_description = ''
requirement = kwargs['requirement']
if requirement == 'all':
if not 'id' in kwargs:
return jsonbak.dumps(gdpr_requirements.gdpr)
the_id = kwargs['id']
url,auth,verify = self.get_credentials(the_id)
opt_endpoint = '/rules/gdpr'
request = self.session.get(
url + opt_endpoint, params=kwargs, auth=auth,
verify=verify).json()
if request['error'] != 0:
return jsonbak.dumps({'error':request['error']})
data = request['data']['items']
result = {}
for item in data:
result[item] = gdpr_requirements.gdpr[item]
return jsonbak.dumps(result)
else:
if not requirement in gdpr_requirements.gdpr:
return jsonbak.dumps({'error':'Requirement not found.'})
pci_description = gdpr_requirements.gdpr[requirement]
result = {}
result['gdpr'] = {}
result['gdpr']['requirement'] = requirement
result['gdpr']['description'] = pci_description
return jsonbak.dumps(result)
except Exception as e:
self.logger.error("Error getting PCI-DSS requirements: %s" % (str(e)))
return jsonbak.dumps({"error": str(e)})
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
define(['../module'], function(module) {
define(['../module'], function (module) {
'use strict'

module.config([
'$stateProvider',
'BASE_URL',
function($stateProvider, BASE_URL) {
function ($stateProvider, BASE_URL) {
$stateProvider

// agents
Expand Down Expand Up @@ -40,14 +40,14 @@ define(['../module'], function(module) {
select: 'version'
}),
responseStatus &&
responseStatus.data &&
responseStatus.data.data &&
responseStatus.data.data.enabled === 'yes' &&
responseStatus.data.data.running === 'yes'
responseStatus.data &&
responseStatus.data.data &&
responseStatus.data.data.enabled === 'yes' &&
responseStatus.data.data.running === 'yes'
? $requestService.apiReq('/agents/stats/distinct', {
fields: 'node_name',
select: 'node_name'
})
fields: 'node_name',
select: 'node_name'
})
: Promise.resolve(false),
$requestService.apiReq('/agents/groups', {})
])
Expand Down Expand Up @@ -525,6 +525,23 @@ define(['../module'], function(module) {
$state.go('agents')
}
}
],
gdprTabs: [
'$requestService',
'$state',
async ($requestService, $state) => {
try {
const gdprTabs = []
const data = await $requestService.httpReq('GET', '/api/gdpr?requirement=all')
if (!data) return []
for (const key in data.data) {
gdprTabs.push({ title: key, content: data.data[key] })
}
return gdprTabs
} catch (err) {
$state.go('settings.api')
}
}
]
}
})
Expand Down Expand Up @@ -599,7 +616,25 @@ define(['../module'], function(module) {
$state.go('agents')
}
}
],
pciTabs: [
'$requestService',
'$state',
async ($requestService, $state) => {
try {
const pciTabs = []
const data = await $requestService.httpReq('GET', '/api/pci?requirement=all')
if (!data) return []
for (const key in data.data) {
pciTabs.push({ title: key, content: data.data[key] })
}
return pciTabs
} catch (err) {
$state.go('settings.api')
}
}
]

}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,26 @@ define(['../module'], function(module) {
onEnter: $navigationService => {
$navigationService.storeRoute('ow-pci')
},
controller: 'overviewPciCtrl'
controller: 'overviewPciCtrl',
resolve: {
pciTabs: [
'$requestService',
'$state',
async ($requestService, $state) => {
try {
const pciTabs = []
const data = await $requestService.httpReq('GET','/api/pci?requirement=all')
if (!data) return []
for (const key in data.data) {
pciTabs.push({ title: key, content: data.data[key] })
}
return pciTabs
} catch (err) {
$state.go('settings.api')
}
}
]
}
})
// Overview - GDPR
.state('ow-gdpr', {
Expand All @@ -171,8 +190,28 @@ define(['../module'], function(module) {
onEnter: $navigationService => {
$navigationService.storeRoute('ow-gdpr')
},
controller: 'overviewGdprCtrl'
controller: 'overviewGdprCtrl',
resolve: {
gdprTabs: [
'$requestService',
'$state',
async ($requestService, $state) => {
try {
const gdprTabs = []
const data = await $requestService.httpReq('GET','/api/gdpr?requirement=all')
if (!data) return []
for (const key in data.data) {
gdprTabs.push({ title: key, content: data.data[key] })
}
return gdprTabs
} catch (err) {
$state.go('settings.api')
}
}
]
}
})

// Overview - Vulnerabilities
.state('ow-vul', {
templateUrl:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
<div layout="row" layout-align="center stretch">
<md-card flex class="wz-md-card" ng-show='gdprTabs'>
<md-tabs md-selected="selectedPciIndex" class="wz-md-tab" md-border-bottom>
<md-tab ng-repeat="tab in gdprTabs" label="{{tab.rule}}">
<md-tab ng-repeat="tab in gdprTabs" label="{{tab.title}}">
<div class="md-padding">
<span class="wz-headline-title">GDPR Requirement: {{tab.rule}}</span>
<span class="wz-headline-title">GDPR Requirement: {{tab.title}}</span>
<md-divider class="wz-margin-top-10"></md-divider>
<div layout="row" class="wz-padding-top-10 wz-line-height">
<div>{{tab.description}}</div>
<div>{{tab.content}}</div>
</div>
</div>
</md-tab>
Expand Down
Loading