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

fix(api): Resource::list return empty list if the regex is not valid #8437

Merged
merged 19 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
declare(strict_types=1);

namespace Centreon\Domain\Monitoring\Exception;

class ResourceRegExpException extends \PDOException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ public function findResources(ResourceFilter $filter): array;
* @return ResourceRepositoryInterface
*/
public function setContact(ContactInterface $contact): self;

/**
* Sets the access groups that will be used to filter services and the host.
*
* @param \Centreon\Domain\Security\AccessGroup[]|null $accessGroups
* @return ResourceRepositoryInterface
*/
public function filterByAccessGroups(?array $accessGroups): ResourceRepositoryInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,15 @@ interface ResourceServiceInterface
* @throws \Exception
*/
public function findResources(ResourceFilter $filter): array;

/**
* Used to filter requests according to a contact.
* If the filter is defined, all requests will use the ACL of the contact
* to fetch data.
*
* @param mixed $contact Contact to use as a ACL filter
* @return ResourceServiceInterface
* @throws \Exception
*/
public function filterByContact($contact);
}
8 changes: 7 additions & 1 deletion src/Centreon/Domain/Monitoring/ResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Centreon\Domain\Security\Interfaces\AccessGroupRepositoryInterface;
use Centreon\Domain\Service\AbstractCentreonService;
use Centreon\Domain\Monitoring\ResourceFilter;
use Centreon\Domain\Monitoring\Exception\ResourceRegExpException;
vhr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Service manage the resources in real-time monitoring : hosts and services.
Expand Down Expand Up @@ -87,7 +88,12 @@ public function filterByContact($contact): self
*/
public function findResources(ResourceFilter $filter): array
{
$list = $this->resourceRepository->findResources($filter);
// try to avoid exception from the regexp bad syntax in search criteria
try {
$list = $this->resourceRepository->findResources($filter);
} catch (ResourceRegExpException $exception) {
$list = [];
}

// set paths to endpoints
foreach ($list as $resource) {
callapa marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
18 changes: 13 additions & 5 deletions src/Centreon/Infrastructure/Monitoring/ResourceRepositoryRDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
use Centreon\Infrastructure\DatabaseConnection;
use Centreon\Infrastructure\RequestParameters\SqlRequestParametersTranslator;
use Centreon\Infrastructure\CentreonLegacyDB\StatementCollector;
use CentreonDuration;
use Centreon\Domain\Monitoring\Exception\ResourceRegExpException;
use PDO;

/**
Expand All @@ -48,7 +48,7 @@
final class ResourceRepositoryRDB extends AbstractRepositoryDRB implements ResourceRepositoryInterface
{
/**
* @var AccessGroup[] List of access group used to filter the requests
* @var AccessGroup[]|null List of access group used to filter the requests
Copy link
Contributor

Choose a reason for hiding this comment

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

List can be emty, not null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it

*/
private $accessGroups = [];

Expand Down Expand Up @@ -131,9 +131,9 @@ public function setSqlRequestTranslator(SqlRequestParametersTranslator $sqlReque
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function filterByAccessGroups(?array $accessGroups): self
public function filterByAccessGroups(?array $accessGroups): ResourceRepositoryInterface
{
$this->accessGroups = $accessGroups;

Expand Down Expand Up @@ -229,7 +229,15 @@ public function findResources(ResourceFilter $filter): array
$statement = $this->db->prepare($request);
$collector->bind($statement);

$statement->execute();
try {
$statement->execute();
} catch (\PDOException $exception) {
if ($exception->getCode() !== "42000") {
throw $exception;
}

throw new ResourceRegExpException($exception->getMessage(), (int)$exception->getCode(), $exception);
}

$this->sqlRequestTranslator->getRequestParameters()->setTotal(
(int)$this->db->query('SELECT FOUND_ROWS()')->fetchColumn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ private function createQueryOnKeyValue(string $key, $valueOrArray): string
|| $searchOperator === RequestParameters::OPERATOR_NOT_LIKE
|| $searchOperator === RequestParameters::OPERATOR_REGEXP
) {
// We check the regex
if ($searchOperator === RequestParameters::OPERATOR_REGEXP) {
try {
preg_match($mixedValue, '');
vhr marked this conversation as resolved.
Show resolved Hide resolved
} catch (\Throwable $ex) {
throw new RequestParametersTranslatorException('Bad regex format \'' . $mixedValue . '\'', 0, $ex);
callapa marked this conversation as resolved.
Show resolved Hide resolved
}
}
$type = \PDO::PARAM_STR;
$bindKey = ':value_' . (count($this->searchValues) + 1);
$this->searchValues[$bindKey] = [$type => $mixedValue];
Expand Down