Upgrade to Pro — share decks privately, control downloads, hide ads and more …

認証にシークレットも証明書も不要?! オンプレミスのサーバーでマネージドIDを利用!

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

認証にシークレットも証明書も不要?! オンプレミスのサーバーでマネージドIDを利用!

Avatar for Masahiko Ebisuda

Masahiko Ebisuda

March 08, 2024
Tweet

More Decks by Masahiko Ebisuda

Other Decks in Technology

Transcript

  1. 日本ビジネスシステムズ株式会社 胡田 昌彦(えびすだ まさひこ) Youtube https://youtube.com/@ebibibi 自己紹介 日本ビジネスシステムズ株式会社 こんな方にオススメ! 

    企業の情報システム部で働く方  一般ユーザーだけど、コンピューターに興味 があって、もっと詳しくなりたい方  Windows, Azure, M365等のMicrosoft 関連技術に興味がある方 チャンネル登録よろしくお願いします!
  2. 何を言っているのかわからない? まずは実際にやってみましょう! Resource Groups - Create Or Update - REST

    API (Azure Resource Management) | Microsoft Docs https://docs.microsoft.com/ja- jp/rest/api/resources/resource-groups/create-or-update
  3. 誰がどうやってトークンを取得するのか? 様々なバリエーションがある Microsoft ID プラットフォームの認証フローとアプリのシナ リオ - Microsoft Entra |

    Microsoft Docs https://docs.microsoft.com/ja-jp/azure/active- directory/develop/authentication-flows-app- scenarios#scenarios-and-supported-authentication- flows ※日本語版は誤植が過去あったので注意)
  4. 誰が  ユーザープリンシパル  サービスプリンシパル  マネージドID どうやって  対話操作

     資格情報 他にも色々あるけどまずはこれだけわかれば 「自動化」程度なら困らない(と思う)
  5. PowerShell # 認証する Connect-AzAccount # 現在のコンテキストを確認する(認証したID,AADテナント,対象のサブスクリプション等) Get-AzContext # 対象サブスクリプションが適切でない場合には切り替える #Get-AzSubscription

    #Select-AzSubscription -Subscription <SubscriptionIDを入力> # トークンが取得できることを確認する Get-AzAccessToken $token = (Get-AzAccessToken).token # リソースグループを作成する $subscriptionId = (Get-AzContext).Subscription.Id $resourceGroupName = "testrg1" $url = "https://management.azure.com/subscriptions/${subscriptionId}/resourcegroups/${resourceGroupName}?api- version=2021-04-01" $headers = @{ "Authorization" = "Bearer $token" "Content-type" = "application/json" } $body = '{location:"JapanEast"}' Invoke-RestMethod -Method PUT -Uri $url -Headers $headers -Body $body
  6. PowerShell # 認証する Connect-AzAccount # トークンを取得する $token = (Get-AzAccessToken).token #

    リソースグループを作成する $subscriptionId = (Get-AzContext).Subscription.Id $resourceGroupName = "testrg1" $url = "https://management.azure.com/subscriptions/${subscriptionId}/resourcegroups/$ {resourceGroupName}?api-version=2021-04-01" $headers = @{ "Authorization" = "Bearer $token" "Content-type" = "application/json" } $body = '{location:"JapanEast"}' Invoke-RestMethod -Method PUT -Uri $url -Headers $headers -Body $body
  7. サービスプリンシパル / パスワードベースの認証 1. AADにアプリケーションを登録しサービスプリンシパルを作成する - アプリケーション - サービスプリンシパル 2.

    サービスプリンシパルにRBACで権限を付与する 3. シークレットを作成する 4. PowerShellでサービスプリンシパルとして認証する PowerShell $applicationId = "" $clientSecret = "" $tenantId = "" $securedSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force $creds = New-Object System.Management.Automation.PSCredential($applicationId, $securedSecret) Connect-AzAccount -ServicePrincipal -Tenant $tenantId -Credential $creds Get-AzContext Get-AzAccessToken New-AzResourceGroup -Name testrg3 -Location JapanEast
  8. サービスプリンシパル / パスワードベースの認証 1. AADにアプリケーションを登録しサービスプリンシパルを作成する - アプリケーション - サービスプリンシパル 2.

    サービスプリンシパルにRBACで権限を付与する 3. シークレットを作成する 4. PowerShellでサービスプリンシパルとして認証する PowerShell $applicationId = "" $clientSecret = "" $tenantId = "" $securedSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force $creds = New-Object System.Management.Automation.PSCredential($applicationId, $securedSecret) Connect-AzAccount -ServicePrincipal -Tenant $tenantId -Credential $creds Get-AzContext Get-AzAccessToken New-AzResourceGroup -Name testrg3 -Location JapanEast シークレットを生で書くのはダメ!
  9. サービスプリンシパル / 証明書ベースの認証 1. AADにアプリケーションを登録しサービスプリンシパルを作成する - アプリケーション - サービスプリンシパル 2.

    サービスプリンシパルにRBACで権限を付与する 3. (どこで作成してもいいが、今回はローカルのWindows上で)証明書を生成する 4. サービスプリンシパルに証明書をアップロードする 5. PowerShellでサービスプリンシパルとして認証する PowerShell $applicationId = "" $tenantId = "" # 証明書作成 New-SelfSignedCertificate -CertStoreLocation "cert:¥CurrentUser¥My" ` -Subject "CN=CertforSP" -KeySpec KeyExchange $Thumbprint = (Get-ChildItem cert:¥CurrentUser¥My¥ | Where-Object {$_.Subject -eq "CN=CertforSP" })[0].Thumbprint # 認証 Connect-AzAccount -ServicePrincipal -CertificateThumbprint $Thumbprint ` -ApplicationId $applicationId -TenantId $tenantId Get-AzContext Get-AzAccessToken New-AzResourceGroup -Name testrg4 -Location JapanEast
  10. サービスプリンシパル / 証明書ベースの認証 1. AADにアプリケーションを登録しサービスプリンシパルを作成する - アプリケーション - サービスプリンシパル 2.

    サービスプリンシパルにRBACで権限を付与する 3. (どこで作成してもいいが、今回はローカルのWindows上で)証明書を生成する 4. サービスプリンシパルに証明書をアップロードする 5. PowerShellでサービスプリンシパルとして認証する PowerShell $applicationId = "" $tenantId = "" # 証明書作成 New-SelfSignedCertificate -CertStoreLocation "cert:¥CurrentUser¥My" ` -Subject "CN=CertforSP" -KeySpec KeyExchange $Thumbprint = (Get-ChildItem cert:¥CurrentUser¥My¥ | Where-Object {$_.Subject -eq "CN=CertforSP" })[0].Thumbprint # 認証 Connect-AzAccount -ServicePrincipal -CertificateThumbprint $Thumbprint ` -ApplicationId $applicationId -TenantId $tenantId Get-AzContext Get-AzAccessToken New-AzResourceGroup -Name testrg4 -Location JapanEast 証明書の管理がめんどくさい!
  11. Azure VM上でマネージドIDのアクセストー クンを取得する Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oaut h2/token?api-version=2018-02- 01&resource=https%3A%2F%2Fmanagement.azure.com %2F’ `

    -Headers @{Metadata=“true”} 169.254.169.254 というIPアドレスからトークンが取得できる! マネージドIDがONであれば何もしなくてもいきなりトークンが取れる!
  12. How managed identities for Azure resources work with Azure virtual

    machines - Managed identities for Azure resources | Microsoft Learn https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/how-managed-identities-work-vm