Skip to content

Commit

Permalink
Merge pull request #6 from julienloizelet/feat/update-2023-01-06
Browse files Browse the repository at this point in the history
Feat/update 2023 01 06
  • Loading branch information
julienloizelet committed Jan 12, 2023
2 parents bc1de4f + 77e427c commit c694432
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 28 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.4.0](https://github.com/crowdsecurity/php-lapi-client/releases/tag/v0.4.0) - 2023-01-12
[_Compare with previous release_](https://github.com/crowdsecurity/php-lapi-client/compare/v0.3.0...v0.4.0)

### Changed

- Unexpected configuration keys are automatically removed by a new `cleanConfigs` method
- Update some logs

---


## [0.3.0](https://github.com/crowdsecurity/php-lapi-client/releases/tag/v0.3.0) - 2023-01-05
[_Compare with previous release_](https://github.com/crowdsecurity/php-lapi-client/compare/v0.2.0...v0.3.0)

Expand Down
8 changes: 6 additions & 2 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function __construct(
$logger->pushHandler(new NullHandler());
}
$this->logger = $logger;
$this->logger->debug('Instantiate client', [
'type' => 'CLIENT_INIT',
'configs' => array_merge($configs, ['api_key' => '***'])
]);
}

/**
Expand Down Expand Up @@ -97,7 +101,7 @@ protected function request(
$method = strtoupper($method);
if (!in_array($method, $this->allowedMethods)) {
$message = "Method ($method) is not allowed.";
$this->logger->error($message, ['type' => 'LAPI_CLIENT_REQUEST']);
$this->logger->error($message, ['type' => 'CLIENT_REQUEST']);
throw new ClientException($message);
}

Expand Down Expand Up @@ -133,7 +137,7 @@ private function formatResponseBody(Response $response): array

if (null === $decoded) {
$message = 'Body response is not a valid json';
$this->logger->error($message, ['type' => 'LAPI_CLIENT_FORMAT_RESPONSE']);
$this->logger->error($message, ['type' => 'CLIENT_FORMAT_RESPONSE']);
throw new ClientException($message);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bouncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function configure(array $configs): void
{
$configuration = new Configuration();
$processor = new Processor();
$this->configs = $processor->processConfiguration($configuration, [$configs]);
$this->configs = $processor->processConfiguration($configuration, [$configuration->cleanConfigs($configs)]);
}

/**
Expand Down
73 changes: 49 additions & 24 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* The Watcher configuration.
* The LAPI client configuration.
*
* @author CrowdSec team
*
Expand All @@ -21,6 +21,30 @@
*/
class Configuration implements ConfigurationInterface
{
/** @var array<string> The list of each configuration tree key */
protected $keys = [
'user_agent_suffix',
'user_agent_version',
'api_url',
'auth_type',
'api_key',
'tls_cert_path',
'tls_key_path',
'tls_ca_cert_path',
'tls_verify_peer',
'api_timeout'
];

/**
* Keep only necessary configs
* @param array $configs
* @return array
*/
public function cleanConfigs(array $configs): array
{
return array_intersect_key($configs, array_flip($this->keys));
}

/**
* @throws \InvalidArgumentException
* @throws \RuntimeException
Expand Down Expand Up @@ -54,8 +78,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
;
$this->validate($rootNode);
$this->addConnectionNodes($rootNode);
$this->validate($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -109,35 +133,36 @@ private function addConnectionNodes($rootNode)
*/
private function validate($rootNode)
{
$rootNode->validate()
->ifTrue(function (array $v) {
if (Constants::AUTH_KEY === $v['auth_type'] && empty($v['api_key'])) {
return true;
}
$rootNode
->validate()
->ifTrue(function (array $v) {
if (Constants::AUTH_KEY === $v['auth_type'] && empty($v['api_key'])) {
return true;
}

return false;
})
->thenInvalid('Api key is required as auth type is api_key')
return false;
})
->thenInvalid('Api key is required as auth type is api_key')
->end()
->validate()
->ifTrue(function (array $v) {
if (Constants::AUTH_TLS === $v['auth_type']) {
return empty($v['tls_cert_path']) || empty($v['tls_key_path']);
}
->ifTrue(function (array $v) {
if (Constants::AUTH_TLS === $v['auth_type']) {
return empty($v['tls_cert_path']) || empty($v['tls_key_path']);
}

return false;
})
->thenInvalid('Bouncer certificate and key paths are required for tls authentification.')
return false;
})
->thenInvalid('Bouncer certificate and key paths are required for tls authentification.')
->end()
->validate()
->ifTrue(function (array $v) {
if (Constants::AUTH_TLS === $v['auth_type'] && true === $v['tls_verify_peer']) {
return empty($v['tls_ca_cert_path']);
}
->ifTrue(function (array $v) {
if (Constants::AUTH_TLS === $v['auth_type'] && true === $v['tls_verify_peer']) {
return empty($v['tls_ca_cert_path']);
}

return false;
})
->thenInvalid('CA path is required for tls authentification with verify_peer.')
return false;
})
->thenInvalid('CA path is required for tls authentification with verify_peer.')
->end();
}
}
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class Constants
/**
* @var string The current version of this library
*/
public const VERSION = 'v0.3.0';
public const VERSION = 'v0.4.0';
}
1 change: 1 addition & 0 deletions tests/Unit/AbstractClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @uses \CrowdSec\LapiClient\Configuration::addConnectionNodes
* @uses \CrowdSec\LapiClient\Configuration::validate
* @uses \CrowdSec\LapiClient\RequestHandler\AbstractRequestHandler::__construct
* @uses \CrowdSec\LapiClient\Configuration::cleanConfigs
*
* @covers \CrowdSec\LapiClient\AbstractClient::__construct
* @covers \CrowdSec\LapiClient\AbstractClient::getConfig
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/BouncerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
* @uses \CrowdSec\LapiClient\RequestHandler\Curl::handle
* @uses \CrowdSec\LapiClient\RequestHandler\AbstractRequestHandler::__construct
*
*
*
* @covers \CrowdSec\LapiClient\Configuration::cleanConfigs
* @covers \CrowdSec\LapiClient\Bouncer::__construct
* @covers \CrowdSec\LapiClient\Bouncer::configure
* @covers \CrowdSec\LapiClient\Bouncer::manageRequest
Expand Down Expand Up @@ -274,5 +277,20 @@ public function testConfigure()
$error,
'CA cert path should be required if verify peer is true'
);
// Unexpected conf
$client = new Bouncer(['api_key' => '1111', 'user_agent_version' => 'v4.56.7', 'unexpected' => true]);

$this->assertEquals(
'v4.56.7',
$client->getConfig('user_agent_version'),
'user_agent_version should be configurable'
);

$this->assertEquals(
null,
$client->getConfig('unexpected'),
'Unexpected config should have been removed with no exception'
);

}
}
1 change: 1 addition & 0 deletions tests/Unit/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @uses \CrowdSec\LapiClient\Bouncer::formatUserAgent
* @uses \CrowdSec\LapiClient\Configuration::addConnectionNodes
* @uses \CrowdSec\LapiClient\Configuration::validate
* @uses \CrowdSec\LapiClient\Configuration::cleanConfigs
*
* @covers \CrowdSec\LapiClient\RequestHandler\Curl::createOptions
* @covers \CrowdSec\LapiClient\RequestHandler\Curl::handle
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/FileGetContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @uses \CrowdSec\LapiClient\Bouncer::manageRequest
* @uses \CrowdSec\LapiClient\Configuration::addConnectionNodes
* @uses \CrowdSec\LapiClient\Configuration::validate
* @uses \CrowdSec\LapiClient\Configuration::cleanConfigs
*
* @covers \CrowdSec\LapiClient\RequestHandler\FileGetContents::handle
* @covers \CrowdSec\LapiClient\RequestHandler\FileGetContents::createContextConfig
Expand Down

0 comments on commit c694432

Please sign in to comment.