normalian blog

Let's talk about Microsoft Azure, ASP.NET and Java!

Azure AD へサービス プリンシパル認証をさせるための簡単な azure-cli 向けスクリプト

自分の備忘録となるが、Microsoft Azure のリソースを操作する場合は Azure リソース マネージャーでのサービス プリンシパルの認証 に記載されている内容を一通り租借をしなければならない。
既に PowerShell 版は Azure AD へサービス プリンシパル認証をさせるための簡単な PowerShell スクリプト で記載したが、azure-cli 版がなかったので備忘録として記載しておく。

# Azure Resource Manager モードに設定
$ azure config mode arm

# ログイン
$ azure login

# サービスプリンシパルを作成し、結果から Object Id を取得する
$ azure ad sp create -n <your app name. e.g.: mylinuxexampleapp> --home-page <your home-page name. e.g.: http://normalianmylinuxapp.azurewebsite.net> --identifier-uris <your identifier-uris name. e.g.: http://normalianmylinuxapp.azurewebsite.net> -p <your password>
info:    Executing command ad sp create
+ Creating application <your app name. e.g.: mylinuxexampleapp>
+ Creating service principal for application <your application-id>
data:    Object Id:               <your object-id>
data:    Display Name:            <your app name. e.g.: mylinuxexampleapp>
data:    Service Principal Names:
data:                             <your application-id>
data:                             <your identifier-uris name. e.g.: http://normalianmylinuxapp.azurewebsite.net>
info:    ad sp create command OK


# 取得した Object Id からロールアサインを作成する。この際、Reader 権限だと仮想マシンの作成等ができないので注意
$ azure role assignment create --objectId <your object-id> -o Owner -c /subscriptions/<your subscriptionid>/
info:    Executing command role assignment create
+ Finding role with specified name
/data:    RoleAssignmentId     : /subscriptions/<your subscriptionid>/providers/Microsoft.Authorization/roleAssignments/<your role assignment id>
data:    RoleDefinitionName   : Owner
data:    RoleDefinitionId     : <role definition id>
data:    Scope                : /subscriptions/<your subscriptionid>
data:    Display Name         : <your app name. e.g.: mylinuxexampleapp>
data:    SignInName           : undefined
data:    ObjectId             : <your object-id>
data:    ObjectType           : ServicePrincipal
data:
+
info:    role assignment create command OK


# サブスクリプションの情報一覧を取得する
$ azure account list
info:    Executing command account list
data:    Name                      Id                                    Current  State
data:    -------------------       ------------------------------------  -------  -------
data:    <your subscription name>  <your subscriptionid>                 true     Enabled
info:    account list command OK


# サブスクリプション一覧から特定のサブスクリプション情報を取得し、Tenant ID を取得する
$ azure account show <your subscriptionid>
info:    Executing command account show
data:    Name                        : <your subscription name>
data:    ID                          : <your subscriptionid>
data:    State                       : Enabled
data:    Tenant ID                   : <tenant-id>
data:    Is Default                  : true
data:    Environment                 : AzureCloud
data:    Has Certificate             : No
data:    Has Access Token            : Yes
data:    User name                   : my-mail-address@hotmail.com
data:
info:    account show command OK

# 作成済のサービスプリンシパルでログインする
$ azure login -u <your identifier-uris name. e.g.: http://normalianmylinuxapp.azurewebsite.net> --service-principal --tenant <tenant-id> -p <your password>

ご参考までに。

参考

詳細な情報を知りたい場合は以下のリンクを参考にしてほしい。