Skip to content

Commit

Permalink
Merge pull request #9 from AmpersandHQ/add-admin-ui-button-to-get-key
Browse files Browse the repository at this point in the history
Add admin panel interface to see the verbose log key
  • Loading branch information
convenient authored Nov 21, 2023
2 parents 49764e3 + 4f82122 commit 2fb5f09
Show file tree
Hide file tree
Showing 22 changed files with 495 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ script:
- composer run test-static-analysis
- composer run test:unit
# Install magento
- CURRENT_EXTENSION="." vendor/bin/mtest-make $TEST_GROUP
- CURRENT_EXTENSION="." COMPOSER_MODULE_SYMLINK=0 UNIT_TESTS_PATH='dev/Test/Unit' INTEGRATION_TESTS_PATH='dev/Test/Integration' vendor/bin/mtest-make $TEST_GROUP
# Ensure magento composer installer is creating the di.xml file as necessary
- vendor/bin/mtest 'test -f app/etc/di.xml_ampersand_magento2_verbose_log_request/di.xml'
# Integration tests
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ Update your `.gitignore` to ignore
app/etc/di.xml_ampersand_magento2_verbose_log_request/di.xml
```

If you want to give certain admin users permissions to get the key via the `Admin Panel -> Account Settings -> Get Verbose Log Key` create `app/etc/di.xml_ampersand_magento2_verbose_log_request/allowed_emails_di.xml` and define the allowed emails/domains.

```xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails">
<arguments>
<!-- It is recommended to allow specific emails -->
<argument name="allowedEmails" xsi:type="array">
<item name="1" xsi:type="string">[email protected]</item>
</argument>
<!-- There is support for whitelisting a whole domain, but this is less strict than the above -->
<argument name="allowedDomains" xsi:type="array">
<item name="1" xsi:type="string">secondexample.com</item>
</argument>
</arguments>
</type>
</config>
```

## Security considerations

As all we are doing is writing to the log files the biggest "risk" is to your disk space.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"phpunit/phpunit": "^9.5"
},
"extra": {
"magento-deploystrategy": "none",
"magento-deploy-ignore": {
"*": [
"/"
Expand Down
75 changes: 75 additions & 0 deletions dev/Test/Integration/Adminhtml/AccountControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Ampersand\VerboseLogRequest\Test\Integration\Adminhtml;

use Magento\TestFramework\Helper\Bootstrap;

class AccountControllerTest extends \Magento\TestFramework\TestCase\AbstractBackendController
{
public function testNotAllowed()
{
$allowedEmails = Bootstrap::getObjectManager()
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class);
$allowedEmails->__construct([], []); // no allowed emails or domains
$this->dispatch('backend/admin/system_account/index');
$this->assertStringContainsString('My Account', $this->getResponse()->getBody());
$this->assertStringNotContainsString('Get Verbose Log Key', $this->getResponse()->getBody());
}

public function testAllowedEmail()
{
$emails = [
'[email protected]',
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109
'[email protected]',
'[email protected]',
];
$allowedEmails = Bootstrap::getObjectManager()
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class);
$allowedEmails->__construct($emails, []);
$this->dispatch('backend/admin/system_account/index');
$this->assertStringContainsString('My Account', $this->getResponse()->getBody());
$this->assertStringContainsString('Get Verbose Log Key', $this->getResponse()->getBody());
}

public function testAllowedDomain()
{
$domains = [
'email.com',
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109
'@example.com',
'example.com',
'foobar.com'
];
$allowedEmails = Bootstrap::getObjectManager()
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class);
$allowedEmails->__construct([], $domains);
$this->dispatch('backend/admin/system_account/index');
$this->assertStringContainsString('My Account', $this->getResponse()->getBody());
$this->assertStringContainsString('Get Verbose Log Key', $this->getResponse()->getBody());
}

public function testAllowedEmailAndDomain()
{
$emails = [
'[email protected]',
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109
'[email protected]',
'[email protected]',
];
$domains = [
'email.com',
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109
'@example.com',
'example.com',
'foobar.com'
];

$allowedEmails = Bootstrap::getObjectManager()
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class);
$allowedEmails->__construct($emails, $domains);
$this->dispatch('backend/admin/system_account/index');
$this->assertStringContainsString('My Account', $this->getResponse()->getBody());
$this->assertStringContainsString('Get Verbose Log Key', $this->getResponse()->getBody());
}
}
74 changes: 74 additions & 0 deletions dev/Test/Integration/Adminhtml/GetControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Ampersand\VerboseLogRequest\Test\Integration\Adminhtml;

use Magento\TestFramework\Helper\Bootstrap;

class GetControllerTest extends \Magento\TestFramework\TestCase\AbstractBackendController
{
public function testNotAllowed()
{
$allowedEmails = Bootstrap::getObjectManager()
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class);
$allowedEmails->__construct([], []); // no allowed emails or domains
$this->dispatch('backend/ampersandverboselogrequest/key/get');
$this->assertRedirect($this->stringContains('admin/dashboard/index'));
}

public function testAllowedEmail()
{
$emails = [
'[email protected]',
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109
'[email protected]',
'[email protected]',
];
$allowedEmails = Bootstrap::getObjectManager()
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class);
$allowedEmails->__construct($emails, []);
$this->dispatch('backend/ampersandverboselogrequest/key/get');
$this->assertStringContainsString('The current key is', $this->getResponse()->getBody());
$this->assertStringContainsString('The current key will expire at', $this->getResponse()->getBody());
}

public function testAllowedDomain()
{
$domains = [
'email.com',
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109
'@example.com',
'example.com',
'foobar.com'
];
$allowedEmails = Bootstrap::getObjectManager()
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class);
$allowedEmails->__construct([], $domains);
$this->dispatch('backend/ampersandverboselogrequest/key/get');
$this->assertStringContainsString('The current key is', $this->getResponse()->getBody());
$this->assertStringContainsString('The current key will expire at', $this->getResponse()->getBody());
}

public function testAllowedEmailAndDomain()
{
$emails = [
'[email protected]',
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109
'[email protected]',
'[email protected]',
];
$domains = [
'email.com',
// https://github.com/AmpersandHQ/magento-docker-test-instance/blob/ec8b3cf09d286e19f01b40e3a09d5e17d65a7edc/Dockerfile-assets/magento-install.sh#L109
'@example.com',
'example.com',
'foobar.com'
];

$allowedEmails = Bootstrap::getObjectManager()
->get(\Ampersand\VerboseLogRequest\Service\Adminhtml\AllowedEmails::class);
$allowedEmails->__construct($emails, $domains);
$this->dispatch('backend/ampersandverboselogrequest/key/get');
$this->assertStringContainsString('The current key is', $this->getResponse()->getBody());
$this->assertStringContainsString('The current key will expire at', $this->getResponse()->getBody());
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<testsuites>
<testsuite name="Ampersand Module Unit Test Suite">
<directory>src/Test/Unit</directory>
<directory>dev/Test/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
74 changes: 74 additions & 0 deletions src/Block/Adminhtml/GetKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Ampersand\VerboseLogRequest\Block\Adminhtml;

use Ampersand\VerboseLogRequest\Service\Adminhtml\UserChecker;
use Ampersand\VerboseLogRequest\Service\GetKey as GetKeyService;
use Magento\Framework\View\Element\Template;

class GetKey extends Template
{
/**
* @var UserChecker
*/
private UserChecker $userChecker;

/**
* @var string
*/
private string $timestamp;

/**
* @var string|false
*/
private $key;

/**
* @param Template\Context $context
* @param GetKeyService $getKey
* @param UserChecker $userChecker
* @param mixed[] $data
*/
public function __construct(
Template\Context $context,
GetKeyService $getKey,
UserChecker $userChecker,
array $data = []
) {
parent::__construct($context, $data);
$this->userChecker = $userChecker;

$this->key = $getKey->execute();
$this->timestamp = date('Y-m-d H', strtotime('now +1 hour')) . ':00:00';
}

/**
* Is the current admin user allowed to acces the key
*
* @return bool
*/
public function isAllowed()
{
return $this->userChecker->isAdminUserAllowed();
}

/**
* Get the current verbose log key
*
* @return false|string
*/
public function getKey()
{
return $this->key;
}

/**
* Get the timestamp that the current key expires
*
* @return string
*/
public function getTimestamp()
{
return $this->timestamp;
}
}
63 changes: 63 additions & 0 deletions src/Controller/Adminhtml/Key/Get.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Ampersand\VerboseLogRequest\Controller\Adminhtml\Key;

use Ampersand\VerboseLogRequest\Service\Adminhtml\UserChecker;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\View\Result\PageFactory;

class Get extends Action implements HttpGetActionInterface
{
/**
* @var PageFactory
*/
private PageFactory $pageFactory;

/**
* @var Redirect
*/
protected Redirect $resultRedirect;

/**
* @var UserChecker
*/
private UserChecker $userChecker;

/**
* @param Context $context
* @param UserChecker $userChecker
* @param PageFactory $pageFactory
* @param Redirect $redirect
*/
public function __construct(
Context $context,
UserChecker $userChecker,
PageFactory $pageFactory,
Redirect $redirect
) {
parent::__construct($context);
$this->userChecker = $userChecker;
$this->pageFactory = $pageFactory;
$this->resultRedirect = $redirect;
}

/**
* Display the Verbose Log Request Key to the allowed admin user
*
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
if (!$this->userChecker->isAdminUserAllowed()) {
return $this->resultRedirect->setPath('admin/dashboard/index');
}

$resultPage = $this->pageFactory->create();
// @phpstan-ignore-next-line
$resultPage->getConfig()->getTitle()->prepend(__('Ampersand Verbose Log Request Key'));
return $resultPage;
}
}
Loading

0 comments on commit 2fb5f09

Please sign in to comment.