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

feat(remote-server): allow usage of domain names #7250

Merged
merged 2 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this preparation should be done before IP is pass to the repository method

$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 @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remove a return annotation

*/
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