Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
feat(remote-server): allow usage of domain names (#7250)
Browse files Browse the repository at this point in the history
* feat(remote-server): allow usage of domain names
Allows the user to define remote server by address not only by IP
Resolves MON-3257

* add validator change
  • Loading branch information
victorvassilev authored Mar 22, 2019
1 parent 1de4a4a commit 35f00ea
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 40 deletions.
7 changes: 6 additions & 1 deletion src/Centreon/Domain/Repository/InformationsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ public function authorizeMaster(string $ip): void
$sql = "DELETE FROM `informations` WHERE `key` = 'authorizedMaster'";
$stmt = $this->db->prepare($sql);
$stmt->execute();

/*
* resolve the address down to IP
*/
$ipAddress = gethostbyname($ip);
$sql = "INSERT INTO `informations` (`key`, `value`) VALUES ('authorizedMaster', :ip)";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':ip', $ip, PDO::PARAM_STR);
$stmt->bindParam(':ip', $ipAddress, PDO::PARAM_STR);
$stmt->execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,12 @@ public static function getName() : string
public function enableRemote(string $string_ip)
{
$ipList = explode(',', $string_ip);
foreach ($ipList as $ip) {
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
printf("Incorrect IP parameter: '%s', please pass `-v IP` of the master server\n", $ip);
return null;
}
}

echo "Starting Centreon Remote enable process: \n";

echo "Limiting Menu Access...";
$result = $this->getDi()['centreon.db-manager']->getRepository(TopologyRepository::class)->disableMenus();
echo ($result) ? 'Success' : 'Fail' . "\n";
echo ($result) ? 'Success' . "\n" : 'Fail' . "\n";

echo "Limiting Actions...";
$this->getDi()['centreon.db-manager']->getRepository(InformationsRepository::class)->toggleRemote('yes');
Expand All @@ -67,7 +61,6 @@ public function enableRemote(string $string_ip)
echo "Done\n";

echo "Notifying Master...";
$result = $this->getDi()['centreon.notifymaster']->pingMaster($ip);
$result = "";
foreach ($ipList as $ip) {
$result = $this->getDi()['centreon.notifymaster']->pingMaster($ip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,11 @@ private function validateServerGeneralFields(): void
);
}

if (!filter_var($_POST['server_ip'], FILTER_VALIDATE_IP)) {
throw new \RestBadRequestException(
sprintf(_('%s is not valid.'), 'server_ip')
);
}

if (!isset($_POST['centreon_central_ip']) || !$_POST['centreon_central_ip']) {
throw new \RestBadRequestException(
sprintf(_($missingParameterMessage), 'centreon_central_ip')
);
}

if (!filter_var($_POST['centreon_central_ip'], FILTER_VALIDATE_IP)) {
throw new \RestBadRequestException(
sprintf(_('%s is not valid.'), 'centreon_central_ip')
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ public function postAddToWaitList(): string
$ip = $_SERVER['REMOTE_ADDR'] ?? null;

if (!$ip) {
throw new \RestBadRequestException('Can not access your IP address.');
}

if (!filter_var($ip, FILTER_VALIDATE_IP)) {
throw new \RestBadRequestException('IP is not valid.');
throw new \RestBadRequestException('Can not access your address.');
}

if (!isset($_POST['app_key']) || !$_POST['app_key']) {
Expand All @@ -85,7 +81,7 @@ public function postAddToWaitList(): string
$result = $statement->fetch();

if ((bool) $result['count']) {
throw new \RestConflictException('IP already in wait list.');
throw new \RestConflictException('Address already in wait list.');
}

$createdAt = date('Y-m-d H:i:s');
Expand Down
24 changes: 13 additions & 11 deletions src/CentreonRemote/Domain/Service/NotifyMasterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class NotifyMasterService
const CANT_CONNECT = 'Could not connect';
const TIMEOUT = 'Timeout';
const UNKNOWN_ERROR = 'Unknown Error';
const WRONG_IP = 'Wrong IP';
const NO_APP_KEY = 'No Application Key found';

/**
Expand All @@ -36,13 +35,21 @@ class NotifyMasterService
private $curl;

/**
* @return Curl
* @return void
*/
public function setCurl(Curl $curl): void
{
$this->curl = $curl;
}

/**
* @return Curl
*/
public function getCurl(): Curl
{
return $this->curl;
}

/**
* NotifyMasterService constructor.
*
Expand All @@ -61,12 +68,6 @@ public function __construct(CentreonDBManagerService $dbManager)
*/
public function pingMaster($ip)
{
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
return [
'status' => self::FAIL,
'details' => self::WRONG_IP
];
}

$url = "{$ip}/centreon/api/external.php?object=centreon_remote_server&action=addToWaitList";
$repository = $this->dbManager->getRepository(InformationsRepository::class);
Expand All @@ -85,10 +86,11 @@ public function pingMaster($ip)
'app_key' => $applicationKey->getValue(),
'version' => $version->getValue(),
];
$this->curl->post($url, $curlData);

if ($curl->error) {
switch ($curl->error_code) {
$this->getCurl()->post($url, $curlData);

if ($this->getCurl()->error) {
switch ($this->getCurl()->error_code) {
case 6:
$details = self::CANT_RESOLVE_HOST;
break;
Expand Down
2 changes: 0 additions & 2 deletions www/front_src/src/helpers/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ export const serverNameValidator = serverName =>
export const serverIpAddressValidator = serverIpAddress => {
let message = "";
message = !serverIpAddress || serverIpAddress.length < 1 ? "The field is required" : "";
message = (message === "" ? validateIPaddress(serverIpAddress) : message) ;
return message;
}

export const centralIpAddressValidator = centralIpAddress => {
let message = "";
message = !centralIpAddress || centralIpAddress.length < 1 ? "The field is required" : "";
message = (message === "" ? validateIPaddress(centralIpAddress) : message) ;
return message;
}

Expand Down

0 comments on commit 35f00ea

Please sign in to comment.