Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAML 認証機能の追加 #309

Merged
merged 10 commits into from
Feb 8, 2024
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ npm run cdk:deploy
- [AWS WAF による制限を有効化する](/docs/DEPLOY_OPTION.md#aws-waf-による制限を有効化する)
- [IP 制限](/docs/DEPLOY_OPTION.md#IP-アドレスによる制限)
- [地理的制限](/docs/DEPLOY_OPTION.md#地理的制限)
- [SAML 認証](/docs/DEPLOY_OPTION.md#SAML-認証)
- [モニタリング用のダッシュボードの有効化](/docs/DEPLOY_OPTION.md#モニタリング用のダッシュボードの有効化)

## その他
Expand Down
16 changes: 16 additions & 0 deletions docs/DEPLOY_OPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ Web ページへのアクセスをアクセス元の国で制限したい場合
npx -w packages/cdk cdk bootstrap --region us-east-1
```

### SAML 認証

Microsoft Entra ID (旧 Azure Active Directory) などの IdP が提供する SAML 認証機能と連携ができます。
[こちらに Microsoft Entra ID と SAML 設定を行う参考手順](SAML_WITH_ENTRA_ID.md) があります。Microsoft Entra ID の設定を含めた詳細な手順があるので、こちらもご活用ください。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

分けたの素晴らしい判断 👍


**[packages/cdk/cdk.json](/packages/cdk/cdk.json) を編集**

```json
"samlAuthEnabled": true,
"samlCognitoDomainName": "your-preferred-name.auth.ap-northeast-1.amazoncognito.com",
"samlCognitoFederatedIdentityProviderName": "EntraID",
```
- samlAuthEnabled : `true` にすることで、SAML 専用の認証画面に切り替わります。Cognito user pools を利用した従来の認証機能は利用できなくなります。
- samlCognitoDomainName : Cognito の App integration で設定する Cognito Domain 名を指定します。
- samlCognitoFederatedIdentityProviderName : Cognito の Sign-in experience で設定する Identity Provider の名前を指定します。

## モニタリング用のダッシュボードの有効化

入力/出力 Token 数や直近のプロンプト集などが集約されたダッシュボードを作成します。
Expand Down
235 changes: 235 additions & 0 deletions docs/SAML_WITH_ENTRA_ID.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
# Microsoft Entra ID と SAML 連携
Microsoft Entra ID (旧 Azure Active Directory) と SAML 連携を行う参考手順を紹介します。詳細なパラメーターは各環境に合わせて変更してください。

# 事前作業
Generative AI Use Cases JP の初回デプロイを行います。初回デプロイを行ったあと、Cognito と Entra ID の SAML 連携を行います。

CloudFormation の Stack の画面で Outputs タブを開き、WebUrl をメモします。

![image-20240205185011526](assets/SAML_WITH_ENTRA_ID/image-20240205185011526.png)

次に、Resource タブから、Cognito user pool の Physical ID をメモします。

![image-20240128114108630](assets/SAML_WITH_ENTRA_ID/image-20240128114108630.png)

# Cognito の設定 : Domain 設定
Cognito domain の設定を進めます。
Cognito user pool の画面で、App integration タブを開き、Domain に関する画面を表示します。Cognito Domain が空白なので、Actions から Create Cognito domain を選択します。

![image-20240128115029927](assets/SAML_WITH_ENTRA_ID/image-20240128115029927.png)

適当に任意の名前を付け、Create Cognito domain を押します。この手順では、`your-preferred-name` とします。世界で一意の名前を付ける必要があります。

![image-20240128115448597](assets/SAML_WITH_ENTRA_ID/image-20240128115448597.png)

Cognito domain が設定されました。

![image-20240128115539920](assets/SAML_WITH_ENTRA_ID/image-20240128115539920-1707114112930.png)

# Microsoft Entra ID の設定
Microsoft Entra ID (旧 Azure Active Directory) で SAML 連携を有効化します。

Microsoft Azure から Microsoft Entra ID の設定画面を開きます。

![image-20240128121313366](assets/SAML_WITH_ENTRA_ID/image-20240128121313366.png)


Enterprise Applications を選択します。

![image-20240128121505749](assets/SAML_WITH_ENTRA_ID/image-20240128121505749.png)



New application を選択します。

![image-20240128121555503](assets/SAML_WITH_ENTRA_ID/image-20240128121555503.png)



Create your own application を選択します。

![image-20240128121629558](assets/SAML_WITH_ENTRA_ID/image-20240128121629558-1707115633384.png)





任意のアプリケーション名を入れて、Create を押します。この例では、`generative-ai-use-cases-jp` としています。

![image-20240128121916888](assets/SAML_WITH_ENTRA_ID/image-20240128121916888.png)





Single sign-on のメニューから SAML を選択します。

![image-20240128122006365](assets/SAML_WITH_ENTRA_ID/image-20240128122006365.png)





Basic SAML Configuration にある Edit を押します。

![image-20240128122115335](assets/SAML_WITH_ENTRA_ID/image-20240128122115335.png)



次のパラメータを入れて保存を押します。[事前作業](#事前作業)で確認した Cognito user pool の ID や、[Cognito の設定 : Domain設定](#Cognito-の設定-:-Domain-設定) で確認した Domain の値を利用します。

Identifier (Entity ID)

```
# フォーマット
urn:amazon:cognito:sp:<UserPoolID>

# 入力例
urn:amazon:cognito:sp:ap-northeast-1_p0oD4M3F0
```



Reply URL (Assertion Consumer Service URL)

```
# フォーマット
https://<入力した値>.auth.yourRegion.amazoncognito.com/saml2/idpresponse

# 入力例
https://your-preferred-name.auth.ap-northeast-1.amazoncognito.com/saml2/idpresponse
```



値を指定して、Save を押します。

![image-20240128122339147](assets/SAML_WITH_ENTRA_ID/image-20240128122339147.png)



設定が反映されました。

![image-20240128122454341](assets/SAML_WITH_ENTRA_ID/image-20240128122454341.png)



Federation Metadata XML の Download を選択して、XML ファイルを入手します。

![image-20240128122534056](assets/SAML_WITH_ENTRA_ID/image-20240128122534056.png)



このアプリケーションに紐づけるユーザーやグループを追加します。ここで紐づけたユーザーやグループのみログインできます。

![image-20240128122707248](assets/SAML_WITH_ENTRA_ID/image-20240128122707248.png)



この例では、事前に作成していたユーザーを指定します。環境に合わせて指定してください。

![image-20240128122807048](assets/SAML_WITH_ENTRA_ID/image-20240128122807048.png)



Assign を押します。

![image-20240128122832158](assets/SAML_WITH_ENTRA_ID/image-20240128122832158.png)

# Cognito の設定 : Federation

AWS マネジメントコンソールで Cognito の設定作業に戻ります。
Cognito User Pool の画面を開き、Sign-in experience タブから、Add identity provider を選択します。

![image-20240128124451746](assets/SAML_WITH_ENTRA_ID/image-20240128124451746.png)



Entra ID の連携で SAML を利用するため、SAML を選択します。

![image-20240128124529523](assets/SAML_WITH_ENTRA_ID/image-20240128124529523.png)



Provider name に任意の識別しやすい名前を入れます。ここで指定した Provider name を、後の手順で cdk.json に記載します。
Choose file を選び、Entra ID からダウンロードしてきた「Federation Metadata XML」をアップロードします。

![image-20240128124624371](assets/SAML_WITH_ENTRA_ID/image-20240128124624371.png)


User pool attribute に email を指定します。
SAML attribute に、次の文字列を選択して、Add identity provider を選択します。

```
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
```



![image-20240128124827012](assets/SAML_WITH_ENTRA_ID/image-20240128124827012.png)



設定が追加されました。

![image-20240128125053814](assets/SAML_WITH_ENTRA_ID/image-20240128125053814.png)



# Cognito の設定 : Hosted UI

追加した Entra ID との連携を Hosted UI で利用するための設定をしていきます。App Integration タブを選択します。

![image-20240128125211108](assets/SAML_WITH_ENTRA_ID/image-20240128125211108.png)



既存の App Client を指定します。

![image-20240128125243769](assets/SAML_WITH_ENTRA_ID/image-20240128125243769.png)



Edit を押します。

![image-20240128125314475](assets/SAML_WITH_ENTRA_ID/image-20240128125314475.png)


[事前作業](#事前作業)で確認した WebUrl の値を Allowed callback URLs と Allowed sign-out URLs にに入力します。

![image-20240205185602299](assets/SAML_WITH_ENTRA_ID/image-20240205185602299.png)


Identity Provider で EntraID を選択します。また、Cognito user pool を利用した認証は停止したいため、Cognito user pool のチェックボックスを外します。

![image-20240207123836497](assets/SAML_WITH_ENTRA_ID/image-20240207123836497.png)


Save changes を押します。

![image-20240128132707060](assets/SAML_WITH_ENTRA_ID/image-20240128132707060.png)



追加されました。

![image-20240128132652553](assets/SAML_WITH_ENTRA_ID/image-20240128132652553.png)

# cdk.json の編集

これで設定が完了したため、cdk.json の値を変更します。

- samlAuthEnabled : `true` を指定します。SAML 専用の認証画面に切り替わり、Cognito user pools を利用した従来の認証機能は利用できなくなります。
- samlCognitoDomainName : [「Cognito の設定 : Domain設定」](#Cognito-の設定-:-Domain-設定) で指定した Cognito Domain 名を入力します。
- samlCognitoFederatedIdentityProviderName : [「Cognito の設定 : Federation」](#Cognito-の設定-:-Federation) で設定した Identity Provider の名前を入力します。


```json
"context": {
   <省略>
"samlAuthEnabled": true,
"samlCognitoDomainName": "your-preferred-name.auth.ap-northeast-1.amazoncognito.com",
"samlCognitoFederatedIdentityProviderName": "EntraID",
```

設定後、再度デプロイを行うと SAML 連携が有効化されます。
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/cdk/cdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"kendraDataSourceBucketName": null,
"selfSignUpEnabled": true,
"allowedSignUpEmailDomains": null,
"samlAuthEnabled": false,
"samlCognitoDomainName": "",
"samlCognitoFederatedIdentityProviderName": "",
"modelRegion": "us-east-1",
"modelIds": [
"anthropic.claude-v2"
Expand Down
7 changes: 6 additions & 1 deletion packages/cdk/lambda/getSharedChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ export const handler = async (
const userId = res.userId;
const chatId = res.chatId;

const chat = await findChatById(userId.split('#')[1], chatId.split('#')[1]);
const chat = await findChatById(
// SAML 認証だと userId に # が含まれるため
// 例: user#EntraID_hogehoge.com#EXT#@hogehoge.onmicrosoft.com
userId.split('#').slice(1).join('#'),
chatId.split('#')[1]
);
const messages = await listMessages(chatId.split('#')[1]);

return {
Expand Down
4 changes: 3 additions & 1 deletion packages/cdk/lambda/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ export const findShareId = async (
export const deleteShareId = async (_shareId: string): Promise<void> => {
const userIdAndChatId = await findUserIdAndChatId(_shareId);
const share = await findShareId(
userIdAndChatId!.userId.split('#')[1],
// SAML 認証だと userId に # が含まれるため
// 例: user#EntraID_hogehoge.com#EXT#@hogehoge.onmicrosoft.com
userIdAndChatId!.userId.split('#').slice(1).join('#'),
userIdAndChatId!.chatId.split('#')[1]
);

Expand Down
4 changes: 3 additions & 1 deletion packages/cdk/lib/construct/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
export interface AuthProps {
selfSignUpEnabled: boolean;
allowedSignUpEmailDomains: string[] | null | undefined;
samlAuthEnabled: boolean;
}

export class Auth extends Construct {
Expand All @@ -26,7 +27,8 @@ export class Auth extends Construct {
super(scope, id);

const userPool = new UserPool(this, 'UserPool', {
selfSignUpEnabled: props.selfSignUpEnabled,
// SAML 認証を有効化する場合、UserPool を利用したセルフサインアップは利用しない。セキュリティを意識して閉じる。
selfSignUpEnabled: props.samlAuthEnabled ? false : props.selfSignUpEnabled,
signInAliases: {
username: false,
email: true,
Expand Down
6 changes: 6 additions & 0 deletions packages/cdk/lib/construct/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export interface WebProps {
modelIds: string[];
imageGenerationModelIds: string[];
endpointNames: string[];
samlAuthEnabled: boolean;
samlCognitoDomainName: string;
samlCognitoFederatedIdentityProviderName: string;
agentNames: string[];
}

Expand Down Expand Up @@ -113,6 +116,9 @@ export class Web extends Construct {
VITE_APP_MODEL_IDS: JSON.stringify(props.modelIds),
VITE_APP_IMAGE_MODEL_IDS: JSON.stringify(props.imageGenerationModelIds),
VITE_APP_ENDPOINT_NAMES: JSON.stringify(props.endpointNames),
VITE_APP_SAMLAUTH_ENABLED: props.samlAuthEnabled.toString(),
VITE_APP_SAML_COGNITO_DOMAIN_NAME: props.samlCognitoDomainName.toString(),
VITE_APP_SAML_COGNITO_FEDERATED_IDENTITY_PROVIDER_NAME: props.samlCognitoFederatedIdentityProviderName.toString(),
VITE_APP_AGENT_NAMES: JSON.stringify(props.agentNames),
},
});
Expand Down
23 changes: 23 additions & 0 deletions packages/cdk/lib/generative-ai-use-cases-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class GenerativeAiUseCasesStack extends Stack {
this.node.tryGetContext('selfSignUpEnabled')!;
const allowedSignUpEmailDomains: string[] | null | undefined =
this.node.tryGetContext('allowedSignUpEmailDomains');
const samlAuthEnabled: boolean = this.node.tryGetContext('samlAuthEnabled')!;
const samlCognitoDomainName: string = this.node.tryGetContext('samlCognitoDomainName')!;
const samlCognitoFederatedIdentityProviderName: string = this.node.tryGetContext('samlCognitoFederatedIdentityProviderName')!;
const agentEnabled = this.node.tryGetContext('agentEnabled') || false;

if (typeof ragEnabled !== 'boolean') {
Expand All @@ -54,9 +57,14 @@ export class GenerativeAiUseCasesStack extends Stack {
throw new Error(errorMessageForBooleanContext('selfSignUpEnabled'));
}

if (typeof samlAuthEnabled !== 'boolean') {
throw new Error(errorMessageForBooleanContext('samlAuthEnabled'));
}

const auth = new Auth(this, 'Auth', {
selfSignUpEnabled,
allowedSignUpEmailDomains,
samlAuthEnabled,
});
const database = new Database(this, 'Database');
const api = new Api(this, 'API', {
Expand Down Expand Up @@ -100,6 +108,9 @@ export class GenerativeAiUseCasesStack extends Stack {
modelIds: api.modelIds,
imageGenerationModelIds: api.imageGenerationModelIds,
endpointNames: api.endpointNames,
samlAuthEnabled,
samlCognitoDomainName,
samlCognitoFederatedIdentityProviderName,
agentNames: api.agentNames,
});

Expand Down Expand Up @@ -168,6 +179,18 @@ export class GenerativeAiUseCasesStack extends Stack {
value: JSON.stringify(api.endpointNames),
});

new CfnOutput(this, 'SamlAuthEnabled', {
value: samlAuthEnabled.toString(),
});

new CfnOutput(this, 'SamlCognitoDomainName', {
value: samlCognitoDomainName.toString(),
});

new CfnOutput(this, 'SamlCognitoFederatedIdentityProviderName', {
value: samlCognitoFederatedIdentityProviderName.toString(),
});

new CfnOutput(this, 'AgentNames', {
value: JSON.stringify(api.agentNames),
});
Expand Down
Loading
Loading