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

enh(api): Added Rest API to display and update the default configuration of the Centreon proxy #8195

Merged
merged 14 commits into from
Dec 17, 2019
Merged
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"symfony/translation": "4.3.*",
"symfony/expression-language": "4.3.*",
"ext-json": "*",
"justinrainbow/json-schema": "^5.2"
"justinrainbow/json-schema": "^5.2",
"ext-pdo": "*"
kduret marked this conversation as resolved.
Show resolved Hide resolved
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions config/Modules/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ services:

Centreon\Domain\Acknowledgement\Interfaces\AcknowledgementServiceInterface:
class: Centreon\Domain\Acknowledgement\AcknowledgementService

# Proxy configuration
Centreon\Domain\Proxy\Interfaces\ProxyServiceInterface:
class: Centreon\Domain\Proxy\ProxyService

Centreon\Domain\Proxy\Interfaces\ProxyRepositoryInterface:
class: Centreon\Infrastructure\Proxy\ProxyRepositoryRDB
24 changes: 23 additions & 1 deletion config/packages/validator/validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,26 @@ Centreon\Domain\Downtime\Downtime:
type: integer
pollerId:
- Type:
type: integer
type: integer

# Used to validate the Proxy entity
Centreon\Domain\Proxy\Proxy:
properties:
url:
- Type:
type: string
- NotNull: ~
port:
- Type:
type: integer
- PositiveOrZero: ~
- LessThanOrEqual: 65535
- NotNull: ~
user:
- Type:
type: string
- NotNull: ~
password:
- Type:
type: string
- NotNull: ~
59 changes: 38 additions & 21 deletions doc/API/centreon-api-v2.html

Large diffs are not rendered by default.

79 changes: 78 additions & 1 deletion doc/API/centreon-api-v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ info:
+ Added acknowledgement for hosts and services
+ Added downtimes for hosts and services
+ Added the list of monitoring server configurations
+ Added the proxy configuration
# Information
All dates are in **ISO 8601** format
# Authentication
Expand Down Expand Up @@ -48,6 +49,11 @@ tags:
description: |
This API allow to:
* Show the monitoring server configurations
- name: Proxy
description: |
This API allow to:
* Show the default proxy configuration of Centreon
* Update the default proxy configuration of Centreon
- name: Downtime
description: |
This API allow to:
Expand Down Expand Up @@ -296,6 +302,56 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ErrorApiResponse'
/configuration/proxy:
get:
tags:
- Proxy
summary: Display the default configuration of the Centreon proxy
responses:
200:
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Configuration.Proxy'
403:
kduret marked this conversation as resolved.
Show resolved Hide resolved
description: Forbidden
content:
application/json:
schema:
$ref: '#/components/schemas/ForbiddenApiResponse'
500:
description: Internal Server Error
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorApiResponse'
post:
tags:
- Proxy
summary: Update the default configuration of the Centreon proxy
requestBody:
description: Proxy configuration
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Configuration.Proxy'
responses:
204:
description: Command sent
403:
description: Forbidden
content:
application/json:
schema:
$ref: '#/components/schemas/ForbiddenApiResponse'
500:
description: Internal Server Error
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorApiResponse'
/monitoring/hosts/downtimes:
get:
tags:
Expand Down Expand Up @@ -1947,7 +2003,28 @@ components:
hosts:
type: object
$ref: '#/components/schemas/Monitoring.Host_min'

Configuration.Proxy:
type: object
required:
- url
- port
- user
kduret marked this conversation as resolved.
Show resolved Hide resolved
- password
properties:
url:
type: string
description: Url of the proxy
port:
type: integer
description: Port of the proxy
minimum: 0
kduret marked this conversation as resolved.
Show resolved Hide resolved
maximum: 65535
user:
type: string
description: Login of the proxy
password:
type: string
description: Password of the proxy
securitySchemes:
Token:
description: |
Expand Down
121 changes: 121 additions & 0 deletions src/Centreon/Application/Controller/Configuration/ProxyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/*
* Copyright 2005 - 2019 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\Application\Controller\Configuration;

use Centreon\Domain\Entity\EntityValidator;
use Centreon\Domain\Proxy\Interfaces\ProxyServiceInterface;
use Centreon\Domain\Proxy\Proxy;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations as Rest;
use JMS\Serializer\Exception\ValidationFailedException;
use JMS\Serializer\SerializerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
* This class is design to manage all API REST requests concerning the proxy configuration.
*
* @package Centreon\Application\Controller\Configuration
*/
class ProxyController extends AbstractFOSRestController
{
/**
* @var ProxyServiceInterface
*/
private $proxyService;

/**
* ProxyController constructor.
*
* @param ProxyServiceInterface $proxyService
*/
public function __construct(ProxyServiceInterface $proxyService)
{
$this->proxyService = $proxyService;
}

/**
* @IsGranted("ROLE_API_CONFIGURATION", message="You are not authorized to access this resource")
* @Rest\Get(
* "/configuration/proxy",
* condition="request.attributes.get('version.is_beta') == true",
* name="configuration.proxy.getProxy")
* @return View
* @throws \Exception
*/
public function getProxy(): View
{
if (!$this->getUser()->isAdmin() && !$this->isGranted('ROLE_ADMINISTRATION_PARAMETERS_CENTREON_UI_RW')) {
return $this->view(null, Response::HTTP_FORBIDDEN);
kduret marked this conversation as resolved.
Show resolved Hide resolved
}
return $this->view($this->proxyService->getProxy());
}

/**
* @IsGranted("ROLE_API_CONFIGURATION", message="You are not authorized to access this resource")
* @Rest\Post(
* "/configuration/proxy",
* condition="request.attributes.get('version.is_beta') == true",
* name="configuration.proxy.updateProxy")
* @param Request $request
* @param EntityValidator $entityValidator
* @param SerializerInterface $serializer
* @return View
* @throws \Exception
*/
public function updateProxy(
Request $request,
EntityValidator $entityValidator,
SerializerInterface $serializer
): View {
if (!$this->getUser()->isAdmin() && !$this->isGranted('ROLE_ADMINISTRATION_PARAMETERS_CENTREON_UI_RW')) {
return $this->view(null, Response::HTTP_FORBIDDEN);
}
$data = json_decode((string) $request->getContent(), true);
if ($data === null) {
throw new HttpException(json_last_error(), 'Invalid json message received');
}
$errors = $entityValidator->validateEntity(
Proxy::class,
json_decode((string) $request->getContent(), true),
['Default'],
false // We don't allow extra fields
);
if ($errors->count() > 0) {
throw new ValidationFailedException($errors);
}
/**
* @var Proxy $proxy
*/
$proxy = $serializer->deserialize(
(string)$request->getContent(),
Proxy::class,
'json'
);

$this->proxyService->updateProxy($proxy);
return $this->view();
}
}
26 changes: 24 additions & 2 deletions src/Centreon/Domain/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class Contact implements UserInterface, ContactInterface
*/
private $roles = [];

/**
* @var string[] List of names of topology rules to which the contact can access
*/
private $topologyRulesNames = [];

/**
* @return int
*/
Expand Down Expand Up @@ -303,7 +308,7 @@ public function setEncodedPassword(?string $encodedPassword): self
*/
public function getRoles()
{
return $this->roles;
return array_merge($this->roles, $this->topologyRulesNames);
}

/**
Expand Down Expand Up @@ -347,7 +352,7 @@ public function getUsername()
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
public function eraseCredentials(): void
{
// Nothing to do. But we must to define this method
}
Expand Down Expand Up @@ -421,8 +426,25 @@ public function addRole(string $roleName): void
}
}

/**
* Removes an existing roles.
*
* @param string $roleName Role name to remove
*/
private function removeRole(string $roleName): void
{
unset($this->roles[$roleName]);
}

/**
* Added a topology rule.
*
* @param string $topologyRuleName Topology rule name
*/
public function addTopologyRule(string $topologyRuleName): void
{
if (!in_array($topologyRuleName, $this->topologyRulesNames)) {
$this->topologyRulesNames[] = $topologyRuleName;
}
}
}
41 changes: 41 additions & 0 deletions src/Centreon/Domain/Proxy/Interfaces/ProxyRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* Copyright 2005 - 2019 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\Proxy\Interfaces;

use Centreon\Domain\Proxy\Proxy;

interface ProxyRepositoryInterface
{
/**
* Updates the proxy.
*
* @param Proxy $proxy Proxy to add
*/
public function updateProxy(Proxy $proxy): void;

/**
* Retrieve the proxy.
*
* @return Proxy
*/
public function getProxy(): Proxy;
}
Loading