Skip to content

Commit 8aebf72

Browse files
Merge pull request #9 from julienloizelet/feat/lib-0-22-0-no-more-session
Feat/lib 0 22 0 no more session
2 parents be2cc6c + c713340 commit 8aebf72

File tree

20 files changed

+200
-157
lines changed

20 files changed

+200
-157
lines changed

Block/Adminhtml/System/Config/Geolocation/Country.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class Country extends Button
6161
*/
6262
protected $_forcedTestIpField = 'crowdsec_bouncer_advanced_debug_forced_test_ip';
6363

64+
/**
65+
* Save result field name
66+
*
67+
* @var string
68+
*/
69+
protected $_saveResultField = 'crowdsec_bouncer_advanced_geolocation_save_result';
70+
6471
/** @var string */
6572
protected $template = 'CrowdSec_Bouncer::system/config/geolocation/country.phtml';
6673
/** @var string */
@@ -106,6 +113,16 @@ public function getForcedTestIpField(): string
106113
return $this->_forcedTestIpField;
107114
}
108115

116+
/**
117+
* Save result field name
118+
*
119+
* @return string
120+
*/
121+
public function getSaveResultField(): string
122+
{
123+
return $this->_saveResultField;
124+
}
125+
109126
/**
110127
* Get the button and scripts contents
111128
*

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.3.0] - 2022-06-09
8+
9+
### Added
10+
- Add configuration to set captcha flow cache lifetime
11+
- Add configuration to set geolocation result cache lifetime
12+
### Changed
13+
- Use cache instead of session to store some values
14+
### Fixed
15+
- Fix wrong deleted decisions count during cache refresh
716

817
## [1.2.0] - 2022-05-12
918

Constants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Constants extends LibConstants
3333
{
3434

3535
/** @var string The last version of this library */
36-
public const VERSION = 'v1.2.0';
36+
public const VERSION = 'v1.3.0';
3737

3838
/** @var string The user agent used to send request to LAPI or CAPI */
3939
public const BASE_USER_AGENT = 'Magento 2 CrowdSec Bouncer/'.self::VERSION;

Controller/Adminhtml/System/Config/Geolocation/Country.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
namespace CrowdSec\Bouncer\Controller\Adminhtml\System\Config\Geolocation;
2929

3030
use CrowdSec\Bouncer\Controller\Adminhtml\System\Config\Action;
31+
use CrowdSec\Bouncer\Registry\CurrentBouncer as RegistryBouncer;
3132
use Exception;
3233
use Magento\Backend\App\Action\Context;
3334
use Magento\Framework\App\Action\HttpPostActionInterface;
@@ -56,25 +57,33 @@ class Country extends Action implements HttpPostActionInterface
5657
/** @var DirectoryList */
5758
protected $directoryList;
5859

60+
/**
61+
* @var RegistryBouncer
62+
*/
63+
protected $registryBouncer;
64+
5965
/**
6066
* @param Context $context
6167
* @param JsonFactory $resultJsonFactory
6268
* @param Helper $helper
6369
* @param Geolocation $geolocation
6470
* @param DirectoryList $directoryList
71+
* @param RegistryBouncer $registryBouncer
6572
*/
6673
public function __construct(
6774
Context $context,
6875
JsonFactory $resultJsonFactory,
6976
Helper $helper,
7077
Geolocation $geolocation,
71-
DirectoryList $directoryList
78+
DirectoryList $directoryList,
79+
RegistryBouncer $registryBouncer
7280
) {
7381
parent::__construct($context);
7482
$this->resultJsonFactory = $resultJsonFactory;
7583
$this->helper = $helper;
7684
$this->geolocation = $geolocation;
7785
$this->directoryList = $directoryList;
86+
$this->registryBouncer = $registryBouncer;
7887
}
7988

8089
/**
@@ -85,15 +94,22 @@ public function __construct(
8594
public function execute(): Json
8695
{
8796
try {
97+
if (!($bouncer = $this->registryBouncer->get())) {
98+
$bouncer = $this->registryBouncer->create();
99+
}
100+
$configs = $this->helper->getBouncerConfigs();
101+
$bouncer = $bouncer->init($configs);
102+
$apiCache = $bouncer->getApiCache();
88103

89104
$type = $this->getRequest()->getParam('geolocation_type');
90105
$maxmindType = $this->getRequest()->getParam('geolocation_maxmind_database_type');
91106
$maxmindPath = $this->getRequest()->getParam('geolocation_maxmind_database_path');
92107
$forcedIP = $this->getRequest()->getParam('forced_test_ip');
108+
$saveResult = (bool) $this->getRequest()->getParam('save_result');
93109
$ip = !empty($forcedIP) ? $forcedIP : $this->helper->getRemoteIp();
94110
$geolocConfig = [
95111
'type' => $type,
96-
'save_in_session' => false,
112+
'save_result' => $saveResult,
97113
];
98114
if ($type === Constants::GEOLOCATION_TYPE_MAXMIND) {
99115
$geolocConfig['maxmind'] = [
@@ -102,7 +118,7 @@ public function execute(): Json
102118
];
103119
}
104120

105-
$countryResult = $this->geolocation->getCountryResult($geolocConfig, $ip);
121+
$countryResult = $this->geolocation->getCountryResult($geolocConfig, $ip, $apiCache);
106122
$countryMessage = null;
107123
$result = false;
108124
$message = __('Geolocation test result: failed.');

Helper/Config.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ class Config extends AbstractHelper
8686
public const XML_PATH_ADVANCED_CACHE_MEMCACHED_DSN = self::SECTION . '/advanced/cache/memcached_dsn';
8787
public const XML_PATH_ADVANCED_CACHE_CLEAN = self::SECTION . '/advanced/cache/clean_ip_cache_duration';
8888
public const XML_PATH_ADVANCED_CACHE_BAD = self::SECTION . '/advanced/cache/bad_ip_cache_duration';
89+
public const XML_PATH_ADVANCED_CACHE_CAPTCHA = self::SECTION . '/advanced/cache/captcha_cache_duration';
90+
public const XML_PATH_ADVANCED_CACHE_GEO = self::SECTION . '/advanced/cache/geolocation_cache_duration';
8991
public const XML_PATH_ADVANCED_DEBUG_LOG = self::SECTION . '/advanced/debug/log';
9092
public const XML_PATH_ADVANCED_DISPLAY_ERRORS = self::SECTION . '/advanced/debug/display_errors';
9193
public const XML_PATH_ADVANCED_DISABLE_PROD_LOG = self::SECTION . '/advanced/debug/disable_prod_log';
9294
public const XML_PATH_ADVANCED_FORCED_TEST_IP = self::SECTION . '/advanced/debug/forced_test_ip';
9395

9496
public const XML_PATH_ADVANCED_GEOLOCATION_ENABLED = self::SECTION . '/advanced/geolocation/enabled';
9597
public const XML_PATH_ADVANCED_GEOLOCATION_TYPE = self::SECTION . '/advanced/geolocation/type';
96-
public const XML_PATH_ADVANCED_GEOLOCATION_SAVE_SESSION = self::SECTION . '/advanced/geolocation/save_in_session';
98+
public const XML_PATH_ADVANCED_GEOLOCATION_SAVE_RESULT = self::SECTION . '/advanced/geolocation/save_result';
9799
public const XML_PATH_ADVANCED_GEOLOCATION_MAXMIND_DB_TYPE = self::SECTION .
98100
'/advanced/geolocation/maxmind_database_type';
99101
public const XML_PATH_ADVANCED_GEOLOCATION_MAXMIND_DB_PATH = self::SECTION .
@@ -140,6 +142,8 @@ class Config extends AbstractHelper
140142
'memcached_dsn' => null,
141143
'clean_ip_duration' => null,
142144
'bad_ip_duration' => null,
145+
'captcha_duration' => null,
146+
'geolocation_duration' => null,
143147
'trusted_forwarded_ip' => null,
144148
'geolocation' => null,
145149
];
@@ -344,8 +348,8 @@ public function getGeolocation(): array
344348
$result = ['enabled' => false];
345349
if ($this->scopeConfig->getValue(self::XML_PATH_ADVANCED_GEOLOCATION_ENABLED)) {
346350
$result['enabled'] = true;
347-
$result['save_in_session'] =
348-
(bool)$this->scopeConfig->getValue(self::XML_PATH_ADVANCED_GEOLOCATION_SAVE_SESSION);
351+
$result['save_result'] =
352+
(bool)$this->scopeConfig->getValue(self::XML_PATH_ADVANCED_GEOLOCATION_SAVE_RESULT);
349353
$type = (string)$this->scopeConfig->getValue(self::XML_PATH_ADVANCED_GEOLOCATION_TYPE);
350354
$result['type'] = $type;
351355
if ($type === Constants::GEOLOCATION_TYPE_MAXMIND) {
@@ -506,6 +510,38 @@ public function getBadIpCacheDuration(): int
506510
return (int) $this->_globals['bad_ip_duration'];
507511
}
508512

513+
/**
514+
* Get captcha cache duration config
515+
*
516+
* @return int
517+
*/
518+
public function getCaptchaCacheDuration(): int
519+
{
520+
if (!isset($this->_globals['captcha_duration'])) {
521+
$this->_globals['captcha_duration'] = (int)$this->scopeConfig->getValue(
522+
self::XML_PATH_ADVANCED_CACHE_CAPTCHA
523+
);
524+
}
525+
526+
return (int) $this->_globals['captcha_duration'];
527+
}
528+
529+
/**
530+
* Get geolocation cache duration config
531+
*
532+
* @return int
533+
*/
534+
public function getGeolocationCacheDuration(): int
535+
{
536+
if (!isset($this->_globals['geolocation_duration'])) {
537+
$this->_globals['geolocation_duration'] = (int)$this->scopeConfig->getValue(
538+
self::XML_PATH_ADVANCED_CACHE_GEO
539+
);
540+
}
541+
542+
return (int) $this->_globals['geolocation_duration'];
543+
}
544+
509545
/**
510546
* Get bouncing level config
511547
*

Helper/Data.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ public function getBouncerConfigs(): array
418418
'memcached_dsn' => $this->getMemcachedDSN(),
419419
'clean_ip_cache_duration' => $this->getCleanIpCacheDuration(),
420420
'bad_ip_cache_duration' => $this->getBadIpCacheDuration(),
421+
'captcha_cache_duration' => $this->getCaptchaCacheDuration(),
422+
'geolocation_cache_duration' => $this->getGeolocationCacheDuration(),
421423
// Geolocation
422424
'geolocation' => $this->getGeolocation(),
423425
// Extra configs

Model/Bouncer.php

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
use Psr\Cache\InvalidArgumentException;
3939

4040
/**
41-
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
41+
*
4242
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4343
*/
4444
class Bouncer extends AbstractBounce implements IBounce
@@ -49,11 +49,6 @@ class Bouncer extends AbstractBounce implements IBounce
4949
*/
5050
protected $response;
5151

52-
/**
53-
* @var Session
54-
*/
55-
protected $session;
56-
5752
/**
5853
* @var Helper
5954
*/
@@ -77,20 +72,17 @@ class Bouncer extends AbstractBounce implements IBounce
7772
* Constructor
7873
*
7974
* @param Http $response
80-
* @param Session $session
8175
* @param Helper $helper
8276
* @param CacheFactory $cacheFactory
8377
* @param BouncerFactory $bouncerInstanceFactory
8478
*/
8579
public function __construct(
8680
Http $response,
87-
Session $session,
8881
Helper $helper,
8982
CacheFactory $cacheFactory,
9083
BouncerFactory $bouncerInstanceFactory
9184
) {
9285
$this->response = $response;
93-
$this->session = $session;
9486
$this->helper = $helper;
9587
$this->cacheFactory = $cacheFactory;
9688
$this->bouncerInstanceFactory = $bouncerInstanceFactory;
@@ -152,6 +144,7 @@ public function getBouncerInstance(array $settings = [], bool $forceReload = fal
152144
}
153145

154146
try {
147+
/** @var BouncerInstance $bouncerInstance */
155148
$bouncerInstance =
156149
$this->bouncerInstanceFactory->create(
157150
['cacheAdapter' => $cacheAdapter, 'logger' => $this->logger ]
@@ -181,6 +174,8 @@ public function getBouncerInstance(array $settings = [], bool $forceReload = fal
181174
'memcached_dsn' => $settings['memcached_dsn'],
182175
'clean_ip_cache_duration' => $settings['clean_ip_cache_duration'],
183176
'bad_ip_cache_duration' => $settings['bad_ip_cache_duration'],
177+
'captcha_cache_duration' => $settings['captcha_cache_duration'],
178+
'geolocation_cache_duration' => $settings['geolocation_cache_duration'],
184179
// Geolocation
185180
'geolocation' => $settings['geolocation']
186181
]);
@@ -270,50 +265,6 @@ public function getTrustForwardedIpBoundsList(): array
270265
return $this->helper->getTrustedForwardedIps();
271266
}
272267

273-
/**
274-
* Get session variable value
275-
*
276-
* @param string $name
277-
* @return mixed
278-
*/
279-
public function getSessionVariable(string $name)
280-
{
281-
return $this->session->getData($name);
282-
}
283-
284-
/**
285-
* Get all session variables
286-
*
287-
* @return mixed
288-
*/
289-
public function getAllSessionVariables()
290-
{
291-
return $this->session->getData();
292-
}
293-
294-
/**
295-
* Set a session variable.
296-
*
297-
* @param string $name
298-
* @param mixed $value
299-
* @return void
300-
*/
301-
public function setSessionVariable(string $name, $value): void
302-
{
303-
$this->session->setData($name, $value);
304-
}
305-
306-
/**
307-
* Unset a session variable, throw an error if this does not exist.
308-
*
309-
* @param string $name
310-
* @return void
311-
*/
312-
public function unsetSessionVariable(string $name): void
313-
{
314-
$this->session->unsetData($name);
315-
}
316-
317268
/**
318269
* Get the value of a posted field.
319270
*

Model/Cache.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@
2727

2828
namespace CrowdSec\Bouncer\Model;
2929

30+
use CrowdSec\Bouncer\Exception\CrowdSecException;
31+
use CrowdSec\Bouncer\Constants;
32+
use CrowdSecBouncer\Fixes\Memcached\TagAwareAdapter as MemcachedTagAwareAdapter;
3033
use ErrorException;
3134
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
3235
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
3336
use Symfony\Component\Cache\Adapter\RedisAdapter;
37+
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
38+
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
3439
use Symfony\Component\Cache\Exception\CacheException;
3540
use Symfony\Component\Cache\Exception\InvalidArgumentException;
36-
use CrowdSec\Bouncer\Exception\CrowdSecException;
37-
use CrowdSec\Bouncer\Constants;
3841

3942
class Cache
4043
{
@@ -65,7 +68,7 @@ public function getAdapter(
6568
$cacheSystem = $forcedCacheSystem ?: $cacheSystem;
6669
switch ($cacheSystem) {
6770
case Constants::CACHE_SYSTEM_PHPFS:
68-
$cacheAdapterInstance = new PhpFilesAdapter('', 0, $fsCachePath);
71+
$cacheAdapterInstance = new TagAwareAdapter(new PhpFilesAdapter('', 0, $fsCachePath));
6972
break;
7073

7174
case Constants::CACHE_SYSTEM_MEMCACHED:
@@ -75,7 +78,9 @@ public function getAdapter(
7578
' Please set a Memcached DSN or select another cache technology.'
7679
);
7780
}
78-
$cacheAdapterInstance = new MemcachedAdapter(MemcachedAdapter::createConnection($memcachedDsn));
81+
$cacheAdapterInstance = new MemcachedTagAwareAdapter(
82+
new MemcachedAdapter(MemcachedAdapter::createConnection($memcachedDsn))
83+
);
7984
break;
8085
case Constants::CACHE_SYSTEM_REDIS:
8186
if (empty($redisDsn)) {
@@ -84,7 +89,7 @@ public function getAdapter(
8489
}
8590

8691
try {
87-
$cacheAdapterInstance = new RedisAdapter(RedisAdapter::createConnection($redisDsn));
92+
$cacheAdapterInstance = new RedisTagAwareAdapter(RedisAdapter::createConnection($redisDsn));
8893
} catch (InvalidArgumentException $e) {
8994
throw new CrowdSecException('Error when connecting to Redis.' .
9095
' Please fix the Redis DSN or select another cache technology.');

0 commit comments

Comments
 (0)