diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml index 414588f..9fc8bab 100644 --- a/.github/workflows/coding-standards.yml +++ b/.github/workflows/coding-standards.yml @@ -26,7 +26,7 @@ jobs: runs-on: ubuntu-latest if: ${{ !contains(github.event.head_commit.message, 'chore(') }} env: - EXTENSION_PATH: "my-own-modules/lapi-client" + EXTENSION_PATH: "my-code/lapi-client" steps: - name: Clone DDEV files diff --git a/.github/workflows/unit-and-integration-test.yml b/.github/workflows/unit-and-integration-test.yml index 61c2f6b..bcba999 100644 --- a/.github/workflows/unit-and-integration-test.yml +++ b/.github/workflows/unit-and-integration-test.yml @@ -30,7 +30,7 @@ jobs: runs-on: ubuntu-latest if: ${{ !contains(github.event.head_commit.message, 'chore(') }} env: - EXTENSION_PATH: "my-own-modules/lapi-client" + EXTENSION_PATH: "my-code/lapi-client" steps: - name: Clone DDEV files diff --git a/CHANGELOG.md b/CHANGELOG.md index b5a7929..00f46c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 The [public API](https://semver.org/spec/v2.0.0.html#spec-item-1) of this library consists of all public or protected methods, properties and constants belonging to the `src` folder. +--- + +## [3.1.0](https://github.com/crowdsecurity/php-lapi-client/releases/tag/v3.1.0) - 2023-04-27 +[_Compare with previous release_](https://github.com/crowdsecurity/php-lapi-client/compare/v3.0.0...v3.1.0) + +### Changed + + +- `api_url` configuration must not be empty + + --- ## [3.0.0](https://github.com/crowdsecurity/php-lapi-client/releases/tag/v3.0.0) - 2023-03-09 diff --git a/docs/DEVELOPER.md b/docs/DEVELOPER.md index 46faa49..0930e33 100644 --- a/docs/DEVELOPER.md +++ b/docs/DEVELOPER.md @@ -50,26 +50,19 @@ For a quick start, follow the below steps. This project is fully compatible with DDEV 1.21.4, and it is recommended to use this specific version. For the DDEV installation, please follow the [official instructions](https://ddev.readthedocs.io/en/stable/users/install/ddev-installation/). - #### Prepare DDEV PHP environment The final structure of the project will look like below. ``` -crowdsec-lapi-client-dev-project (choose the name you want for this folder) +crowdsec-lapi-dev-project (choose the name you want for this folder) │ │ -└───.ddev (do not change this folder name) -│ │ -│ │ (Cloned sources of a PHP specific ddev repo) -│ -│ -└─── cfssl (do not change this folder name) +└───.ddev │ │ -│ │ (Copy of some provided files for TLS authentification) -│ +│ │ (DDEV files) │ -└───my-own-modules (do not change this folder name) +└───my-code (do not change this folder name) │ │ └───lapi-client (do not change this folder name) @@ -80,50 +73,29 @@ crowdsec-lapi-client-dev-project (choose the name you want for this folder) - Create an empty folder that will contain all necessary sources: ```bash -mkdir crowdsec-lapi-client-dev-project +mkdir crowdsec-lapi-dev-project ``` -- Create a `my-own-modules/lapi-client` folder with sources of this repo: +- Create a DDEV php project: ```bash -cd crowdsec-lapi-client-dev-project -mkdir -p my-own-modules/lapi-client -cd my-own-modules/lapi-client && git clone git@github.com:crowdsecurity/php-lapi-client.git ./ +cd crowdsec-lapi-dev-project +ddev config --project-type=php --php-version=8.2 --project-name=crowdsec-lapi-client ``` -- Create an empty `.ddev` folder for DDEV and clone our pre-configured DDEV repo: +- Add some DDEV add-ons: ```bash -cd crowdsec-lapi-client-dev-project -mkdir .ddev && cd .ddev && git clone git@github.com:julienloizelet/ddev-php.git ./ +ddev get julienloizelet/ddev-tools +ddev get julienloizelet/ddev-crowdsec-php ``` -Copy the CrowdSec related docker-compose file: -```bash -cd crowdsec-lapi-client-dev-project -cp .ddev/additional_docker_compose/docker-compose.crowdsec.yaml .ddev/docker-compose.crowdsec.yaml -``` - -Then, as we use a TLS ready CrowdSec container, you have to copy some certificates and key in a `cfssl` folder: - -```bash -mkdir cfssl -cp -r ../.ddev/custom_files/crowdsec/cfssl/* cfssl -``` - -By default, ddev will launch a PHP 7.2 container. If you want to work with another PHP version, copy the -corresponding config file. For example: - -```bash - -cp .ddev/config_overrides/config.php74.yaml .ddev/config.php74.yaml -``` -- Launch DDEV +- Clone this repo sources in a `my-code/lapi-client` folder: ```bash -cd .ddev && ddev start +mkdir -p my-code/lapi-client +cd my-code/lapi-client && git clone git@github.com:crowdsecurity/php-lapi-client.git ./ ``` -This should take some times on the first launch as this will download all necessary docker images. ### DDEV Usage @@ -134,13 +106,13 @@ This should take some times on the first launch as this will download all necess Run: ```bash -ddev composer update --working-dir ./my-own-modules/lapi-client +ddev composer update --working-dir ./my-code/lapi-client ``` #### Unit test ```bash -ddev php ./my-own-modules/lapi-client/vendor/bin/phpunit ./my-own-modules/lapi-client/tests/Unit --testdox +ddev php ./my-code/lapi-client/vendor/bin/phpunit ./my-code/lapi-client/tests/Unit --testdox ``` #### Integration test @@ -148,7 +120,7 @@ ddev php ./my-own-modules/lapi-client/vendor/bin/phpunit ./my-own-modules/lapi- In order to launch integration tests, we have to set some environment variables: ```bash -ddev exec BOUNCER_KEY= AGENT_TLS_PATH=/var/www/html/cfssl LAPI_URL=https://crowdsec:8080 php ./my-own-modules/lapi-client/vendor/bin/phpunit ./my-own-modules/lapi-client/tests/Integration --testdox +ddev exec BOUNCER_KEY= AGENT_TLS_PATH=/var/www/html/cfssl LAPI_URL=https://crowdsec:8080 php ./my-code/lapi-client/vendor/bin/phpunit ./my-code/lapi-client/tests/Integration --testdox ``` `` should have been created and retrieved before this test by running `ddev create-bouncer`. @@ -156,7 +128,7 @@ ddev exec BOUNCER_KEY= AGENT_TLS_PATH=/var/www/html/cfssl LAPI_URL= If you need to test with a TLS authentication, you should launch: ```bash -ddev exec BOUNCER_TLS_PATH=/var/www/html/cfssl AGENT_TLS_PATH=/var/www/html/cfssl LAPI_URL=https://crowdsec:8080 php ./my-own-modules/lapi-client/vendor/bin/phpunit ./my-own-modules/lapi-client/tests/Integration --testdox +ddev exec BOUNCER_TLS_PATH=/var/www/html/cfssl AGENT_TLS_PATH=/var/www/html/cfssl LAPI_URL=https://crowdsec:8080 php ./my-code/lapi-client/vendor/bin/phpunit ./my-code/lapi-client/tests/Integration --testdox ``` #### Coding standards @@ -165,7 +137,7 @@ We set up some coding standards tools that you will find in the `tools/coding-st In order to use these, you will need to work with a PHP version >= 7.4 and run first: ```bash -ddev composer update --working-dir=./my-own-modules/lapi-client/tools/coding-standards +ddev composer update --working-dir=./my-code/lapi-client/tools/coding-standards ``` ##### PHPCS Fixer @@ -176,7 +148,7 @@ With ddev, you can do the following: ```bash -ddev phpcsfixer my-own-modules/lapi-client/tools/coding-standards/php-cs-fixer ../ +ddev phpcsfixer my-code/lapi-client/tools/coding-standards/php-cs-fixer ../ ``` @@ -186,7 +158,7 @@ To use the [PHPSTAN](https://github.com/phpstan/phpstan) tool, you can run: ```bash -ddev phpstan /var/www/html/my-own-modules/lapi-client/tools/coding-standards phpstan/phpstan.neon /var/www/html/my-own-modules/lapi-client/src +ddev phpstan /var/www/html/my-code/lapi-client/tools/coding-standards phpstan/phpstan.neon /var/www/html/my-code/lapi-client/src ``` @@ -196,7 +168,7 @@ ddev phpstan /var/www/html/my-own-modules/lapi-client/tools/coding-standards php To use the [PHPMD](https://github.com/phpmd/phpmd) tool, you can run: ```bash -ddev phpmd ./my-own-modules/lapi-client/tools/coding-standards phpmd/rulesets.xml ../../src +ddev phpmd ./my-code/lapi-client/tools/coding-standards phpmd/rulesets.xml ../../src ``` @@ -205,13 +177,13 @@ ddev phpmd ./my-own-modules/lapi-client/tools/coding-standards phpmd/rulesets.xm To use [PHP Code Sniffer](https://github.com/squizlabs/PHP_CodeSniffer) tools, you can run: ```bash -ddev phpcs ./my-own-modules/lapi-client/tools/coding-standards my-own-modules/lapi-client/src PSR12 +ddev phpcs ./my-code/lapi-client/tools/coding-standards my-code/lapi-client/src PSR12 ``` and: ```bash -ddev phpcbf ./my-own-modules/lapi-client/tools/coding-standards my-own-modules/lapi-client/src PSR12 +ddev phpcbf ./my-code/lapi-client/tools/coding-standards my-code/lapi-client/src PSR12 ``` @@ -220,7 +192,7 @@ ddev phpcbf ./my-own-modules/lapi-client/tools/coding-standards my-own-modules/ To use [PSALM](https://github.com/vimeo/psalm) tools, you can run: ```bash -ddev psalm ./my-own-modules/lapi-client/tools/coding-standards ./my-own-modules/lapi-client/tools/coding-standards/psalm +ddev psalm ./my-code/lapi-client/tools/coding-standards ./my-code/lapi-client/tools/coding-standards/psalm ``` ##### PHP Unit Code coverage @@ -235,7 +207,7 @@ ddev xdebug To generate a html report, you can run: ```bash -ddev php -dxdebug.mode=coverage ./my-own-modules/lapi-client/tools/coding-standards/vendor/bin/phpunit --configuration ./my-own-modules/lapi-client/tools/coding-standards/phpunit/phpunit.xml +ddev php -dxdebug.mode=coverage ./my-code/lapi-client/tools/coding-standards/vendor/bin/phpunit --configuration ./my-code/lapi-client/tools/coding-standards/phpunit/phpunit.xml ``` You should find the main report file `dashboard.html` in `tools/coding-standards/phpunit/code-coverage` folder. @@ -244,7 +216,7 @@ You should find the main report file `dashboard.html` in `tools/coding-standards If you want to generate a text report in the same folder: ```bash -ddev php -dxdebug.mode=coverage ./my-own-modules/lapi-client/tools/coding-standards/vendor/bin/phpunit --configuration ./my-own-modules/lapi-client/tools/coding-standards/phpunit/phpunit.xml --coverage-text=./my-own-modules/lapi-client/tools/coding-standards/phpunit/code-coverage/report.txt +ddev php -dxdebug.mode=coverage ./my-code/lapi-client/tools/coding-standards/vendor/bin/phpunit --configuration ./my-code/lapi-client/tools/coding-standards/phpunit/phpunit.xml --coverage-text=./my-code/lapi-client/tools/coding-standards/phpunit/code-coverage/report.txt ``` ## Commit message diff --git a/src/Configuration.php b/src/Configuration.php index eef7ca4..e386563 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -86,7 +86,7 @@ public function getConfigTreeBuilder(): TreeBuilder private function addConnectionNodes($rootNode) { $rootNode->children() - ->scalarNode('api_url')->defaultValue(Constants::DEFAULT_LAPI_URL)->end() + ->scalarNode('api_url')->cannotBeEmpty()->defaultValue(Constants::DEFAULT_LAPI_URL)->end() ->enumNode('auth_type') ->values( [ diff --git a/src/Constants.php b/src/Constants.php index 7dffc02..c079d13 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -37,5 +37,5 @@ class Constants extends CommonConstants /** * @var string The current version of this library */ - public const VERSION = 'v3.0.0'; + public const VERSION = 'v3.1.0'; } diff --git a/tests/Integration/BouncerTest.php b/tests/Integration/BouncerTest.php index 4d6f4de..b994bfe 100644 --- a/tests/Integration/BouncerTest.php +++ b/tests/Integration/BouncerTest.php @@ -29,6 +29,15 @@ final class BouncerTest extends TestCase * @var array */ protected $configs; + /** + * @var string + */ + protected $useTls; + /** + * @var WatcherClient + */ + protected $watcherClient; + private function addTlsConfig(&$bouncerConfigs, $tlsPath) { diff --git a/tests/Integration/WatcherClient.php b/tests/Integration/WatcherClient.php index b602d33..5d1eee6 100644 --- a/tests/Integration/WatcherClient.php +++ b/tests/Integration/WatcherClient.php @@ -20,6 +20,10 @@ class WatcherClient extends AbstractClient /** @var string */ private $token; + /** + * @var array|string[] + */ + protected $headers = []; public function __construct(array $configs) { diff --git a/tests/Unit/BouncerTest.php b/tests/Unit/BouncerTest.php index 0904dc9..2e8b7ce 100644 --- a/tests/Unit/BouncerTest.php +++ b/tests/Unit/BouncerTest.php @@ -168,6 +168,20 @@ public function testConfigure() 'user_agent_suffix length should be <16' ); + $error = ''; + try { + new Bouncer(['api_url' => '']); + } catch (\Exception $e) { + $error = $e->getMessage(); + } + + PHPUnitUtil::assertRegExp( + $this, + '/cannot contain an empty value/', + $error, + 'api_url must not be empty' + ); + $error = ''; try { new Bouncer(['api_key' => TestConstants::API_KEY, 'user_agent_suffix' => 'aaaaa a']);