5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mastra Cloudを少しセキュアに使いましょう

Last updated at Posted at 2025-05-29

前書き

F920A4B2-99B4-45BD-9AAD-C8A061760914.jpeg

2025年3月4日にMastra Cloudがベータ公開されました。まだベータ版ですが、誰でも無料でMastra製のAI AgentやWorkflowをデプロイすることができます。

デプロイの手順

デプロイするためには、GitHubMastra Cloudを連携する必要があります。
ログイン時にリポジトリを連携すれば、Create new projectから連携済みのリポジトリを使ってデプロイできます。

9048D598-758D-431C-8001-0D06C47211EB.jpeg

必要な環境変数を入力し、プロジェクトのディレクトリを選択すればデプロイ完了です。

137126A7-5293-40F9-B0B6-52E0E5CAC07C.jpeg

ストレージが必要な場合は、Mastra Cloudが提供するものを利用することも可能です。

A89D6D7B-8B56-4BED-8145-DDE38FDF7E35_4_5005_c.jpeg

デプロイが完了すると、ローカルのPlaygroundと同じ感覚でテストできます。
また、独自のドメインが付与され、API感覚でMastraのAgentやWorkflowを呼び出すことができます。

EEE45606-DAE9-4264-B266-BD9327A183E8.jpeg

セキュリティの考慮事項

そのままModelを呼び出すキーとMastraプロジェクトをデプロイしてしまうと、URLを知る全ての人がアクセス可能になってしまいます:frowning2::point_up:

以下のような設定を行うことをお勧めします

mastra/index.ts
export const mastra = new Mastra({
...
  server: {
    middleware: [
      {
        handler: async (c, next) => {
        const isFromMastraCloud = c.req.header('x-mastra-cloud') === 'true';
        const isDevPlayground = c.req.header('x-mastra-dev-playground') === 'true'
        if(isFromMastraCloud || isDevPlayground) {
          await next();
          return;
        }

        const authHeader = c.req.header("Authorization");
        if (!authHeader || !authHeader.startsWith('Bearer ')) {
          return new Response('Unauthorized', { status: 401 });
        }
        const token = authHeader.substring(7);
        const validApiKey = process.env.BEARER_KEY || 'your-secret-api-key';
        if (token !== validApiKey) {
        return new Response('Invalid token', { status: 401 });
        }
        await next();
        },
        path: "/api/*",
      },
    ]
  }
});

このコードでは以下の認証を実装しています:

  • Bearer tokenの確認
  • x-mastra-cloudヘッダー:Mastra Cloud特有のヘッダーで、Mastra Cloudからのリクエストを識別
  • x-mastra-dev-playgroundヘッダー:ローカルPlayground特有のヘッダーで、ローカル環境でのAgent/Workflow検知に必要

現在の課題

上記の設定を行うと、Mastra CloudからAI Agentを検知はできますが、呼び出しができなくなる問題が発生します。
推測される原因は、AgentやWorkflowを呼び出す際にx-mastra-cloudヘッダーが含まれていない可能性があります。GitHubでコードを検索しても、x-mastra-cloudを考慮した実装は私のリポジトリ以外に見つかりませんでした。

実装の問題なのか、Mastra Cloudがまだベータ版のため考慮漏れなのかは現時点では不明です。:thinking:

2025/06/03に追記

Mastra Cloudからエージェントなどの呼び出しが正常になりました、Mastra開発チームに感謝です。

まとめ

現状でも無料環境にデプロイできるだけでも価値があるのではないでしょうか。

本番運用にはまだお勧めできませんが、検証目的であれば試す価値は十分にあると思います。
新しい情報があれば、また追記いたします。

ご興味ある方は自己責任でお試しください:smile:

参考資料

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?