Skip to content

Added URL filter #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 4, 2025
2 changes: 1 addition & 1 deletion Model/Clean.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function execute()
$collection = $this->logCollectionFactory->create();
$collection = $collection->addFieldToSelect(LogResourceModel::LOG_ID)
->addFieldToFilter(LogResourceModel::CREATED_AT, ['lt' => $datetime])
->setPageSize(2);
->setPageSize(1000);

$pageCount = $collection->getLastPageNumber();
$currentPage = 1;
Expand Down
31 changes: 31 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Config
*/
protected const WEBAPI_LOGS_IS_ENABLED_CONFIG_PATH = 'webapi_logs/log/enabled';


protected const WEBAPI_LOGS_DISABLED_AJAX_CALLS = 'webapi_logs/log/disable_ajax_calls';
/**
* string
*/
Expand All @@ -33,6 +35,11 @@ class Config
*/
protected const WEBAPI_LOGS_LOG_CLEAN_OLDER_THAN_HOURS = 'webapi_logs/log/clean_older_than_hours';

/**
* @var string
*/
protected const WEBAPI_LOGS_LOG_FILTER_REQUEST_PATHS = 'webapi_logs/log/filter_request_paths';

/**
* @var ScopeConfigInterface
*/
Expand All @@ -58,6 +65,15 @@ public function isEnabled(): bool
);
}

public function isAjaxCallsDisabled(): bool
{
return $this->scopeConfig->isSetFlag(
self::WEBAPI_LOGS_DISABLED_AJAX_CALLS,
ScopeInterface::SCOPE_WEBSITE
);
}


/**
* @return bool
*/
Expand Down Expand Up @@ -91,4 +107,19 @@ public function getCleanOlderThanHours(): int
ScopeInterface::SCOPE_WEBSITE
);
}

/**
* @return array
*/
public function getFilterRequestPaths(): array
{
$value = $this->scopeConfig->getValue(
self::WEBAPI_LOGS_LOG_FILTER_REQUEST_PATHS,
ScopeInterface::SCOPE_WEBSITE
);
if ($value == null) {
return [];
}
return preg_split('/\n|\r\n?/', $value);
}
}
60 changes: 41 additions & 19 deletions Model/LogHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,26 @@ public function before(
string $requestBody,
string $requestDateTime
) {
try {
if ($this->config->isSecretMode()) {
$requestorIp = $this->secretParser->parseIp();
$requestHeaders = $this->secretParser->parseHeades($requestHeaders);
$requestBody = $this->secretParser->parseBody($requestBody);
}
$filter = $this->config->getFilterRequestPaths();

// Only log this request if the path is not filtered.
if (!$this->filterRequestPath($requestPath, $filter)) {
if ($this->config->isSecretMode()) {
$requestorIp = $this->secretParser->parseIp();
$requestHeaders = $this->secretParser->parseHeades($requestHeaders);
$requestBody = $this->secretParser->parseBody($requestBody);
}

$log = $this->logFactory->create();
$log->setData([
'request_method' => $requestMethod,
'requestor_ip' => $requestorIp,
'request_url' => $requestPath,
'request_headers' => $requestHeaders,
'request_body' => $requestBody,
'request_datetime' => $requestDateTime
]);
$this->logResourceModel->save($log);
$this->lastLog = $log;
} catch (Exception $exception) {
$this->logger->error(__('Cant complete webapi log save because of error: %1', $exception->getMessage()));
$log = $this->logFactory->create();
$log->setData([
'request_method' => $requestMethod,
'requestor_ip' => $requestorIp,
'request_url' => $requestPath,
'request_headers' => $requestHeaders,
'request_body' => $requestBody,
'request_datetime' => $requestDateTime
]);
$this->lastLog = $log;
}
}

Expand Down Expand Up @@ -128,4 +128,26 @@ public function after(
$this->logger->error(__('Cant complete webapi log save because of error: %1', $exception->getMessage()));
}
}

/**
* Check if request path is among the filters.
*
* @param string $requestPath
* @param array $filters
*
* @return bool
*/
private function filterRequestPath(
string $requestPath,
array $filters
): bool {
foreach ($filters as $filter) {
if ($filter != '') {
if (stripos($requestPath, $filter) !== false) {
return true;
}
}
}
return false;
}
}
7 changes: 6 additions & 1 deletion Plugin/FrontControllerDispatchAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public function __construct(
*/
public function afterDispatch(Rest $subject, $result, RequestInterface $request)
{
if ($this->config->isEnabled()) {
if ($this->config->isEnabled()
&& (
!$request->isXmlHttpRequest()
|| !$this->config->isAjaxCallsDisabled()
)
) {
$exceptions = $result->getException();

if (!empty($exceptions)) {
Expand Down
7 changes: 6 additions & 1 deletion Plugin/FrontControllerDispatchBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ public function __construct(
*/
public function beforeDispatch(Rest $subject, RequestInterface $request)
{
if ($this->config->isEnabled()) {
if ($this->config->isEnabled()
&& (
!$request->isXmlHttpRequest()
|| !$this->config->isAjaxCallsDisabled()
)
) {
$requestMethod = $request->getMethod();
$requestorIp = $request->getClientIp();
$requestPath = $request->getUriString();
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This module allows you to analyze all the webapi rest done call toward your mage

# Install

`composer require ghostunicorns/module-webapi-logs`
`composer require youwe/module-webapi-logs`

# Configure

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ghostunicorns/module-webapi-logs",
"name": "youwe/module-webapi-logs",
"description": "Magento Webapi Log Details accessible via backend",
"type": "magento2-module",
"require": {
Expand Down
12 changes: 12 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<label>Enable Webapi Logs</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="disable_ajax_calls" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Disable Webapi logs for AJAX calls</label>
<comment><![CDATA[Ajax calls are used in cases of the checkout for example]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="secret_mode" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Enable Secret Mode</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand All @@ -39,6 +44,13 @@
</depends>
<validate>validate-number validate-greater-than-zero</validate>
</field>
<field id="filter_request_paths" translate="label" type="textarea" sortOrder="50" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Filter request paths</label>
<depends>
<field id="webapi_logs/log/enabled">1</field>
</depends>
<comment><![CDATA[A list of request paths that should not be logged, separated by a simple new line]]></comment>
</field>
</group>
</section>
</system>
Expand Down
3 changes: 3 additions & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
<index referenceId="WEBAPILOGS_ENTITY_ID_ENTITY_ID" indexType="btree">
<column name="log_id"/>
</index>
<index referenceId="WEBAPILOGS_CREATED_AT_CREATED_AT" indexType="btree">
<column name="created_at"/>
</index>
</table>
</schema>