File tree Expand file tree Collapse file tree 8 files changed +106
-3
lines changed
Expand file tree Collapse file tree 8 files changed +106
-3
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,10 @@ class Config
1717 * string
1818 */
1919 protected const WEBAPI_LOGS_IS_ENABLED_CONFIG_PATH = 'webapi_logs/log/enabled ' ;
20+ /**
21+ * string
22+ */
23+ protected const WEBAPI_LOGS_LOG_SECRET_MODE = 'webapi_logs/log/secret_mode ' ;
2024
2125 /**
2226 * @var ScopeConfigInterface
@@ -42,4 +46,15 @@ public function isEnabled(): bool
4246 ScopeInterface::SCOPE_WEBSITE
4347 );
4448 }
49+
50+ /**
51+ * @return bool
52+ */
53+ public function isSecretMode (): bool
54+ {
55+ return $ this ->scopeConfig ->isSetFlag (
56+ self ::WEBAPI_LOGS_LOG_SECRET_MODE ,
57+ ScopeInterface::SCOPE_WEBSITE
58+ );
59+ }
4560}
Original file line number Diff line number Diff line change 1414
1515class Code implements OptionSourceInterface
1616{
17- private LogCollectionFactory $ logCollectionFactory ;
17+ private $ logCollectionFactory ;
1818
1919 /**
2020 * @param LogCollectionFactory $logCollectionFactory
Original file line number Diff line number Diff line change 1414
1515class Methods implements OptionSourceInterface
1616{
17- private LogCollectionFactory $ logCollectionFactory ;
17+ private $ logCollectionFactory ;
1818
1919 /**
2020 * @param LogCollectionFactory $logCollectionFactory
Original file line number Diff line number Diff line change 1414
1515class RequestorIp implements OptionSourceInterface
1616{
17- private LogCollectionFactory $ logCollectionFactory ;
17+ private $ logCollectionFactory ;
1818
1919 /**
2020 * @param LogCollectionFactory $logCollectionFactory
Original file line number Diff line number Diff line change @@ -31,19 +31,35 @@ class LogHandle
3131 */
3232 private $ logger ;
3333
34+ /**
35+ * @var Config
36+ */
37+ private $ config ;
38+
39+ /**
40+ * @var SecretParser
41+ */
42+ private $ secretParser ;
43+
3444 /**
3545 * @param LogFactory $logFactory
3646 * @param LogResourceModel $logResourceModel
47+ * @param SecretParser $secretParser
48+ * @param Config $config
3749 * @param LoggerInterface $logger
3850 */
3951 public function __construct (
4052 LogFactory $ logFactory ,
4153 LogResourceModel $ logResourceModel ,
54+ SecretParser $ secretParser ,
55+ Config $ config ,
4256 LoggerInterface $ logger
4357 ) {
4458 $ this ->logFactory = $ logFactory ;
4559 $ this ->logResourceModel = $ logResourceModel ;
60+ $ this ->config = $ config ;
4661 $ this ->logger = $ logger ;
62+ $ this ->secretParser = $ secretParser ;
4763 }
4864
4965 /**
@@ -64,6 +80,14 @@ public function before(
6480 ) {
6581 try {
6682 $ newLog = $ this ->logFactory ->create ();
83+
84+ if ($ this ->config ->isSecretMode ()) {
85+ $ requestorIp = $ this ->secretParser ->ipParser ();
86+ $ requestHeaders = $ this ->secretParser ->headersParser ($ requestHeaders );
87+ $ requestBody = $ this ->secretParser ->bodyParser ($ requestBody );
88+ $ requestPath = $ this ->secretParser ->pathParser ($ requestPath );
89+ }
90+
6791 $ newLog ->setData ([
6892 'request_method ' => $ requestMethod ,
6993 'requestor_ip ' => $ requestorIp ,
Original file line number Diff line number Diff line change 1+ <?php
2+ /*
3+ * Copyright © Ghost Unicorns snc. All rights reserved.
4+ * See LICENSE for license details.
5+ */
6+
7+ declare (strict_types=1 );
8+
9+ namespace GhostUnicorns \WebapiLogs \Model ;
10+
11+
12+ class SecretParser
13+ {
14+ /**
15+ * @param string $requestBody
16+ * @return string
17+ */
18+ public function bodyParser (string $ requestBody ): string
19+ {
20+ $ result = $ requestBody ;
21+ return $ result ;
22+ }
23+
24+ /**
25+ * @param string $requestHeaders
26+ * @return string
27+ */
28+ public function headersParser (string $ requestHeaders ): string
29+ {
30+ $ result = preg_replace ('/Cookie:(.*)/ ' , 'Cookie: ******** ' , $ requestHeaders );
31+ $ result = preg_replace ('/User-Agent:(.*)/ ' , 'User-Agent: ******** ' , $ result );
32+ $ result = preg_replace ('/Authorization:(.*)/ ' , 'Authorization: ******** ' , $ result );
33+ return preg_replace ('/Host:(.*)/ ' , 'Host: ******** ' , $ result );
34+ }
35+
36+ /**
37+ * @return string
38+ */
39+ public function ipParser (): string
40+ {
41+ return '***.***.***.*** ' ;
42+ }
43+
44+ /**
45+ * @param string $requestPath
46+ * @return string
47+ */
48+ public function pathParser (string $ requestPath ): string
49+ {
50+ $ segments = parse_url ($ requestPath );
51+
52+ if (array_key_exists ('path ' , $ segments )) {
53+ return $ segments ['path ' ];
54+ }
55+
56+ return $ requestPath ;
57+ }
58+ }
Original file line number Diff line number Diff line change 1717 <label >Enable Webapi Logs</label >
1818 <source_model >Magento\Config\Model\Config\Source\Yesno</source_model >
1919 </field >
20+ <field id =" secret_mode" translate =" label" type =" select" sortOrder =" 10" showInDefault =" 1" showInWebsite =" 0" showInStore =" 0" >
21+ <label >Enable Secret Mode</label >
22+ <source_model >Magento\Config\Model\Config\Source\Yesno</source_model >
23+ <comment ><![CDATA[ If enabled, the module will not save any sensitive data, showing only secure logs.]]> </comment >
24+ </field >
2025 </group >
2126 </section >
2227 </system >
Original file line number Diff line number Diff line change 1010 <webapi_logs >
1111 <log >
1212 <enabled >1</enabled >
13+ <secret_mode >1</secret_mode >
1314 </log >
1415 </webapi_logs >
1516 </default >
You can’t perform that action at this time.
0 commit comments