Merge pull request #165 from wazuh/3.1-dev-api-fixes

Some API fixes
This commit is contained in:
Pedro Sanchez 2018-01-04 14:06:17 +01:00 committed by GitHub
commit 75fb64322a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 57 deletions

View File

@ -8,11 +8,12 @@ let app = require('ui/modules').get('app/wazuh', []).controller('settingsControl
// Initialize
const notify = new Notifier({ location: 'Settings' });
var currentApiEntryIndex;
$scope.formData = {};
$scope.formData.user = "";
$scope.formData.password = "";
$scope.formData.url = "";
let currentApiEntryIndex;
$scope.formData = {
user : '',
password: '',
url : ''
};
$scope.accept_ssl = true;
$scope.editConfiguration = true;
$scope.menuNavItem = 'settings';
@ -165,14 +166,15 @@ let app = require('ui/modules').get('app/wazuh', []).controller('settingsControl
return notify.error(invalid);
}
let tmpData = {
'user': $scope.formData.user,
'password': base64.encode($scope.formData.password),
'url': $scope.formData.url,
'port': $scope.formData.port,
'cluster_info': {},
'insecure': 'true',
'id': $scope.apiEntries.length
const tmpData = {
user: $scope.formData.user,
password: base64.encode($scope.formData.password),
url: $scope.formData.url,
port: $scope.formData.port,
cluster_info: {},
insecure: 'true',
id: (Array.isArray($scope.apiEntries)) ? $scope.apiEntries.length : 0,
extensions: { oscap: true, audit: true, pci: true }
};
testAPI.check(tmpData)
@ -180,12 +182,6 @@ let app = require('ui/modules').get('app/wazuh', []).controller('settingsControl
// API Check correct. Get Cluster info
tmpData.cluster_info = data.data;
tmpData.extensions = {
"oscap": true,
"audit": true,
"pci": true
};
// Insert new API entry
genericReq.request('PUT', '/api/wazuh-api/settings', tmpData)
.then((data) => {
@ -291,43 +287,29 @@ let app = require('ui/modules').get('app/wazuh', []).controller('settingsControl
};
// Check manager connectivity
$scope.checkManager = (item) => {
let index = $scope.apiEntries.indexOf(item);
$scope.checkManager = item => {
const index = $scope.apiEntries.indexOf(item);
let tmpData = {
'user': $scope.apiEntries[index]._source.api_user,
'password': $scope.apiEntries[index]._source.api_password,
'url': $scope.apiEntries[index]._source.url,
'port': $scope.apiEntries[index]._source.api_port,
'cluster_info': {},
'insecure': 'true',
'id': $scope.apiEntries[index]._id
const tmpData = {
user: $scope.apiEntries[index]._source.api_user,
password: $scope.apiEntries[index]._source.api_password,
url: $scope.apiEntries[index]._source.url,
port: $scope.apiEntries[index]._source.api_port,
cluster_info: {},
insecure: 'true',
id: $scope.apiEntries[index]._id
};
testAPI.check(tmpData)
testAPI
.check(tmpData)
.then(data => {
let tmpData = {};
tmpData.cluster_info = data.data;
let tmpUrl = `/api/wazuh-api/updateApiHostname/${$scope.apiEntries[index]._id}`;
genericReq
.request('PUT', tmpUrl , { "cluster_info": tmpData.cluster_info })
.then(() => {
$scope.apiEntries[index]._source.cluster_info = tmpData.cluster_info;
});
if (tmpData.cluster_info.status === 'disabled') {
appState.setCurrentAPI(JSON.stringify({name: tmpData.cluster_info.manager, id: $scope.apiEntries[index]._id }));
} else {
appState.setCurrentAPI(JSON.stringify({name: tmpData.cluster_info.cluster, id: $scope.apiEntries[index]._id }));
}
$scope.$emit('updateAPI', {});
$scope.currentDefault = JSON.parse(appState.getCurrentAPI()).id;
const tmpUrl = `/api/wazuh-api/updateApiHostname/${$scope.apiEntries[index]._id}`;
return genericReq.request('PUT', tmpUrl , { cluster_info: tmpData.cluster_info })
})
.then(() => {
$scope.apiEntries[index]._source.cluster_info = tmpData.cluster_info;
$rootScope.apiIsDown = null;
notify.info("Connection success");
})
.catch(error => printError(error));
@ -423,8 +405,15 @@ let app = require('ui/modules').get('app/wazuh', []).controller('settingsControl
case 'invalid_port':
text = 'Wrong Wazuh API port, please check it and try again';
break;
case 'socket_hang_up':
if(error.https){
text = 'Wrong Wazuh API protocol, please check try again with http instead https';
} else {
text = 'Could not connect with Wazuh API, please check url and port and try again'
}
break;
default:
text = `Unexpected error. ${error.message}`;
text = `Unexpected error. ${error.message || ''}`;
}
notify.error(text);
if(!updating) $scope.messageError = text;

View File

@ -4,8 +4,9 @@ require('ui/modules').get('app/wazuh', [])
return {
check_stored: data => {
let defered = $q.defer();
$http.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,{timeout: 4000})
.then((response) => {
$http
.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,{timeout: 4000})
.then(response => {
if (response.error) {
defered.reject(response);
} else {
@ -15,7 +16,7 @@ require('ui/modules').get('app/wazuh', [])
.catch(error => {
if(error.status && error.status === -1){
defered.reject({data: 'request_timeout_checkstored'});
} else if (error.error) {
} else {
defered.reject(error);
}
});
@ -23,8 +24,10 @@ require('ui/modules').get('app/wazuh', [])
},
check: data => {
let defered = $q.defer();
$http.post(chrome.addBasePath("/api/wazuh-api/checkAPI"), data, {timeout: 4000})
.then((response) => {
const url = chrome.addBasePath("/api/wazuh-api/checkAPI");
$http
.post(url, data, {timeout: 4000})
.then(response => {
if (response.error) {
defered.reject(response);
} else {
@ -40,7 +43,9 @@ require('ui/modules').get('app/wazuh', [])
defered.reject({data: 'request_timeout_checkapi'});
} else if (error.data && error.data.message && error.data.message === 'wrong_credentials') {
defered.reject({data: 'wrong_credentials'});
} else if (error.error) {
} else if(error.data && ((error.data.message && error.data.message === 'socket hang up') || (parseInt(error.data.error) === 5))) {
defered.reject({data:'socket_hang_up',https: (data.url && data.url.includes('https'))});
} else {
defered.reject(error);
}
});