Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure AD add keys via Azure CLI

I'm trying to add a key in my Azure AD application using Azure CLI. But looking throught the Azure CLI API it seems that there is no such command.

For exmaple:

I'm trying to automate the task from the link below via Azure CLI: http://blog.davidebbo.com/2014/12/azure-service-principal.html

I can create AD application, service principal, but I can't find a way to add key for newly create AD application.

I'll appreciate any ideas and directions :)

Thanks in advance !

like image 762
jhose99 Avatar asked Apr 12 '16 06:04

jhose99


2 Answers

For a new AD application, you can specify a key with -p while creating. For example,

azure ad app create -n <your application name> --home-page <the homepage of you application> -i <the identifier URI of you application> -p <your key>

For an existing AD application, surely the Graph API is able to update the AD Application Credential. Read this API reference, and you can see that the password credential is able to use "POST, GET, PATCH". However, it's too complicated to use the Graph API. I have check the Azure CLI. That functionality is not yet implemented, and the source is unreadable for me. Then, I took a look at Azure SDK for Python, because I am familiar with python, and I found out that they have already implemented it in 2.0.0rc2. See the GitHub Repo

I have written a python script. But, in order to use my script you need to install not only azure2.0.0rc2, but also msrest and msrestazure.

from azure.common.credentials import UserPassCredentials
from azure.graphrbac import GraphRbacManagementClient, GraphRbacManagementClientConfiguration
from azure.graphrbac.models import ApplicationCreateParameters, PasswordCredential

credentials = UserPassCredentials("<your Azure Account>", "<your password>")

subscription_id = "<your subscription id>"

tenant_id = "<your tenant id>"

graphrbac_client = GraphRbacManagementClient(
    GraphRbacManagementClientConfiguration(
        credentials,
        subscription_id,
        tenant_id
    )
)

application = graphrbac_client.application.get('<your application object id>')

passwordCredential = PasswordCredential(start_date="2016-04-13T06:08:04.0863895Z", 
                                        end_date="2018-04-13T06:08:04.0863895Z",
                                        value="<your new key>")

parameters = ApplicationCreateParameters(application.available_to_other_tenants,
                                     application.display_name,
                                     "<the homepage of your AD application>",
                                     application.identifier_uris,
                                     reply_urls=application.reply_urls,
                                     password_credentials = [passwordCredential])

application = graphrbac_client.application.update('<your application object id>', parameters)

The only problem with this script is that you are only able to override all the existing keys of you AD application. You are not able to append a new key. This is a problem of the Graph API. The Graph API does not allow users to read an existing key. One possible solution would be storing your existing keys somewhere else. But, this will bring extra security risk.

like image 107
Jack Zeng Avatar answered Oct 19 '22 09:10

Jack Zeng


I don't have any experience of automating adding the key, I'm not sure it's even possible to be honest. However have a look at the ApplicationEntity documentation in the Graph API, it might be possible using a POST request to the web service.

like image 22
Martyn C Avatar answered Oct 19 '22 07:10

Martyn C