きっかけ
- とある案件で現行システムを大体AWSにリフトアップする案件があり、既存FTPサーバをTransfer family for FTPを含む構成で実現したく、事前にカスタムIdp含む構築手順の検証をするため
構築Step
- Lambda 構築
- API Gateway 構築
- Transfer Family For FTP 構築
- 動作確認(カスタムIDプロバイダ)
1. Lambda 構築
ソースコードを以下に修正、デプロイ実施(https://docs.aws.amazon.com/ja_jp/transfer/latest/userguide/custom-identity-provider-users.html を参考)
'use strict';
// GetUserConfig Lambda
exports.handler = (event, context, callback) => {
console.log("Username:", event.username, "ServerId: ", event.serverId);
var response;
// Check if the username presented for authentication is correct. This doesn't check the value of the serverId, only that it is provided.
if (event.password == '' && ( event.protocol == 'FTP' || event.protocol == 'FTPS')) {
// Return HTTP status 200 but with no role in the response to indicate authentication failure
response = {};
} else if (event.serverId !== "" && event.username == '${UserName}') {
response = {
Role: '${UserRoleArn}', // The user will be authenticated if and only if the Role field is not blank
Policy: '', // Optional JSON blob to further restrict this user's permissions
HomeDirectory: '${UserHomeDirectory}' // Not required, defaults to '/'
};
// Check if password is provided
if (event.password == "") {
// If no password provided, return the user's SSH public key
response['PublicKeys'] = [ "${UserPublicKey1}" ];
// Check if password is correct
} else if (event.password !== '${UserPassword}') {
// Return HTTP status 200 but with no role in the response to indicate authentication failure
response = {};
}
} else {
// Return HTTP status 200 but with no role in the response to indicate authentication failure
response = {};
}
callback(null, response);
};
- 環境変数に以下5つを設定
API Gateway 構築
モデルを以下内容で新規作成
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "Error Schema",
"type" : "object",
"properties" : {
"message" : { "type" : "string" }
}
}
APIのリソースを順に作成。(リソース名は任意) リソースパスを順にserver > {serverId} >users > {username} > config と作成
メソッドレスポンスの200の場合を修正。レスポンス本文をコンテンツタイプ:application/json, モデルを作成したモデルに修正
統合リクエストのマッピングテンプレートを修正。パススルーを'テンプレートが定義されていない場合 (推奨) 'とし、コンテンツタイプをapplication/json、テンプレートを以下の内容で設定
{
"username": "$input.params('username')",
"password":"$util.escapeJavaScript($input.params('Password')).replaceAll("\'","'")",
"serverId": "$input.params('serverId')",
"protocol": "$input.params('protocol')","sourceIp": "$input.params('sourceIp')"
}
3. Transfer Family For FTP 構築
4. 動作確認(カスタムIDプロバイダ)
- 作成したサーバ画面からテストを実施。Lambda上でパラメータ設定したユーザ名、パスワードを設定、サーバプロトコルはFTP、送信元IPはVPC内の適当なIPを入力し、テストを実施
- StatusCode:200が返ってくればOK