Skip to content
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

Logging the code run via psysh #651

Open
Riimu opened this issue Aug 26, 2020 · 2 comments
Open

Logging the code run via psysh #651

Riimu opened this issue Aug 26, 2020 · 2 comments

Comments

@Riimu
Copy link

Riimu commented Aug 26, 2020

We currently use psysh in production to do custom debugging and sometimes run one off scripts for mini migrations or gathering data via our framework.

However, since people can essentially run anything via the console, it would be really nice to be able to get some kind of audit log of what people have actually run via the console and especially if some sort of accidents happen.

I tried to go through the documentation, but couldn't find any way to currently achieve this.

So, would it be possible to implement, for example, some kind of configuration option like logCallback, that could take a callback as a value that is simply called for each piece of code that is run via psysh?

@IonBazan
Copy link

IonBazan commented Oct 28, 2020

@Riimu You can use loop listeners to achieve that but it requires you to extend PsySH Shell class. See example:

<?php
use Psy\ExecutionLoop\AbstractListener;
use Psy\Shell;

class LoggerLoopListener extends AbstractListener
{
    public function onExecute(Shell $shell, $code)
    {
        print $code;
    }

    public static function isSupported()
    {
        return true;
    }
}

class MyShell extends Shell
{
    protected function getDefaultLoopListeners()
    {
        $listeners = parent::getDefaultLoopListeners();
        $listeners[] = new LoggerLoopListener();

        return $listeners;
    }
}

Then create an instance of your shell and run it:

(new MyShell())->run();

Please note that you can easily override other methods from AbstractListener to log the session start and end too. It's possible to add user context to the logs too - just ask the developer about his name before starting the shell session, pass it to your LoggerLoopListener and log it together with the code executed.

This is actually related to #565 because there is currently no way to customize loop listeners without extending the Shell class and overriding getDefaultLoopListeners() method.

@bobthecow
Copy link
Owner

People can run essentially anything through their shell access, too, right? Would it be better to instrument input at that level rather than just inside PsySH?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants