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

ユースケースから考える、サーバーレスで実現する API 認可

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

ユースケースから考える、サーバーレスで実現する API 認可

API認可の設計で考えるべきポイントを、「ユーザーが介在するか」「認可サーバーの有無」「アプリケーションで必要な認可」の3つの観点から整理し、Amazon Cognito・API Gateway Lambda オーソライザー・Amazon Verified Permissionsを組み合わせたサーバーレスアーキテクチャの実装例を紹介します。
M2M(Machine to Machine)とユーザーが介在する認可の2つのユースケースを取り上げています。

Avatar for Yuya Matsumoto

Yuya Matsumoto

October 20, 2024
Tweet

More Decks by Yuya Matsumoto

Other Decks in Technology

Transcript

  1. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 松本 侑也 アマゾン ウェブ サービス ジャパン合同会社 ソリューションアーキテクト ユースケースから考える、 サーバーレスで実現する API 認可 T 4 - 2 画⾯に映る資料の撮影などによる本セッション資料の転⽤を禁⽌しております
  2. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. • 本資料では 2024年 8⽉ 時点でのサービス内容および価格に基づいたスライドや説明になっています。 最 新の情報は AWS 公式ウェブサイト (http://aws.amazon.com) にて ご確認ください。 • 資料作成には⼗分注意しておりますが、資料内の価格と AWS 公式ウェブサイト記載の価格に相違が あった場合、AWS 公式ウェブサイトの価格を優先とさせていただきます。 • AWS does not offer binding price quotes. AWS pricing is publicly available and is subject to change in accordance with the AWS Customer Agreement available at http://aws.amazon.com/agreement/. Any pricing information included in this document is provided only as an estimate of usage charges for AWS services based on certain information that you have provided. Monthly charges will be based on your actual use of AWS services, and may vary from the estimates provided. 内容についての注意点
  3. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⾃⼰紹介 • 松本 侑也 (Matsumoto, Yuya) • パブリックセクター 技術統括本部 ソリューションアーキテクト ⾃治体担当 最近の活動 • ガバメントクラウドへの基幹システム移⾏ • データ連携周りの Dive Deep
  4. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションの対象者とゴール ゴール • API の認可で考えるべきポイントをご理解いただく • AWS での API 認可 (M2M/ユーザー) の アーキテクチャ例をご理解いただく 対象者 • アーキテクト、システム意思決定者 • API の認可設計に携わる⽅ カバーしないこと • 各 AWS サービスの基本的な説明 • OAuth2.0 / OIDC 等の詳細説明
  5. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. • API 認可の課題 • 近年の API 認可の⽅法 • M2M の認可 • ユーザーが介在する認可 • まとめ アジェンダ
  6. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API 認可の課題
  7. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API の認証と認可について • 認証 Authentication • 通信の相⼿が誰(何)であるかを確認すること • 認可 Authorization • エンティティが何の権限でリソースにアクセスできるかを 確認し、制御すること
  8. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Web システムの⽂脈における “API” とは? “Application Programming Interface” → プログラムやソフトウェア同⼠がやり取りするための取り決め・仕様 API API 提供者はインターフェイスを公開し 利⽤者に宣⾔する 利⽤者は公開者の宣⾔にしたがって API を利⽤する
  9. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API 利⽤者の環境が多様化・複雑化してきている 企業のシステム API ⼀般ユーザー 管理者 IoT デバイス * 企業
  10. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API の利⽤元が多様化するにつれ、認可も複雑に ⼀般ユーザー 企業のシステム API IoT デバイス * テナント(企業)ごとのアクセス制御 ユーザーが認可に介在しない場合 アプリケーション独⾃のロールに基づくアクセス制御 管理者 API 認可 → 適切な権限を持つユーザーやアプリケーションのみが API を使⽤できるようにする * 今回は例としてユーザーが介在しない場合を挙げています 企業
  11. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. OWASP API Security Top 10 • API1:2023 - Broken Object Level Authorization • API2:2023 - Broken Authentication • API3:2023 - Broken Object Property Level Authorization • API4:2023 - Unrestricted Resource Consumption • API5:2023 - Broken Function Level Authorization • API6:2023 - Unrestricted Access to Sensitive Business Flows • API7:2023 - Server Side Request Forgery • API8:2023 - Security Misconfiguration • API9:2023 - Improper Inventory Management • API10:2023 - Unsafe Consumption of APIs 出典: https://owasp.org/API-Security/editions/2023/en/0x11-t10/
  12. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 従来の API の認可の⼿法と課題 API 提供者 (プロバイダー) IP アドレス制限 API キー API 独⾃の認証 API での Bearer トークンの発⾏
  13. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 従来の API の認可の⼿法と課題 API 提供者 (プロバイダー) IP アドレス制限 API ①永続的な認証情報を 利⽤するリスク 独⾃の認証 API での Bearer トークンの発⾏ ②より細かい粒度の認可 クラウドの台頭やモバイル端末の利⽤によ り IP アドレスは流動的に ③標準化されていない⽅法 API キー
  14. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 近年の API 認可の⽅法
  15. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. OAuth 2.0 を利⽤した API 認可 ユーザー 参考: https://datatracker.ietf.org/doc/html/rfc6749 認可サーバー クライアント ①有効期限付きのトークン ③標準的な仕様 ②ユーザーによる 柔軟な認可 API リソースサーバー 例: • ネイティブアプリ • ブラウザ上の SPA • サーバーサイドのアプリ
  16. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API へのアクセスパターン • ⾃社のサーバーとのデータ連携 • 他社システムへの API 提供 • サードパーティーアプリケーションからのアクセス • SPA からのアクセス API API ⾃社システム IoT デバイス 他社システム フロントエンド User User ユーザーが介在しない Machine to Machine: M2M ユーザーが介在する
  17. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API へのアクセスパターン User クライアント リソースサーバー クライアント アクセストークン の払い出し 認可画⾯ (*OAuth2.0 認可コードフローなど) (*OAuth2.0 クライアントクレデンシャルズフロー) 事前にクライアントが API サーバに対して 何ができるかをビジネス的に決めておく ユーザーの同意によってアクセストークンに 紐づく権限が変化する 認可サーバー 認可サーバー アクセストークン の払い出し リソースサーバー API API ユーザーが介在しない Machine to Machine: M2M ユーザーが介在する シークレットを安全に保持できることが前提 フロントエンドでも利⽤できるユースケースがある
  18. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API 認可で整理することの例 ユーザー 認可サーバー クライアント API ①ユーザーが介在するか ②利⽤したい/すべき 認可サーバーがあるか ③アプリケーションとして どういった認可が必要か
  19. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションで取り上げる例 ユーザー 認可サーバー クライアント API ①ユーザーが介在する場合/しない場合 ②利⽤したい/すべき 認可サーバーがあるか ③アプリケーションとして どういった認可が必要か
  20. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションで取り上げる例 ユーザー クライアント API ②利⽤したい/すべき 認可サーバーがあるか ③アプリケーションとして どういった認可が必要か Amazon Cognito ①ユーザーが介在する場合/しない場合
  21. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションで取り上げる例 ユーザー クライアント API ②利⽤したい/すべき 認可サーバーがあるか ③アプリケーションとして どういった認可が必要か Amazon Cognito ①ユーザーが介在する場合/しない場合
  22. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 従来の認可管理に対するアプローチ データベース <application code> If nurse.type = ‘regular’ and patient.dept and nurse.assignedFloor = patient.floor then ALLOW else DENY <more application code> ①ルールの変更には コードの変更が必要 ②複数の箇所・アプリで認可を ⾏う場合、認可のロジックが分散 ③個別に認可のロジックを テストすることが困難 アプリケーション リクエスト 認可ロジック
  23. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Verified Permissions の導⼊ データベース <application code> isAuthorized() <more application code> アプリケーション リクエスト 認可ロジック Amazon Verified Permissions ①アプケーションコードと 独⽴してポリシーを変更可能 ②認可のロジックを 1 箇所で集中管理 ③個別に認可のロジックを テストすることが可能
  24. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Cedar という独⾃⾔語でポリシーを記述 Can Principal perform Action on Resource with the Context ? この条件において、この⼈はこのリソースに対するこの操作を実⾏できるか︖ Principal: アクションを⾏う実⾏者 Action: 実⾏しようとしている操作 Resource: 操作対象のリソース Context: その他の条件 ”PARC” モデルでポリシーを定義 head condition permit( principal in User::"alice", action == Action::"view", resource in Album::"Vacation") when { resource.owner == "alice" };
  25. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションで取り上げる例 ユーザー クライアント API ①ユーザーが介在する場合/しない場合 ②利⽤したい/すべき 認可サーバーがあるか ③アプリケーションとして どういった認可が必要か Amazon Cognito Amazon Verified Permissions
  26. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS での API 構成例 Elastic Load Balancing Compute Elastic Load Balancing Compute Amazon API Gateway Amazon API Gateway AWS で API を実装するために利⽤できるサービスは複数ある User traffic
  27. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS での API 構成例 Elastic Load Balancing Compute Elastic Load Balancing Compute Amazon API Gateway Amazon API Gateway AWS で API を実装するために利⽤できるサービスは複数ある User traffic Amazon API Gateway の 機能を使った実装についてご紹介
  28. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon API Gateaway における認可の選択肢 AWS Identity and Access Management (IAM) Lambda オーソライザー COGNITO_USER_POOLS JWT • AWS 署名 v4 による 認証と認可 • 認証処理を委譲する Lambda 関数を指定して API へのアクセスを制御 • OIDC または OAuth2.0 仕様で取得したトークン を利⽤ Amazon API Gateway には REST/HTTP/WebSocket の機能が存在 本セッションでは REST API x Lambda Authorizer を例にご説明 • Cognito で発⾏した ID トークン/アクセス トークンを利⽤ REST/HTTP/WebSocket REST HTTP Amazon Cognito
  29. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda オーソライザーの仕組み アクセス元がどの API へアクセスできるかを IAM ポリシーで返す Amazon API Gateway ポリシー 認証されたプリンシパルの権限で バックエンドサービスを呼び出し Context + Token or Request params 1 2 3 4 { ... "Action": "execute-api:Invoke", "Effect": Allow", "Resource": ”arn:aws:execute-api:xxxxx: xxxxxxx:xxxxx/*/GET/pets” ... } Lambda オーソライザー バックエンド API pets/ GET へアクセス可能
  30. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションで取り上げる例 ユーザー クライアント ①ユーザーが介在する場合/しない場合 ②利⽤したい/すべき 認可サーバーがあるか ③アプリケーションとして どういった認可が必要か Amazon Cognito Amazon API Gateway (REST) Lambda オーソライザー Amazon Verified Permissions
  31. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. M2M の認可
  32. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. シナリオ - M2M の認可 センサー情報提供サービス API IoT センサー情報 提供 API /water/temperature GET /sunlight GET 企業 A (漁業) 企業 B (農業) *クライアントの環境でシークレットは安全に保持 • 漁業カテゴリの企業のみ利⽤可能 • 農業カテゴリの企業のみ利⽤可能
  33. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. サンプルアーキテクチャ Amazon API Gateway Amazon Cognito Lambda オーソライザー Amazon DynamoDB *追加の認可情報が 必要な場合 企業 A (漁業) 企業 B (農業) センサー情報提供サービス ①アクセストークンの取得 ②API へアクセス ③認可の判断 ④バックエンドへ アクセス バックエンド API API アクセスの実施 認可の判断 認可に利⽤する情報の保持 Amazon Verified Permissions
  34. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API アクセスまでの流れ 1. アクセストークンの取得 2. API リクエスト 認可を委譲 3. 認可情報の取得 4. 認可の判断 IAM Policy を返却 アクセストークン バックエンドへアクセス 3. API レスポンス クライアント の登録 Amazon Cognito Amazon API Gateway Amazon Verified Permissions Lambda オーソライザー 企業サーバー Amazon DynamoDB バックエンド
  35. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API アクセスまでの流れ 1. アクセストークンの取得 2. API リクエスト 認可を委譲 3. 認可情報の取得 4. 認可の判断 IAM Policy を返却 アクセストークン バックエンドへアクセス 3. API レスポンス Amazon Cognito Amazon API Gateway Amazon Verified Permissions Lambda オーソライザー 企業サーバー Amazon DynamoDB バックエンド クライアント の登録
  36. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. クライアント ID/クライアントシークレットの 払い出し Amazon Cognito 1. リソースサーバーの登録 1. カスタムスコープの登録 • water.temprature.read • sunlight.read 2. アプリケーションクライアント 1. Client Id 2. Client Secret 3. カスタムスコープの割り当て 企業 A (漁業) 企業 B (農業) Client Id: xxxxxx Client Secret: xxxxxx Client Id: xxxxxx Client Secret: xxxxxx Amazon API Gateway iot-api • /water/temperature GET • /sunlight GET
  37. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Cognito の設定画⾯例 リソースサーバーの登録 アプリケーションクライアントの作成 どのアプリケーションクライアントに どのスコープを紐づけていくかを選択 リソースサーバーと アクセスのスコープを作成
  38. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API アクセスまでの流れ 1. アクセストークンの取得 2. API リクエスト 認可を委譲 3. 認可情報の取得 4. 認可の判断 IAM Policy を返却 アクセストークン バックエンドへアクセス 3. API レスポンス クライアント の登録 Amazon Cognito Amazon API Gateway Amazon Verified Permissions Lambda オーソライザー 企業サーバー Amazon DynamoDB バックエンド
  39. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. アクセストークンのリクエスト { "sub": "3ck2b0lj37hnqj7ecxxxxxxx", "token_use": "access", "scope": ”iot-api/water.temperature", "auth_time": 1705216966, "iss": "https://cognito-idp.../us-east-1_xxxxxxx", "exp": 170522xxxxx, "iat": 170521xxxxx, "client_id": "3ck2b0lj37hnqj7xxxxxxxxx" } Cognito のアクセストークン例 Amazon Cognito 企業 A (漁業) Client Id: xxx Client Secret: xxx トークンの種類 スコープ 有効期限 アプリケーション クライアント アクセストークン * Cognito のアクセストークンは JWT (JWS) 形式
  40. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. API アクセスまでの流れ 1. アクセストークンの取得 2. API リクエスト 認可を委譲 3. 認可情報の取得 4. 認可の判断 IAM Policy を返却 アクセストークン 3. API レスポンス バックエンドへアクセス クライアント の登録 Amazon Cognito Amazon API Gateway Amazon Verified Permissions Lambda オーソライザー 企業サーバー Amazon DynamoDB バックエンド
  41. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda オーソライザーで⾏う処理 Lambda オーソライザー Amazon API Gateway Amazon Verified Permissions Amazon DynamoDB 参考: https://docs.aws.amazon.com/ja_jp/cognito/latest/developerguide/amazon-cognito-user-pools-using- tokens-verifying-a-jwt.html 1. (JWT の検証) 2. 認可情報の取得 3. 認可の判断 4. IAM Policy の⽣成
  42. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda オーソライザー JWT (JWS) の検証 Amazon API Gateway 参考: https://docs.aws.amazon.com/ja_jp/cognito/latest/developerguide/amazon-cognito-user-pools-using- tokens-verifying-a-jwt.html • 署名の検証 • クレームの検証 • 有効期限 • token_use etc... {ヘッダー} . {ペイロード}. {署名} JWT (JWS) 1. (JWT の検証) 2. 認可情報の取得 3. 認可の判断 4. IAM Policy の⽣成 NodeJS を利⽤している場合、”aws-jwt-verify”を利⽤ Verified Permissions の IsAuthorizedWithToken API を 利⽤時は Verified Permissions へ委譲可能 * Cognito のアクセストークンは JWT 形式 Amazon Verified Permissions Amazon DynamoDB
  43. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda オーソライザー Lambda オーソライザーで⾏う処理 Amazon API Gateway 追加で認可に利⽤する情報が必要な場合 例) • アプリケーションクライアントに紐づく 料⾦プラン • エンタープライズプラン/ スタンダードプラン • アプリケーション独⾃のロール • Admin/Reader 等 参考: https://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/saas- multitenant-api-access-authorization/external-data-avp.html 1. (JWT の検証) 2. 認可情報の取得 3. 認可の判断 4. IAM Policy の⽣成 { “companyName”: “company-A” "clientId": "xxxxxxxxx", "plan": "enterprise" } Amazon Verified Permissions Amazon DynamoDB
  44. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Verified Permissions Lambda オーソライザー Lambda オーソライザーで⾏う処理 Amazon API Gateway 参考: https://docs.aws.amazon.com/ja_jp/verifiedpermissions/latest/usergui de/identity-sources_map-token-to-schema.html permit(principal, action in [IotApi::Action::"get /water/temperature"], resource) when { context.token.scope.contains(”iot-api/water.temperature.read") }; isAuthorizedWithToken API を利⽤した認可の判断 1. (JWT の検証) 2. 認可情報の取得 3. 認可の判断 4. IAM Policy の⽣成 • REST API では COGNITO_USER_POOLS 利⽤時はマネージドにアクセストークンの スコープとアクセスできる API を判断する機能を提供 Amazon DynamoDB
  45. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda オーソライザー Lambda オーソライザーで⾏う処理 Amazon API Gateway 参考: https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide /apigateway-use-lambda-authorizer.html { ... "Action": "execute-api:Invoke", "Effect": Allow", "Resource": ”arn:xxxxx:water.temperature GET” ... } 認可の判断に沿って IAM Policy を⽣成 認可の判断をキャッシュしてレイテンシを下げることが可能 IAM Policy • ヘッダー • ステージ • クエリパラメーター • $Context 組み合わせをキャッシュキーとして利⽤ ボディの値は Lambda オーソライザーで利⽤不可 (Request Authorizer) 1. (JWT の検証) 2. 認可情報の取得 3. 認可の判断 4. IAM Policy の⽣成
  46. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. M2M 認可における API アクセスまでの流れ 2. API リクエスト 認可を委譲 3. 認可情報の取得 4. 認可の判断 IAM Policy を返却 アクセストークン 3. API レスポンス Lambda オーソライザーのキャッシュで 処理をスキップ可能 バックエンドへアクセス クライアント の登録 Amazon Cognito Amazon API Gateway Amazon Verified Permissions Lambda オーソライザー 企業サーバー Amazon DynamoDB バックエンド
  47. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. M2M 認可における API アクセスまでの流れ 1. アクセストークンの取得 2. API リクエスト 認可を委譲 3. 認可情報の取得 4. 認可の判断 IAM Policy を返却 アクセストークン 3. API レスポンス バックエンドへアクセス クライアント の登録 Amazon Cognito Amazon API Gateway Amazon Verified Permissions Lambda オーソライザー 企業サーバー Amazon DynamoDB バックエンド
  48. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda オーソライザーとバックエンドでの 認可の棲み分け例 Amazon API Gateway Context + Token or Request params Lambda オーソライザー バックエンド API Amazon Verified Permissions Property Level の認可 →リクエストボディに 基づく認可 * あるリソースの特定の値は 更新不可、等 Object Level の認可 →クエリパラメーターや リクエストヘッダーに 基づく認可
  49. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. M2M における API 認可まとめ Amazon API Gateway Lambda オーソライザー *追加の認可情報が 必要な場合 企業A (漁業) 企業B (農業) センサー情報提供サービス ①アクセストークンの取得 ②API へアクセス ③認可の判断 ④バックエンドへ アクセス バックエンド API Amazon Cognito Amazon DynamoDB 認可の判断 認可に利⽤する情報の保持 Amazon Verified Permissions
  50. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 【補⾜】AWS IAM を利⽤した API 認可 もっと簡易に API の認可を実施したい場合 • アクセス元が AWS 上にあるなど、簡易に AWS IAM を利⽤できる環境にある • API 提供元がクライアントの実装に直接関与できるシステム Amazon API Gateway システム API-ADMIN ①スイッチロール ②アクセス
  51. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. ユーザーが介在する API の認可
  52. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. シナリオ ⽂書管理 SaaS サービス API ⽂書管理 SaaS サービス /permissions 管理者 ⼀般ユーザー /documents/{documentId} • 「管理者」はアクセス可能 • 「⼀般ユーザー」は⾃分が所有 するドキュメントにアクセス可能 • 「管理者」はアクセス可能 • 「⼀般ユーザー」はアクセス不可 (Single Page Application: SPA で提供)
  53. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. サンプルアーキテクチャ – ユーザーが介在する場合 ⽂書管理 SaaS サービス Amazon API Gateway Amazon Cognito Lambda オーソライザー Amazon DynamoDB Amazon Verified Permissions ⼀般ユーザー/ 管理者 ②API アクセス ③認可の委譲 API アクセスの実施 ユーザーのロール情報、 ユーザーとリソースのマッピングを保持 認可の判断 ④バックエンドへ アクセス バックエンド API ①ログイン
  54. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3. 認可の判断 API アクセスまでの流れ Amazon Cognito Amazon API Gateway Amazon Verified Permissions Lambda オーソライザー 1. ログイン処理 2. API リクエスト 認可を委譲 IAM Policy を返却 バックエンドへアクセス API レスポンス Pre Token Generation Lambda IDトークン アクセストークン返却 フロント エンド トークンカスタマイズ ロール情報 リソース情報 バックエンド
  55. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3. 認可の判断 API アクセスまでの流れ Lambda オーソライザー 1. ログイン処理 2. API リクエスト 認可を委譲 IAM Policy を返却 API レスポンス Pre Token Generation Lambda IDトークン アクセストークン返却 フロント エンド トークンカスタマイズ ロール情報 リソース情報 バックエンド バックエンドへアクセス Amazon Cognito Amazon API Gateway Amazon Verified Permissions
  56. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Cognito を利⽤してログイン Amplify Auth を利⽤する例 Amazon Cognito Cognito Identity Provider API を利⽤ IDトークン アクセストークン を返却
  57. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 関数でアクセストークンをカスタマイズ Amplify Auth を利⽤する例 Amazon Cognito Pre Token Generation Lambda 参考: https://docs.aws.amazon.com/ja_jp/cognito/latest/developerguid e/user-pool-lambda-pre-token-generation.html Amazon DynamoDB Cognito Identity Provider API を利⽤ IDトークン アクセストークン を返却 { "sub": "3ck2b0lj37hnqj7ecxxxxxxx", "token_use": "access", "scope": "", ”app_role" “admin”: } アプリケーションで 利⽤する claim を 追加 (* ID トークンもカスタマイズ可能)
  58. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3. 認可の判断 API アクセスまでの流れ Lambda オーソライザー 1. ログイン処理 2. API リクエスト 認可を委譲 IAM Policy を返却 API レスポンス Pre Token Generation Lambda IDトークン アクセストークン返却 フロント エンド トークンカスタマイズ ロール情報 リソース情報 バックエンド バックエンドへアクセス Amazon Cognito Amazon API Gateway Amazon Verified Permissions
  59. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 参考: https://docs.aws.amazon.com/ja_jp/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens- verifying-a-jwt.html フロントエンドから API へアクセス Amazon API Gateway Lambda オーソライザー 1. 認可情報の取得 2. 認可の判断 3. IAM Policy の⽣成 Amazon Verified Permissions アクセストークン (*API Gateway は ID トークンの有無を検証することも可能) Amazon DynamoDB 認可に必要な情報を取得 (例: ドキュメントの所有者) /documents/{documentId}
  60. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Verified Permissions で認可 Amazon API Gateway isAuthorizedWithToken() Amazon Verified Permissions アクセストークン 1. 認可情報の取得 2. 認可の判断 3. IAM Policy の⽣成 Lambda オーソライザー
  61. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Verified Permissions で認可 Amazon Verified Permissions • リクエストしてきたユーザーと ⽂書のオーナーが同じだった場合、ドキュメントへのアクセスを許可 permit( principal,action in [DocApp::Action::"get /documents/{documentId}"],resource ) when { resource.owner == principal }; • ユーザーが管理者ロールを持っている場合、ドキュメントへのアクセスを許可する permit( principal, action in [DocApp::Action::"get /documents/{documentId}"],resource ) when { principal.role == "admin" };
  62. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 認可の判断に沿って IAM Policy を⽣成 参考: https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/ apigateway-use-lambda-authorizer.html { ... "Action": "execute-api:Invoke", "Effect": Allow", "Resource":arn:/documents/{documentId}GET” ... } API Gateway のキャッシュを利⽤することによってレイテンシを下げることが可能 IAM Policy • ヘッダー • ステージ • クエリパラメーター • $Context 1. 認可情報の取得 2. 認可の判断 3. IAM Policy の⽣成 Lambda オーソライザー 組み合わせをキャッシュキーとして利⽤ ボディの値は Lambda オーソライザーで利⽤不可 (Request Authorizer)
  63. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. フロントエンドでデータを描画 Amazon API Gateway Lambda オーソライザー Amazon Verified Permissions アクセストークン データ
  64. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ – ユーザーが介在する場合 ⽂書管理 SaaS サービス Amazon API Gateway Lambda オーソライザー ⼀般ユーザー/ 管理者 ②API アクセス ③認可の委譲 API アクセスの実施 ユーザーのロール情報、 ユーザーとリソースのマッピングを保持 認可の判断 ④バックエンドへ アクセス バックエンド API ①ログイン Amazon Cognito Amazon DynamoDB Amazon Verified Permissions
  65. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションで取り上げた例 ユーザー クライアント ①ユーザーが介在する場合/しない場合 ②利⽤したい/すべき 認可サーバーがあるか ③アプリケーションとして どういった認可が必要か Amazon Cognito Amazon Verified Permissions Amazon API Gateway
  66. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 各コンポーネントが置き換わっても基本的な 考え⽅は同じ ユーザー クライアント ①ユーザーが介在する場合/しない場合 ②利⽤したい/すべき 認可サーバーがあるか ③アプリケーションとして どういった認可が必要か Elastic Load Balancing Amazon Verified Permissions Keycloak
  67. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ
  68. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションのまとめ • API の認可は OAuth2.0 のトークンベースで⾏われることが増えてきた • まずはアクセス元や認可サーバー、アプリケーションで⾏う 認可などについて整理 • 2 つのサンプルアーキテクチャをご紹介 • ユーザーが介在しない (M2M) 場合 • ユーザーが介在する場合 • Amazon API Gateway のキャッシュ機能や、Amazon Verified Permissions を 組み合わて最適な API 認可を設計する
  69. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ご清聴ありがとうございました © 2024, Amazon Web Services, Inc. or its affiliates. All rights reserved. 松本 侑也 アマゾン ウェブ サービス ジャパン ソリューションアーキテクト