Skip to content

Commit d473fa2

Browse files
authored
Merge pull request #27 from crowdsecurity/move-scripts
move scripts
2 parents c283e76 + 063c612 commit d473fa2

11 files changed

+97
-59
lines changed

docs/contribute.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Here is the development environment for this library:
2222
First of all, install composer dependencies:
2323

2424
```bash
25-
./composer-install.sh
25+
docker-compose run app composer install
2626
```
2727
Then run tests:
2828
```bash
@@ -67,17 +67,19 @@ We use the git workflow [Github Flow](https://guides.github.com/introduction/flo
6767
#### New feature
6868

6969
```bash
70-
git checkout -b <basic-name> # the name is not important now, you can type "new-features"
71-
git commit # as mush as necessary.
70+
git checkout -b <branch-name>
71+
git commit # as much as necessary.
7272

7373
PHP_CS_FIXER_IGNORE_ENV=1 tools/php-cs-fixer/vendor/bin/php-cs-fixer fix # fix coding standards
7474
docker run -e "FILTER_REGEX_INCLUDE=/tmp/lint/src/.*" -e RUN_LOCAL=true -v ${PWD}:/tmp/lint github/super-linter # super linter local pass
7575
./tests-local.sh # check tests are still OK
7676
docker-compose run --rm app vendor/bin/phpdoc-md # Regenerate php doc
7777

78-
git branch -m <name-of-the-branch> # to rename the branch to what has really be done.
79-
git push origin :<basic-name> && git push origin <name-of-the-branch> # Only if already pushed
78+
# Rename branch if necessary
79+
git branch -m <new-name>
80+
git push origin :<old-name> && git push origin <new-name>
8081

82+
# Create PR
8183
gh pr create --fill
8284
```
8385

@@ -90,4 +92,3 @@ git checkout main && git pull
9092
git describe --tags `git rev-list --tags --max-count=1` # to verify what is the current tag
9193
export NEW_GIT_VERSION=v #...X.X.X
9294
./scripts/publish-release.sh
93-
```

examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Let's get started and follow the guide!
1111
Start the containers:
1212

1313
```bash
14-
./composer-install.sh
15-
./setup-local-crowdsec.sh
14+
docker-compose run app composer install
15+
./scripts/setup-local-crowdsec.sh
1616
```
1717

1818
Then get a bouncer API key with:

phpdoc.dist.xml

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/sh
22

33
# Delete existing LAPI database.
4-
[ -e ./var/docker-data/crowdsec.db ] && rm ./var/docker-data/crowdsec.db
4+
[ -e ../var/docker-data/crowdsec.db ] && rm ../var/docker-data/crowdsec.db
55

66
# Start containers.
77
docker-compose up --force-recreate -d crowdsec
88
docker-compose up --remove-orphans -d redis memcached
99

1010
# Create a bouncer with cscli and copy expose generated key.
11-
docker-compose exec crowdsec /usr/local/bin/cscli bouncers add bouncer-php-library -o raw > .bouncer-key
11+
docker-compose exec crowdsec /usr/local/bin/cscli bouncers add bouncer-php-library -o raw > ../.bouncer-key
1212

1313
# Create a watcher with cscli.
1414
docker-compose exec crowdsec cscli machines add PhpUnitTestMachine --password PhpUnitTestMachinePassword > /dev/null 2>&1

src/ApiCache.php

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Psr\Log\LoggerInterface;
99
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1010
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
11+
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
1112
use Symfony\Component\Cache\PruneableInterface;
1213

1314
/**
@@ -243,7 +244,7 @@ private function saveRemediations(array $decisions): bool
243244
}
244245
}
245246

246-
return $this->adapter->commit();
247+
return $this->commit();
247248
}
248249

249250
private function removeRemediations(array $decisions): bool
@@ -268,7 +269,7 @@ private function removeRemediations(array $decisions): bool
268269
}
269270
}
270271

271-
return $this->adapter->commit();
272+
return $this->commit();
272273
}
273274

274275
/**
@@ -291,17 +292,22 @@ private function saveRemediationsForIp(array $decisions, string $ip): string
291292
$remediation = $this->formatRemediationFromDecision(null);
292293
$remediationResult = $this->addRemediationToCacheItem($ip, $remediation[0], $remediation[1], $remediation[2]);
293294
}
294-
$this->adapter->commit();
295+
$this->commit();
295296

296297
return $remediationResult;
297298
}
298299

299300
public function clear(): bool
300301
{
301-
$cleared = $this->adapter->clear();
302+
$this->setCustomErrorHandler();
303+
try {
304+
$cleared = $this->adapter->clear();
305+
} finally {
306+
$this->unsetCustomErrorHandler();
307+
}
302308
$this->warmedUp = false;
303309
$this->defferUpdateCacheConfig(['warmed_up' => $this->warmedUp]);
304-
$this->adapter->commit();
310+
$this->commit();
305311
$this->logger->info('', ['type' => 'CACHE_CLEARED']);
306312

307313
return $cleared;
@@ -328,7 +334,7 @@ public function warmUp(): int
328334
if ($newDecisions) {
329335
$this->warmedUp = $this->saveRemediations($newDecisions);
330336
$this->defferUpdateCacheConfig(['warmed_up' => $this->warmedUp]);
331-
$this->adapter->commit();
337+
$this->commit();
332338
if (!$this->warmedUp) {
333339
throw new BouncerException('Unable to warm the cache up');
334340
}
@@ -338,7 +344,7 @@ public function warmUp(): int
338344
// Store the fact that the cache has been warmed up.
339345
$this->defferUpdateCacheConfig(['warmed_up' => true]);
340346

341-
$this->adapter->commit();
347+
$this->commit();
342348
$this->logger->info('', ['type' => 'CACHE_WARMED_UP', 'added_decisions' => $nbNew]);
343349

344350
return $nbNew;
@@ -447,6 +453,9 @@ public function get(string $ip): string
447453
return $remediation;
448454
}
449455

456+
/**
457+
* Prune the cache (only when using PHP File System cache).
458+
*/
450459
public function prune(): bool
451460
{
452461
if ($this->adapter instanceof PruneableInterface) {
@@ -458,4 +467,59 @@ public function prune(): bool
458467

459468
throw new BouncerException('Cache Adapter'.\get_class($this->adapter).' is not prunable.');
460469
}
470+
471+
/**
472+
* When Memcached connection fail, it throw an unhandled warning.
473+
* To catch this warning as a clean execption we have to temporarily change the error handler.
474+
*/
475+
private function setCustomErrorHandler(): void
476+
{
477+
if ($this->adapter instanceof MemcachedAdapter) {
478+
set_error_handler(function () {
479+
throw new BouncerException('Error when connecting to Memcached. Please fix the Memcached DSN or select another cache technology.');
480+
});
481+
}
482+
}
483+
484+
/**
485+
* When the selected cache adapter is MemcachedAdapter, revert to the previous error handler.
486+
* */
487+
private function unsetCustomErrorHandler(): void
488+
{
489+
if ($this->adapter instanceof MemcachedAdapter) {
490+
restore_error_handler();
491+
}
492+
}
493+
494+
/**
495+
* Wrap the cacheAdapter to catch warnings.
496+
*
497+
* @throws BouncerException if the connection was not successful
498+
* */
499+
private function commit(): bool
500+
{
501+
$this->setCustomErrorHandler();
502+
try {
503+
$result = $this->adapter->commit();
504+
} finally {
505+
$this->unsetCustomErrorHandler();
506+
}
507+
508+
return $result;
509+
}
510+
511+
/**
512+
* Test the connection to the cache system (Redis or Memcached).
513+
*
514+
* @throws BouncerException if the connection was not successful
515+
* */
516+
public function testConnection(): void
517+
{
518+
$this->setCustomErrorHandler();
519+
try {
520+
$this->adapter->getItem(' ');
521+
} finally {
522+
$this->unsetCustomErrorHandler();
523+
}
524+
}
461525
}

src/Bouncer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,14 @@ public function checkCaptcha(string $expected, string $try, string $ip)
196196

197197
return $solved;
198198
}
199+
200+
/**
201+
* Test the connection to the cache system (Redis or Memcached)
202+
*
203+
* @throws BouncerException if the connection was not successful
204+
* */
205+
public function testConnection()
206+
{
207+
return $this->apiCache->testConnection();
208+
}
199209
}

src/Constants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Constants
1818
public const CAPI_URL = 'https://api.crowdsec.net/v2/';
1919

2020
/** @var string The last version of this library */
21-
public const VERSION = 'v0.6.0';
21+
public const VERSION = 'v0.7.0';
2222

2323
/** @var string The user agent used to send request to LAPI or CAPI */
2424
public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/'.self::VERSION;

tests-local-php7.3.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

33
# Setup local CrowdSec instance
4-
./setup-local-crowdsec.sh
4+
./scripts/setup-local-crowdsec.sh
55

66
docker-compose run --rm app-php7.3 ./vendor/bin/phpunit --testdox --colors --exclude-group ignore tests/IpVerificationTest.php

tests-local-php7.4.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

33
# Setup local CrowdSec instance
4-
./setup-local-crowdsec.sh
4+
./scripts/setup-local-crowdsec.sh
55

66
docker-compose run --rm app-php7.4 ./vendor/bin/phpunit --testdox --colors --exclude-group ignore tests/IpVerificationTest.php

tests-local-php8.0.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

33
# Setup local CrowdSec instance
4-
./setup-local-crowdsec.sh
4+
./scripts/setup-local-crowdsec.sh
55

66
docker-compose run --rm app-php8.0 ./vendor/bin/phpunit --testdox --colors --exclude-group ignore tests/IpVerificationTest.php

0 commit comments

Comments
 (0)