r/PowerShell • • Nov 20 '19

Opinion - The Microsoft Partner Center Powershell Module is not fit for purpose for delegated administration

For those who don't know, a number of months ago Microsoft enforced a number of security requirements on MS Partners to enforce two-factor security.

Two-factor authentication is an absolute must for partners AND our users/clients - this is something I will openly champion. Unfortunately, Microsoft rushed in to this head first without appropriately preparing their own Powershell modules for delegated administration. Upon implementation we were left in a position where we had to scramble to re-write existing scripts to make them work. We completely lost all of our centralised security monitoring for clients Exchange tenants because Microsoft had zero way at the time of performing delegated administration through Powershell.

I know outside of our own experience that huge vendors struggled to implement the model too with some of their Office 365 integrations that relied on Powershell.

I am not a Powershell pro, but I am not a Powershell noob either but I find the Secure App Model extremely difficult to implement in Powershell. I know others do too because the Microsoft Partner Community Forums is full of MS Partners who are having the same difficulties in consuming it.

To get an indication of the difficulty in consuming it, see here for a script on how to start setting it up. https://www.cyberdrain.com/connect-to-exchange-online-automated-when-mfa-is-enabled-using-the-secureapp-model/ . The methods that partners have to go through are not intuitive in the slightest.

To make it even harder the Refresh Token that you eventually get that allows you to perform delegated administration expires every 90 days and there are only two ways to get a new one:

  1. It generates a new refresh token each time you request a new token, leaving you with two tokens which means you need to find a way to update the existing token that you have stored somewhere

  2. If for whatever reason that initial token expires you need to request a new one - that's an interactive process that you need to go through each time with no way to automate it.

To outline how ridiculous it is, look at what steps are required for me as a partner to get some mailbox information out for my clients using Powershell.

  1. Generate a new App in Azure with explicit permissions

  2. Generate and then store credentials that can then interact with the app, an AppID and a Key which is essentially the password

  3. Request a token from the PartnerCenter Module that then becomes a refresh token for that Partner App (this requires an interactive login)

  4. Request a different token that becomes your Exchange Refresh token (this requires an interactive login)

  5. Store all of the above securely on the machine you are running these on

  6. Get an Azure Active Directory graph token using the credentials generated in step 2 and the refresh token generated in step 3

  7. Get a standard MS Graph token using the credentials generated in step 2 and the refresh token generated in step 3

  8. Connect to the MSOnline Powershell module to get a list of your clients and their tenant IDs using the aadGraphToken and the MsGraphToken.

  9. Loop through each customer and then...

  10. For each customer request a specific Exchange Refresh Token for that particular customer using the Partner Center Module Powershell

  11. Manipulate the token to be able to facilitate generating a New-PSSession for which you need to send in your manipulated token that you've had to prefix and manipulate with Bearer to get to work

  12. Get-Mailbox

  13. Remove the session and loop back to 9.

To keep this running automated too, in the main commands each time a refresh token is issued you need to update any existing tokens you get stored. If you are storing the credentials appropriately and want to run this on different machines you have to go through the entire process on each machine because it's a different refreshtoken each time.

In my opinion this is FAR too difficult and short of providing better security for partners it will simply encourage the ones that care a bit less to adopt security practices that are FAR worse like embedding credentials. I'd never do that, but there are plenty of crappy MSPs that will.

To top this off, a number of these cmdlets don't even work properly. I have two Github issues open right now for inherent problems that exist in the modules. Imagine how hard this is when you are requesting tokens and it only provides a refresh token 10% of the time you run the Cmdlet. Imagine the difficulty when certain commands that you run with your delegated tokens just error out, not with reasonable errors but with errors that represent the module is actually crashing. Imagine them changing how the Partner Center Module worked from 1.5 to 2 with them implementing changes that break existing scripts.

But hey, they released V2 of the Exchange Online Powershell Module! Great! https://docs.microsoft.com/en-gb/powershell/module/exchange/powershell-v2-module/connect-exchangeonline?view=exchange-ps Oh wait... it doesn't work with Delegated Administration at all at the moment.

I apologise if this comes across a bit ranty, but I wanted to draw attention to how messed up the situation is at present.

116 Upvotes

Duplicates