Skip to content

Commit e0c23fe

Browse files
style(*): Pass through coding standards
1 parent e3f1400 commit e0c23fe

File tree

9 files changed

+109
-55
lines changed

9 files changed

+109
-55
lines changed

scripts/refresh-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929

3030
// Refresh the blocklist cache
3131
$bouncer->refreshBlocklistCache();
32-
echo "Cache successfully refreshed.\n";
32+
echo "Cache successfully refreshed.\n";

src/AbstractBounce.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ protected function handleForwardedFor(string $ip, array $configs): string
168168
{
169169
$forwardedIp = null;
170170
if (empty($configs['forced_test_forwarded_ip'])) {
171-
$XForwardedForHeader = $this->getHttpRequestHeader('X-Forwarded-For');
172-
if (null !== $XForwardedForHeader) {
173-
$ipList = array_map('trim', array_values(array_filter(explode(',', $XForwardedForHeader))));
171+
$xForwardedForHeader = $this->getHttpRequestHeader('X-Forwarded-For');
172+
if (null !== $xForwardedForHeader) {
173+
$ipList = array_map('trim', array_values(array_filter(explode(',', $xForwardedForHeader))));
174174
$forwardedIp = end($ipList);
175175
}
176176
} elseif ($configs['forced_test_forwarded_ip'] === Constants::X_FORWARDED_DISABLED) {

src/templates/_base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,4 @@ function crowdSecBaseTemplatePart3(array $config, $footer): void
145145

146146
</html>
147147
<?php
148-
} ?>
148+
} ?>

src/templates/access-forbidden.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ function displayAccessForbiddenTemplate(array $config): void
77
<h1><?php echo htmlentities($config['text']['ban_wall']['title'], \ENT_QUOTES); ?></h1>
88
<p class="desc"><?php echo htmlentities($config['text']['ban_wall']['subtitle'], \ENT_QUOTES); ?></p>
99
<?php crowdSecBaseTemplatePart3($config, $config['text']['ban_wall']['footer']);
10-
} ?>
10+
} ?>

src/templates/captcha.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ function displayCaptchaTemplate(bool $error, string $captchaImageSrc, string $ca
5858
<button type="submit" /><?php echo htmlentities($config['text']['captcha_wall']['send_button'], \ENT_QUOTES); ?></button>
5959
</form>
6060
<?php crowdSecBaseTemplatePart3($config, $config['text']['captcha_wall']['footer']);
61-
} ?>
61+
} ?>

tests/Integration/GeolocationTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
namespace CrowdSecBouncer\Tests\Integration;
56

67
use CrowdSecBouncer\ApiCache;
@@ -39,7 +40,7 @@ private function handleMaxMindConfig(array $maxmindConfig): array
3940
{
4041
// Check if MaxMind database exist
4142
if (!file_exists($maxmindConfig['database_path'])) {
42-
$this->fail('There must be a MaxMind Database here: '.$maxmindConfig['database_path']);
43+
$this->fail('There must be a MaxMind Database here: ' . $maxmindConfig['database_path']);
4344
}
4445

4546
return [
@@ -74,7 +75,7 @@ public function testCanVerifyIpAndCountryWithMaxmindInLiveMode(array $maxmindCon
7475
'api_url' => TestHelpers::getLapiUrl(),
7576
'geolocation' => $geolocationConfig,
7677
'use_curl' => $this->useCurl,
77-
'api_user_agent' => 'Unit test/'.Constants::BASE_USER_AGENT,
78+
'api_user_agent' => 'Unit test/' . Constants::BASE_USER_AGENT,
7879
'cache_system' => Constants::CACHE_SYSTEM_PHPFS,
7980
'fs_cache_path' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
8081
];
@@ -148,7 +149,7 @@ public function testCanVerifyIpAndCountryWithMaxmindInStreamMode(array $maxmindC
148149
'stream_mode' => true,
149150
'geolocation' => $geolocationConfig,
150151
'use_curl' => $this->useCurl,
151-
'api_user_agent' => 'Unit test/'.Constants::BASE_USER_AGENT,
152+
'api_user_agent' => 'Unit test/' . Constants::BASE_USER_AGENT,
152153
'cache_system' => Constants::CACHE_SYSTEM_PHPFS,
153154
'fs_cache_path' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
154155
];

tests/Integration/IpVerificationTest.php

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
5757
'memcached_dsn' => getenv('MEMCACHED_DSN'),
5858
'fs_cache_path' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
5959
];
60-
if($this->useTls){
60+
if ($this->useTls) {
6161
$bouncerConfigs['tls_cert_path'] = $this->useTls . '/bouncer.pem';
6262
$bouncerConfigs['tls_key_path'] = $this->useTls . '/bouncer-key.pem';
6363
$bouncerConfigs['tls_ca_cert_path'] = $this->useTls . '/ca-chain.pem';
6464
$bouncerConfigs['tls_verify_peer'] = true;
65-
6665
}
6766

6867
$bouncer = new Bouncer($bouncerConfigs, $this->logger);
@@ -135,7 +134,7 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
135134

136135
// Reconfigure the bouncer to set maximum remediation level to "captcha"
137136
$bouncerConfigs['max_remediation_level'] = 'captcha';
138-
$bouncer = new Bouncer( $bouncerConfigs, $this->logger);
137+
$bouncer = new Bouncer($bouncerConfigs, $this->logger);
139138
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
140139
$this->assertEquals('captcha', $cappedRemediation, 'The remediation for the banned IP should now be "captcha"');
141140
// Reset the max remediation level to its origin state
@@ -144,16 +143,30 @@ public function testCanVerifyIpInLiveModeWithCacheSystem($cacheAdapterName, $ori
144143

145144
$this->logger->info('', ['message' => 'set "Large IPV4 range banned" state']);
146145
$this->watcherClient->deleteAllDecisions();
147-
$this->watcherClient->addDecision(new \DateTime(), '24h', WatcherClient::HOURS24, TestHelpers::BAD_IP.'/'
148-
.TestHelpers::LARGE_IPV4_RANGE, 'ban');
146+
$this->watcherClient->addDecision(new \DateTime(), '24h', WatcherClient::HOURS24, TestHelpers::BAD_IP . '/'
147+
. TestHelpers::LARGE_IPV4_RANGE, 'ban');
149148
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
150-
$this->assertEquals('ban', $cappedRemediation, 'The remediation for the banned IP with a too large range should now be "ban" as we are in live mode');
149+
$this->assertEquals(
150+
'ban',
151+
$cappedRemediation,
152+
'The remediation for the banned IP with a too large range should now be "ban" as we are in live mode'
153+
);
151154

152155
$this->logger->info('', ['message' => 'set "IPV6 range banned" state']);
153156
$this->watcherClient->deleteAllDecisions();
154-
$this->watcherClient->addDecision(new \DateTime(), '24h', WatcherClient::HOURS24, TestHelpers::BAD_IPV6.'/'.TestHelpers::IPV6_RANGE, 'ban');
157+
$this->watcherClient->addDecision(
158+
new \DateTime(),
159+
'24h',
160+
WatcherClient::HOURS24,
161+
TestHelpers::BAD_IPV6 . '/' . TestHelpers::IPV6_RANGE,
162+
'ban'
163+
);
155164
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IPV6);
156-
$this->assertEquals('ban', $cappedRemediation, 'The remediation for the banned IPV6 with a too large range should now be "ban" as we are in live mode');
165+
$this->assertEquals(
166+
'ban',
167+
$cappedRemediation,
168+
'The remediation for the banned IPV6 with a too large range should now be "ban" as we are in live mode'
169+
);
157170
}
158171

159172
/**
@@ -177,7 +190,7 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
177190
'memcached_dsn' => getenv('MEMCACHED_DSN'),
178191
'fs_cache_path' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
179192
];
180-
if($this->useTls){
193+
if ($this->useTls) {
181194
$bouncerConfigs['tls_cert_path'] = $this->useTls . '/bouncer.pem';
182195
$bouncerConfigs['tls_key_path'] = $this->useTls . '/bouncer-key.pem';
183196
$bouncerConfigs['tls_ca_cert_path'] = $this->useTls . '/ca-chain.pem';
@@ -283,12 +296,11 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
283296
'memcached_dsn' => getenv('MEMCACHED_DSN'),
284297
'fs_cache_path' => TestHelpers::PHP_FILES_CACHE_ADAPTER_DIR
285298
];
286-
if($this->useTls){
299+
if ($this->useTls) {
287300
$bouncerConfigs['tls_cert_path'] = $this->useTls . '/bouncer.pem';
288301
$bouncerConfigs['tls_key_path'] = $this->useTls . '/bouncer-key.pem';
289302
$bouncerConfigs['tls_ca_cert_path'] = $this->useTls . '/ca-chain.pem';
290303
$bouncerConfigs['tls_verify_peer'] = true;
291-
292304
}
293305

294306
$bouncer = new Bouncer($bouncerConfigs, $this->logger);
@@ -301,14 +313,34 @@ public function testCanVerifyIpInStreamModeWithCacheSystem($cacheAdapterName, $o
301313

302314
$this->logger->info('', ['message' => 'set "Large IPV4 range banned" + "IPV6 range banned" state']);
303315
$this->watcherClient->deleteAllDecisions();
304-
$this->watcherClient->addDecision(new \DateTime(), '24h', WatcherClient::HOURS24, TestHelpers::BAD_IP.'/'.TestHelpers::LARGE_IPV4_RANGE, 'ban');
305-
$this->watcherClient->addDecision(new \DateTime(), '24h', WatcherClient::HOURS24, TestHelpers::BAD_IPV6.'/'.TestHelpers::IPV6_RANGE, 'ban');
316+
$this->watcherClient->addDecision(
317+
new \DateTime(),
318+
'24h',
319+
WatcherClient::HOURS24,
320+
TestHelpers::BAD_IP . '/' . TestHelpers::LARGE_IPV4_RANGE,
321+
'ban'
322+
);
323+
$this->watcherClient->addDecision(
324+
new \DateTime(),
325+
'24h',
326+
WatcherClient::HOURS24,
327+
TestHelpers::BAD_IPV6 . '/' . TestHelpers::IPV6_RANGE,
328+
'ban'
329+
);
306330
// Pull updates
307331
$bouncer->refreshBlocklistCache();
308332

309333
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IP);
310-
$this->assertEquals('bypass', $cappedRemediation, 'The remediation for the banned IP with a too large range should now be "bypass" as we are in stream mode');
334+
$this->assertEquals(
335+
'bypass',
336+
$cappedRemediation,
337+
'The remediation for the banned IP with a too large range should now be "bypass" as we are in stream mode'
338+
);
311339
$cappedRemediation = $bouncer->getRemediationForIp(TestHelpers::BAD_IPV6);
312-
$this->assertEquals('bypass', $cappedRemediation, 'The remediation for the banned IPV6 with a too large range should now be "bypass" as we are in stream mode');
340+
$this->assertEquals(
341+
'bypass',
342+
$cappedRemediation,
343+
'The remediation for the banned IPV6 with a too large range should now be "bypass" as we are in stream mode'
344+
);
313345
}
314346
}

tests/Integration/TestHelpers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TestHelpers
3030
public const IP_FRANCE = '78.119.253.85';
3131
public const UNIT_TEST_AGENT_PREFIX = 'Unit test';
3232

33-
public const PHP_FILES_CACHE_ADAPTER_DIR = __DIR__.'/../var/phpFiles.cache';
33+
public const PHP_FILES_CACHE_ADAPTER_DIR = __DIR__ . '/../var/phpFiles.cache';
3434

3535
public const LOG_LEVEL = Logger::DEBUG; // set to Logger::DEBUG to get high verbosity
3636

@@ -64,11 +64,11 @@ public static function maxmindConfigProvider(): array
6464
return [
6565
'country database' => [[
6666
'database_type' => 'country',
67-
'database_path' => __DIR__.'/../GeoLite2-Country.mmdb',
67+
'database_path' => __DIR__ . '/../GeoLite2-Country.mmdb',
6868
]],
6969
'city database' => [[
7070
'database_type' => 'city',
71-
'database_path' => __DIR__.'/../GeoLite2-City.mmdb',
71+
'database_path' => __DIR__ . '/../GeoLite2-City.mmdb',
7272
]],
7373
];
7474
}

tests/Integration/WatcherClient.php

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace CrowdSecBouncer\Tests\Integration;
66

7-
8-
use CrowdSecBouncer\BouncerException;
97
use CrowdSecBouncer\Constants;
108
use CrowdSecBouncer\RestClient\FileGetContents;
119
use CrowdSecBouncer\RestClient\Curl;
@@ -46,17 +44,19 @@ public function __construct(array $configs, LoggerInterface $logger)
4644
$this->configs['api_url'] = $apiUrl;
4745
$this->configs['api_timeout'] = 2;
4846
$agentTlsPath = getenv('AGENT_TLS_PATH');
49-
if(!$agentTlsPath){
47+
if (!$agentTlsPath) {
5048
throw new \Exception('Using TLS auth for agent is required. Please set AGENT_TLS_PATH env.');
5149
}
5250
$this->configs['auth_type'] = Constants::AUTH_TLS;
53-
$this->configs['tls_cert_path'] = $agentTlsPath. '/agent.pem';
54-
$this->configs['tls_key_path'] = $agentTlsPath. '/agent-key.pem';
51+
$this->configs['tls_cert_path'] = $agentTlsPath . '/agent.pem';
52+
$this->configs['tls_key_path'] = $agentTlsPath . '/agent-key.pem';
5553
$this->configs['tls_verify_peer'] = false;
5654

5755
$useCurl = !empty($this->configs['use_curl']);
5856
$this->watcherClient = $useCurl ? new Curl($this->configs, $this->logger) : new FileGetContents(
59-
$this->configs, $this->logger);
57+
$this->configs,
58+
$this->logger
59+
);
6060
$this->logger->info('', ['message' => 'Watcher client initialized', 'use_curl' => $useCurl]);
6161
}
6262

@@ -67,7 +67,7 @@ public function setInitialState(): void
6767
$this->deleteAllDecisions();
6868
$now = new \DateTime();
6969
$this->addDecision($now, '12h', '+12 hours', TestHelpers::BAD_IP, 'captcha');
70-
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::BAD_IP.'/'.TestHelpers::IP_RANGE, 'ban');
70+
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::BAD_IP . '/' . TestHelpers::IP_RANGE, 'ban');
7171
$this->addDecision($now, '24h', '+24 hours', TestHelpers::JAPAN, 'captcha', Constants::SCOPE_COUNTRY);
7272
}
7373

@@ -78,7 +78,8 @@ public function setSecondState(): void
7878
$this->deleteAllDecisions();
7979
$now = new \DateTime();
8080
$this->addDecision($now, '36h', '+36 hours', TestHelpers::NEWLY_BAD_IP, 'ban');
81-
$this->addDecision($now, '48h', '+48 hours', TestHelpers::NEWLY_BAD_IP.'/'.TestHelpers::IP_RANGE, 'captcha');
81+
$this->addDecision($now, '48h', '+48 hours', TestHelpers::NEWLY_BAD_IP . '/' .
82+
TestHelpers::IP_RANGE, 'captcha');
8283
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::JAPAN, 'captcha', Constants::SCOPE_COUNTRY);
8384
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::IP_JAPAN, 'ban');
8485
$this->addDecision($now, '24h', self::HOURS24, TestHelpers::IP_FRANCE, 'ban');
@@ -97,18 +98,31 @@ private function ensureLogin(): void
9798
/** @var array */
9899
$credentials = $this->watcherClient->request('/v1/watchers/login', null, $data, 'POST');
99100
$this->token = $credentials['token'];
100-
$this->baseHeaders['Authorization'] = 'Bearer '.$this->token;
101+
$this->baseHeaders['Authorization'] = 'Bearer ' . $this->token;
101102
}
102103
}
103104

104105
/**
105106
* Request the Watcher API.
106107
*/
107-
private function request(string $endpoint, array $queryParams = null, array $bodyParams = null, string $method = 'GET', array $headers = null, int $timeout = null): ?array
108-
{
108+
private function request(
109+
string $endpoint,
110+
array $queryParams = null,
111+
array $bodyParams = null,
112+
string $method = 'GET',
113+
array $headers = null,
114+
int $timeout = null
115+
): ?array {
109116
$this->ensureLogin();
110117

111-
return $this->watcherClient->request($endpoint, $queryParams, $bodyParams, $method, $headers ?: $this->baseHeaders, $timeout);
118+
return $this->watcherClient->request(
119+
$endpoint,
120+
$queryParams,
121+
$bodyParams,
122+
$method,
123+
$headers ?: $this->baseHeaders,
124+
$timeout
125+
);
112126
}
113127

114128
public function deleteAllDecisions(): void
@@ -123,23 +137,29 @@ protected function getFinalScope($scope, $value)
123137
return (Constants::SCOPE_IP === $scope && 2 === count(explode('/', $value))) ? Constants::SCOPE_RANGE : $scope;
124138
}
125139

126-
public function addDecision(\DateTime $now, string $durationString, string $dateTimeDurationString, string
127-
$value, string $type, string $scope = Constants::SCOPE_IP)
128-
{
140+
public function addDecision(
141+
\DateTime $now,
142+
string $durationString,
143+
string $dateTimeDurationString,
144+
string $value,
145+
string $type,
146+
string $scope = Constants::SCOPE_IP
147+
) {
129148
$stopAt = (clone $now)->modify($dateTimeDurationString)->format('Y-m-d\TH:i:s.000\Z');
130149
$startAt = $now->format('Y-m-d\TH:i:s.000\Z');
131150

132151
$body = [
133152
'capacity' => 0,
134153
'decisions' => [
135-
[
136-
'duration' => $durationString,
137-
'origin' => 'cscli',
138-
'scenario' => $type.' for scope/value ('.$scope.'/'.$value.') for '.$durationString.' for PHPUnit tests',
139-
'scope' => $this->getFinalScope($scope, $value),
140-
'type' => $type,
141-
'value' => $value,
142-
],
154+
[
155+
'duration' => $durationString,
156+
'origin' => 'cscli',
157+
'scenario' => $type . ' for scope/value (' . $scope . '/' . $value . ') for '
158+
. $durationString . ' for PHPUnit tests',
159+
'scope' => $this->getFinalScope($scope, $value),
160+
'type' => $type,
161+
'value' => $value,
162+
],
143163
],
144164
'events' => [
145165
],
@@ -152,13 +172,14 @@ public function addDecision(\DateTime $now, string $durationString, string $date
152172
'scenario_version' => '',
153173
'simulated' => false,
154174
'source' => [
155-
'scope' => $this->getFinalScope($scope, $value),
156-
'value' => $value,
175+
'scope' => $this->getFinalScope($scope, $value),
176+
'value' => $value,
157177
],
158178
'start_at' => $startAt,
159179
'stop_at' => $stopAt,
160-
];
180+
];
161181
$result = $this->request('/v1/alerts', null, [$body], 'POST');
162-
$this->logger->info('', ['message' => 'Decision '.$result[0].' added: '.$body['decisions'][0]['scenario'].'']);
182+
$this->logger->info('', ['message' => 'Decision ' . $result[0] . ' added: ' .
183+
$body['decisions'][0]['scenario'] . '']);
163184
}
164185
}

0 commit comments

Comments
 (0)