Skip to content

Commit

Permalink
feat(*): Add api_connect_timeout setting (#31)
Browse files Browse the repository at this point in the history
* feat(config): Add api_connect_timeout setting

* feat(*): Prepare for v3.1.0
  • Loading branch information
julienloizelet committed Dec 7, 2023
1 parent fba17e4 commit 2641478
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 92 deletions.
4 changes: 2 additions & 2 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if [ -z "$1" ]; then
exit 1
fi

commitTitle="$(cat $1 | head -n1)"
commitTitle="$(head -n 1 < "$1")"

# ignore merge
if echo "$commitTitle" | grep -qE "^Merge"; then
Expand All @@ -15,7 +15,7 @@ fi

# check commit message
REGEX='^(feat|fix|docs|style|refactor|ci|test|chore|comment)\(.*\)\:.*'
if ! echo "$commitTitle" | grep -qE ${REGEX}; then
if ! echo "$commitTitle" | grep -qE "${REGEX}"; then
echo "Your commit title '$commitTitle' did not follow conventional commit message rules:"
echo "Please comply with the regex ${REGEX}"
exit 1
Expand Down
44 changes: 19 additions & 25 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2']
php-version: ['7.4', '8.0', '8.1', '8.2', '8.3']

name: Coding standards test
runs-on: ubuntu-latest
Expand All @@ -29,35 +29,29 @@ jobs:
EXTENSION_PATH: "my-code/capi-client"

steps:
- name: Clone DDEV files
uses: actions/checkout@v3
with:
path: .ddev
repository: julienloizelet/ddev-php
ref: "main"

- name: Install DDEV
env:
DDEV_VERSION: v1.21.4
run: |
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
sudo apt-get -qq update
sudo apt-get -qq -y install libnss3-tools
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
bash install_ddev.sh ${{env.DDEV_VERSION}}
ddev config global --instrumentation-opt-in=false --omit-containers=dba,ddev-ssh-agent
rm install_ddev.sh
- name: Set PHP_VERSION_CODE env
# used in some directory path and conventional file naming
# Example : 7.4 => php74
curl -fsSL https://apt.fury.io/drud/gpg.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/ddev.gpg > /dev/null
echo "deb [signed-by=/etc/apt/trusted.gpg.d/ddev.gpg] https://apt.fury.io/drud/ * *" | sudo tee /etc/apt/sources.list.d/ddev.list
sudo apt-get -q update
sudo apt-get -q -y install libnss3-tools ddev
mkcert -install
ddev config global --instrumentation-opt-in=false --omit-containers=ddev-ssh-agent
- name: Create empty PHP DDEV project
run: ddev config --project-type=php --project-name=crowdsec-capi-client --php-version=${{ matrix.php-version }}

- name: Add-ons install
run: ddev get julienloizelet/ddev-tools

- name: Add Redis, Memcached and X-Debug
if: ${{ matrix.php-version == '8.3' }}
run: |
echo "PHP_VERSION_CODE=$(echo php${{ matrix.php-version }} | sed 's/\.//g' )" >> $GITHUB_ENV
cp .ddev/okaeli-add-on/common/custom_files/config.php83missing.yaml .ddev/config.php83missing.yaml
- name: Start DDEV with PHP ${{ matrix.php-version }}
run: |
cp .ddev/config_overrides/config.${{ env.PHP_VERSION_CODE }}.yaml .ddev/config.${{ env.PHP_VERSION_CODE }}.yaml
ddev start
run: ddev start

- name: Some DEBUG information
run: |
Expand Down Expand Up @@ -102,4 +96,4 @@ jobs:
run: |
ddev xdebug
ddev php -dxdebug.mode=coverage ./${{env.EXTENSION_PATH}}/tools/coding-standards/vendor/bin/phpunit --configuration ./${{env.EXTENSION_PATH}}/tools/coding-standards/phpunit/phpunit.xml --coverage-text=./${{env.EXTENSION_PATH}}/coding-standards/phpunit/code-coverage/report.txt
cat ${{env.EXTENSION_PATH}}/coding-standards/phpunit/code-coverage/report.txt
cat ${{env.EXTENSION_PATH}}/coding-standards/phpunit/code-coverage/report.txt
40 changes: 17 additions & 23 deletions .github/workflows/unit-and-integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,29 @@ jobs:
EXTENSION_PATH: "my-code/capi-client"

steps:
- name: Clone DDEV files
uses: actions/checkout@v3
with:
path: .ddev
repository: julienloizelet/ddev-php
ref: "main"

- name: Install DDEV
env:
DDEV_VERSION: v1.21.4
run: |
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
sudo apt-get -qq update
sudo apt-get -qq -y install libnss3-tools
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
bash install_ddev.sh ${{env.DDEV_VERSION}}
ddev config global --instrumentation-opt-in=false --omit-containers=dba,ddev-ssh-agent
rm install_ddev.sh
curl -fsSL https://apt.fury.io/drud/gpg.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/ddev.gpg > /dev/null
echo "deb [signed-by=/etc/apt/trusted.gpg.d/ddev.gpg] https://apt.fury.io/drud/ * *" | sudo tee /etc/apt/sources.list.d/ddev.list
sudo apt-get -q update
sudo apt-get -q -y install libnss3-tools ddev
mkcert -install
ddev config global --instrumentation-opt-in=false --omit-containers=ddev-ssh-agent
- name: Set PHP_VERSION_CODE env
# used in some directory path and conventional file naming
# Example : 7.4 => 74
- name: Create empty PHP DDEV project
run: ddev config --project-type=php --project-name=crowdsec-capi-client --php-version=${{ matrix.php-version }}

- name: Add-ons install
run: ddev get julienloizelet/ddev-tools

- name: Add Redis, Memcached and X-Debug
if: ${{ matrix.php-version == '8.3' }}
run: |
echo "PHP_VERSION_CODE=$(echo php${{ matrix.php-version }} | sed 's/\.//g' )" >> $GITHUB_ENV
cp .ddev/okaeli-add-on/common/custom_files/config.php83missing.yaml .ddev/config.php83missing.yaml
- name: Start DDEV with PHP ${{ matrix.php-version }}
run: |
cp .ddev/config_overrides/config.${{ env.PHP_VERSION_CODE }}.yaml .ddev/config.${{ env.PHP_VERSION_CODE }}.yaml
ddev start
run: ddev start

- name: Some DEBUG information
run: |
Expand Down Expand Up @@ -100,4 +94,4 @@ jobs:
if: |
github.event.inputs.integration_tests == 'true' ||
github.event_name == 'push'
run: ddev php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit ./${{env.EXTENSION_PATH}}/tests/Integration --testdox
run: ddev php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit ./${{env.EXTENSION_PATH}}/tests/Integration --testdox
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ The [public API](https://semver.org/spec/v2.0.0.html#spec-item-1) of this librar

---

## [3.1.0](https://github.com/crowdsecurity/php-capi-client/releases/tag/v3.1.0) - 2023-12-07
[_Compare with previous release_](https://github.com/crowdsecurity/php-capi-client/compare/v3.0.0...v3.1.0)


### Added

- Add `api_connect_timeout` configuration

---

## [3.0.0](https://github.com/crowdsecurity/php-capi-client/releases/tag/v3.0.0) - 2023-03-09
[_Compare with previous release_](https://github.com/crowdsecurity/php-capi-client/compare/v2.1.0...v3.0.0)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"require": {
"php": "^7.2.5 || ^8.0",
"crowdsec/common": "^2.0.0",
"crowdsec/common": "^2.2.0",
"ext-json": "*",
"symfony/config": "^4.4.44 || ^5.4.11 || ^6.0.11",
"symfony/uid": "^5.4.19 || ^6.2.5",
Expand Down
4 changes: 2 additions & 2 deletions docs/USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ You have to pass an array of CrowdSec scenarios that will be used to log in your
You should find a list of available scenarios on the [CrowdSec hub collections page](https://hub.crowdsec.net/browse/).


Each scenario must match the regular expression `#^[A-Za-z0-9]{0,16}\/[A-Za-z0-9_-]{0,32}$#`.
Each scenario must match the regular expression `#^[A-Za-z0-9]{0,16}\/[A-Za-z0-9_-]{0,64}$#`.


### CAPI timeout
Expand Down Expand Up @@ -586,4 +586,4 @@ php tests/scripts/watcher/enroll.php <SCENARIOS_JSON> <NAME> <OVERWRITE> <ENROLL

```bash
php tests/scripts/watcher/enroll.php '["crowdsecurity/http-backdoors-attempts", "crowdsecurity/http-bad-user-agent"]' TESTWATCHER 0 YourEnrollKey '["tag1", "tag2"]'
```
```
3 changes: 0 additions & 3 deletions src/Client/CapiHandler/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/
class Curl extends CommonCurl implements CapiHandlerInterface
{
/**
* {@inheritdoc}
*/
public function getListDecisions(string $url, array $headers = []): string
{
$handle = curl_init();
Expand Down
3 changes: 0 additions & 3 deletions src/Client/CapiHandler/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/
class FileGetContents extends CommonFileGetContents implements CapiHandlerInterface
{
/**
* {@inheritdoc}
*/
public function getListDecisions(string $url, array $headers = []): string
{
$config = $this->createListContextConfig($headers);
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Watcher extends AbstractConfiguration
'user_agent_version',
'scenarios',
'api_timeout',
'api_connect_timeout',
'metrics',
];

Expand Down Expand Up @@ -105,6 +106,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarPrototype()->cannotBeEmpty()->end()
->end()
->integerNode('api_timeout')->defaultValue(Constants::API_TIMEOUT)->end()
->integerNode('api_connect_timeout')->defaultValue(Constants::API_CONNECT_TIMEOUT)->end()
->end()
;
$this->addMetricsNodes($rootNode);
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,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';
}
24 changes: 0 additions & 24 deletions src/Storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,34 @@ public function __construct(string $rootDir = __DIR__, string $env = Constants::
$this->env = $env;
}

/**
* {@inheritdoc}
*/
public function retrieveMachineId(): ?string
{
$storageContent = $this->readFile($this->getBasePath() . self::MACHINE_ID_FILE);

return !empty($storageContent['machine_id']) ? $storageContent['machine_id'] : null;
}

/**
* {@inheritdoc}
*/
public function retrievePassword(): ?string
{
$storageContent = $this->readFile($this->getBasePath() . self::PASSWORD_FILE);

return !empty($storageContent['password']) ? $storageContent['password'] : null;
}

/**
* {@inheritdoc}
*/
public function retrieveScenarios(): ?array
{
$storageContent = $this->readFile($this->getBasePath() . self::SCENARIOS_FILE);

return !empty($storageContent['scenarios']) ? $storageContent['scenarios'] : null;
}

/**
* {@inheritdoc}
*/
public function retrieveToken(): ?string
{
$storageContent = $this->readFile($this->getBasePath() . self::TOKEN_FILE);

return !empty($storageContent['token']) ? $storageContent['token'] : null;
}

/**
* {@inheritdoc}
*/
public function storeMachineId(string $machineId): bool
{
try {
Expand All @@ -93,9 +78,6 @@ public function storeMachineId(string $machineId): bool
return true;
}

/**
* {@inheritdoc}
*/
public function storePassword(string $password): bool
{
try {
Expand All @@ -108,9 +90,6 @@ public function storePassword(string $password): bool
return true;
}

/**
* {@inheritdoc}
*/
public function storeScenarios(array $scenarios): bool
{
try {
Expand All @@ -123,9 +102,6 @@ public function storeScenarios(array $scenarios): bool
return true;
}

/**
* {@inheritdoc}
*/
public function storeToken(string $token): bool
{
try {
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/WatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function testPushSignals($requestHandler)
public function testEnroll($requestHandler)
{
$enrollmentKey = file_get_contents(__DIR__ . '/.enrollment_key.txt');
$enrollmentKey = str_replace(array("\n", "\r"), '', $enrollmentKey);
$enrollmentKey = str_replace(["\n", "\r"], '', $enrollmentKey);

if (!$enrollmentKey) {
throw new Exception('Error while trying to get content of .enrollment_key.txt file');
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/WatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public function testRegisterParams()
'POST',
Constants::REGISTER_ENDPOINT,
self::callback(function ($params): bool {
return 2 === count($params) &&
!empty($params['password']) &&
Constants::PASSWORD_LENGTH === strlen($params['password']) &&
!empty($params['machine_id']) &&
Constants::MACHINE_ID_LENGTH === strlen($params['machine_id']) &&
0 === substr_compare(
return 2 === count($params)
&& !empty($params['password'])
&& Constants::PASSWORD_LENGTH === strlen($params['password'])
&& !empty($params['machine_id'])
&& Constants::MACHINE_ID_LENGTH === strlen($params['machine_id'])
&& 0 === substr_compare(
$params['machine_id'],
TestConstants::MACHINE_ID_PREFIX,
0,
Expand Down Expand Up @@ -622,7 +622,7 @@ public function testConfigure()

$error = '';
try {
new Watcher(['scenarios' => ['testtooloong/abcdefghijiklmnopqrstuvwxyzabcdefghijiklmnopqrstuvwxy']], new FileStorage());
new Watcher(['scenarios' => ['testtooloong/abcdefghijiklmnopqrstuvwxyzabcdefghijiklmnopqrstuvwxyaaaaaaaaaaaaaa']], new FileStorage());
} catch (\Exception $e) {
$error = $e->getMessage();
}
Expand Down

0 comments on commit 2641478

Please sign in to comment.