Calling CLICommandInvoker.invoke() changes Jenkins authorization as a side effect which means that many operations against Jenkins model failed later.
It is a quite common to prepare data in Jenkins instance, call CLI command and verify the state of Jenkins model later.
- in CLICommandInvoker.setAuth() save current SecurityRealm, AuthorizationStrategy and thread's SecurityContext
- in CLICommandInvoker.invoke() after result = new Result(returnCode, out, err); call a new method restoreAuth()
- implement a new method CLICommandInvoker.restoreAuth() where SecurityRealm, AuthorizationStrategy and thread's SecurityContext is set to previously saved values