---
title: .NETラボ勉強会_シークレットのローテーション管理231028
tags: 
author: [Yoriko Yokoyama](https://www.docswell.com/user/yoriko-y)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/L7358DN2E5.jpg?width=480
description: .NETラボ勉強会_シークレットのローテーション管理231028 by Yoriko Yokoyama
published: October 28, 23
canonical: https://www.docswell.com/s/yoriko-y/KNRQN7-2023-10-28-200134
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/L7358DN2E5.jpg)

.NET ラボ 勉強会
Azure サービスを使用した
シークレットローテーションの自動化


# Page. 2

![Page Image](https://bcdn.docswell.com/page/87DDMPW67G.jpg)

アジェンダ
1. シークレットの管理
2. Azure サービスを使用した自動化の構成
3. 自動化の結果確認
2


# Page. 3

![Page Image](https://bcdn.docswell.com/page/VJPXRNXZ78.jpg)

1. シークレットの管理


# Page. 4

![Page Image](https://bcdn.docswell.com/page/2EVLRKLMJQ.jpg)

シークレットの管理
セキュアに管理したい大切な文字列をどう管理するか
⚫
⚫
⚫
シークレットの格納場所
シークレットへのアクセス制御
シークレットの定期的なローテーション
4


# Page. 5

![Page Image](https://bcdn.docswell.com/page/57GXMQXXEL.jpg)

2. Azure サービスを使用した自動化の構成


# Page. 6

![Page Image](https://bcdn.docswell.com/page/4EQ8RK85JP.jpg)

シークレットのローテーションの自動化
使用する Azure サービス
⚫
⚫
⚫
Key Vault
Event Grid
Azure Functions
③ シークレットの有効期限が
近づいていることを通知
Event Grid
② シークレットの有効期限が
近づいているイベントを発行
④ シークレットの
新しいバージョン
作成
① シークレット格納
Key Vault
出典：https://learn.microsoft.com/ja-jp/azure/key-vault/secrets/tutorial-rotation
Functions
⑤ シークレット更新
SQL Server
6


# Page. 7

![Page Image](https://bcdn.docswell.com/page/KJ428P2VE1.jpg)

① シークレット格納
シークレットの格納先として Key Vault を使用
Event Grid
Functions
① シークレット格納
Key Vault
SQL Server
7


# Page. 8

![Page Image](https://bcdn.docswell.com/page/LE1M29M47G.jpg)

Azure Key Vault
大切な情報を格納する金庫のようなサービス
⚫
⚫
⚫
暗号化キー
シークレット (文字列)
証明書
アクセス制御
⚫
RBAC
⚫
⚫
ロールベースでアクセスを制御
Azure Key Vault
アクセスポリシー
⚫
細かな単位でアクセスを制御
キー
証明書
シークレット
RBAC
• 所有者
• 共同作成者
• キーコンテナー管理者
アクセスポリシー
• シークレットの管理操作など
(取得、一覧、その他)
• 暗号化キーの管理操作など
• 証明書の管理操作など
8


# Page. 9

![Page Image](https://bcdn.docswell.com/page/GEW3RQ3ZE2.jpg)

シークレットの格納方法
シークレット作成画面で以下を設定
⚫
⚫
⚫
⚫
⚫
名前
シークレット値
アクティブ化する日
有効期限
有効化/無効化
9


# Page. 10

![Page Image](https://bcdn.docswell.com/page/47ZVRWVLE3.jpg)

② シークレットの有効期限が近づいているイベントを発行
Key Vault シークレットの有効期限の設定値に基づいて Event Grid にイベントを送信
⚫
30 日前にイベントが送信される
Event Grid
② シークレットの有効期限が
近づいているイベントを発行
Functions
Key Vault
SQL Server
10


# Page. 11

![Page Image](https://bcdn.docswell.com/page/YJ65RG5M7V.jpg)

Azure Event Grid
フルマネージドの パブリッシャー/サブスクライブ メッセージ配信サービス
⚫
⚫
⚫
Azure リソースのイベントやカスタムなイベントを扱うことができる
軽量のイベント (JSON) をイベントハンドラーに送信
リアクティブなシステムを構築可能
出典：https://learn.microsoft.com/ja-jp/azure/event-grid/overview
11


# Page. 12

![Page Image](https://bcdn.docswell.com/page/GJ5Y8VYQJ4.jpg)

Event Grid のイベントソースとイベントハンドラー
イベントの発生元とイベントの送信先
Blob Storage
Azure Functions
リソースグループ
Logic Apps
Azure
サブスクリプション
Azure Automation
Key Vault
Webhook
App Service
IoT Hub
Queue storage
Event Grid
Service Bus
ハイブリッド接続
カスタム トピック
Event Hubs
イベント ソース
イベント ハンドラー
12


# Page. 13

![Page Image](https://bcdn.docswell.com/page/9E2GK3GWER.jpg)

Event Grid で収集するイベント
Azure Key Vault のイベント
Certificate New Version Created
⚫ Certificate Near Expiry
⚫ Certificate Expired
⚫ Secret New Version Created
⚫ Secret Near Expiry
⚫ Secret Expired
⚫ Key New Version Created
⚫ Key Near Expiry
⚫ Key Expired
⚫ Vault Access Policy Changed
⚫
Secret
Near Expiry
イベント
13


# Page. 14

![Page Image](https://bcdn.docswell.com/page/D7YVN1V97M.jpg)

送信されるイベントの中身
{
&quot;id&quot;: &quot;c17679db-16ae-4b5c-a404-ba5f41a079d2&quot;,
&quot;topic&quot;: &quot;/subscriptions/2cd136a4-91dc-4acf-b800-31bc40ee392c/resourceGroups/dotnetlab231028y/providers/Microsoft.KeyVault/vaults/dotnetlab-231028y&quot;,
&quot;subject&quot;: &quot;sqlPassword&quot;,
&quot;eventType&quot;: &quot;Microsoft.KeyVault.SecretNearExpiry&quot;,
&quot;data&quot;: {
&quot;Id&quot;: &quot;https://dotnetlab-231028y.vault.azure.net/secrets/sqlPassword2/d771ad9e251846e8977139c60be15c12&quot;,
&quot;VaultName&quot;: &quot;dotnetlab-231028y&quot;,
&quot;ObjectType&quot;: &quot;Secret&quot;,
&quot;ObjectName&quot;: &quot;sqlPassword&quot;,
&quot;Version&quot;: &quot;d771ad9e251846e8977139c60be15c12&quot;,
&quot;NBF&quot;: null,
&quot;EXP&quot;: 1696152289
},
&quot;dataVersion&quot;: &quot;1&quot;,
&quot;metadataVersion&quot;: &quot;1&quot;,
&quot;eventTime&quot;: &quot;2023-09-30T10:24:15.7272132Z&quot;
}
14


# Page. 15

![Page Image](https://bcdn.docswell.com/page/VENMX2MDJ8.jpg)

③ Azure Event Grid と Azure Functions の連携
Event Grid トリガーの Functions を作成し、イベントを連携
③ シークレットの有効期限が
近づいていることを通知
Event Grid
Functions
Key Vault
SQL Server
15


# Page. 16

![Page Image](https://bcdn.docswell.com/page/Y79N81N873.jpg)

Azure Functions
イベント駆動のサーバーレス コンピューティングサービス
⚫
何らかのイベントが発生したことをきっかけに動き出し、処理を実行する
⚫
⚫
⚫
⚫
⚫
BLOB トリガー
HTTP トリガー
Timer トリガー
Cosmos DB トリガー
Event Grid トリガー
プログラム
イベント
検知
格納
画像
Image.jpg
ストレージ
アカウント
実行
Azure
Functions
サムネイル
生成
画像
Image-resize.jpg
16


# Page. 17

![Page Image](https://bcdn.docswell.com/page/G78K84KZJD.jpg)

Azure Functions の Event Grid トリガー
Azure Functions の Event Grid トリガー作成方法
⚫
⚫
⚫
Azure portal
VS Code
Visual Studio
17


# Page. 18

![Page Image](https://bcdn.docswell.com/page/L7LZR6Z1ER.jpg)

④⑤ シークレットの更新と格納
Azure Functions によるシークレットの更新と格納
⚫
⚫
Key Vault に新しいバージョンのシークレットを格納
SQL Server の管理者パスワードを更新
Event Grid
Functions
④ シークレットの
新しいバージョン
作成
Key Vault
⑤ シークレット更新
SQL Server
18


# Page. 19

![Page Image](https://bcdn.docswell.com/page/4EMVR5V5EW.jpg)

Functions から Key Vault へのアクセス
マネージド ID の作成と、Key Vault へのアクセスを許可構成
⚫
⚫
Azure Functions のマネージド ID を作成
Key Vault シークレットにアクセスするための
アクセスポリシーを構成
Functions の
マネージド ID
Event Grid
マネージド ID 作成
Functions
Functions の ID に
シークレットへの
アクセスポリシーを付与
• 取得 (get)
• 一覧 (list)
• セット (set)
④ シークレットの
新しいバージョン
作成
Key Vault
⑤ シークレット更新
SQL Server
19


# Page. 20

![Page Image](https://bcdn.docswell.com/page/PER2R32Z79.jpg)

マネージド ID の有効化
Microsoft Entra ID にマネージド ID を作成
⚫
Azure Functions の ID 画面で 「状態」 を 「オン」 にする
20


# Page. 21

![Page Image](https://bcdn.docswell.com/page/P7XDR4D1JX.jpg)

Key Vault のアクセスポリシー構成
Azure Functions のマネージド ID に シークレットのアクセス許可を付与
⚫
⚫
⚫
取得
一覧
セット
21


# Page. 22

![Page Image](https://bcdn.docswell.com/page/37KXRZXMJD.jpg)

サンプルコード
https://github.com/Azure-Samples/KeyVault-Rotation-SQLPassword-Csharp
22


# Page. 23

![Page Image](https://bcdn.docswell.com/page/LJ358D5275.jpg)

EventGridTriggerCSharp1.cs
23


# Page. 24

![Page Image](https://bcdn.docswell.com/page/8JDDMPD6JG.jpg)

SecretRotator.cs
SecretRotator.cs の概要
⚫
⚫
⚫
⚫
⚫
マネージド ID を使用したKey Vaultへの接続
シークレットとタグ情報を取得
新しいパスワードを作成
新しいパスワードを使用して新しいシークレットを作成
SQL Server のパスワードを更新
24


# Page. 25

![Page Image](https://bcdn.docswell.com/page/VEPXRN6ZJ8.jpg)

25


# Page. 26

![Page Image](https://bcdn.docswell.com/page/27VLRK3MEQ.jpg)

3. 自動化の結果確認


# Page. 27

![Page Image](https://bcdn.docswell.com/page/5JGXMQ3X7L.jpg)

Key Vault へのシークレット作成コマンド
$tomorrowDate = (get-date).AddDays(+1).ToString(&quot;yyy-MM-ddThh:mm:ssZ&quot;)
az keyvault secret set --name sqlPassword --vault-name dotnetlab-231028y --value
&quot;Simple123&quot; --tags &quot;CredentialId=sqlAdmin&quot;
&quot;ProviderAddress=/subscriptions/2cd136a4-91dc-4acf-b80031bc40ee392c/resourceGroups/dotnetlab231028/providers/Microsoft.Sql/servers/dotnetlab-231028y&quot; &quot;ValidityPeriodDays=90&quot;
--expires $tomorrowDate
27


# Page. 28

![Page Image](https://bcdn.docswell.com/page/47Q8RK55EP.jpg)

新しいバージョンのシークレットが格納される (動画)
XXX
⚫
XXX
28


# Page. 29

![Page Image](https://bcdn.docswell.com/page/KE428P9V71.jpg)

補足：Azure SQL Server の認証方法
SQL 認証
⚫
⚫
サーバーの作成時に、ユーザー名とパスワードを使用してサーバー管理者ログインを指定する
ユーザーが管理者ログイン用のユーザー名とパスワードを使用して、
Azure SQL Database や Azure SQL Managed Instance に接続する
Microsoft Entra ID 認証
⚫
⚫
⚫
Microsoft Entra ID 内の ID を使用して Azure SQL Database や
Azure SQL Managed Instance に接続する
Microsoft Entra ID 管理者と呼ばれるサーバー管理者を作成する
Multi-Factor Authentication などにも対応
29


# Page. 30

![Page Image](https://bcdn.docswell.com/page/L71M29K4JG.jpg)

応用
ストレージアカウントなど
さまざまなサービスに応用
できると思うので、
ぜひ色々試してみてください！
30


# Page. 31

![Page Image](https://bcdn.docswell.com/page/G7W3RQDZ72.jpg)

新発売書籍：AZ-900
MCP 教科書 Microsoft Azure Fundamentals (試験番号 : AZ-900)
⚫
⚫
⚫
⚫
⚫
発売日：2023 年 9 月 19 日
出版社：翔泳社
各章末には練習問題
巻末には模擬問題を 1 回分収録
「ボーナス問題」 (試験 2 回分) もダウンロード可能
よろしく
お願いします！
出典：https://www.shoeisha.co.jp/book/detail/9784798180809
31


