2016年1月4日月曜日

【Microsoft Azure】Azure AD を利用して azure-xplat-cli からログイン

azure-xplat-cli を使う必要に迫られて、ログインでハマったのでざっくりまとめておきます。

コマンドラインからログインする方法は以下の3通りです。

・組織アカウント
・Microsoftアカウント(Version 0.9.10 以降)
・Active Directory Service Principal(Version 0.8.8 以降)

通常はこちらに書かれてる通りでログインは可能です。 https://azure.microsoft.com/ja-jp/documentation/articles/xplat-cli-connect/

今回求められることは、ARM対応であること、コマンドのみで完結することの2点。
非対話形式だと、上のリンクではARMは使えません。

そこで、手間はかかりますが、AD設定してログインできるようにします。
設定した通りに記述していきます。

まず、ADの設定をするために、ARMモードで対話形式かつMicrosoftアカウントでログインします。
コマンド入力後、Webブラウザから表示されたURLへアクセス、Microsoftアカウントでログイン後にコード(ここではXXXXXXXXX)を入力します。
なお、組織アカウントでもかまいません。
ログインすると、アカウントに紐付いたサブスクリプションが表示されます。
$ azure config mode arm
info:    Executing command config mode
info:    New mode is arm

info:    config mode command OK
$ azure login
info:    Executing command login
-info:    To sign in, use a web browser to open the page https://aka.ms/devicelogin. Enter the code XXXXXXXXXX to authenticate. If you're signing in as an Azure AD application, use the --username and --password parameters.
/info:    Added subscription Subscription1 as default
+
info:    login command OK

一旦対象のサブスクリプションをセットして、ADの設定をします。
設定項目は以下のとおりです。
サブスクリプション名:Subscription1
サブスクリプションID:1111-1111-1111-1111
アプリケーション名:authtest
アプリケーションURL(identifier-urisも):https://example.com
パスワード:Password
$ azure account list
info:    Executing command account list
data:    Name                                Id                                    Current  State  
data:    ----------------------------------  ------------------------------------  -------  -------
data:    Subscription1                   1111-1111-1111-1111     true     Enabled
info:    account list command OK
$ azure account set 1111-1111-1111-1111
info:    Executing command account set
info:    Setting subscription to "Subscription1" with id "1111-1111-1111-1111".
info:    Changes saved
info:    account set command OK
$ azure ad app create --name "authtest" --home-page "https://example.com" --identifier-uris "https://example.com" --password "Password"
info:    Executing command ad app create
+ Creating application authtest0                                               
data:    Application Id:          2222-2222-2222-2222
data:    Application Object Id:   3333-3333-3333-3333
data:    Application Permissions:  
data:                             claimValue:  user_impersonation
data:                             description:  Allow the application to access authtest on behalf of the signed-in user.
data:                             directAccessGrantTypes: 
data:                             displayName:  Access authtest
data:                             impersonationAccessGrantTypes:  impersonated=User, impersonator=Application
data:                             isDisabled: 
data:                             origin:  Application
data:                             permissionId:  4444-4444-4444-4444
data:                             resourceScopeType:  Personal
data:                             userConsentDescription:  Allow the application to access authtest on your behalf.
data:                             userConsentDisplayName:  Access authtest
data:                             lang: 
info:    ad app create command OK

前のコマンドの出力結果より、アプリケーションIDを使ってServicePrincipalを作成します。
アプリケーションID: 2222-2222-2222-2222
$ azure ad sp create 2222-2222-2222-2222
info:    Executing command ad sp create
+ Creating service principal for application 2222-2222-2222-2222
data:    Object Id:               5555-5555-5555-5555
data:    Display Name:            authtest
data:    Service Principal Names:
data:                             2222-2222-2222-2222
data:                             https://example.com
info:    ad sp create command OK

アクセス許可を行います。
オブジェクトID: 5555-5555-5555-5555
$ azure role assignment create --objectId 5555-5555-5555-5555 -o Reader -c /subscriptions/1111-1111-1111-1111
info:    Executing command role assignment create
+ Finding role with specified name                                             
/data:    RoleAssignmentId     : /subscriptions/1111-1111-1111-1111/providers/Microsoft.Authorization/roleAssignments/0000-0000-0000-0000
data:    RoleDefinitionName   : Reader
data:    RoleDefinitionId     : 6666-6666-6666-6666
data:    Scope                : /subscriptions/1111-1111-1111-1111
data:    Display Name         : authtest
data:    SignInName           :
data:    ObjectId             : 7777-7777-7777-7777
data:    ObjectType           : ServicePrincipal
data:    
+
info:    role assignment create command OK

最後にテナントIDを確認して、ログイン情報を取得します。
$ azure account list --json
[
  {
    "id": "1111-1111-1111-1111",
    "name": "Subscription1",
    "user": {
      "name": "admin@example.com",
      "type": "user"
    },
    "tenantId": "8888-8888-8888-8888",
    "state": "Enabled",
    "isDefault": true,
    "registeredProviders": [],
    "environmentName": "AzureCloud"
  }
]

やっとログイン情報が出揃いました。
必要な情報は以下のとおり。
・アプリケーションID:2222-2222-2222-2222
・パスワード:Password
・テナントID:8888-8888-8888-8888
$ azure login -u "2222-2222-2222-2222" -p "Password" --service-principal --tenant "8888-8888-8888-8888"
info:    Executing command login
|info:    Added subscription Subscription1                
info:    Setting subscription "Subscription1" as default
+
info:    login command OK

これで、簡単にログインができるようになりました。
Linuxのシェルに組み込む場合などに便利かと思います。