Azure / azure-cli

Azure Command-Line Interface
MIT License
3.97k stars 2.95k forks source link

possibility of adding service principal owners with the cli #9250

Open mariojacobo opened 5 years ago

mariojacobo commented 5 years ago

"az ad sp owner add" would be nice to have. we currently add owners as a manual step after the environment build completes. Is there something similar in the CLI package ?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

jurjenoskam commented 5 years ago

It seems this should have been part of microsoftgraph/microsoft-graph-docs#7578, which says it "Graph: support add/remove/list owners on app, sp, and group". The PR did this for app and group, but appears to have forgotten to include code for sp. Looking at the commits in that PR it only removes a comment under "ad sp owner": "# TODO: Add support for 'add' and 'remove'", but doesn't add code to actually add and remove owners from service principals.

yugangw-msft commented 5 years ago

Okay, i will follow up to onboard the support since we have the ask now.

jonaspetersorensen commented 5 years ago

Any progress on this?

yugangw-msft commented 5 years ago

The related API is missing in the spec. Before it gets fixed, you can use az rest. A bit more detail is needed from this command; otherwise it is just like other ones:

az rest --method post --uri https://graph.windows.net/<tenantId>/servicePrincipals/<object id of the service principal>/$links/owners?api-version=1.6 --body "{\"url\":\"https://graph.windows.net/<tenantId>/directoryObjects/<owner's object id>\"}"
miicahjardine commented 5 years ago

Yugangw-msft, I don't suppose you know the az rest command to remove a owner??

kautsig commented 4 years ago

I ran into the same issue of having to add an additional owner to an existing SP.

Unfortunately the API responds with "bad request": Unsupported resource type 'DirectoryObject' for operation 'Create'.

My first suspicion was a permission problem, but I would expect a proper response then. Any ideas?

pgroene commented 4 years ago

@yugangw-msft

The related API is missing in the spec. Before it gets fixed, you can use az rest. A bit more detail is needed from this command; otherwise it is just like other ones:

az rest --method post --uri https://graph.windows.net/<tenantId>/servicePrincipals/<object id of the service principal>/$links/owners?api-version=1.6 --body "{\"url\":\"https://graph.windows.net/<tenantId>/directoryObjects/<owner's object id>\"}"

I tried this, but the owners are not added if the call succeeds.

When is the release for the cli ad sp scheduled to be released?

dekimsey commented 4 years ago

Yugangw-msft, I don't suppose you know the az rest command to remove a owner??

@miicahjardine, I've found the following works to delete owners:

az rest --method=delete --uri=https://graph.windows.net/<tenantId>/servicePrincipals/<object id of the service principal>/$links/owners/<owner object id>?api-version=1.6
yonzhan commented 4 years ago

Can we close this issue?

trvsmtchll commented 4 years ago

Is this fixed I tried using "az rest ..." and also got the "Unsupported resource type 'DirectoryObject' for operation 'Create'." error.

drdamour commented 4 years ago

for whatever reason i'm getting the Unsupported resource type in powershell...but in cmd.exe it works fine.

pwsh equiv:

az rest --method post --uri https://graph.windows.net/<tenant>/servicePrincipals/<sp id>/$links/owners?api-version=1.6 --body "{\`"url\`":\`"https://graph.windows.net/<tenant>/directoryObjects/<user object id>\`"}"

i think it has something to do with escaping in pwsh, didn't sniff the traffic yet

drdamour commented 4 years ago

well crap it' sthe $link which resolves in powershell to nothing...so escape it and it'll work

az rest --method post --uri https://graph.windows.net/<tenant>/servicePrincipals/<sp id>/`$links/owners?api-version=1.6 --body "{\`"url\`":\`"https://graph.windows.net/<tenant>/directoryObjects/<user object id>\`"}"

or

az rest --method post --uri https://graph.windows.net/<tenant>/servicePrincipals/<sp id>/`$links/owners?api-version=1.6 --body '{\"url\":\"https://graph.windows.net/<tenant>/directoryObjects/<user object id>\"}'
jiasli commented 4 years ago

@trvsmtchll, could you share the command with sensitive information removed? Also what is the environment?

naymore commented 4 years ago

I cannot reset something where I am not an owner of. So I really need the possibility to add owners to SPN.

Please fix the incomplete CLI.

trvsmtchll commented 4 years ago

Looking for any updates on how to add a service principal completely with CLI without going to the GUI/Portal at all please.

dekimsey commented 4 years ago

I cannot reset something where I am not an owner of.

Made worse by the fact that owners must be User objects, so I cannot even set my team's security group here. I have to enumerate and add every individual member.

jiasli commented 4 years ago

As AAD is deprecating AD Graph API, for now you may use az rest to call MS Graph API.

Add owners to an application

MS Graph API: application: Add owner

# bash
appId=93dde3da-9fca-47dd-aee2-409b402ffed3
appObjectId=$(az ad app show --id $appId --query objectId -o tsv)

# Get the object Id for the current user
ownerObjectId=$(az ad signed-in-user show --query objectId -o tsv)

# This applies to both user and service principal as owners
az rest -m POST -u https://graph.microsoft.com/beta/applications/$appObjectId/owners/\$ref --headers Content-Type=application/json -b "{\"@odata.id\": \"https://graph.microsoft.com/beta/directoryObjects/$ownerObjectId\"}"

# To add a user as an owner
az rest -m POST -u https://graph.microsoft.com/beta/applications/$appObjectId/owners/\$ref --headers Content-Type=application/json -b "{\"@odata.id\": \"https://graph.microsoft.com/beta/users/$ownerObjectId\"}"

# To add a service principal as an owner
az rest -m POST -u https://graph.microsoft.com/beta/applications/$appObjectId/owners/\$ref --headers Content-Type=application/json -b "{\"@odata.id\": \"https://graph.microsoft.com/beta/servicePrincipals/$ownerObjectId\"}"

Also see https://github.com/microsoftgraph/microsoft-graph-docs-contrib/issues/8095, https://blogs.aaddevsup.xyz/2018/11/how-to-add-an-owner-to-an-azure-ad-application/

Add owners to a service principal

MS Graph API: servicePrincipal: Add owner

Note that the doc for request body is not accurate at the moment (https://github.com/microsoftgraph/microsoft-graph-docs-contrib/issues/7380).

# bash
appId=93dde3da-9fca-47dd-aee2-409b402ffed3
spObjectId=$(az ad sp show --id $appId --query objectId --output tsv)

# Get the object Id for the current user
ownerObjectId=$(az ad signed-in-user show --query objectId -o tsv)

# This applies to both user and service principal as owners
az rest -m POST -u https://graph.microsoft.com/beta/servicePrincipals/$spObjectId/owners/\$ref --headers Content-Type=application/json -b "{\"@odata.id\": \"https://graph.microsoft.com/beta/directoryObjects/$ownerObjectId\"}"

# To add a user as an owner
az rest -m POST -u https://graph.microsoft.com/beta/servicePrincipals/$spObjectId/owners/\$ref --headers Content-Type=application/json -b "{\"@odata.id\": \"https://graph.microsoft.com/beta/users/$ownerObjectId\"}"

# To add a service principal as an owner
az rest -m POST -u https://graph.microsoft.com/beta/servicePrincipals/$spObjectId/owners/\$ref --headers Content-Type=application/json -b "{\"@odata.id\": \"https://graph.microsoft.com/beta/servicePrincipals/$ownerObjectId\"}"
jiasli commented 4 years ago

@dekimsey, MS Graph currently doesn't support group as owner.

$ az rest -m POST -u https://graph.microsoft.com/beta/applications/$appObjectId/owners/\$ref --headers Content-Type=application/json -b "{\"@odata.id\": \"https://graph.microsoft.com/beta/directoryObjects/5a197067-7d4e-4862-a692-cb5933646da1\"}"
Bad Request({
  "error": {
    "code": "Request_BadRequest",
    "message": "The reference target 'Group_5a197067-7d4e-4862-a692-cb5933646da1' of type 'Group' is invalid for the 'owners' reference.",
    "innerError": {
      "request-id": "daf41148-02ab-426a-9dc0-fe07060fe87f",
      "date": "2020-03-25T03:21:30"
    }
  }

I will mark this as service attention. You may vote on these feedback pages:

https://feedback.azure.com/forums/169401-azure-active-directory/suggestions/37337278-add-group-as-owner-on-azure-ad-application-and-ser https://feedback.azure.com/forums/169401-azure-active-directory/suggestions/6688284-ad-groups-in-application-owners https://feedback.azure.com/forums/169401-azure-active-directory/suggestions/39240190-app-registration-owners-should-allow-for-groups

drdamour commented 4 years ago

@yonzhan why did this get closed? regardless of the adding groups, it still be good to add users without having to use az rest

yonzhan commented 4 years ago

@drdamour Is it a requirement for CLI or AAD team?

drdamour commented 4 years ago

cli

yonzhan commented 4 years ago

I will discuss with @jiasli about this and keep this issue reopen.

jiasli commented 4 years ago

This will be implemented after we migrate to MS Graph. Moving to backlog as a feature request. We will track MS Graph issues at https://github.com/Azure/azure-cli/issues/12946

drdamour commented 4 years ago

seems like the graph.windows.net solution no longer functions at all, and the MS Graph suggestion requires a TON of permissions that no sane admin would grant...

drdamour commented 4 years ago

i stand corrected, the application one is pretty sane ReadWrite.OwnedBy i thought it required write.all

jiasli commented 4 years ago

Hi @drdamour, thanks for the supplemental information. Yes, for both application: Add owner and servicePrincipal: Add owner APIs, all you need is Application.ReadWrite.OwnedBy and Directory.Read.All, as given by the document itself:

Permission type Permissions (from least to most privileged)
Delegated (work or school account) Application.ReadWrite.All and Directory.Read.All, Directory.AccessAsUser.All
Delegated (personal Microsoft account) Not supported.
Application Application.ReadWrite.OwnedBy and Directory.Read.All, Application.ReadWrite.All and Directory.Read.All
swisman commented 3 years ago

More than one year has passed, any news on this? Would be great if this will be implemented in my opinion.

23min commented 2 years ago

Trying to follow the official docs for adding an owner to a service principal (pscore)

az rest `
   --method POST --uri https://graph.microsoft.com/v1.0/servicePrincipals/$($objectId)/owners/\$ref `
   --headers Content-Type=application/json `
   --body '{\"@odata.id\":\"https://graph.microsoft.com/v1.0/directoryObjects/$($userId)\"}'

responds with

ERROR: Bad Request({"error":{"code":"Request_BadRequest","message":"Unsupported resource type 'DirectoryObject' for operation 'Create'.","innerError":{"date":"2021-09-20 .... SNIP
drdamour commented 2 years ago

@23min i've always use User which inherits directoryObjects, agree docs are strange, fwiw u linked the beta docs...but even the 1.0 docs example shows using beta for the post target...so weird

az rest -m POST -u https://graph.microsoft.com/beta/servicePrincipals/<sp object id>/owners/`$ref --headers Content-Type=application/json -b '{\"@odata.id\": \"https://graph.microsoft.com/beta/users/<user id>\"}'
jiasli commented 2 years ago

Hi folks, as for the behavior of Microsoft Graph API, you may reach out to AAD support by creating a support ticket. A support engineer will gladly help you with it.

saldroubi commented 2 years ago

I also would like for this to be implemented in az cli as well. Why is this issue closed? One should not have to use the rest API with az. Please reopen! Also, using MS Graph is not an option because would give permission to use it in our environment. Also, why are all these MS tools require different authentications, PowerShell Az module, Power Shell MS Graph, and Az CLI, ...etc.

jiasli commented 2 years ago

I will reopen this issue and mark it as feature request.

Also, using MS Graph is not an option because would give permission to use it in our environment.

As AD Graph has been deprecated, using Microsoft Graph is the only option.

Also, why are all these MS tools require different authentications, PowerShell Az module, Power Shell MS Graph, and Az CLI, ...etc.

Thanks for the feedback. Personally, I also like the idea to share login context between Azure tools (tracked by https://github.com/Azure/azure-cli/issues/16460).

saldroubi commented 1 year ago

Is this in the works? It has been requested a long time ago.

Mykp3 commented 1 year ago

This is such a ... limitation, why SP cannot be owner of other application regardless of initial creator responding with bad request? This role currently is rather useless... Application.ReadWrite.OwnedBy

saldroubi commented 1 year ago

I really wish this gets implemented soon. It has been so loooooooong.

Mykp3 commented 1 year ago

@yugangw-msft when shall we expect advancement in Application.ReadWrite.OwnedBy API Permission, in particular,

Ability to assign AD application as an owner to existing AD application, e.g. not necessarily actually created by that application?

notaturkey commented 1 year ago

bump, wish i could do this

Mykp3 commented 1 year ago

@saldroubi @notaturkey It is possible to make application registration to be owner of other application registration.

Requirements:

From the main page of application registration of the future owner navigate to the link under "Managed application in local directory" description - it will redirect you to enterprise application page, copy object ID From the main page of application registration of application ,that you want to owned by any other app, copy object id and paste below

Perform next cmdlet Add-AzureADApplicationOwner -ObjectId $app.ObjectId -RefObjectId $spn.owner.ObjectId

It should allow you to manage app under another app. If not enough you may also grab of enterprise application of desired owned application and perform slightly different ( SPN level assignment)

Add-AzureADServicePrincipalOwner -ObjectId $spn.ObjectId -RefObjectId $spn.owner.ObjectId

So we always interested to grant enterprise app (SPN) of future owner, with target SPN and/or regular application registration object ID that we want to own.

drdamour commented 1 year ago

Yes you certainly can use powershell to add sp’s as owners, but this ticket is about az cli not ps module

kevinpauli commented 1 year ago

What the heck, four years later and it's not in there yet?

ncook-hxgn commented 1 year ago

bumpy bump bump

Annesars90 commented 1 year ago

BUMPPPP

rcomanne commented 1 year ago

Would be very nice to be able to manage owners of a service principle by the Azure CLI... Any progress update? Or are we just supposed to use the Graph API directly and this will not be implemented?

uracharla1 commented 1 year ago

Hi there, am currently trying to assign SPN owner to enterprise application using microsoft graph but not seems to working. From the portal is only allowing to add user only. Is there any solution to fix it?

sodds-eq commented 10 months ago

bump

dalekseevs commented 8 months ago

bump

jordan-lee-accessgroup commented 7 months ago

bump

hoivikaj commented 6 months ago

While this is still missing from AZ CLI native as of today, wanted to drop in the command that currently works for me via AZ REST. Not sure if BETA endpoint is still required but its what I am currently using.

Azure Service Principal to be owned by Azure Service Principal:

az rest --method POST --uri https://graph.microsoft.com/beta/servicePrincipals/{TARGET SP OBJECT ID}/owners/\$ref --body '{"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/{OWNER SP OBJECT ID}"}'

I was previously having trouble as I missed the escape of $ for $ref.

chadcarlton commented 4 months ago

Please implement this... pretty unbelievable that 4 years later this is not in place.

DariuszPorowski commented 1 month ago

+1