Skip to content

Commit

Permalink
Merge pull request #12 from julienloizelet/fix/no-empty-url
Browse files Browse the repository at this point in the history
Fix/no empty url
  • Loading branch information
julienloizelet committed Apr 27, 2023
2 parents 86e85c2 + 237ee03 commit ac15b04
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-and-integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 27 additions & 55 deletions docs/DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -134,29 +106,29 @@ 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

In order to launch integration tests, we have to set some environment variables:

```bash
ddev exec BOUNCER_KEY=<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=<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
```

`<BOUNCER_KEY>` should have been created and retrieved before this test by running `ddev create-bouncer`.

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
Expand All @@ -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
Expand All @@ -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 ../

```

Expand All @@ -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

```

Expand All @@ -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

```

Expand All @@ -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
```


Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down
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 extends CommonConstants
/**
* @var string The current version of this library
*/
public const VERSION = 'v3.0.0';
public const VERSION = 'v3.1.0';
}
9 changes: 9 additions & 0 deletions tests/Integration/BouncerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 4 additions & 0 deletions tests/Integration/WatcherClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class WatcherClient extends AbstractClient

/** @var string */
private $token;
/**
* @var array|string[]
*/
protected $headers = [];

public function __construct(array $configs)
{
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/BouncerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit ac15b04

Please sign in to comment.